c98e9fe4e5eb40c1bf8a4b355c47db5e92901682
[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 {
8     function init() {
10         $this->title = get_string('blockname','block_glossary_random');
11         $this->version = 2007101509;
13     }
15     function specialization() {
16         global $CFG, $DB;
17         $this->course = $this->page->course;
19         // load userdefined title and make sure it's never empty
20         if (empty($this->config->title)) {
21             $this->title = get_string('pluginname','block_glossary_random');
22         } else {
23             $this->title = $this->config->title;
24         }
26         if (empty($this->config->glossary)) {
27             return false;
28         }
30         if (!isset($this->config->nexttime)) {
31             $this->config->nexttime = 0;
32         }
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 = $DB->count_records('glossary_entries',
39                                                        array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
40                 $this->config->cache = get_string('noentriesyet','block_glossary_random');
41                 $this->instance_config_commit();
42             }
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;
60                     }
61                     if ($i > $numberofentries) {  // Loop back to beginning
62                         $i = 1;
63                     }
64                     $limitfrom = $i-1;
65                     $SORT = 'ASC';
66                     break;
68                 default:  // BGR_LASTMODIFIED
69                     $i = $numberofentries;
70                     $limitfrom = 0;
71                     $SORT = 'DESC';
72                     break;
73             }
75             if ($entry = $DB->get_records_sql("SELECT concept, definition, definitionformat, definitiontrust
76                                                  FROM {glossary_entries}
77                                                 WHERE glossaryid = ? AND approved = 1
78                                              ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
80                 $entry = reset($entry);
82                 if (empty($this->config->showconcept)) {
83                     $text = '';
84                 } else {
85                     $text = "<h3>".format_string($entry->concept,true)."</h3>";
86                 }
88                 $options = new object;
89                 $options->trusted = $entry->definitiontrust;
90                 $text .= format_text($entry->definition, $entry->definitionformat, $options);
92                 $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
93                 $this->config->previous = $i;
95             } else {
96                 $text = get_string('noentriesyet','block_glossary_random');
97             }
98             // store the text
99             $this->config->cache = $text;
100             $this->instance_config_commit();
101         }
102     }
104     function instance_allow_multiple() {
105     // Are you going to allow multiple instances of each block?
106     // If yes, then it is assumed that the block WILL USE per-instance configuration
107         return true;
108     }
110     function get_content() {
111         global $USER, $CFG, $DB;
113         if (empty($this->config->glossary)) {
114             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
115             $this->content->footer = '';
116             return $this->content;
117         }
119         $glossaryid = $this->config->glossary;
121         $course = $this->page->course;
123         require_once($CFG->dirroot.'/course/lib.php');
124         $modinfo = get_fast_modinfo($course);
126         if (!isset($modinfo->instances['glossary'][$glossaryid])) {
127             // we can get here if the glossary has been deleted, so
128             // unconfigure the glossary from the block..
129             $this->config->glossary = 0;
130             $this->config->cache = '';
131             $this->instance_config_commit();
133             $this->content->text   = get_string('notyetconfigured','block_glossary_random');
134             $this->content->footer = '';
135             return $this->content;
136         }
138         $cm = $modinfo->instances['glossary'][$glossaryid];
140         if (empty($this->config->cache)) {
141             $this->config->cache = '';
142         }
144         if ($this->content !== NULL) {
145             return $this->content;
146         }
148         $this->content = new stdClass;
149         $this->content->text = $this->config->cache;
151         // place link to glossary in the footer if the glossary is visible
153         //Obtain the visible property from the instance
154         if ($cm->uservisible) {
155             if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
156                 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
157                 .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
158             } else {
159                 $this->content->footer = '';
160             }
162             $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
163                 .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
165         // otherwise just place some text, no link
166         } else {
167             $this->content->footer = $this->config->invisible;
168         }
170         return $this->content;
171     }
173     function hide_header() {
174         if (empty($this->config->title)) {
175             return true;
176         }
177         return false;
178     }
180     /**
181      * Executed after block instance has been created, we use it to recode
182      * the glossary config setting to point to the new (restored) one
183      */
184     function after_restore($restore) {
185     /// We need to transform the glossary->id from the original one to the restored one
186         if ($rec = backup_getid($restore->backup_unique_code, 'glossary', $this->config->glossary)) {
187             $this->config->glossary = $rec->new_id;
188             $this->instance_config_commit();
189         }
190     }