New file added
[moodle.git] / blocks / glossary_random / block_glossary_random.php
CommitLineData
163dc56b 1<?PHP
2define("RANDOMLY", "0");
3define("LASTMODIFIED", "1");
4define("NEXTONE","2");
5
6class block_glossary_random extends block_base {
7 function init() {
8
9 $this->title = get_string('blockname','block_glossary_random');
10 $this->content_type = BLOCK_TYPE_TEXT;
11 $this->version = 2005010300;
12
13 }
14
15 function specialization() {
16 global $CFG;
17 $this->course = get_record('course', 'id', $this->instance->pageid);
18
19 // load userdefined title and make sure it's never empty
20 if (empty($this->config->title)) {
21 $this->title = get_string('blockname','block_glossary_random');
22 } else {
23 $this->title = $this->config->title;
24 }
95c62b56 25
26 if (empty($this->config->glossary)) {
27 return false;
28 }
29
30 if (!isset($this->config->nexttime)) {
31 $this->config->nexttime = 0;
32 }
163dc56b 33
34 //check if it's time to put a new entry in cache
35 if (time() > $this->config->nexttime) {
36
37 // place glossary concept and definition in $pref->cache
38 $numberofentries = count_records("glossary_entries","glossaryid",$this->config->glossary,"approved",1)-1;
39 switch ($this->config->type) {
40
41 case RANDOMLY:
42 $i = rand(0,$numberofentries);
43 break;
44
45 case NEXTONE:
46 $i = 1 + $this->config->previous;
47 if ($i < $numberofentries) {
48 break;
49 }
50 //otherwise fall through
51
52 case LASTMODIFIED:
53 $i=$numberofentries;
54 break;
95c62b56 55
56 default:
57 $i = 0;
163dc56b 58
59 }
60
61 if ($entries = get_records_sql("SELECT concept, definition, format FROM {$CFG->prefix}glossary_entries WHERE glossaryid = {$this->config->glossary} and approved = 1 ORDER BY timemodified LIMIT {$i},1")) {
62 // get all entries, normally there is only one entry returned
63 foreach ($entries as $entry) {
64 $text = "<b> $entry->concept</b><br>";
65 $text .= format_text($entry->definition, $entry->format);
66
67 }
68
69 $this->config->nexttime = usergetmidnight(time())+60*60*24*$this->config->refresh;
70 $this->config->previous = $i;
71
72 } else {
73 $text = get_string('notyetconfigured','block_glossary_random');
74 }
75 // store the text
76 $this->config->cache= $text;
77 parent::instance_config_save($this->config);
78 }
79 }
80
81 function instance_allow_multiple() {
82 // Are you going to allow multiple instances of each block?
83 // If yes, then it is assumed that the block WILL USE per-instance configuration
84 return true;
85 }
86
87 function instance_config_print() {
88 global $CFG;
89
90 if (empty($this->config->nexttime)) {
91 // ... teacher has not yet configured the block, let's put some default values here to explain things
92 $this->config->title = get_string('blockname','block_glossary_random');
93 $this->config->refresh = 0;
94
95 $this->config->cache= get_string("notyetconfigured","block_glossary_random");
96 $this->config->addentry=get_string("addentry", "block_glossary_random");
97 $this->config->viewglossary=get_string("viewglossary", "block_glossary_random");
98 $this->config->invisible=get_string("invisible", "block_glossary_random");
99 }
100
101 // select glossaries to put in dropdown box ...
102 $glossaries = get_records_select_menu("glossary", "course=".$this->course->id,"name","id,name");
103
104 // and select quotetypes to put in dropdown box
105 $type[0] = get_string("random","block_glossary_random");
106 $type[1] = get_string("lastmodified","block_glossary_random");
107 $type[2] = get_string("nextone","block_glossary_random");
108
109 $this->config->nexttime = usergetmidnight(time())+24*60*60*$this->config->refresh;
110
111 // display the form
112
113 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
114 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
115 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
116 print_simple_box_end();
117 } else {
118 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
119 }
120
121 return true;
122 }
123
124 function get_content() {
125 global $USER, $CFG;
126
127 if($this->content !== NULL) {
128 return $this->content;
129 }
130
131 $this->content = new stdClass;
132 $this->content->text = $this->config->cache;
95c62b56 133
134 if (empty($this->config->glossary)) {
135 return $this->content;
136 }
163dc56b 137
138 // place link to glossary in the footer if the glossary is visible
139 $glossaryid = $this->config->glossary;
140 $glossary=get_record("glossary", "id", $glossaryid);
141 $studentcanpost = $glossary->studentcanpost; //needed to decide on which footer
142
143 //Create a temp valid module structure (course,id)
144 $tempmod->course = $this->course->id;
145 $tempmod->id = $glossaryid;
146
147 //Obtain the visible property from the instance
148 if (instance_is_visible('glossary', $tempmod)) {
149
150 $cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
151 if ($studentcanpost) {
152 $footertext = $this->config->addentry;
153 } else {
154 $footertext = $this->config->viewglossary;
155 }
156 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/'
157 .(($studentcanpost == 1)?'edit':'view').'.php?id='.$cm->id
158 .'" title="'.$footertext.'">'.$footertext.'</a>';
159
160 // otherwise just place some text, no link
161 } else {
162 $this->content->footer = $this->config->invisible;
163 }
164
165 return $this->content;
166 }
167
168 function hide_header() {
95c62b56 169 if (empty($this->config->title)) {
170 return false;
171 }
163dc56b 172 if ($this->config->title == "") {
173 return true;
95c62b56 174 }
175 return false;
163dc56b 176 }
177
178}
179?>