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