Fixed mistake on url-trimming and added some debug info.
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
163dc56b 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() {
9
10 $this->title = get_string('blockname','block_glossary_random');
11 $this->content_type = BLOCK_TYPE_TEXT;
12 $this->version = 2005010300;
13
14 }
15
16 function specialization() {
17 global $CFG;
18 $this->course = get_record('course', 'id', $this->instance->pageid);
19
20 // load userdefined title and make sure it's never empty
21 if (empty($this->config->title)) {
22 $this->title = get_string('blockname','block_glossary_random');
23 } else {
24 $this->title = $this->config->title;
25 }
95c62b56 26
27 if (empty($this->config->glossary)) {
28 return false;
29 }
30
31 if (!isset($this->config->nexttime)) {
32 $this->config->nexttime = 0;
33 }
163dc56b 34
35 //check if it's time to put a new entry in cache
36 if (time() > $this->config->nexttime) {
37
38 // place glossary concept and definition in $pref->cache
2896553e 39 if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config->glossary,
40 'approved',1)) {
41 $this->config->cache = get_string('noentriesyet','block_glossary_random');
42 parent::instance_config_save($this->config);
43 }
44
163dc56b 45 switch ($this->config->type) {
46
2896553e 47 case BGR_RANDOMLY:
48 $i = rand(1,$numberofentries);
49 $LIMIT = sql_paging_limit($i-1, 1);
50 $SORT = 'ASC';
163dc56b 51 break;
52
2896553e 53 case BGR_NEXTONE:
54 if (isset($this->config->previous)) {
55 $i = $this->config->previous + 1;
56 } else {
57 $i = 1;
58 }
59 if ($i > $numberofentries) { // Loop back to beginning
60 $i = 1;
163dc56b 61 }
2896553e 62 $LIMIT = sql_paging_limit($i-1, 1);
63 $SORT = 'ASC';
64 break;
163dc56b 65
2896553e 66 default: // BGR_LASTMODIFIED
67 $i = $numberofentries;
68 $LIMIT = 'LIMIT 1'; // The last one
69 $SORT = 'DESC';
163dc56b 70 break;
163dc56b 71 }
2896553e 72
73 if ($entry = get_record_sql(' SELECT concept, definition, format '.
74 ' FROM '.$CFG->prefix.'glossary_entries'.
75 ' WHERE glossaryid = '.$this->config->glossary.
76 ' AND approved = 1 '.
77 'ORDER BY timemodified '.$SORT.' '.$LIMIT)) {
78
79 $text = "<b>$entry->concept</b><br />";
80 $text .= format_text($entry->definition, $entry->format);
163dc56b 81
82 $this->config->nexttime = usergetmidnight(time())+60*60*24*$this->config->refresh;
83 $this->config->previous = $i;
84
85 } else {
2896553e 86 $text = get_string('noentriesyet','block_glossary_random');
163dc56b 87 }
88 // store the text
89 $this->config->cache= $text;
90 parent::instance_config_save($this->config);
91 }
92 }
93
94 function instance_allow_multiple() {
95 // Are you going to allow multiple instances of each block?
96 // If yes, then it is assumed that the block WILL USE per-instance configuration
97 return true;
98 }
99
100 function instance_config_print() {
101 global $CFG;
102
103 if (empty($this->config->nexttime)) {
104 // ... teacher has not yet configured the block, let's put some default values here to explain things
105 $this->config->title = get_string('blockname','block_glossary_random');
106 $this->config->refresh = 0;
107
2896553e 108 $this->config->cache= get_string('notyetconfigured','block_glossary_random');
109 $this->config->addentry=get_string('addentry', 'block_glossary_random');
110 $this->config->viewglossary=get_string('viewglossary', 'block_glossary_random');
111 $this->config->invisible=get_string('invisible', 'block_glossary_random');
163dc56b 112 }
113
114 // select glossaries to put in dropdown box ...
2896553e 115 $glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name');
163dc56b 116
117 // and select quotetypes to put in dropdown box
2896553e 118 $type[0] = get_string('random','block_glossary_random');
119 $type[1] = get_string('lastmodified','block_glossary_random');
120 $type[2] = get_string('nextone','block_glossary_random');
163dc56b 121
122 $this->config->nexttime = usergetmidnight(time())+24*60*60*$this->config->refresh;
123
124 // display the form
125
126 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
127 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
128 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
129 print_simple_box_end();
130 } else {
131 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
132 }
133
2896553e 134 return true;
163dc56b 135 }
136
137 function get_content() {
138 global $USER, $CFG;
95c62b56 139
140 if (empty($this->config->glossary)) {
2896553e 141 $this->content->text = get_string('notyetconfigured','block_glossary_random');
142 return $this->content->text;
143 }
144
145 if (empty($this->config->cache)) {
146 $this->config->cache = '';
147 }
148
149 if ($this->content !== NULL) {
95c62b56 150 return $this->content;
151 }
2896553e 152
153 $this->content = new stdClass;
154 $this->content->text = $this->config->cache;
163dc56b 155
156 // place link to glossary in the footer if the glossary is visible
157 $glossaryid = $this->config->glossary;
2896553e 158 $glossary=get_record('glossary', 'id', $glossaryid);
163dc56b 159 $studentcanpost = $glossary->studentcanpost; //needed to decide on which footer
160
161 //Create a temp valid module structure (course,id)
2896553e 162 $tempmod->course = $this->course->id;
163 $tempmod->id = $glossaryid;
163dc56b 164
165 //Obtain the visible property from the instance
166 if (instance_is_visible('glossary', $tempmod)) {
167
168 $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
169 if ($studentcanpost) {
170 $footertext = $this->config->addentry;
171 } else {
172 $footertext = $this->config->viewglossary;
173 }
174 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/'
175 .(($studentcanpost == 1)?'edit':'view').'.php?id='.$cm->id
176 .'" title="'.$footertext.'">'.$footertext.'</a>';
177
178 // otherwise just place some text, no link
179 } else {
180 $this->content->footer = $this->config->invisible;
181 }
182
183 return $this->content;
184 }
185
186 function hide_header() {
95c62b56 187 if (empty($this->config->title)) {
163dc56b 188 return true;
95c62b56 189 }
190 return false;
163dc56b 191 }
192
193}
194?>