96978238 |
1 | <?php // $Id$ |
2896553e |
2 | |
3 | define('BGR_RANDOMLY', '0'); |
4 | define('BGR_LASTMODIFIED', '1'); |
5 | define('BGR_NEXTONE', '2'); |
163dc56b |
6 | |
7 | class block_glossary_random extends block_base { |
8 | function init() { |
96978238 |
9 | |
10 | $this->title = get_string('blockname','block_glossary_random'); |
163dc56b |
11 | $this->version = 2005010300; |
96978238 |
12 | |
13 | } |
14 | |
163dc56b |
15 | function specialization() { |
16 | global $CFG; |
17 | $this->course = get_record('course', 'id', $this->instance->pageid); |
96978238 |
18 | |
163dc56b |
19 | // load userdefined title and make sure it's never empty |
20 | if (empty($this->config->title)) { |
21 | $this->title = get_string('blockname','block_glossary_random'); |
96978238 |
22 | } else { |
163dc56b |
23 | $this->title = $this->config->title; |
96978238 |
24 | } |
95c62b56 |
25 | |
96978238 |
26 | if (empty($this->config->glossary)) { |
95c62b56 |
27 | return false; |
28 | } |
29 | |
96978238 |
30 | if (!isset($this->config->nexttime)) { |
95c62b56 |
31 | $this->config->nexttime = 0; |
32 | } |
96978238 |
33 | |
163dc56b |
34 | //check if it's time to put a new entry in cache |
96978238 |
35 | if (time() > $this->config->nexttime) { |
36 | |
37 | // place glossary concept and definition in $pref->cache |
38 | if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config->glossary, |
2896553e |
39 | 'approved',1)) { |
96978238 |
40 | $this->config->cache = get_string('noentriesyet','block_glossary_random'); |
6d908bc6 |
41 | $this->instance_config_commit(); |
2896553e |
42 | } |
43 | |
96978238 |
44 | switch ($this->config->type) { |
45 | |
46 | case BGR_RANDOMLY: |
47 | $i = rand(1,$numberofentries); |
2896553e |
48 | $LIMIT = sql_paging_limit($i-1, 1); |
49 | $SORT = 'ASC'; |
96978238 |
50 | break; |
51 | |
2896553e |
52 | case BGR_NEXTONE: |
53 | if (isset($this->config->previous)) { |
96978238 |
54 | $i = $this->config->previous + 1; |
2896553e |
55 | } else { |
56 | $i = 1; |
57 | } |
96978238 |
58 | if ($i > $numberofentries) { // Loop back to beginning |
2896553e |
59 | $i = 1; |
96978238 |
60 | } |
2896553e |
61 | $LIMIT = sql_paging_limit($i-1, 1); |
62 | $SORT = 'ASC'; |
63 | break; |
96978238 |
64 | |
2896553e |
65 | default: // BGR_LASTMODIFIED |
96978238 |
66 | $i = $numberofentries; |
2896553e |
67 | $LIMIT = 'LIMIT 1'; // The last one |
68 | $SORT = 'DESC'; |
96978238 |
69 | break; |
70 | } |
2896553e |
71 | |
96978238 |
72 | if ($entry = get_record_sql(' SELECT concept, definition, format '. |
2896553e |
73 | ' FROM '.$CFG->prefix.'glossary_entries'. |
74 | ' WHERE glossaryid = '.$this->config->glossary. |
75 | ' AND approved = 1 '. |
76 | 'ORDER BY timemodified '.$SORT.' '.$LIMIT)) { |
77 | |
78 | $text = "<b>$entry->concept</b><br />"; |
96978238 |
79 | $text .= format_text($entry->definition, $entry->format); |
80 | |
6d908bc6 |
81 | $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh; |
96978238 |
82 | $this->config->previous = $i; |
83 | |
84 | } else { |
85 | $text = get_string('noentriesyet','block_glossary_random'); |
86 | } |
163dc56b |
87 | // store the text |
6d908bc6 |
88 | $this->config->cache = $text; |
89 | $this->instance_config_commit(); |
96978238 |
90 | } |
163dc56b |
91 | } |
96978238 |
92 | |
163dc56b |
93 | function instance_allow_multiple() { |
94 | // Are you going to allow multiple instances of each block? |
95 | // If yes, then it is assumed that the block WILL USE per-instance configuration |
96 | return true; |
97 | } |
96978238 |
98 | |
163dc56b |
99 | function instance_config_print() { |
96978238 |
100 | global $CFG; |
101 | |
ed4c7bd3 |
102 | if (!isset($this->config)) { |
163dc56b |
103 | // ... teacher has not yet configured the block, let's put some default values here to explain things |
104 | $this->config->title = get_string('blockname','block_glossary_random'); |
105 | $this->config->refresh = 0; |
96978238 |
106 | |
2896553e |
107 | $this->config->cache= get_string('notyetconfigured','block_glossary_random'); |
108 | $this->config->addentry=get_string('addentry', 'block_glossary_random'); |
96978238 |
109 | $this->config->viewglossary=get_string('viewglossary', 'block_glossary_random'); |
2896553e |
110 | $this->config->invisible=get_string('invisible', 'block_glossary_random'); |
163dc56b |
111 | } |
96978238 |
112 | |
163dc56b |
113 | // select glossaries to put in dropdown box ... |
96978238 |
114 | $glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name'); |
115 | |
163dc56b |
116 | // and select quotetypes to put in dropdown box |
96978238 |
117 | $type[0] = get_string('random','block_glossary_random'); |
118 | $type[1] = get_string('lastmodified','block_glossary_random'); |
119 | $type[2] = get_string('nextone','block_glossary_random'); |
120 | |
ed4c7bd3 |
121 | $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh; |
96978238 |
122 | |
163dc56b |
123 | // display the form |
96978238 |
124 | |
163dc56b |
125 | if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) { |
126 | print_simple_box_start('center', '', '', 5, 'blockconfigglobal'); |
127 | include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html'); |
128 | print_simple_box_end(); |
129 | } else { |
130 | notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me())); |
131 | } |
96978238 |
132 | |
2896553e |
133 | return true; |
163dc56b |
134 | } |
96978238 |
135 | |
163dc56b |
136 | function get_content() { |
137 | global $USER, $CFG; |
95c62b56 |
138 | |
96978238 |
139 | if (empty($this->config->glossary)) { |
140 | $this->content->text = get_string('notyetconfigured','block_glossary_random'); |
141 | $this->content->footer = ''; |
142 | return $this->content; |
2896553e |
143 | } |
144 | |
96978238 |
145 | if (empty($this->config->cache)) { |
2896553e |
146 | $this->config->cache = ''; |
147 | } |
96978238 |
148 | |
2896553e |
149 | if ($this->content !== NULL) { |
95c62b56 |
150 | return $this->content; |
151 | } |
2896553e |
152 | |
96978238 |
153 | $this->content = new stdClass; |
2896553e |
154 | $this->content->text = $this->config->cache; |
96978238 |
155 | |
156 | // place link to glossary in the footer if the glossary is visible |
163dc56b |
157 | $glossaryid = $this->config->glossary; |
96978238 |
158 | $glossary=get_record('glossary', 'id', $glossaryid); |
163dc56b |
159 | $studentcanpost = $glossary->studentcanpost; //needed to decide on which footer |
96978238 |
160 | |
163dc56b |
161 | //Create a temp valid module structure (course,id) |
2896553e |
162 | $tempmod->course = $this->course->id; |
163 | $tempmod->id = $glossaryid; |
96978238 |
164 | |
165 | //Obtain the visible property from the instance |
163dc56b |
166 | if (instance_is_visible('glossary', $tempmod)) { |
96978238 |
167 | |
163dc56b |
168 | $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ; |
169 | if ($studentcanpost) { |
ed4c7bd3 |
170 | |
171 | $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id |
172 | .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />'; |
173 | } |
174 | |
175 | $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id |
176 | .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>'; |
96978238 |
177 | |
163dc56b |
178 | // otherwise just place some text, no link |
96978238 |
179 | } else { |
180 | $this->content->footer = $this->config->invisible; |
163dc56b |
181 | } |
96978238 |
182 | |
183 | return $this->content; |
184 | } |
185 | |
163dc56b |
186 | function hide_header() { |
96978238 |
187 | if (empty($this->config->title)) { |
163dc56b |
188 | return true; |
95c62b56 |
189 | } |
190 | return false; |
163dc56b |
191 | } |
96978238 |
192 | |
163dc56b |
193 | } |
194 | ?> |