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