Merge branch 'wip-MDL-35842-master' of git://github.com/phalacee/moodle
[moodle.git] / blocks / glossary_random / block_glossary_random.php
1 <?php
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 {
10     function init() {
11         $this->title = get_string('pluginname','block_glossary_random');
12     }
14     function applicable_formats() {
15         return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
16     }
18     function specialization() {
19         global $CFG, $DB;
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');
28         } else {
29             $this->title = $this->config->title;
30         }
32         if (empty($this->config->glossary)) {
33             return false;
34         }
36         if (!isset($this->config->nexttime)) {
37             $this->config->nexttime = 0;
38         }
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();
48             }
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)) {
52                 return false;
53             }
54             $glossaryctx = context_module::instance($cm->id);
56             $limitfrom = 0;
57             $limitnum = 1;
59             $BROWSE = 'timemodified';
61             switch ($this->config->type) {
63                 case BGR_RANDOMLY:
64                     $i = rand(1,$numberofentries);
65                     $limitfrom = $i-1;
66                     $SORT = 'ASC';
67                     break;
69                 case BGR_NEXTONE:
70                     if (isset($this->config->previous)) {
71                         $i = $this->config->previous + 1;
72                     } else {
73                         $i = 1;
74                     }
75                     if ($i > $numberofentries) {  // Loop back to beginning
76                         $i = 1;
77                     }
78                     $limitfrom = $i-1;
79                     $SORT = 'ASC';
80                     break;
82                 case BGR_NEXTALPHA:
83                     $BROWSE = 'concept';
84                     if (isset($this->config->previous)) {
85                         $i = $this->config->previous + 1;
86                     } else {
87                         $i = 1;
88                     }
89                     if ($i > $numberofentries) {  // Loop back to beginning
90                         $i = 1;
91                     }
92                     $limitfrom = $i-1;
93                     $SORT = 'ASC';
94                     break;
96                 default:  // BGR_LASTMODIFIED
97                     $i = $numberofentries;
98                     $limitfrom = 0;
99                     $SORT = 'DESC';
100                     break;
101             }
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)) {
111                     $text = '';
112                 } else {
113                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
114                 }
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;
125             } else {
126                 $text = get_string('noentriesyet','block_glossary_random');
127             }
128             // store the text
129             $this->config->cache = $text;
130             $this->instance_config_commit();
131         }
132     }
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
137         return true;
138     }
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;
148         }
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;
166         }
168         $cm = $modinfo->instances['glossary'][$glossaryid];
170         if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
171             return '';
172         }
174         if (empty($this->config->cache)) {
175             $this->config->cache = '';
176         }
178         if ($this->content !== NULL) {
179             return $this->content;
180         }
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 />';
192             } else {
193                 $this->content->footer = '';
194             }
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
200         } else {
201             $this->content->footer = $this->config->invisible;
202         }
204         return $this->content;
205     }
207     function hide_header() {
208         if (empty($this->config->title)) {
209             return true;
210         }
211         return false;
212     }