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