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 $BROWSE = 'timemodified';
89 switch ($this->config
->type
) {
92 $i = rand(1,$numberofentries);
98 if (isset($this->config
->previous
)) {
99 $i = $this->config
->previous +
1;
103 if ($i > $numberofentries) { // Loop back to beginning
112 if (isset($this->config
->previous
)) {
113 $i = $this->config
->previous +
1;
117 if ($i > $numberofentries) { // Loop back to beginning
124 default: // BGR_LASTMODIFIED
125 $i = $numberofentries;
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
)) {
141 $text = "<h3>".format_string($entry->concept
,true)."</h3>";
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;
154 $text = get_string('noentriesyet','block_glossary_random');
157 $this->config
->cache
= $text;
158 $this->instance_config_commit();
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
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');
176 $this->content
->text
= '';
178 $this->content
->footer
= '';
179 return $this->content
;
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
))) {
189 $this->config
->courseid
= $glossary->course
;
190 $this->config
->globalglossary
= $glossary->globalglossary
;
191 $this->instance_config_commit();
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');
207 $this->content
->text
= '';
209 $this->content
->footer
= '';
210 return $this->content
;
213 $cm = $modinfo->instances
['glossary'][$this->config
->glossary
];
214 if (!has_capability('mod/glossary:view', context_module
::instance($cm->id
))) {
218 if (empty($this->config
->cache
)) {
219 $this->config
->cache
= '';
222 if ($this->content
!== NULL) {
223 return $this->content
;
226 $this->content
= new stdClass();
228 // Show glossary if visible and place links in footer.
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 />';
235 $this->content
->footer
= '';
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.
243 $this->content
->footer
= $this->config
->invisible
;
246 return $this->content
;