Commit | Line | Data |
---|---|---|
4ca6cfbf | 1 | <?php |
2896553e | 2 | |
3 | define('BGR_RANDOMLY', '0'); | |
4 | define('BGR_LASTMODIFIED', '1'); | |
5 | define('BGR_NEXTONE', '2'); | |
163dc56b | 6 | |
7 | class block_glossary_random extends block_base { | |
8 | function init() { | |
96978238 | 9 | |
10 | $this->title = get_string('blockname','block_glossary_random'); | |
433c242f | 11 | $this->version = 2007101509; |
96978238 | 12 | |
13 | } | |
14 | ||
163dc56b | 15 | function specialization() { |
cb640229 | 16 | global $CFG, $DB; |
17 | $this->course = $this->page->course; | |
96978238 | 18 | |
163dc56b | 19 | // load userdefined title and make sure it's never empty |
20 | if (empty($this->config->title)) { | |
2a71e9f2 | 21 | $this->title = get_string('pluginname','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 | |
f28f2d90 | 38 | if (!$numberofentries = $DB->count_records('glossary_entries', |
39 | array('glossaryid'=>$this->config->glossary, 'approved'=>1))) { | |
96978238 | 40 | $this->config->cache = get_string('noentriesyet','block_glossary_random'); |
6d908bc6 | 41 | $this->instance_config_commit(); |
2896553e | 42 | } |
43 | ||
422770d8 | 44 | $limitfrom = 0; |
45 | $limitnum = 1; | |
46 | ||
96978238 | 47 | switch ($this->config->type) { |
48 | ||
49 | case BGR_RANDOMLY: | |
50 | $i = rand(1,$numberofentries); | |
422770d8 | 51 | $limitfrom = $i-1; |
2896553e | 52 | $SORT = 'ASC'; |
96978238 | 53 | break; |
54 | ||
2896553e | 55 | case BGR_NEXTONE: |
56 | if (isset($this->config->previous)) { | |
96978238 | 57 | $i = $this->config->previous + 1; |
2896553e | 58 | } else { |
59 | $i = 1; | |
60 | } | |
96978238 | 61 | if ($i > $numberofentries) { // Loop back to beginning |
2896553e | 62 | $i = 1; |
96978238 | 63 | } |
422770d8 | 64 | $limitfrom = $i-1; |
2896553e | 65 | $SORT = 'ASC'; |
66 | break; | |
96978238 | 67 | |
2896553e | 68 | default: // BGR_LASTMODIFIED |
96978238 | 69 | $i = $numberofentries; |
422770d8 | 70 | $limitfrom = 0; |
2896553e | 71 | $SORT = 'DESC'; |
96978238 | 72 | break; |
73 | } | |
2896553e | 74 | |
6526cc34 | 75 | if ($entry = $DB->get_records_sql("SELECT concept, definition, definitionformat, definitiontrust |
f28f2d90 | 76 | FROM {glossary_entries} |
77 | WHERE glossaryid = ? AND approved = 1 | |
78 | ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) { | |
2896553e | 79 | |
cb88fbdd | 80 | $entry = reset($entry); |
81 | ||
7ead0ce0 | 82 | if (empty($this->config->showconcept)) { |
837a0fe2 | 83 | $text = ''; |
7ead0ce0 | 84 | } else { |
cc1a28f0 | 85 | $text = "<h3>".format_string($entry->concept,true)."</h3>"; |
4ca6cfbf | 86 | } |
fafa2265 | 87 | |
88 | $options = new object; | |
cbc2b5df | 89 | $options->trusted = $entry->definitiontrust; |
90 | $text .= format_text($entry->definition, $entry->definitionformat, $options); | |
96978238 | 91 | |
6d908bc6 | 92 | $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh; |
96978238 | 93 | $this->config->previous = $i; |
94 | ||
95 | } else { | |
96 | $text = get_string('noentriesyet','block_glossary_random'); | |
97 | } | |
163dc56b | 98 | // store the text |
6d908bc6 | 99 | $this->config->cache = $text; |
100 | $this->instance_config_commit(); | |
96978238 | 101 | } |
163dc56b | 102 | } |
96978238 | 103 | |
163dc56b | 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 | } | |
96978238 | 109 | |
163dc56b | 110 | function get_content() { |
cb640229 | 111 | global $USER, $CFG, $DB; |
95c62b56 | 112 | |
96978238 | 113 | if (empty($this->config->glossary)) { |
114 | $this->content->text = get_string('notyetconfigured','block_glossary_random'); | |
115 | $this->content->footer = ''; | |
116 | return $this->content; | |
2896553e | 117 | } |
118 | ||
f9192b65 | 119 | $glossaryid = $this->config->glossary; |
120 | ||
cb640229 | 121 | $course = $this->page->course; |
51edc06e | 122 | |
123 | require_once($CFG->dirroot.'/course/lib.php'); | |
124 | $modinfo = get_fast_modinfo($course); | |
125 | ||
126 | if (!isset($modinfo->instances['glossary'][$glossaryid])) { | |
f9192b65 | 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(); | |
132 | ||
133 | $this->content->text = get_string('notyetconfigured','block_glossary_random'); | |
134 | $this->content->footer = ''; | |
135 | return $this->content; | |
136 | } | |
137 | ||
51edc06e | 138 | $cm = $modinfo->instances['glossary'][$glossaryid]; |
f9192b65 | 139 | |
96978238 | 140 | if (empty($this->config->cache)) { |
2896553e | 141 | $this->config->cache = ''; |
142 | } | |
96978238 | 143 | |
2896553e | 144 | if ($this->content !== NULL) { |
95c62b56 | 145 | return $this->content; |
146 | } | |
2896553e | 147 | |
96978238 | 148 | $this->content = new stdClass; |
2896553e | 149 | $this->content->text = $this->config->cache; |
96978238 | 150 | |
151 | // place link to glossary in the footer if the glossary is visible | |
96978238 | 152 | |
96978238 | 153 | //Obtain the visible property from the instance |
51edc06e | 154 | if ($cm->uservisible) { |
e73bfa2d | 155 | if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) { |
7ead0ce0 | 156 | $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id |
ed4c7bd3 | 157 | .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />'; |
837a0fe2 | 158 | } else { |
7ead0ce0 | 159 | $this->content->footer = ''; |
4ca6cfbf PS |
160 | } |
161 | ||
ed4c7bd3 | 162 | $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id |
163 | .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>'; | |
96978238 | 164 | |
163dc56b | 165 | // otherwise just place some text, no link |
96978238 | 166 | } else { |
167 | $this->content->footer = $this->config->invisible; | |
163dc56b | 168 | } |
96978238 | 169 | |
170 | return $this->content; | |
171 | } | |
172 | ||
163dc56b | 173 | function hide_header() { |
96978238 | 174 | if (empty($this->config->title)) { |
163dc56b | 175 | return true; |
95c62b56 | 176 | } |
177 | return false; | |
163dc56b | 178 | } |
96978238 | 179 | |
9120630b | 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 | } | |
191 | ||
163dc56b | 192 | } |
4ca6cfbf | 193 |