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