3 define('BGR_RANDOMLY', '0');
4 define('BGR_LASTMODIFIED', '1');
5 define('BGR_NEXTONE', '2');
6 define('BGR_NEXTALPHA', '3');
8 class block_glossary_random extends block_base {
11 $this->title = get_string('pluginname','block_glossary_random');
14 function applicable_formats() {
15 return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
18 function specialization() {
21 require_once($CFG->libdir . '/filelib.php');
23 $this->course = $this->page->course;
25 // load userdefined title and make sure it's never empty
26 if (empty($this->config->title)) {
27 $this->title = get_string('pluginname','block_glossary_random');
29 $this->title = $this->config->title;
32 if (empty($this->config->glossary)) {
36 if (!isset($this->config->nexttime)) {
37 $this->config->nexttime = 0;
40 //check if it's time to put a new entry in cache
41 if (time() > $this->config->nexttime) {
43 // place glossary concept and definition in $pref->cache
44 if (!$numberofentries = $DB->count_records('glossary_entries',
45 array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
46 $this->config->cache = get_string('noentriesyet','block_glossary_random');
47 $this->instance_config_commit();
50 // Get module and context, to be able to rewrite urls
51 if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
54 $glossaryctx = context_module::instance($cm->id);
59 $BROWSE = 'timemodified';
61 switch ($this->config->type) {
64 $i = rand(1,$numberofentries);
70 if (isset($this->config->previous)) {
71 $i = $this->config->previous + 1;
75 if ($i > $numberofentries) { // Loop back to beginning
84 if (isset($this->config->previous)) {
85 $i = $this->config->previous + 1;
89 if ($i > $numberofentries) { // Loop back to beginning
96 default: // BGR_LASTMODIFIED
97 $i = $numberofentries;
103 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
104 FROM {glossary_entries}
105 WHERE glossaryid = ? AND approved = 1
106 ORDER BY $BROWSE $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
108 $entry = reset($entry);
110 if (empty($this->config->showconcept)) {
113 $text = "<h3>".format_string($entry->concept,true)."</h3>";
116 $options = new stdClass();
117 $options->trusted = $entry->definitiontrust;
118 $options->overflowdiv = true;
119 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
120 $text .= format_text($entry->definition, $entry->definitionformat, $options);
122 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
123 $this->config->previous = $i;
126 $text = get_string('noentriesyet','block_glossary_random');
129 $this->config->cache = $text;
130 $this->instance_config_commit();
134 function instance_allow_multiple() {
135 // Are you going to allow multiple instances of each block?
136 // If yes, then it is assumed that the block WILL USE per-instance configuration
140 function get_content() {
141 global $USER, $CFG, $DB;
143 if (empty($this->config->glossary)) {
144 $this->content = new stdClass();
145 $this->content->text = get_string('notyetconfigured','block_glossary_random');
146 $this->content->footer = '';
147 return $this->content;
150 require_once($CFG->dirroot.'/course/lib.php');
151 $course = $this->page->course;
152 $modinfo = get_fast_modinfo($course);
153 $glossaryid = $this->config->glossary;
155 if (!isset($modinfo->instances['glossary'][$glossaryid])) {
156 // we can get here if the glossary has been deleted, so
157 // unconfigure the glossary from the block..
158 $this->config->glossary = 0;
159 $this->config->cache = '';
160 $this->instance_config_commit();
162 $this->content = new stdClass();
163 $this->content->text = get_string('notyetconfigured','block_glossary_random');
164 $this->content->footer = '';
165 return $this->content;
168 $cm = $modinfo->instances['glossary'][$glossaryid];
170 if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
174 if (empty($this->config->cache)) {
175 $this->config->cache = '';
178 if ($this->content !== NULL) {
179 return $this->content;
182 $this->content = new stdClass();
183 $this->content->text = $this->config->cache;
185 // place link to glossary in the footer if the glossary is visible
187 //Obtain the visible property from the instance
188 if ($cm->uservisible) {
189 if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
190 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
191 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
193 $this->content->footer = '';
196 $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
197 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
199 // otherwise just place some text, no link
201 $this->content->footer = $this->config->invisible;
204 return $this->content;
207 function hide_header() {
208 if (empty($this->config->title)) {