Updated the HEAD build version to 20080427
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
96978238 1<?php // $Id$
2896553e 2
3define('BGR_RANDOMLY', '0');
4define('BGR_LASTMODIFIED', '1');
5define('BGR_NEXTONE', '2');
163dc56b 6
7class block_glossary_random extends block_base {
8 function init() {
96978238 9
10 $this->title = get_string('blockname','block_glossary_random');
433c242f 11 $this->version = 2007101509;
96978238 12
13 }
14
163dc56b 15 function specialization() {
1a2ea6f4 16 global $CFG, $COURSE;
17 $this->course = $COURSE;
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
422770d8 44 $limitfrom = 0;
45 $limitnum = 1;
46
96978238 47 switch ($this->config->type) {
48
49 case BGR_RANDOMLY:
50 $i = rand(1,$numberofentries);
422770d8 51 $limitfrom = $i-1;
2896553e 52 $SORT = 'ASC';
96978238 53 break;
54
2896553e 55 case BGR_NEXTONE:
56 if (isset($this->config->previous)) {
96978238 57 $i = $this->config->previous + 1;
2896553e 58 } else {
59 $i = 1;
60 }
96978238 61 if ($i > $numberofentries) { // Loop back to beginning
2896553e 62 $i = 1;
96978238 63 }
422770d8 64 $limitfrom = $i-1;
2896553e 65 $SORT = 'ASC';
66 break;
96978238 67
2896553e 68 default: // BGR_LASTMODIFIED
96978238 69 $i = $numberofentries;
422770d8 70 $limitfrom = 0;
2896553e 71 $SORT = 'DESC';
96978238 72 break;
73 }
2896553e 74
fda5a233 75 if ($entry = get_records_sql(' SELECT concept, definition, format '.
76 ' FROM '.$CFG->prefix.'glossary_entries'.
77 ' WHERE glossaryid = '.$this->config->glossary.
78 ' AND approved = 1 '.
422770d8 79 'ORDER BY timemodified '.$SORT, $limitfrom, $limitnum)) {
2896553e 80
cb88fbdd 81 $entry = reset($entry);
82
7ead0ce0 83 if (empty($this->config->showconcept)) {
837a0fe2 84 $text = '';
7ead0ce0 85 } else {
cc1a28f0 86 $text = "<h3>".format_string($entry->concept,true)."</h3>";
837a0fe2 87 }
fafa2265 88
89 $options = new object;
90 $options->trusttext = true;
91 $text .= format_text($entry->definition, $entry->format, $options);
96978238 92
6d908bc6 93 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 94 $this->config->previous = $i;
95
96 } else {
97 $text = get_string('noentriesyet','block_glossary_random');
98 }
163dc56b 99 // store the text
6d908bc6 100 $this->config->cache = $text;
101 $this->instance_config_commit();
96978238 102 }
163dc56b 103 }
96978238 104
163dc56b 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
108 return true;
109 }
96978238 110
163dc56b 111 function instance_config_print() {
96978238 112 global $CFG;
113
ed4c7bd3 114 if (!isset($this->config)) {
163dc56b 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;
837a0fe2 118 $this->config->showconcept = 1;
2896553e 119 $this->config->cache= get_string('notyetconfigured','block_glossary_random');
120 $this->config->addentry=get_string('addentry', 'block_glossary_random');
96978238 121 $this->config->viewglossary=get_string('viewglossary', 'block_glossary_random');
2896553e 122 $this->config->invisible=get_string('invisible', 'block_glossary_random');
163dc56b 123 }
96978238 124
163dc56b 125 // select glossaries to put in dropdown box ...
96978238 126 $glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name');
30d5bd79 127
1d3a7a85 128 //format menu texts to avoid html and to filter multilang values
30d5bd79 129 if(!empty($glossaries)) {
130 foreach($glossaries as $key => $value) {
131 $glossaries[$key] = strip_tags(format_string($value,true));
132 }
1d3a7a85 133 }
96978238 134
163dc56b 135 // and select quotetypes to put in dropdown box
96978238 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');
139
ed4c7bd3 140 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 141
163dc56b 142 // display the form
96978238 143
163dc56b 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();
148 } else {
149 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
150 }
96978238 151
2896553e 152 return true;
163dc56b 153 }
96978238 154
163dc56b 155 function get_content() {
51edc06e 156 global $USER, $CFG, $COURSE;
95c62b56 157
96978238 158 if (empty($this->config->glossary)) {
159 $this->content->text = get_string('notyetconfigured','block_glossary_random');
160 $this->content->footer = '';
161 return $this->content;
2896553e 162 }
163
f9192b65 164 $glossaryid = $this->config->glossary;
165
51edc06e 166 if ($this->course->id == $COURSE->id) {
167 $course = $COURSE;
168 } else {
169 $course = get_record('course', 'id', $this->course->id);
170 }
171
172 require_once($CFG->dirroot.'/course/lib.php');
173 $modinfo = get_fast_modinfo($course);
174
175 if (!isset($modinfo->instances['glossary'][$glossaryid])) {
f9192b65 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();
181
182 $this->content->text = get_string('notyetconfigured','block_glossary_random');
183 $this->content->footer = '';
184 return $this->content;
185 }
186
51edc06e 187 $cm = $modinfo->instances['glossary'][$glossaryid];
f9192b65 188
96978238 189 if (empty($this->config->cache)) {
2896553e 190 $this->config->cache = '';
191 }
96978238 192
2896553e 193 if ($this->content !== NULL) {
95c62b56 194 return $this->content;
195 }
2896553e 196
96978238 197 $this->content = new stdClass;
2896553e 198 $this->content->text = $this->config->cache;
96978238 199
200 // place link to glossary in the footer if the glossary is visible
96978238 201
96978238 202 //Obtain the visible property from the instance
51edc06e 203 if ($cm->uservisible) {
e73bfa2d 204 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
7ead0ce0 205 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
ed4c7bd3 206 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 207 } else {
7ead0ce0 208 $this->content->footer = '';
209 }
ed4c7bd3 210
211 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
212 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 213
163dc56b 214 // otherwise just place some text, no link
96978238 215 } else {
216 $this->content->footer = $this->config->invisible;
163dc56b 217 }
96978238 218
219 return $this->content;
220 }
221
163dc56b 222 function hide_header() {
96978238 223 if (empty($this->config->title)) {
163dc56b 224 return true;
95c62b56 225 }
226 return false;
163dc56b 227 }
96978238 228
163dc56b 229}
230?>