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