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