moodle_page: MDL-12212 add a couple more useful classes to body
[moodle.git] / blocks / tags / block_tags.php
CommitLineData
d797e50f 1<?PHP //$Id$
2
3class block_tags extends block_base {
4 function init() {
38fb8190 5 $this->version = 2008063001;
d797e50f 6 $this->title = get_string('blocktagstitle', 'tag');
38fb8190 7 // the cron function goes through all users, so only do daily
8 // (this creates rss feeds for personal course tags)
9 // removed until rsslib supports dc/cc
10 // $this->cron = 60*60*24;
d797e50f 11 }
12
13 function instance_allow_multiple() {
14 return true;
15 }
16
17 function has_config() {
38fb8190 18 return true;
d797e50f 19 }
20
21 function applicable_formats() {
cdcaa9f2 22 return array('all' => true);
d797e50f 23 }
24
25 function instance_allow_config() {
26 return true;
27 }
28
29 function specialization() {
30
31 // load userdefined title and make sure it's never empty
32 if (empty($this->config->title)) {
33 $this->title = get_string('blocktagstitle','tag');
34 } else {
35 $this->title = $this->config->title;
36 }
37 }
38
39 function get_content() {
40
11e7b506 41 global $CFG, $SITE, $COURSE, $USER, $SCRIPT;
d797e50f 42
43 if (empty($CFG->usetags)) {
44 $this->content->text = '';
45 return $this->content;
46 }
47
48 if (empty($this->config->numberoftags)) {
49 $this->config->numberoftags = 80;
50 }
51
52 if ($this->content !== NULL) {
53 return $this->content;
54 }
55
56 if (empty($this->instance)) {
57 $this->content = '';
58 return $this->content;
59 }
60
61 $this->content = new stdClass;
62 $this->content->footer = '';
63
64 /// Get a list of tags
65
bb062447 66 require_once($CFG->dirroot.'/tag/lib.php');
d797e50f 67
38fb8190 68 if (empty($CFG->block_tags_showcoursetags) or !$CFG->block_tags_showcoursetags) {
69
70 $this->content->text = tag_print_cloud($this->config->numberoftags, true);
71
72 // start of show course tags section
73 } else {
74
75 require_once($CFG->dirroot.'/tag/coursetagslib.php');
76
77 // Permissions and page awareness
e562ecdc 78 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
79 $isguest = has_capability('moodle/legacy:guest', $systemcontext, $USER->id, false);
38fb8190 80 $loggedin = isloggedin() && !$isguest;
81 $coursepage = $canedit = false;
82 $coursepage = (isset($COURSE->id) && $COURSE->id != SITEID);
11e7b506 83 $mymoodlepage = ($SCRIPT == '/my/index.php') ? true : false;
38fb8190 84 $sitepage = (isset($COURSE->id) && $COURSE->id == SITEID && !$mymoodlepage);
85 $coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id);
86 if ($coursepage) {
e562ecdc 87 $canedit = has_capability('moodle/tag:create', $systemcontext);
38fb8190 88 }
89
90 // Check rss feed - temporarily removed until Dublin Core tags added
91 // provides a feed of users course tags for each unit they have tagged
92 //$rssfeed = '';
93 //if (file_exists($CFG->dataroot.'/'.SITEID.'/usertagsrss/'.$USER->id.'/user_unit_tags_rss.xml')) {
94 // $rssfeed = '/file.php/'.SITEID.'/usertagsrss/'.$USER->id.'/user_unit_tags_rss.xml';
95 //}
96
97 // Language strings
98 $tagslang = 'block_tags';
99
100 // DB hits to get groups of marked up tags (if available)
101 //TODO check whether time limited personal tags are required
102 $numoftags = $this->config->numberoftags;
103 $sort = 'name';
104 $alltags = $officialtags = $coursetags = $commtags = $mytags = $coursetagdivs = $courseflag = '';
105 if ($sitepage or $coursepage) {
106 $alltags = coursetag_print_cloud(coursetag_get_all_tags($sort, $this->config->numberoftags), true);
107 $officialtags = coursetag_print_cloud(coursetag_get_tags(0, 0, 'official', $numoftags, $sort), true);
108 $commtags = coursetag_print_cloud(coursetag_get_tags(0, 0, 'default', $numoftags, $sort), true);
109 if ($loggedin) {
110 $mytags = coursetag_print_cloud(coursetag_get_tags(0, $USER->id, 'default', $numoftags, $sort), true);
111 }
112 }
113 if ($coursepage) {
114 $coursetags = coursetag_print_cloud(coursetag_get_tags($COURSE->id, 0, '', $numoftags, $sort), true);
115 if (!$coursetags) $coursetags = get_string('notagsyet', $tagslang);
116 $courseflag = '&amp;courseid='.$COURSE->id;
117 }
118 if ($mymoodlepage) {
119 $mytags = coursetag_print_cloud(coursetag_get_tags(0, $USER->id, 'default', $numoftags, $sort), true);
120 $officialtags = coursetag_print_cloud(coursetag_get_tags(0, 0, 'official', $numoftags, $sort), true);
121 $commtags = coursetag_print_cloud(coursetag_get_tags(0, 0, 'default', $numoftags, $sort), true);
122 }
123
124 // Prepare the divs and javascript that displays the groups of tags (and which is displayed first)
125 $moretags = $CFG->wwwroot.'/tag/coursetags_more.php';
126 $moretagstitle = get_string('moretags', $tagslang);
127 $moretagsstring = get_string('more', $tagslang);
128 $displayblock = 'style="display:block"';
129 $displaynone = 'style="display:none"'; //only one div created below will be displayed at a time
130 if ($alltags) {
131 if ($sitepage) {
132 $display = $displayblock;
133 } else {
134 $display = $displaynone;
135 }
136 $alltagscontent = '
137 <div id="f_alltags" '.$display.'>'.
138 get_string("alltags", $tagslang).
139 '<div class="coursetag_list">'.$alltags.'</div>
140 <div class="coursetag_morelink">
141 <a href="'.$moretags.'?show=all'.$courseflag.'" title="'.$moretagstitle.'">'.$moretagsstring.'</a>
142 </div>
143 </div>';
144 $coursetagdivs .= '"f_alltags", ';
145 }
146 if ($mytags) {
147 if ($mymoodlepage) {
148 $display = $displayblock;
149 } else {
150 $display = $displaynone;
151 }
152 $mytagscontent = '
153 <div id="f_mytags" '.$display.'>';
154 /*if ($rssfeed) { // - temporarily removed
155 $mytagscontent .= link_to_popup_window(
156 $rssfeed, $name='popup',
157 '<img src="'.$CFG->wwwroot.'/pix/rss.gif" alt="User Unit Tags RSS" /> My Unit Tags RSS',
158 $height=600, $width=800,
159 $title='My Unit Tags RSS', $options='menubar=1,scrollbars,resizable', $return=true).'<br />';
160 }*/
161 $mytagscontent .=
162 get_string('mytags', $tagslang).
163 '<div class="coursetag_list">'.$mytags.'</div>
164 <div class="coursetag_morelink">
165 <a href="'.$moretags.'?show=my'.$courseflag.'" title="'.$moretagstitle.'">'.$moretagsstring.'</a>
166 </div>
167 </div>';
168 $coursetagdivs .= '"f_mytags", ';
169 }
170 if ($officialtags) {
171 if ($mytags or $alltags) {
172 $display = $displaynone;
173 } else {
174 $display = $displayblock;
175 }
176 $officialtagscontent = '
177 <div id="f_officialtags" '.$display.'>'.
178 get_string('officialtags', $tagslang).
179 '<div class="coursetag_list">'.$officialtags.'</div>
180 <div class="coursetag_morelink">
181 <a href="'.$moretags.'?show=official'.$courseflag.'" title="'.$moretagstitle.'">'.$moretagsstring.'</a>
182 </div>
183 </div>';
184 $coursetagdivs .= '"f_officialtags", ';
185 }
186 if ($coursetags) {
187 if ($coursepage) {
188 $display = $displayblock;
189 } else {
190 $display = $displaynone;
191 }
192 $coursetagscontent = '
193 <div id="f_coursetags" '.$display.'>'.
194 get_string('coursetags', $tagslang).
195 '<div class="coursetag_list">'.$coursetags.'</div>
196 <div class="coursetag_morelink">
197 <a href="'.$moretags.'?show=course'.$courseflag.'" title="'.$moretagstitle.'">'.$moretagsstring.'</a>
198 </div>
199 </div>';
200 $coursetagdivs .= '"f_coursetags", ';
201 }
202 if ($commtags) {
203 $commtagscontent = '
204 <div id="f_commtags" '.$displaynone.'>'.
205 get_string('communitytags', $tagslang).
206 '<div class="coursetag_list">'.$commtags.'</div>
207 <div class="coursetag_morelink">
208 <a href="'.$moretags.'?show=community'.$courseflag.'" title="'.$moretagstitle.'">'.$moretagsstring.'</a>
209 </div>
210 </div>';
211 $coursetagdivs .= '"f_commtags", ';
212 }
213 // Tidy up the end of a javascript array and add javascript
214 $coursetagdivs = rtrim($coursetagdivs, ', ');
215 $this->content->text .= coursetag_get_jscript($coursetagdivs);
216
217 // Add the divs (containing the tags) to the block's content
218 if ($alltags) { $this->content->text .= $alltagscontent; }
219 if ($mytags) { $this->content->text .= $mytagscontent; }
220 if ($officialtags) { $this->content->text .= $officialtagscontent; }
221 if ($coursetags) { $this->content->text .= $coursetagscontent; }
222 if ($commtags) { $this->content->text .= $commtagscontent; }
223
224 // add the input form section (allowing a user to tag the current course) and navigation, or loggin message
225 if ($loggedin) {
226 // only show the input form on course pages for those allowed (or not barred)
227 if ($coursepage && $canedit) {
228 //$this->content->footer .= coursetag_get_jscript();
229 $tagthisunit = get_string('tagthisunit', $tagslang);
230 $buttonadd = get_string('add', $tagslang);
231 $arrowtitle = get_string('arrowtitle', $tagslang);
232 $coursetaghelpbutton = helpbutton('addtags', 'adding tags', $tagslang, TRUE, FALSE, '', TRUE);
973d2660 233 $sesskey = sesskey();
38fb8190 234 $this->content->footer .= <<<EOT
235 <hr />
236 <form action="{$CFG->wwwroot}/tag/coursetags_add.php" method="post" id="coursetag"
237 onsubmit="return ctags_checkinput(this.coursetag_new_tag.value)">
238 <div style="display: none;">
239 <input type="hidden" name="entryid" value="$COURSE->id" />
240 <input type="hidden" name="userid" value="$USER->id" />
973d2660 241 <input type="hidden" name="sesskey" value="$sesskey" />
38fb8190 242 </div>
243 <div><label for="coursetag_new_tag">$tagthisunit</label></div>
244 <div class="coursetag_form_wrapper">
245 <div class="coursetag_form_positioner">
246 <div class="coursetag_form_input1">
247 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
248 </div>
249 <div class="coursetag_form_input2">
250 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
251 onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
252 </div>
253 <div class="coursetag_form_input3" id="coursetag_sug_btn">
254 <a title="$arrowtitle">
255 <img src="$CFG->pixpath/t/arrow_left.gif" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
256 </a>
257 </div>
258 </div>
259 <div style="display: inline;">
260 <button type="submit">$buttonadd</button>
261 $coursetaghelpbutton
262 </div>
263 </div>
264 </form>
265EOT;
266 // add the edit link
267 $this->content->footer .= '
268 <div>
269 <a href="'.$CFG->wwwroot.'/tag/coursetags_edit.php?courseid='.$COURSE->id.'"
270 title="'.get_string('edittags', $tagslang).'">'.get_string('edittags', $tagslang).'</a>
271 </div>';
272 }
273
274 // Navigation elements at the bottom of the block
275 // show the alternative displays options if available
276 if ($mytags or $officialtags or $commtags or $coursetags) {
277 $this->content->footer .= '<div id="coursetagslinks"></div>';
278 }
279 // This section sets the order of the links
280 $coursetagslinks = array();
281 if ($mytags) {
282 $coursetagslinks['my'] = array('title'=>get_string('mytags2', $tagslang),
283 'onclick'=>'f_mytags',
284 'text'=>get_string('mytags1', $tagslang));
285 }
286 // because alltags is always present, only show link if there is something else as well
287 if ($alltags and ($mytags or $officialtags or $commtags or $coursetags)) {
288 $coursetagslinks['all'] = array('title'=>get_string('alltags2', $tagslang),
289 'onclick'=>'f_alltags',
290 'text'=>get_string('alltags1', $tagslang));
291 }
292 if ($officialtags) {
293 $coursetagslinks['off'] = array('title'=>get_string('officialtags2', $tagslang),
294 'onclick'=>'f_officialtags',
295 'text'=>get_string('officialtags1', $tagslang));
296 }
297 //if ($commtags) {
298 // $coursetagslinks['com'] = array('title'=>get_string('communitytags2', $tagslang),
299 // 'onclick'=>'f_commtags',
300 // 'text'=>get_string('communitytags1', $tagslang));
301 //}
302 if ($coursetags) {
303 $coursetagslinks['crs'] = array('title'=>get_string('coursetags2', $tagslang),
304 'onclick'=>'f_coursetags',
305 'text'=>get_string('coursetags1', $tagslang));
306 }
307 $this->content->footer .= coursetag_get_jscript_links($coursetagslinks);
308
309 } else {
310 //if not logged in
311 $this->content->footer = '<hr />'.get_string('please', $tagslang).'
93f66983 312 <a href="'.get_login_url().'">'.get_string('login', $tagslang).'
38fb8190 313 </a> '.get_string('tagunits', $tagslang);
314 }
315 }
316 // end of show course tags section
d797e50f 317
318 return $this->content;
319 }
320
321 function instance_config_print() {
322 global $CFG;
323
324 /// set up the numberoftags select field
325 $numberoftags = array();
326 for($i=1;$i<=200;$i++) $numberoftags[$i] = $i;
327
328 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
329 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
330 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
331 print_simple_box_end();
332 } else {
333 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
334 }
335 }
38fb8190 336
337 /*
338 * function removed until rsslib supports dc/cc
339 */
340 /*
341 function cron() {
c34984cf 342 global $CFG;
343 if ($CFG->block_tags_showcoursetags) {
38fb8190 344 require_once($CFG->dirroot.'/tag/coursetagslib.php');
345 return coursetag_rss_feeds();
346 } else {
347 return TRUE;
d797e50f 348}
38fb8190 349 }
350 */
d797e50f 351
38fb8190 352}
d797e50f 353?>