Fixed a path problem that occurs while unzipping
[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() {
16 global $CFG;
17 $this->course = get_record('course', 'id', $this->instance->pageid);
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');
1d3a7a85 121 //format menu texts to avoid html and to filter multilang values
122 foreach($glossaries as $key => $value) {
123 $glossaries[$key] = strip_tags(format_string($value,true));
124 }
96978238 125
163dc56b 126 // and select quotetypes to put in dropdown box
96978238 127 $type[0] = get_string('random','block_glossary_random');
128 $type[1] = get_string('lastmodified','block_glossary_random');
129 $type[2] = get_string('nextone','block_glossary_random');
130
ed4c7bd3 131 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
96978238 132
163dc56b 133 // display the form
96978238 134
163dc56b 135 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
136 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
137 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
138 print_simple_box_end();
139 } else {
140 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
141 }
96978238 142
2896553e 143 return true;
163dc56b 144 }
96978238 145
163dc56b 146 function get_content() {
147 global $USER, $CFG;
95c62b56 148
96978238 149 if (empty($this->config->glossary)) {
150 $this->content->text = get_string('notyetconfigured','block_glossary_random');
151 $this->content->footer = '';
152 return $this->content;
2896553e 153 }
154
96978238 155 if (empty($this->config->cache)) {
2896553e 156 $this->config->cache = '';
157 }
96978238 158
2896553e 159 if ($this->content !== NULL) {
95c62b56 160 return $this->content;
161 }
2896553e 162
96978238 163 $this->content = new stdClass;
2896553e 164 $this->content->text = $this->config->cache;
96978238 165
166 // place link to glossary in the footer if the glossary is visible
163dc56b 167 $glossaryid = $this->config->glossary;
96978238 168 $glossary=get_record('glossary', 'id', $glossaryid);
163dc56b 169 $studentcanpost = $glossary->studentcanpost; //needed to decide on which footer
96978238 170
163dc56b 171 //Create a temp valid module structure (course,id)
2896553e 172 $tempmod->course = $this->course->id;
173 $tempmod->id = $glossaryid;
96978238 174
175 //Obtain the visible property from the instance
163dc56b 176 if (instance_is_visible('glossary', $tempmod)) {
163dc56b 177 $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
178 if ($studentcanpost) {
7ead0ce0 179 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
ed4c7bd3 180 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
837a0fe2 181 } else {
7ead0ce0 182 $this->content->footer = '';
183 }
ed4c7bd3 184
185 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
186 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
96978238 187
163dc56b 188 // otherwise just place some text, no link
96978238 189 } else {
190 $this->content->footer = $this->config->invisible;
163dc56b 191 }
96978238 192
193 return $this->content;
194 }
195
163dc56b 196 function hide_header() {
96978238 197 if (empty($this->config->title)) {
163dc56b 198 return true;
95c62b56 199 }
200 return false;
163dc56b 201 }
96978238 202
163dc56b 203}
204?>