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