MDL-36737 fix random glossary E_STRICT issue
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
4ca6cfbf 1<?php
2896553e 2
3define('BGR_RANDOMLY', '0');
4define('BGR_LASTMODIFIED', '1');
5define('BGR_NEXTONE', '2');
163dc56b 6
7class block_glossary_random extends block_base {
8 function init() {
5a0b9156 9 $this->title = get_string('pluginname','block_glossary_random');
96978238 10 }
11
163dc56b 12 function specialization() {
cb640229 13 global $CFG, $DB;
3aa2fd20
PS
14
15 require_once($CFG->libdir . '/filelib.php');
16
cb640229 17 $this->course = $this->page->course;
96978238 18
163dc56b 19 // load userdefined title and make sure it's never empty
20 if (empty($this->config->title)) {
2a71e9f2 21 $this->title = get_string('pluginname','block_glossary_random');
96978238 22 } else {
163dc56b 23 $this->title = $this->config->title;
96978238 24 }
95c62b56 25
96978238 26 if (empty($this->config->glossary)) {
95c62b56 27 return false;
28 }
29
96978238 30 if (!isset($this->config->nexttime)) {
95c62b56 31 $this->config->nexttime = 0;
32 }
96978238 33
163dc56b 34 //check if it's time to put a new entry in cache
96978238 35 if (time() > $this->config->nexttime) {
36
37 // place glossary concept and definition in $pref->cache
f28f2d90 38 if (!$numberofentries = $DB->count_records('glossary_entries',
39 array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
96978238 40 $this->config->cache = get_string('noentriesyet','block_glossary_random');
6d908bc6 41 $this->instance_config_commit();
2896553e 42 }
43
32df07b4
EL
44 // Get module and context, to be able to rewrite urls
45 if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
46 return false;
47 }
48 $glossaryctx = get_context_instance(CONTEXT_MODULE, $cm->id);
49
422770d8 50 $limitfrom = 0;
51 $limitnum = 1;
52
96978238 53 switch ($this->config->type) {
54
55 case BGR_RANDOMLY:
56 $i = rand(1,$numberofentries);
422770d8 57 $limitfrom = $i-1;
2896553e 58 $SORT = 'ASC';
96978238 59 break;
60
2896553e 61 case BGR_NEXTONE:
62 if (isset($this->config->previous)) {
96978238 63 $i = $this->config->previous + 1;
2896553e 64 } else {
65 $i = 1;
66 }
96978238 67 if ($i > $numberofentries) { // Loop back to beginning
2896553e 68 $i = 1;
96978238 69 }
422770d8 70 $limitfrom = $i-1;
2896553e 71 $SORT = 'ASC';
72 break;
96978238 73
2896553e 74 default: // BGR_LASTMODIFIED
96978238 75 $i = $numberofentries;
422770d8 76 $limitfrom = 0;
2896553e 77 $SORT = 'DESC';
96978238 78 break;
79 }
2896553e 80
32df07b4 81 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
f28f2d90 82 FROM {glossary_entries}
83 WHERE glossaryid = ? AND approved = 1
84 ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
2896553e 85
cb88fbdd 86 $entry = reset($entry);
87
7ead0ce0 88 if (empty($this->config->showconcept)) {
837a0fe2 89 $text = '';
7ead0ce0 90 } else {
cc1a28f0 91 $text = "<h3>".format_string($entry->concept,true)."</h3>";
4ca6cfbf 92 }
fafa2265 93
dd4bee83 94 $options = new stdClass();
cbc2b5df 95 $options->trusted = $entry->definitiontrust;
367a75fa 96 $options->overflowdiv = true;
32df07b4 97 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
cbc2b5df 98 $text .= format_text($entry->definition, $entry->definitionformat, $options);
96978238 99
6d908bc6 100 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 101 $this->config->previous = $i;
102
103 } else {
104 $text = get_string('noentriesyet','block_glossary_random');
105 }
163dc56b 106 // store the text
6d908bc6 107 $this->config->cache = $text;
108 $this->instance_config_commit();
96978238 109 }
163dc56b 110 }
96978238 111
163dc56b 112 function instance_allow_multiple() {
113 // Are you going to allow multiple instances of each block?
114 // If yes, then it is assumed that the block WILL USE per-instance configuration
115 return true;
116 }
96978238 117
163dc56b 118 function get_content() {
cb640229 119 global $USER, $CFG, $DB;
95c62b56 120
96978238 121 if (empty($this->config->glossary)) {
b85b25eb 122 $this->content = new stdClass();
96978238 123 $this->content->text = get_string('notyetconfigured','block_glossary_random');
124 $this->content->footer = '';
125 return $this->content;
2896553e 126 }
127
51edc06e 128 require_once($CFG->dirroot.'/course/lib.php');
bcab2b21 129 $course = $this->page->course;
51edc06e 130 $modinfo = get_fast_modinfo($course);
bcab2b21 131 $glossaryid = $this->config->glossary;
51edc06e 132
133 if (!isset($modinfo->instances['glossary'][$glossaryid])) {
f9192b65 134 // we can get here if the glossary has been deleted, so
135 // unconfigure the glossary from the block..
136 $this->config->glossary = 0;
137 $this->config->cache = '';
138 $this->instance_config_commit();
139
b03f7353 140 $this->content = new stdClass();
f9192b65 141 $this->content->text = get_string('notyetconfigured','block_glossary_random');
142 $this->content->footer = '';
143 return $this->content;
144 }
145
8199b874
AG
146 $cm = $modinfo->instances['glossary'][$glossaryid];
147
148 if (!has_capability('mod/glossary:view', get_context_instance(CONTEXT_MODULE, $cm->id))) {
149 return '';
150 }
151
96978238 152 if (empty($this->config->cache)) {
2896553e 153 $this->config->cache = '';
154 }
96978238 155
2896553e 156 if ($this->content !== NULL) {
95c62b56 157 return $this->content;
158 }
2896553e 159
b85b25eb 160 $this->content = new stdClass();
2896553e 161 $this->content->text = $this->config->cache;
96978238 162
163 // place link to glossary in the footer if the glossary is visible
96978238 164
96978238 165 //Obtain the visible property from the instance
51edc06e 166 if ($cm->uservisible) {
e73bfa2d 167 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
9f76d281 168 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
ed4c7bd3 169 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 170 } else {
7ead0ce0 171 $this->content->footer = '';
4ca6cfbf
PS
172 }
173
ed4c7bd3 174 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
175 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 176
163dc56b 177 // otherwise just place some text, no link
96978238 178 } else {
179 $this->content->footer = $this->config->invisible;
163dc56b 180 }
96978238 181
182 return $this->content;
183 }
184
163dc56b 185 function hide_header() {
96978238 186 if (empty($this->config->title)) {
163dc56b 187 return true;
95c62b56 188 }
189 return false;
163dc56b 190 }
163dc56b 191}
4ca6cfbf 192