2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Glossary Random block.
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
25 define('BGR_RANDOMLY', '0');
26 define('BGR_LASTMODIFIED', '1');
27 define('BGR_NEXTONE', '2');
28 define('BGR_NEXTALPHA', '3');
30 class block_glossary_random extends block_base {
33 $this->title = get_string('pluginname','block_glossary_random');
36 function specialization() {
39 require_once($CFG->libdir . '/filelib.php');
41 $this->course = $this->page->course;
43 // load userdefined title and make sure it's never empty
44 if (empty($this->config->title)) {
45 $this->title = get_string('pluginname','block_glossary_random');
47 $this->title = $this->config->title;
50 if (empty($this->config->glossary)) {
54 if (!isset($this->config->nexttime)) {
55 $this->config->nexttime = 0;
58 //check if it's time to put a new entry in cache
59 if (time() > $this->config->nexttime) {
61 // place glossary concept and definition in $pref->cache
62 if (!$numberofentries = $DB->count_records('glossary_entries',
63 array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
64 $this->config->cache = get_string('noentriesyet','block_glossary_random');
65 $this->instance_config_commit();
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))) {
73 $this->config->globalglossary = $glossary->globalglossary;
75 // Save course id in config, so we can get correct course module.
76 $this->config->courseid = $glossary->course;
78 // Get module and context, to be able to rewrite urls
79 if (! $cm = get_coursemodule_from_instance('glossary', $glossary->id, $this->config->courseid)) {
82 $glossaryctx = context_module::instance($cm->id);
87 $orderby = 'timemodified ASC';
89 switch ($this->config->type) {
92 $i = rand(1,$numberofentries);
97 if (isset($this->config->previous)) {
98 $i = $this->config->previous + 1;
102 if ($i > $numberofentries) { // Loop back to beginning
109 $orderby = 'concept ASC';
110 if (isset($this->config->previous)) {
111 $i = $this->config->previous + 1;
115 if ($i > $numberofentries) { // Loop back to beginning
121 default: // BGR_LASTMODIFIED
122 $i = $numberofentries;
124 $orderby = 'timemodified DESC, id DESC';
128 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
129 FROM {glossary_entries}
130 WHERE glossaryid = ? AND approved = 1
131 ORDER BY $orderby", array($this->config->glossary), $limitfrom, $limitnum)) {
133 $entry = reset($entry);
135 if (empty($this->config->showconcept)) {
138 $text = "<h3>".format_string($entry->concept,true)."</h3>";
141 $options = new stdClass();
142 $options->trusted = $entry->definitiontrust;
143 $options->overflowdiv = true;
144 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
145 $text .= format_text($entry->definition, $entry->definitionformat, $options);
147 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
148 $this->config->previous = $i;
151 $text = get_string('noentriesyet','block_glossary_random');
154 $this->config->cache = $text;
155 $this->instance_config_commit();
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
165 function get_content() {
166 global $USER, $CFG, $DB;
168 if (empty($this->config->glossary)) {
169 $this->content = new stdClass();
170 if ($this->user_can_edit()) {
171 $this->content->text = get_string('notyetconfigured','block_glossary_random');
173 $this->content->text = '';
175 $this->content->footer = '';
176 return $this->content;
179 require_once($CFG->dirroot.'/course/lib.php');
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))) {
186 $this->config->courseid = $glossary->course;
187 $this->config->globalglossary = $glossary->globalglossary;
188 $this->instance_config_commit();
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])
195 || ((empty($this->config->globalglossary) && ($this->config->courseid != $this->page->course->id)))) {
196 $this->config->glossary = 0;
197 $this->config->cache = '';
198 $this->instance_config_commit();
200 $this->content = new stdClass();
201 if ($this->user_can_edit()) {
202 $this->content->text = get_string('notyetconfigured','block_glossary_random');
204 $this->content->text = '';
206 $this->content->footer = '';
207 return $this->content;
210 $cm = $modinfo->instances['glossary'][$this->config->glossary];
211 if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
215 if (empty($this->config->cache)) {
216 $this->config->cache = '';
219 if ($this->content !== NULL) {
220 return $this->content;
223 $this->content = new stdClass();
225 // Show glossary if visible and place links in footer.
227 $this->content->text = $this->config->cache;
228 if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
229 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
230 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
232 $this->content->footer = '';
235 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
236 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
238 // Otherwise just place some text, no link.
240 $this->content->footer = $this->config->invisible;
243 return $this->content;