weekly on-sync release 3.1dev
[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             $BROWSE = 'timemodified';
89             switch ($this->config->type) {
91                 case BGR_RANDOMLY:
92                     $i = rand(1,$numberofentries);
93                     $limitfrom = $i-1;
94                     $SORT = 'ASC';
95                     break;
97                 case BGR_NEXTONE:
98                     if (isset($this->config->previous)) {
99                         $i = $this->config->previous + 1;
100                     } else {
101                         $i = 1;
102                     }
103                     if ($i > $numberofentries) {  // Loop back to beginning
104                         $i = 1;
105                     }
106                     $limitfrom = $i-1;
107                     $SORT = 'ASC';
108                     break;
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;
124                 default:  // BGR_LASTMODIFIED
125                     $i = $numberofentries;
126                     $limitfrom = 0;
127                     $SORT = 'DESC';
128                     break;
129             }
131             if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
132                                                  FROM {glossary_entries}
133                                                 WHERE glossaryid = ? AND approved = 1
134                                              ORDER BY $BROWSE $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
136                 $entry = reset($entry);
138                 if (empty($this->config->showconcept)) {
139                     $text = '';
140                 } else {
141                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
142                 }
144                 $options = new stdClass();
145                 $options->trusted = $entry->definitiontrust;
146                 $options->overflowdiv = true;
147                 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
148                 $text .= format_text($entry->definition, $entry->definitionformat, $options);
150                 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
151                 $this->config->previous = $i;
153             } else {
154                 $text = get_string('noentriesyet','block_glossary_random');
155             }
156             // store the text
157             $this->config->cache = $text;
158             $this->instance_config_commit();
159         }
160     }
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     }
168     function get_content() {
169         global $USER, $CFG, $DB;
171         if (empty($this->config->glossary)) {
172             $this->content = new stdClass();
173             if ($this->user_can_edit()) {
174                 $this->content->text = get_string('notyetconfigured','block_glossary_random');
175             } else {
176                 $this->content->text = '';
177             }
178             $this->content->footer = '';
179             return $this->content;
180         }
182         require_once($CFG->dirroot.'/course/lib.php');
184         // If $this->config->globalglossary is not set then get glossary info from db.
185         if (!isset($this->config->globalglossary)) {
186             if (!$glossary = $DB->get_record('glossary', array('id' => $this->config->glossary))) {
187                 return '';
188             } else {
189                 $this->config->courseid = $glossary->course;
190                 $this->config->globalglossary = $glossary->globalglossary;
191                 $this->instance_config_commit();
192             }
193         }
195         $modinfo = get_fast_modinfo($this->config->courseid);
196         // If deleted glossary or non-global glossary on different course page, then reset.
197         if (!isset($modinfo->instances['glossary'][$this->config->glossary])
198                 || ((empty($this->config->globalglossary) && ($this->config->courseid != $this->page->course->id)))) {
199             $this->config->glossary = 0;
200             $this->config->cache = '';
201             $this->instance_config_commit();
203             $this->content = new stdClass();
204             if ($this->user_can_edit()) {
205                 $this->content->text = get_string('notyetconfigured','block_glossary_random');
206             } else {
207                 $this->content->text = '';
208             }
209             $this->content->footer = '';
210             return $this->content;
211         }
213         $cm = $modinfo->instances['glossary'][$this->config->glossary];
214         if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
215             return '';
216         }
218         if (empty($this->config->cache)) {
219             $this->config->cache = '';
220         }
222         if ($this->content !== NULL) {
223             return $this->content;
224         }
226         $this->content = new stdClass();
228         // Show glossary if visible and place links in footer.
229         if ($cm->visible) {
230             $this->content->text = $this->config->cache;
231             if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
232                 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
233                 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
234             } else {
235                 $this->content->footer = '';
236             }
238             $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
239                 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
241         // Otherwise just place some text, no link.
242         } else {
243             $this->content->footer = $this->config->invisible;
244         }
246         return $this->content;
247     }