MDL-50710 block_glossary_random: more precise sorting
[moodle.git] / blocks / glossary_random / block_glossary_random.php
1 <?php
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/>.
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  */
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 {
32     function init() {
33         $this->title = get_string('pluginname','block_glossary_random');
34     }
36     function specialization() {
37         global $CFG, $DB;
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');
46         } else {
47             $this->title = $this->config->title;
48         }
50         if (empty($this->config->glossary)) {
51             return false;
52         }
54         if (!isset($this->config->nexttime)) {
55             $this->config->nexttime = 0;
56         }
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();
66             }
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             }
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)) {
80                 return false;
81             }
82             $glossaryctx = context_module::instance($cm->id);
84             $limitfrom = 0;
85             $limitnum = 1;
87             $orderby = 'timemodified ASC';
89             switch ($this->config->type) {
91                 case BGR_RANDOMLY:
92                     $i = rand(1,$numberofentries);
93                     $limitfrom = $i-1;
94                     break;
96                 case BGR_NEXTONE:
97                     if (isset($this->config->previous)) {
98                         $i = $this->config->previous + 1;
99                     } else {
100                         $i = 1;
101                     }
102                     if ($i > $numberofentries) {  // Loop back to beginning
103                         $i = 1;
104                     }
105                     $limitfrom = $i-1;
106                     break;
108                 case BGR_NEXTALPHA:
109                     $orderby = 'concept ASC';
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;
119                     break;
121                 default:  // BGR_LASTMODIFIED
122                     $i = $numberofentries;
123                     $limitfrom = 0;
124                     $orderby = 'timemodified DESC, id DESC';
125                     break;
126             }
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)) {
136                     $text = '';
137                 } else {
138                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
139                 }
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;
150             } else {
151                 $text = get_string('noentriesyet','block_glossary_random');
152             }
153             // store the text
154             $this->config->cache = $text;
155             $this->instance_config_commit();
156         }
157     }
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     }
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');
172             } else {
173                 $this->content->text = '';
174             }
175             $this->content->footer = '';
176             return $this->content;
177         }
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))) {
184                 return '';
185             } else {
186                 $this->config->courseid = $glossary->course;
187                 $this->config->globalglossary = $glossary->globalglossary;
188                 $this->instance_config_commit();
189             }
190         }
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');
203             } else {
204                 $this->content->text = '';
205             }
206             $this->content->footer = '';
207             return $this->content;
208         }
210         $cm = $modinfo->instances['glossary'][$this->config->glossary];
211         if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
212             return '';
213         }
215         if (empty($this->config->cache)) {
216             $this->config->cache = '';
217         }
219         if ($this->content !== NULL) {
220             return $this->content;
221         }
223         $this->content = new stdClass();
225         // Show glossary if visible and place links in footer.
226         if ($cm->visible) {
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 />';
231             } else {
232                 $this->content->footer = '';
233             }
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.
239         } else {
240             $this->content->footer = $this->config->invisible;
241         }
243         return $this->content;
244     }