3 define('BGR_RANDOMLY', '0');
4 define('BGR_LASTMODIFIED', '1');
5 define('BGR_NEXTONE', '2');
7 class block_glossary_random
extends block_base
{
10 $this->title
= get_string('blockname','block_glossary_random');
11 $this->version
= 2007101509;
15 function specialization() {
17 $this->course
= $COURSE;
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');
23 $this->title
= $this->config
->title
;
26 if (empty($this->config
->glossary
)) {
30 if (!isset($this->config
->nexttime
)) {
31 $this->config
->nexttime
= 0;
34 //check if it's time to put a new entry in cache
35 if (time() > $this->config
->nexttime
) {
37 // place glossary concept and definition in $pref->cache
38 if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config
->glossary
,
40 $this->config
->cache
= get_string('noentriesyet','block_glossary_random');
41 $this->instance_config_commit();
47 switch ($this->config
->type
) {
50 $i = rand(1,$numberofentries);
56 if (isset($this->config
->previous
)) {
57 $i = $this->config
->previous +
1;
61 if ($i > $numberofentries) { // Loop back to beginning
68 default: // BGR_LASTMODIFIED
69 $i = $numberofentries;
75 if ($entry = get_records_sql(' SELECT concept, definition, format '.
76 ' FROM '.$CFG->prefix
.'glossary_entries'.
77 ' WHERE glossaryid = '.$this->config
->glossary
.
79 'ORDER BY timemodified '.$SORT, $limitfrom, $limitnum)) {
81 $entry = reset($entry);
83 if (empty($this->config
->showconcept
)) {
86 $text = "<h2>".format_string($entry->concept
,true)."</h2>";
89 $options = new object;
90 $options->trusttext
= true;
91 $text .= format_text($entry->definition
, $entry->format
, $options);
93 $this->config
->nexttime
= usergetmidnight(time()) + DAYSECS
* $this->config
->refresh
;
94 $this->config
->previous
= $i;
97 $text = get_string('noentriesyet','block_glossary_random');
100 $this->config
->cache
= $text;
101 $this->instance_config_commit();
105 function instance_allow_multiple() {
106 // Are you going to allow multiple instances of each block?
107 // If yes, then it is assumed that the block WILL USE per-instance configuration
111 function instance_config_print() {
114 if (!isset($this->config
)) {
115 // ... teacher has not yet configured the block, let's put some default values here to explain things
116 $this->config
->title
= get_string('blockname','block_glossary_random');
117 $this->config
->refresh
= 0;
118 $this->config
->showconcept
= 1;
119 $this->config
->cache
= get_string('notyetconfigured','block_glossary_random');
120 $this->config
->addentry
=get_string('addentry', 'block_glossary_random');
121 $this->config
->viewglossary
=get_string('viewglossary', 'block_glossary_random');
122 $this->config
->invisible
=get_string('invisible', 'block_glossary_random');
125 // select glossaries to put in dropdown box ...
126 $glossaries = get_records_select_menu('glossary', 'course='.$this->course
->id
,'name','id,name');
128 //format menu texts to avoid html and to filter multilang values
129 if(!empty($glossaries)) {
130 foreach($glossaries as $key => $value) {
131 $glossaries[$key] = strip_tags(format_string($value,true));
135 // and select quotetypes to put in dropdown box
136 $type[0] = get_string('random','block_glossary_random');
137 $type[1] = get_string('lastmodified','block_glossary_random');
138 $type[2] = get_string('nextone','block_glossary_random');
140 $this->config
->nexttime
= usergetmidnight(time()) + DAYSECS
* $this->config
->refresh
;
144 if (is_file($CFG->dirroot
.'/blocks/'. $this->name() .'/config_instance.html')) {
145 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
146 include($CFG->dirroot
.'/blocks/'. $this->name() .'/config_instance.html');
147 print_simple_box_end();
149 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
155 function get_content() {
156 global $USER, $CFG, $COURSE;
158 if (empty($this->config
->glossary
)) {
159 $this->content
->text
= get_string('notyetconfigured','block_glossary_random');
160 $this->content
->footer
= '';
161 return $this->content
;
164 $glossaryid = $this->config
->glossary
;
166 if ($this->course
->id
== $COURSE->id
) {
169 $course = get_record('course', 'id', $this->course
->id
);
172 require_once($CFG->dirroot
.'/course/lib.php');
173 $modinfo = get_fast_modinfo($course);
175 if (!isset($modinfo->instances
['glossary'][$glossaryid])) {
176 // we can get here if the glossary has been deleted, so
177 // unconfigure the glossary from the block..
178 $this->config
->glossary
= 0;
179 $this->config
->cache
= '';
180 $this->instance_config_commit();
182 $this->content
->text
= get_string('notyetconfigured','block_glossary_random');
183 $this->content
->footer
= '';
184 return $this->content
;
187 $cm = $modinfo->instances
['glossary'][$glossaryid];
189 if (empty($this->config
->cache
)) {
190 $this->config
->cache
= '';
193 if ($this->content
!== NULL) {
194 return $this->content
;
197 $this->content
= new stdClass
;
198 $this->content
->text
= $this->config
->cache
;
200 // place link to glossary in the footer if the glossary is visible
202 //Obtain the visible property from the instance
203 if ($cm->uservisible
) {
204 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE
, $cm->id
))) {
205 $this->content
->footer
= '<a href="'.$CFG->wwwroot
.'/mod/glossary/edit.php?id='.$cm->id
206 .'" title="'.$this->config
->addentry
.'">'.$this->config
->addentry
.'</a><br />';
208 $this->content
->footer
= '';
211 $this->content
->footer
.= '<a href="'.$CFG->wwwroot
.'/mod/glossary/view.php?id='.$cm->id
212 .'" title="'.$this->config
->viewglossary
.'">'.$this->config
->viewglossary
.'</a>';
214 // otherwise just place some text, no link
216 $this->content
->footer
= $this->config
->invisible
;
219 return $this->content
;
222 function hide_header() {
223 if (empty($this->config
->title
)) {
230 * Executed after block instance has been created, we use it to recode
231 * the glossary config setting to point to the new (restored) one
233 function after_restore($restore) {
234 /// We need to transform the glossary->id from the original one to the restored one
235 if ($rec = backup_getid($restore->backup_unique_code
, 'glossary', $this->config
->glossary
)) {
236 $this->config
->glossary
= $rec->new_id
;
237 $this->instance_config_commit();