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