Merge branch 'wip-MDL-35891-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');
7 class block_glossary_random extends block_base {
9     function init() {
10         $this->title = get_string('pluginname','block_glossary_random');
11     }
13     function applicable_formats() {
14         return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
15     }
17     function specialization() {
18         global $CFG, $DB;
20         require_once($CFG->libdir . '/filelib.php');
22         $this->course = $this->page->course;
24         // load userdefined title and make sure it's never empty
25         if (empty($this->config->title)) {
26             $this->title = get_string('pluginname','block_glossary_random');
27         } else {
28             $this->title = $this->config->title;
29         }
31         if (empty($this->config->glossary)) {
32             return false;
33         }
35         if (!isset($this->config->nexttime)) {
36             $this->config->nexttime = 0;
37         }
39         //check if it's time to put a new entry in cache
40         if (time() > $this->config->nexttime) {
42             // place glossary concept and definition in $pref->cache
43             if (!$numberofentries = $DB->count_records('glossary_entries',
44                                                        array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
45                 $this->config->cache = get_string('noentriesyet','block_glossary_random');
46                 $this->instance_config_commit();
47             }
49             // Get module and context, to be able to rewrite urls
50             if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
51                 return false;
52             }
53             $glossaryctx = context_module::instance($cm->id);
55             $limitfrom = 0;
56             $limitnum = 1;
58             switch ($this->config->type) {
60                 case BGR_RANDOMLY:
61                     $i = rand(1,$numberofentries);
62                     $limitfrom = $i-1;
63                     $SORT = 'ASC';
64                     break;
66                 case BGR_NEXTONE:
67                     if (isset($this->config->previous)) {
68                         $i = $this->config->previous + 1;
69                     } else {
70                         $i = 1;
71                     }
72                     if ($i > $numberofentries) {  // Loop back to beginning
73                         $i = 1;
74                     }
75                     $limitfrom = $i-1;
76                     $SORT = 'ASC';
77                     break;
79                 default:  // BGR_LASTMODIFIED
80                     $i = $numberofentries;
81                     $limitfrom = 0;
82                     $SORT = 'DESC';
83                     break;
84             }
86             if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
87                                                  FROM {glossary_entries}
88                                                 WHERE glossaryid = ? AND approved = 1
89                                              ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
91                 $entry = reset($entry);
93                 if (empty($this->config->showconcept)) {
94                     $text = '';
95                 } else {
96                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
97                 }
99                 $options = new stdClass();
100                 $options->trusted = $entry->definitiontrust;
101                 $options->overflowdiv = true;
102                 $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
103                 $text .= format_text($entry->definition, $entry->definitionformat, $options);
105                 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
106                 $this->config->previous = $i;
108             } else {
109                 $text = get_string('noentriesyet','block_glossary_random');
110             }
111             // store the text
112             $this->config->cache = $text;
113             $this->instance_config_commit();
114         }
115     }
117     function instance_allow_multiple() {
118     // Are you going to allow multiple instances of each block?
119     // If yes, then it is assumed that the block WILL USE per-instance configuration
120         return true;
121     }
123     function get_content() {
124         global $USER, $CFG, $DB;
126         if (empty($this->config->glossary)) {
127             $this->content = new stdClass();
128             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
129             $this->content->footer = '';
130             return $this->content;
131         }
133         require_once($CFG->dirroot.'/course/lib.php');
134         $course = $this->page->course;
135         $modinfo = get_fast_modinfo($course);
136         $glossaryid = $this->config->glossary;
138         if (!isset($modinfo->instances['glossary'][$glossaryid])) {
139             // we can get here if the glossary has been deleted, so
140             // unconfigure the glossary from the block..
141             $this->config->glossary = 0;
142             $this->config->cache = '';
143             $this->instance_config_commit();
145             $this->content = new stdClass();
146             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
147             $this->content->footer = '';
148             return $this->content;
149         }
151         $cm = $modinfo->instances['glossary'][$glossaryid];
153         if (!has_capability('mod/glossary:view', context_module::instance($cm->id))) {
154             return '';
155         }
157         if (empty($this->config->cache)) {
158             $this->config->cache = '';
159         }
161         if ($this->content !== NULL) {
162             return $this->content;
163         }
165         $this->content = new stdClass();
166         $this->content->text = $this->config->cache;
168         // place link to glossary in the footer if the glossary is visible
170         //Obtain the visible property from the instance
171         if ($cm->uservisible) {
172             if (has_capability('mod/glossary:write', context_module::instance($cm->id))) {
173                 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
174                 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
175             } else {
176                 $this->content->footer = '';
177             }
179             $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
180                 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
182         // otherwise just place some text, no link
183         } else {
184             $this->content->footer = $this->config->invisible;
185         }
187         return $this->content;
188     }
190     function hide_header() {
191         if (empty($this->config->title)) {
192             return true;
193         }
194         return false;
195     }