MDL-11835 Changing default aggregateonlygraded to false
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
96978238 1<?php // $Id$
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() {
96978238 9
10 $this->title = get_string('blockname','block_glossary_random');
837a0fe2 11 $this->version = 2005040500;
96978238 12
13 }
14
163dc56b 15 function specialization() {
1a2ea6f4 16 global $CFG, $COURSE;
17 $this->course = $COURSE;
96978238 18
163dc56b 19 // load userdefined title and make sure it's never empty
20 if (empty($this->config->title)) {
21 $this->title = get_string('blockname','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
38 if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config->glossary,
2896553e 39 'approved',1)) {
96978238 40 $this->config->cache = get_string('noentriesyet','block_glossary_random');
6d908bc6 41 $this->instance_config_commit();
2896553e 42 }
43
422770d8 44 $limitfrom = 0;
45 $limitnum = 1;
46
96978238 47 switch ($this->config->type) {
48
49 case BGR_RANDOMLY:
50 $i = rand(1,$numberofentries);
422770d8 51 $limitfrom = $i-1;
2896553e 52 $SORT = 'ASC';
96978238 53 break;
54
2896553e 55 case BGR_NEXTONE:
56 if (isset($this->config->previous)) {
96978238 57 $i = $this->config->previous + 1;
2896553e 58 } else {
59 $i = 1;
60 }
96978238 61 if ($i > $numberofentries) { // Loop back to beginning
2896553e 62 $i = 1;
96978238 63 }
422770d8 64 $limitfrom = $i-1;
2896553e 65 $SORT = 'ASC';
66 break;
96978238 67
2896553e 68 default: // BGR_LASTMODIFIED
96978238 69 $i = $numberofentries;
422770d8 70 $limitfrom = 0;
2896553e 71 $SORT = 'DESC';
96978238 72 break;
73 }
2896553e 74
fda5a233 75 if ($entry = get_records_sql(' SELECT concept, definition, format '.
76 ' FROM '.$CFG->prefix.'glossary_entries'.
77 ' WHERE glossaryid = '.$this->config->glossary.
78 ' AND approved = 1 '.
422770d8 79 'ORDER BY timemodified '.$SORT, $limitfrom, $limitnum)) {
2896553e 80
cb88fbdd 81 $entry = reset($entry);
82
7ead0ce0 83 if (empty($this->config->showconcept)) {
837a0fe2 84 $text = '';
7ead0ce0 85 } else {
1d3a7a85 86 $text = "<h2>".format_string($entry->concept,true)."</h2>";
837a0fe2 87 }
fafa2265 88
89 $options = new object;
90 $options->trusttext = true;
91 $text .= format_text($entry->definition, $entry->format, $options);
96978238 92
6d908bc6 93 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 94 $this->config->previous = $i;
95
96 } else {
97 $text = get_string('noentriesyet','block_glossary_random');
98 }
163dc56b 99 // store the text
6d908bc6 100 $this->config->cache = $text;
101 $this->instance_config_commit();
96978238 102 }
163dc56b 103 }
96978238 104
163dc56b 105 function instance_allow_multiple() {
106 // Are you going to allow multiple instances of each block?
107 // If yes, then it is assumed that the block WILL USE per-instance configuration
108 return true;
109 }
96978238 110
163dc56b 111 function instance_config_print() {
96978238 112 global $CFG;
113
ed4c7bd3 114 if (!isset($this->config)) {
163dc56b 115 // ... teacher has not yet configured the block, let's put some default values here to explain things
116 $this->config->title = get_string('blockname','block_glossary_random');
117 $this->config->refresh = 0;
837a0fe2 118 $this->config->showconcept = 1;
2896553e 119 $this->config->cache= get_string('notyetconfigured','block_glossary_random');
120 $this->config->addentry=get_string('addentry', 'block_glossary_random');
96978238 121 $this->config->viewglossary=get_string('viewglossary', 'block_glossary_random');
2896553e 122 $this->config->invisible=get_string('invisible', 'block_glossary_random');
163dc56b 123 }
96978238 124
163dc56b 125 // select glossaries to put in dropdown box ...
96978238 126 $glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name');
30d5bd79 127
1d3a7a85 128 //format menu texts to avoid html and to filter multilang values
30d5bd79 129 if(!empty($glossaries)) {
130 foreach($glossaries as $key => $value) {
131 $glossaries[$key] = strip_tags(format_string($value,true));
132 }
1d3a7a85 133 }
96978238 134
163dc56b 135 // and select quotetypes to put in dropdown box
96978238 136 $type[0] = get_string('random','block_glossary_random');
137 $type[1] = get_string('lastmodified','block_glossary_random');
138 $type[2] = get_string('nextone','block_glossary_random');
139
ed4c7bd3 140 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 141
163dc56b 142 // display the form
96978238 143
163dc56b 144 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
145 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
146 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
147 print_simple_box_end();
148 } else {
149 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
150 }
96978238 151
2896553e 152 return true;
163dc56b 153 }
96978238 154
163dc56b 155 function get_content() {
156 global $USER, $CFG;
95c62b56 157
96978238 158 if (empty($this->config->glossary)) {
159 $this->content->text = get_string('notyetconfigured','block_glossary_random');
160 $this->content->footer = '';
161 return $this->content;
2896553e 162 }
163
96978238 164 if (empty($this->config->cache)) {
2896553e 165 $this->config->cache = '';
166 }
96978238 167
2896553e 168 if ($this->content !== NULL) {
95c62b56 169 return $this->content;
170 }
2896553e 171
96978238 172 $this->content = new stdClass;
2896553e 173 $this->content->text = $this->config->cache;
96978238 174
175 // place link to glossary in the footer if the glossary is visible
163dc56b 176 $glossaryid = $this->config->glossary;
96978238 177 $glossary=get_record('glossary', 'id', $glossaryid);
96978238 178
163dc56b 179 //Create a temp valid module structure (course,id)
2896553e 180 $tempmod->course = $this->course->id;
181 $tempmod->id = $glossaryid;
96978238 182
183 //Obtain the visible property from the instance
163dc56b 184 if (instance_is_visible('glossary', $tempmod)) {
163dc56b 185 $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
e73bfa2d 186 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
7ead0ce0 187 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
ed4c7bd3 188 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 189 } else {
7ead0ce0 190 $this->content->footer = '';
191 }
ed4c7bd3 192
193 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
194 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 195
163dc56b 196 // otherwise just place some text, no link
96978238 197 } else {
198 $this->content->footer = $this->config->invisible;
163dc56b 199 }
96978238 200
201 return $this->content;
202 }
203
163dc56b 204 function hide_header() {
96978238 205 if (empty($this->config->title)) {
163dc56b 206 return true;
95c62b56 207 }
208 return false;
163dc56b 209 }
96978238 210
163dc56b 211}
212?>