Moodle release 2.7rc1
[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
9e7d0979
JF
87 $BROWSE = 'timemodified';
88
96978238 89 switch ($this->config->type) {
90
91 case BGR_RANDOMLY:
92 $i = rand(1,$numberofentries);
422770d8 93 $limitfrom = $i-1;
2896553e 94 $SORT = 'ASC';
96978238 95 break;
96
2896553e 97 case BGR_NEXTONE:
98 if (isset($this->config->previous)) {
96978238 99 $i = $this->config->previous + 1;
2896553e 100 } else {
101 $i = 1;
102 }
96978238 103 if ($i > $numberofentries) { // Loop back to beginning
2896553e 104 $i = 1;
96978238 105 }
422770d8 106 $limitfrom = $i-1;
2896553e 107 $SORT = 'ASC';
108 break;
96978238 109
9e7d0979
JF
110 case BGR_NEXTALPHA:
111 $BROWSE = 'concept';
112 if (isset($this->config->previous)) {
113 $i = $this->config->previous + 1;
114 } else {
115 $i = 1;
116 }
117 if ($i > $numberofentries) { // Loop back to beginning
118 $i = 1;
119 }
120 $limitfrom = $i-1;
121 $SORT = 'ASC';
122 break;
123
2896553e 124 default: // BGR_LASTMODIFIED
96978238 125 $i = $numberofentries;
422770d8 126 $limitfrom = 0;
2896553e 127 $SORT = 'DESC';
96978238 128 break;
129 }
2896553e 130
32df07b4 131 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
f28f2d90 132 FROM {glossary_entries}
133 WHERE glossaryid = ? AND approved = 1
9e7d0979 134 ORDER BY $BROWSE $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
2896553e 135
cb88fbdd 136 $entry = reset($entry);
137
7ead0ce0 138 if (empty($this->config->showconcept)) {
837a0fe2 139 $text = '';
7ead0ce0 140 } else {
cc1a28f0 141 $text = "<h3>".format_string($entry->concept,true)."</h3>";
4ca6cfbf 142 }
fafa2265 143
dd4bee83 144 $options = new stdClass();
cbc2b5df 145 $options->trusted = $entry->definitiontrust;
367a75fa 146 $options->overflowdiv = true;
32df07b4 147 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
cbc2b5df 148 $text .= format_text($entry->definition, $entry->definitionformat, $options);
96978238 149
6d908bc6 150 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 151 $this->config->previous = $i;
152
153 } else {
154 $text = get_string('noentriesyet','block_glossary_random');
155 }
163dc56b 156 // store the text
6d908bc6 157 $this->config->cache = $text;
158 $this->instance_config_commit();
96978238 159 }
163dc56b 160 }
96978238 161
163dc56b 162 function instance_allow_multiple() {
163 // Are you going to allow multiple instances of each block?
164 // If yes, then it is assumed that the block WILL USE per-instance configuration
165 return true;
166 }
96978238 167
163dc56b 168 function get_content() {
cb640229 169 global $USER, $CFG, $DB;
95c62b56 170
96978238 171 if (empty($this->config->glossary)) {
b85b25eb 172 $this->content = new stdClass();
96978238 173 $this->content->text = get_string('notyetconfigured','block_glossary_random');
174 $this->content->footer = '';
175 return $this->content;
2896553e 176 }
177
51edc06e 178 require_once($CFG->dirroot.'/course/lib.php');
51edc06e 179
95556ce4
RT
180 // If $this->config->globalglossary is not set then get glossary info from db.
181 if (!isset($this->config->globalglossary)) {
182 if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
183 return '';
184 } else {
185 $this->config->courseid = $glossary->course;
186 $this->config->globalglossary = $glossary->globalglossary;
187 $this->instance_config_commit();
188 }
189 }
190
191 $modinfo = get_fast_modinfo($this->config->courseid);
192 // If deleted glossary or non-global glossary on different course page, then reset.
193 if (!isset($modinfo->instances['glossary'][$this->config->glossary])
390ef41e 194 || ((empty($this->config->globalglossary) && ($this->config->courseid != $this->page->course->id)))) {
f9192b65 195 $this->config->glossary = 0;
196 $this->config->cache = '';
197 $this->instance_config_commit();
198
443bb9c1 199 $this->content = new stdClass();
f9192b65 200 $this->content->text = get_string('notyetconfigured','block_glossary_random');
201 $this->content->footer = '';
202 return $this->content;
203 }
204
95556ce4 205 $cm = $modinfo->instances['glossary'][$this->config->glossary];
95a6ed77
AG
206 if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
207 return '';
208 }
209
96978238 210 if (empty($this->config->cache)) {
2896553e 211 $this->config->cache = '';
212 }
96978238 213
2896553e 214 if ($this->content !== NULL) {
95c62b56 215 return $this->content;
216 }
2896553e 217
b85b25eb 218 $this->content = new stdClass();
96978238 219
95556ce4
RT
220 // Show glossary if visible and place links in footer.
221 if ($cm->visible) {
222 $this->content->text = $this->config->cache;
fe2fdd11 223 if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
9f76d281 224 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
ed4c7bd3 225 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 226 } else {
7ead0ce0 227 $this->content->footer = '';
4ca6cfbf
PS
228 }
229
ed4c7bd3 230 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
231 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 232
95556ce4 233 // Otherwise just place some text, no link.
96978238 234 } else {
235 $this->content->footer = $this->config->invisible;
163dc56b 236 }
96978238 237
238 return $this->content;
239 }
240
163dc56b 241 function hide_header() {
96978238 242 if (empty($this->config->title)) {
163dc56b 243 return true;
95c62b56 244 }
245 return false;
163dc56b 246 }
163dc56b 247}
4ca6cfbf 248