3 define('BGR_RANDOMLY', '0');
4 define('BGR_LASTMODIFIED', '1');
5 define('BGR_NEXTONE', '2');
7 class block_glossary_random
extends block_base
{
9 $this->title
= get_string('pluginname','block_glossary_random');
12 function specialization() {
15 require_once($CFG->libdir
. '/filelib.php');
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');
23 $this->title
= $this->config
->title
;
26 if (empty($this->config
->glossary
)) {
30 if (!isset($this->config
->nexttime
)) {
31 $this->config
->nexttime
= 0;
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();
44 // Get module and context, to be able to rewrite urls
45 if (! $cm = get_coursemodule_from_instance("glossary", $this->config
->glossary
, $this->course
->id
)) {
48 $glossaryctx = get_context_instance(CONTEXT_MODULE
, $cm->id
);
53 switch ($this->config
->type
) {
56 $i = rand(1,$numberofentries);
62 if (isset($this->config
->previous
)) {
63 $i = $this->config
->previous +
1;
67 if ($i > $numberofentries) { // Loop back to beginning
74 default: // BGR_LASTMODIFIED
75 $i = $numberofentries;
81 if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
82 FROM {glossary_entries}
83 WHERE glossaryid = ? AND approved = 1
84 ORDER BY timemodified $SORT", array($this->config
->glossary
), $limitfrom, $limitnum)) {
86 $entry = reset($entry);
88 if (empty($this->config
->showconcept
)) {
91 $text = "<h3>".format_string($entry->concept
,true)."</h3>";
94 $options = new stdClass();
95 $options->trusted
= $entry->definitiontrust
;
96 $options->overflowdiv
= true;
97 $entry->definition
= file_rewrite_pluginfile_urls($entry->definition
, 'pluginfile.php', $glossaryctx->id
, 'mod_glossary', 'entry', $entry->id
);
98 $text .= format_text($entry->definition
, $entry->definitionformat
, $options);
100 $this->config
->nexttime
= usergetmidnight(time()) + DAYSECS
* $this->config
->refresh
;
101 $this->config
->previous
= $i;
104 $text = get_string('noentriesyet','block_glossary_random');
107 $this->config
->cache
= $text;
108 $this->instance_config_commit();
112 function instance_allow_multiple() {
113 // Are you going to allow multiple instances of each block?
114 // If yes, then it is assumed that the block WILL USE per-instance configuration
118 function get_content() {
119 global $USER, $CFG, $DB;
121 if (empty($this->config
->glossary
)) {
122 $this->content
= new stdClass();
123 $this->content
->text
= get_string('notyetconfigured','block_glossary_random');
124 $this->content
->footer
= '';
125 return $this->content
;
128 require_once($CFG->dirroot
.'/course/lib.php');
129 $course = $this->page
->course
;
130 $modinfo = get_fast_modinfo($course);
131 $glossaryid = $this->config
->glossary
;
133 if (!isset($modinfo->instances
['glossary'][$glossaryid])) {
134 // we can get here if the glossary has been deleted, so
135 // unconfigure the glossary from the block..
136 $this->config
->glossary
= 0;
137 $this->config
->cache
= '';
138 $this->instance_config_commit();
140 $this->content
= new stdClass();
141 $this->content
->text
= get_string('notyetconfigured','block_glossary_random');
142 $this->content
->footer
= '';
143 return $this->content
;
146 $cm = $modinfo->instances
['glossary'][$glossaryid];
148 if (!has_capability('mod/glossary:view', get_context_instance(CONTEXT_MODULE
, $cm->id
))) {
152 if (empty($this->config
->cache
)) {
153 $this->config
->cache
= '';
156 if ($this->content
!== NULL) {
157 return $this->content
;
160 $this->content
= new stdClass();
161 $this->content
->text
= $this->config
->cache
;
163 // place link to glossary in the footer if the glossary is visible
165 //Obtain the visible property from the instance
166 if ($cm->uservisible
) {
167 if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE
, $cm->id
))) {
168 $this->content
->footer
= '<a href="'.$CFG->wwwroot
.'/mod/glossary/edit.php?cmid='.$cm->id
169 .'" title="'.$this->config
->addentry
.'">'.$this->config
->addentry
.'</a><br />';
171 $this->content
->footer
= '';
174 $this->content
->footer
.= '<a href="'.$CFG->wwwroot
.'/mod/glossary/view.php?id='.$cm->id
175 .'" title="'.$this->config
->viewglossary
.'">'.$this->config
->viewglossary
.'</a>';
177 // otherwise just place some text, no link
179 $this->content
->footer
= $this->config
->invisible
;
182 return $this->content
;
185 function hide_header() {
186 if (empty($this->config
->title
)) {