Should be $COURSE not $course
[moodle-linuxchix.git] / blocks / glossary_random / block_glossary_random.php
blob315e83014b1e35dd7c9ebcea2c8d8ccc816b6a08
1 <?php // $Id$
3 define('BGR_RANDOMLY', '0');
4 define('BGR_LASTMODIFIED', '1');
5 define('BGR_NEXTONE', '2');
7 class block_glossary_random extends block_base {
8 function init() {
10 $this->title = get_string('blockname','block_glossary_random');
11 $this->version = 2005040500;
15 function specialization() {
16 global $CFG, $COURSE;
17 $this->course = $COURSE;
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');
22 } else {
23 $this->title = $this->config->title;
26 if (empty($this->config->glossary)) {
27 return false;
30 if (!isset($this->config->nexttime)) {
31 $this->config->nexttime = 0;
34 //check if it's time to put a new entry in cache
35 if (time() > $this->config->nexttime) {
37 // place glossary concept and definition in $pref->cache
38 if (!$numberofentries = count_records('glossary_entries','glossaryid',$this->config->glossary,
39 'approved',1)) {
40 $this->config->cache = get_string('noentriesyet','block_glossary_random');
41 $this->instance_config_commit();
44 $limitfrom = 0;
45 $limitnum = 1;
47 switch ($this->config->type) {
49 case BGR_RANDOMLY:
50 $i = rand(1,$numberofentries);
51 $limitfrom = $i-1;
52 $SORT = 'ASC';
53 break;
55 case BGR_NEXTONE:
56 if (isset($this->config->previous)) {
57 $i = $this->config->previous + 1;
58 } else {
59 $i = 1;
61 if ($i > $numberofentries) { // Loop back to beginning
62 $i = 1;
64 $limitfrom = $i-1;
65 $SORT = 'ASC';
66 break;
68 default: // BGR_LASTMODIFIED
69 $i = $numberofentries;
70 $limitfrom = 0;
71 $SORT = 'DESC';
72 break;
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 '.
79 'ORDER BY timemodified '.$SORT, $limitfrom, $limitnum)) {
81 $entry = reset($entry);
83 if (empty($this->config->showconcept)) {
84 $text = '';
85 } else {
86 $text = "<h2>".format_string($entry->concept,true)."</h2>";
89 $options = new object;
90 $options->trusttext = true;
91 $text .= format_text($entry->definition, $entry->format, $options);
93 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
94 $this->config->previous = $i;
96 } else {
97 $text = get_string('noentriesyet','block_glossary_random');
99 // store the text
100 $this->config->cache = $text;
101 $this->instance_config_commit();
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;
111 function instance_config_print() {
112 global $CFG;
114 if (!isset($this->config)) {
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;
118 $this->config->showconcept = 1;
119 $this->config->cache= get_string('notyetconfigured','block_glossary_random');
120 $this->config->addentry=get_string('addentry', 'block_glossary_random');
121 $this->config->viewglossary=get_string('viewglossary', 'block_glossary_random');
122 $this->config->invisible=get_string('invisible', 'block_glossary_random');
125 // select glossaries to put in dropdown box ...
126 $glossaries = get_records_select_menu('glossary', 'course='.$this->course->id,'name','id,name');
128 //format menu texts to avoid html and to filter multilang values
129 if(!empty($glossaries)) {
130 foreach($glossaries as $key => $value) {
131 $glossaries[$key] = strip_tags(format_string($value,true));
135 // and select quotetypes to put in dropdown box
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');
140 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
142 // display the form
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()));
152 return true;
155 function get_content() {
156 global $USER, $CFG;
158 if (empty($this->config->glossary)) {
159 $this->content->text = get_string('notyetconfigured','block_glossary_random');
160 $this->content->footer = '';
161 return $this->content;
164 if (empty($this->config->cache)) {
165 $this->config->cache = '';
168 if ($this->content !== NULL) {
169 return $this->content;
172 $this->content = new stdClass;
173 $this->content->text = $this->config->cache;
175 // place link to glossary in the footer if the glossary is visible
176 $glossaryid = $this->config->glossary;
177 $glossary=get_record('glossary', 'id', $glossaryid);
179 //Create a temp valid module structure (course,id)
180 $tempmod->course = $this->course->id;
181 $tempmod->id = $glossaryid;
183 //Obtain the visible property from the instance
184 if (instance_is_visible('glossary', $tempmod)) {
185 $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
186 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
187 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
188 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
189 } else {
190 $this->content->footer = '';
193 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
194 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
196 // otherwise just place some text, no link
197 } else {
198 $this->content->footer = $this->config->invisible;
201 return $this->content;
204 function hide_header() {
205 if (empty($this->config->title)) {
206 return true;
208 return false;