MDL-65788 tool_policy: Prevent two modals rendering for guests.
[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
9c371085
MG
32 /**
33 * @var cm_info|stdClass has properties 'id' (course module id) and 'uservisible'
34 * (whether the glossary is visible to the current user)
35 */
36 protected $glossarycm = null;
37
163dc56b 38 function init() {
5a0b9156 39 $this->title = get_string('pluginname','block_glossary_random');
96978238 40 }
bfe20414 41
163dc56b 42 function specialization() {
cb640229 43 global $CFG, $DB;
3aa2fd20
PS
44
45 require_once($CFG->libdir . '/filelib.php');
46
cb640229 47 $this->course = $this->page->course;
96978238 48
163dc56b 49 // load userdefined title and make sure it's never empty
50 if (empty($this->config->title)) {
2a71e9f2 51 $this->title = get_string('pluginname','block_glossary_random');
96978238 52 } else {
a0b417da 53 $this->title = format_string($this->config->title, true, ['context' => $this->context]);
96978238 54 }
95c62b56 55
96978238 56 if (empty($this->config->glossary)) {
95c62b56 57 return false;
58 }
59
96978238 60 if (!isset($this->config->nexttime)) {
95c62b56 61 $this->config->nexttime = 0;
62 }
96978238 63
163dc56b 64 //check if it's time to put a new entry in cache
96978238 65 if (time() > $this->config->nexttime) {
66
9c371085
MG
67 if (!($cm = $this->get_glossary_cm()) || !$cm->uservisible) {
68 // Skip generating of the cache if we can't display anything to the current user.
69 return false;
70 }
71
96978238 72 // place glossary concept and definition in $pref->cache
f28f2d90 73 if (!$numberofentries = $DB->count_records('glossary_entries',
74 array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
96978238 75 $this->config->cache = get_string('noentriesyet','block_glossary_random');
6d908bc6 76 $this->instance_config_commit();
2896553e 77 }
78
fe2fdd11 79 $glossaryctx = context_module::instance($cm->id);
32df07b4 80
422770d8 81 $limitfrom = 0;
82 $limitnum = 1;
83
f7ff14a1 84 $orderby = 'timemodified ASC';
9e7d0979 85
96978238 86 switch ($this->config->type) {
87
88 case BGR_RANDOMLY:
9e21f12d 89 $i = ($numberofentries > 1) ? rand(1, $numberofentries) : 1;
422770d8 90 $limitfrom = $i-1;
96978238 91 break;
92
2896553e 93 case BGR_NEXTONE:
94 if (isset($this->config->previous)) {
96978238 95 $i = $this->config->previous + 1;
2896553e 96 } else {
97 $i = 1;
98 }
96978238 99 if ($i > $numberofentries) { // Loop back to beginning
2896553e 100 $i = 1;
96978238 101 }
422770d8 102 $limitfrom = $i-1;
2896553e 103 break;
96978238 104
9e7d0979 105 case BGR_NEXTALPHA:
f7ff14a1 106 $orderby = 'concept ASC';
9e7d0979
JF
107 if (isset($this->config->previous)) {
108 $i = $this->config->previous + 1;
109 } else {
110 $i = 1;
111 }
112 if ($i > $numberofentries) { // Loop back to beginning
113 $i = 1;
114 }
115 $limitfrom = $i-1;
9e7d0979
JF
116 break;
117
2896553e 118 default: // BGR_LASTMODIFIED
96978238 119 $i = $numberofentries;
422770d8 120 $limitfrom = 0;
f7ff14a1 121 $orderby = 'timemodified DESC, id DESC';
96978238 122 break;
123 }
2896553e 124
32df07b4 125 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
f28f2d90 126 FROM {glossary_entries}
127 WHERE glossaryid = ? AND approved = 1
f7ff14a1 128 ORDER BY $orderby", array($this->config->glossary), $limitfrom, $limitnum)) {
2896553e 129
cb88fbdd 130 $entry = reset($entry);
131
7ead0ce0 132 if (empty($this->config->showconcept)) {
837a0fe2 133 $text = '';
7ead0ce0 134 } else {
cc1a28f0 135 $text = "<h3>".format_string($entry->concept,true)."</h3>";
4ca6cfbf 136 }
fafa2265 137
dd4bee83 138 $options = new stdClass();
cbc2b5df 139 $options->trusted = $entry->definitiontrust;
367a75fa 140 $options->overflowdiv = true;
32df07b4 141 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
cbc2b5df 142 $text .= format_text($entry->definition, $entry->definitionformat, $options);
96978238 143
6d908bc6 144 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 145 $this->config->previous = $i;
146
147 } else {
148 $text = get_string('noentriesyet','block_glossary_random');
149 }
163dc56b 150 // store the text
6d908bc6 151 $this->config->cache = $text;
152 $this->instance_config_commit();
96978238 153 }
163dc56b 154 }
96978238 155
9c371085
MG
156 /**
157 * Replace the instance's configuration data with those currently in $this->config;
158 */
159 function instance_config_commit($nolongerused = false) {
160 // Unset config variables that are no longer used.
161 unset($this->config->globalglossary);
162 unset($this->config->courseid);
163 parent::instance_config_commit($nolongerused);
163dc56b 164 }
96978238 165
9c371085
MG
166 /**
167 * Checks if glossary is available - it should be either located in the same course or be global
168 *
169 * @return null|cm_info|stdClass object with properties 'id' (course module id) and 'uservisible'
170 */
171 protected function get_glossary_cm() {
172 global $DB;
96978238 173 if (empty($this->config->glossary)) {
9c371085
MG
174 // No glossary is configured.
175 return null;
2896553e 176 }
177
9c371085
MG
178 if (!empty($this->glossarycm)) {
179 return $this->glossarycm;
180 }
51edc06e 181
9c371085
MG
182 if (!empty($this->page->course->id)) {
183 // First check if glossary belongs to the current course (we don't need to make any DB queries to find it).
184 $modinfo = get_fast_modinfo($this->page->course);
185 if (isset($modinfo->instances['glossary'][$this->config->glossary])) {
186 $this->glossarycm = $modinfo->instances['glossary'][$this->config->glossary];
187 if ($this->glossarycm->uservisible) {
188 // The glossary is in the same course and is already visible to the current user,
189 // no need to check if it is global, save on DB query.
190 return $this->glossarycm;
191 }
95556ce4
RT
192 }
193 }
194
9c371085
MG
195 // Find course module id for the given glossary, only if it is global.
196 $cm = $DB->get_record_sql("SELECT cm.id, cm.visible AS uservisible
197 FROM {course_modules} cm
198 JOIN {modules} md ON md.id = cm.module
199 JOIN {glossary} g ON g.id = cm.instance
200 WHERE g.id = :instance AND md.name = :modulename AND g.globalglossary = 1",
201 ['instance' => $this->config->glossary, 'modulename' => 'glossary']);
202
203 if ($cm) {
204 // This is a global glossary, create an object with properties 'id' and 'uservisible'. We don't need any
205 // other information so why bother retrieving it. Full access check is skipped for global glossaries for
206 // performance reasons.
207 $this->glossarycm = $cm;
208 } else if (empty($this->glossarycm)) {
209 // Glossary does not exist. Remove it in the config so we don't repeat this check again later.
f9192b65 210 $this->config->glossary = 0;
f9192b65 211 $this->instance_config_commit();
9c371085 212 }
f9192b65 213
9c371085
MG
214 return $this->glossarycm;
215 }
216
217 function instance_allow_multiple() {
218 // Are you going to allow multiple instances of each block?
219 // If yes, then it is assumed that the block WILL USE per-instance configuration
220 return true;
221 }
222
223 function get_content() {
224 if ($this->content !== null) {
f9192b65 225 return $this->content;
226 }
9c371085 227 $this->content = (object)['text' => '', 'footer' => ''];
f9192b65 228
9c371085
MG
229 if (!$cm = $this->get_glossary_cm()) {
230 if ($this->user_can_edit()) {
231 $this->content->text = get_string('notyetconfigured', 'block_glossary_random');
232 }
233 return $this->content;
95a6ed77
AG
234 }
235
96978238 236 if (empty($this->config->cache)) {
2896553e 237 $this->config->cache = '';
238 }
96978238 239
9c371085
MG
240 if ($cm->uservisible) {
241 // Show glossary if visible and place links in footer.
95556ce4 242 $this->content->text = $this->config->cache;
fe2fdd11 243 if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
9c371085
MG
244 $this->content->footer = html_writer::link(new moodle_url('/mod/glossary/edit.php', ['cmid' => $cm->id]),
245 format_string($this->config->addentry)) . '<br/>';
4ca6cfbf
PS
246 }
247
9c371085
MG
248 $this->content->footer .= html_writer::link(new moodle_url('/mod/glossary/view.php', ['id' => $cm->id]),
249 format_string($this->config->viewglossary));
96978238 250 } else {
9c371085
MG
251 // Otherwise just place some text, no link.
252 $this->content->footer = format_string($this->config->invisible);
163dc56b 253 }
96978238 254
255 return $this->content;
256 }
163dc56b 257}
4ca6cfbf 258