Updated the HEAD build version to 20091015
[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 = '';
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
7b2b1a91 66 require_once($CFG->dirroot.'/tag/locallib.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;
cb640229 82 $coursepage = (isset($this->page->course->id) && $this->page->course->id != SITEID);
11e7b506 83 $mymoodlepage = ($SCRIPT == '/my/index.php') ? true : false;
cb640229 84 $sitepage = (isset($this->page->course->id) && $this->page->course->id == SITEID && !$mymoodlepage);
85 $coursecontext = get_context_instance(CONTEXT_COURSE, $this->page->course->id);
38fb8190 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) {
cb640229 114 $coursetags = coursetag_print_cloud(coursetag_get_tags($this->page->course->id, 0, '', $numoftags, $sort), true);
38fb8190 115 if (!$coursetags) $coursetags = get_string('notagsyet', $tagslang);
cb640229 116 $courseflag = '&amp;courseid='.$this->page->course->id;
38fb8190 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);
25c56af8 232 $coursetaghelpbutton = $OUTPUT->help_icon(moodle_help_icon::make('addtags', 'adding tags', $tagslang));
973d2660 233 $sesskey = sesskey();
4096752d 234 $arrowright = $OUTPUT->old_icon_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;">
cae83708 240 <input type="hidden" name="entryid" value="$COURSE->id" />
38fb8190 241 <input type="hidden" name="userid" value="$USER->id" />
973d2660 242 <input type="hidden" name="sesskey" value="$sesskey" />
38fb8190 243 </div>
244 <div><label for="coursetag_new_tag">$tagthisunit</label></div>
245 <div class="coursetag_form_wrapper">
246 <div class="coursetag_form_positioner">
247 <div class="coursetag_form_input1">
248 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
249 </div>
250 <div class="coursetag_form_input2">
251 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
252 onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
253 </div>
254 <div class="coursetag_form_input3" id="coursetag_sug_btn">
255 <a title="$arrowtitle">
4096752d 256 <img src="$arrowright" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
38fb8190 257 </a>
258 </div>
259 </div>
260 <div style="display: inline;">
261 <button type="submit">$buttonadd</button>
262 $coursetaghelpbutton
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
277 if ($mytags or $officialtags or $commtags or $coursetags) {
278 $this->content->footer .= '<div id="coursetagslinks"></div>';
279 }
280 // This section sets the order of the links
281 $coursetagslinks = array();
282 if ($mytags) {
283 $coursetagslinks['my'] = array('title'=>get_string('mytags2', $tagslang),
284 'onclick'=>'f_mytags',
285 'text'=>get_string('mytags1', $tagslang));
286 }
287 // because alltags is always present, only show link if there is something else as well
288 if ($alltags and ($mytags or $officialtags or $commtags or $coursetags)) {
289 $coursetagslinks['all'] = array('title'=>get_string('alltags2', $tagslang),
290 'onclick'=>'f_alltags',
291 'text'=>get_string('alltags1', $tagslang));
292 }
293 if ($officialtags) {
294 $coursetagslinks['off'] = array('title'=>get_string('officialtags2', $tagslang),
295 'onclick'=>'f_officialtags',
296 'text'=>get_string('officialtags1', $tagslang));
297 }
298 //if ($commtags) {
299 // $coursetagslinks['com'] = array('title'=>get_string('communitytags2', $tagslang),
300 // 'onclick'=>'f_commtags',
301 // 'text'=>get_string('communitytags1', $tagslang));
302 //}
303 if ($coursetags) {
304 $coursetagslinks['crs'] = array('title'=>get_string('coursetags2', $tagslang),
305 'onclick'=>'f_coursetags',
306 'text'=>get_string('coursetags1', $tagslang));
307 }
308 $this->content->footer .= coursetag_get_jscript_links($coursetagslinks);
309
310 } else {
311 //if not logged in
312 $this->content->footer = '<hr />'.get_string('please', $tagslang).'
93f66983 313 <a href="'.get_login_url().'">'.get_string('login', $tagslang).'
38fb8190 314 </a> '.get_string('tagunits', $tagslang);
315 }
316 }
317 // end of show course tags section
d797e50f 318
319 return $this->content;
320 }
38fb8190 321}
d797e50f 322?>