MDL-39028 fix sqlsrv concat type conversion
[moodle.git] / blocks / glossary_random / block_glossary_random.php
blobc06d0783ac176a1df1d6248f206a23ba897b427e
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() {
9 $this->title = get_string('pluginname','block_glossary_random');
12 function specialization() {
13 global $CFG, $DB;
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');
22 } else {
23 $this->title = $this->config->title;
26 if (empty($this->config->glossary)) {
27 return false;
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)) {
46 return false;
48 $glossaryctx = get_context_instance(CONTEXT_MODULE, $cm->id);
50 $limitfrom = 0;
51 $limitnum = 1;
53 switch ($this->config->type) {
55 case BGR_RANDOMLY:
56 $i = rand(1,$numberofentries);
57 $limitfrom = $i-1;
58 $SORT = 'ASC';
59 break;
61 case BGR_NEXTONE:
62 if (isset($this->config->previous)) {
63 $i = $this->config->previous + 1;
64 } else {
65 $i = 1;
67 if ($i > $numberofentries) { // Loop back to beginning
68 $i = 1;
70 $limitfrom = $i-1;
71 $SORT = 'ASC';
72 break;
74 default: // BGR_LASTMODIFIED
75 $i = $numberofentries;
76 $limitfrom = 0;
77 $SORT = 'DESC';
78 break;
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)) {
89 $text = '';
90 } else {
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;
103 } else {
104 $text = get_string('noentriesyet','block_glossary_random');
106 // store the text
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
115 return true;
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))) {
149 return '';
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 />';
170 } else {
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
178 } else {
179 $this->content->footer = $this->config->invisible;
182 return $this->content;
185 function hide_header() {
186 if (empty($this->config->title)) {
187 return true;
189 return false;