weekly release 2.7dev
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
4ca6cfbf 1<?php
2896553e 2
3define('BGR_RANDOMLY', '0');
4define('BGR_LASTMODIFIED', '1');
5define('BGR_NEXTONE', '2');
9e7d0979 6define('BGR_NEXTALPHA', '3');
163dc56b 7
8class block_glossary_random extends block_base {
bfe20414 9
163dc56b 10 function init() {
5a0b9156 11 $this->title = get_string('pluginname','block_glossary_random');
96978238 12 }
bfe20414 13
163dc56b 14 function specialization() {
cb640229 15 global $CFG, $DB;
3aa2fd20
PS
16
17 require_once($CFG->libdir . '/filelib.php');
18
cb640229 19 $this->course = $this->page->course;
96978238 20
163dc56b 21 // load userdefined title and make sure it's never empty
22 if (empty($this->config->title)) {
2a71e9f2 23 $this->title = get_string('pluginname','block_glossary_random');
96978238 24 } else {
163dc56b 25 $this->title = $this->config->title;
96978238 26 }
95c62b56 27
96978238 28 if (empty($this->config->glossary)) {
95c62b56 29 return false;
30 }
31
96978238 32 if (!isset($this->config->nexttime)) {
95c62b56 33 $this->config->nexttime = 0;
34 }
96978238 35
163dc56b 36 //check if it's time to put a new entry in cache
96978238 37 if (time() > $this->config->nexttime) {
38
39 // place glossary concept and definition in $pref->cache
f28f2d90 40 if (!$numberofentries = $DB->count_records('glossary_entries',
41 array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
96978238 42 $this->config->cache = get_string('noentriesyet','block_glossary_random');
6d908bc6 43 $this->instance_config_commit();
2896553e 44 }
45
95556ce4
RT
46 // Get glossary instance, if not found then return without error, as this will be handled in get_content.
47 if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
48 return false;
49 }
50
51 $this->config->globalglossary = $glossary->globalglossary;
52
53 // Save course id in config, so we can get correct course module.
54 $this->config->courseid = $glossary->course;
55
32df07b4 56 // Get module and context, to be able to rewrite urls
95556ce4 57 if (! $cm = get_coursemodule_from_instance('glossary', $glossary->id, $this->config->courseid)) {
32df07b4
EL
58 return false;
59 }
fe2fdd11 60 $glossaryctx = context_module::instance($cm->id);
32df07b4 61
422770d8 62 $limitfrom = 0;
63 $limitnum = 1;
64
9e7d0979
JF
65 $BROWSE = 'timemodified';
66
96978238 67 switch ($this->config->type) {
68
69 case BGR_RANDOMLY:
70 $i = rand(1,$numberofentries);
422770d8 71 $limitfrom = $i-1;
2896553e 72 $SORT = 'ASC';
96978238 73 break;
74
2896553e 75 case BGR_NEXTONE:
76 if (isset($this->config->previous)) {
96978238 77 $i = $this->config->previous + 1;
2896553e 78 } else {
79 $i = 1;
80 }
96978238 81 if ($i > $numberofentries) { // Loop back to beginning
2896553e 82 $i = 1;
96978238 83 }
422770d8 84 $limitfrom = $i-1;
2896553e 85 $SORT = 'ASC';
86 break;
96978238 87
9e7d0979
JF
88 case BGR_NEXTALPHA:
89 $BROWSE = 'concept';
90 if (isset($this->config->previous)) {
91 $i = $this->config->previous + 1;
92 } else {
93 $i = 1;
94 }
95 if ($i > $numberofentries) { // Loop back to beginning
96 $i = 1;
97 }
98 $limitfrom = $i-1;
99 $SORT = 'ASC';
100 break;
101
2896553e 102 default: // BGR_LASTMODIFIED
96978238 103 $i = $numberofentries;
422770d8 104 $limitfrom = 0;
2896553e 105 $SORT = 'DESC';
96978238 106 break;
107 }
2896553e 108
32df07b4 109 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
f28f2d90 110 FROM {glossary_entries}
111 WHERE glossaryid = ? AND approved = 1
9e7d0979 112 ORDER BY $BROWSE $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
2896553e 113
cb88fbdd 114 $entry = reset($entry);
115
7ead0ce0 116 if (empty($this->config->showconcept)) {
837a0fe2 117 $text = '';
7ead0ce0 118 } else {
cc1a28f0 119 $text = "<h3>".format_string($entry->concept,true)."</h3>";
4ca6cfbf 120 }
fafa2265 121
dd4bee83 122 $options = new stdClass();
cbc2b5df 123 $options->trusted = $entry->definitiontrust;
367a75fa 124 $options->overflowdiv = true;
32df07b4 125 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
cbc2b5df 126 $text .= format_text($entry->definition, $entry->definitionformat, $options);
96978238 127
6d908bc6 128 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 129 $this->config->previous = $i;
130
131 } else {
132 $text = get_string('noentriesyet','block_glossary_random');
133 }
163dc56b 134 // store the text
6d908bc6 135 $this->config->cache = $text;
136 $this->instance_config_commit();
96978238 137 }
163dc56b 138 }
96978238 139
163dc56b 140 function instance_allow_multiple() {
141 // Are you going to allow multiple instances of each block?
142 // If yes, then it is assumed that the block WILL USE per-instance configuration
143 return true;
144 }
96978238 145
163dc56b 146 function get_content() {
cb640229 147 global $USER, $CFG, $DB;
95c62b56 148
96978238 149 if (empty($this->config->glossary)) {
b85b25eb 150 $this->content = new stdClass();
96978238 151 $this->content->text = get_string('notyetconfigured','block_glossary_random');
152 $this->content->footer = '';
153 return $this->content;
2896553e 154 }
155
51edc06e 156 require_once($CFG->dirroot.'/course/lib.php');
51edc06e 157
95556ce4
RT
158 // If $this->config->globalglossary is not set then get glossary info from db.
159 if (!isset($this->config->globalglossary)) {
160 if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
161 return '';
162 } else {
163 $this->config->courseid = $glossary->course;
164 $this->config->globalglossary = $glossary->globalglossary;
165 $this->instance_config_commit();
166 }
167 }
168
169 $modinfo = get_fast_modinfo($this->config->courseid);
170 // If deleted glossary or non-global glossary on different course page, then reset.
171 if (!isset($modinfo->instances['glossary'][$this->config->glossary])
390ef41e 172 || ((empty($this->config->globalglossary) && ($this->config->courseid != $this->page->course->id)))) {
f9192b65 173 $this->config->glossary = 0;
174 $this->config->cache = '';
175 $this->instance_config_commit();
176
443bb9c1 177 $this->content = new stdClass();
f9192b65 178 $this->content->text = get_string('notyetconfigured','block_glossary_random');
179 $this->content->footer = '';
180 return $this->content;
181 }
182
95556ce4 183 $cm = $modinfo->instances['glossary'][$this->config->glossary];
95a6ed77
AG
184 if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
185 return '';
186 }
187
96978238 188 if (empty($this->config->cache)) {
2896553e 189 $this->config->cache = '';
190 }
96978238 191
2896553e 192 if ($this->content !== NULL) {
95c62b56 193 return $this->content;
194 }
2896553e 195
b85b25eb 196 $this->content = new stdClass();
96978238 197
95556ce4
RT
198 // Show glossary if visible and place links in footer.
199 if ($cm->visible) {
200 $this->content->text = $this->config->cache;
fe2fdd11 201 if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
9f76d281 202 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
ed4c7bd3 203 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 204 } else {
7ead0ce0 205 $this->content->footer = '';
4ca6cfbf
PS
206 }
207
ed4c7bd3 208 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
209 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 210
95556ce4 211 // Otherwise just place some text, no link.
96978238 212 } else {
213 $this->content->footer = $this->config->invisible;
163dc56b 214 }
96978238 215
216 return $this->content;
217 }
218
163dc56b 219 function hide_header() {
96978238 220 if (empty($this->config->title)) {
163dc56b 221 return true;
95c62b56 222 }
223 return false;
163dc56b 224 }
163dc56b 225}
4ca6cfbf 226