MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / tags / block_tags.php
1 <?php
3 class block_tags extends block_base {
4     function init() {
5         $this->version = 2008063001;
6         $this->title = get_string('pluginname', 'block_tags');
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;
11     }
13     function instance_allow_multiple() {
14         return true;
15     }
17     function has_config() {
18         return true;
19     }
21     function applicable_formats() {
22         return array('all' => true);
23     }
25     function instance_allow_config() {
26         return true;
27     }
29     function specialization() {
31         // load userdefined title and make sure it's never empty
32         if (empty($this->config->title)) {
33             $this->title = get_string('pluginname','block_tags');
34         } else {
35             $this->title = $this->config->title;
36         }
37     }
39     function get_content() {
41         global $CFG, $COURSE, $SITE, $USER, $SCRIPT, $OUTPUT;
43         if (empty($CFG->usetags)) {
44             $this->content->text = '';
45             if ($this->page->user_is_editing()) {
46                 $this->content->text = get_string('disabledtags', 'block_tags');
47             }
48             return $this->content;
49         }
51         if (empty($this->config->numberoftags)) {
52             $this->config->numberoftags = 80;
53         }
55         if ($this->content !== NULL) {
56             return $this->content;
57         }
59         if (empty($this->instance)) {
60             $this->content = '';
61             return $this->content;
62         }
64         $this->content = new stdClass;
65         $this->content->footer = '';
67         /// Get a list of tags
69         require_once($CFG->dirroot.'/tag/locallib.php');
71         if (empty($CFG->block_tags_showcoursetags) or !$CFG->block_tags_showcoursetags) {
73             $this->content->text = tag_print_cloud($this->config->numberoftags, true);
75         // start of show course tags section
76         } else {
78             require_once($CFG->dirroot.'/tag/coursetagslib.php');
80             // Permissions and page awareness
81             $systemcontext = get_context_instance(CONTEXT_SYSTEM);
82             $loggedin = isloggedin() && !isguestuser();
83             $coursepage = $canedit = false;
84             $coursepage = (isset($this->page->course->id) && $this->page->course->id != SITEID);
85             $mymoodlepage = ($SCRIPT == '/my/index.php') ? true : false;
86             $sitepage = (isset($this->page->course->id) && $this->page->course->id == SITEID && !$mymoodlepage);
87             $coursecontext = get_context_instance(CONTEXT_COURSE, $this->page->course->id);
88             if ($coursepage) {
89                 $canedit =  has_capability('moodle/tag:create', $systemcontext);
90             }
92             // Check rss feed - temporarily removed until Dublin Core tags added
93             // provides a feed of users course tags for each unit they have tagged
94             //$rssfeed = '';
95             //if (file_exists($CFG->dataroot.'/'.SITEID.'/usertagsrss/'.$USER->id.'/user_unit_tags_rss.xml')) {
96             //    $rssfeed = '/file.php/'.SITEID.'/usertagsrss/'.$USER->id.'/user_unit_tags_rss.xml';
97             //}
99             // Language strings
100             $tagslang = 'block_tags';
102             // DB hits to get groups of marked up tags (if available)
103             //TODO check whether time limited personal tags are required
104             $numoftags = $this->config->numberoftags;
105             $sort = 'name';
106             $coursetagdivs = array();
107             $alltags = $officialtags = $coursetags = $commtags = $mytags = $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) {
117                 $coursetags = coursetag_print_cloud(coursetag_get_tags($this->page->course->id, 0, '', $numoftags, $sort), true);
118                 if (!$coursetags) $coursetags = get_string('notagsyet', $tagslang);
119                 $courseflag = '&amp;courseid='.$this->page->course->id;
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             }
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             coursetag_get_jscript($coursetagdivs);
219             // Add the divs (containing the tags) to the block's content
220             if ($alltags) { $this->content->text .= $alltagscontent; }
221             if ($mytags) { $this->content->text .= $mytagscontent; }
222             if ($officialtags) { $this->content->text .= $officialtagscontent; }
223             if ($coursetags) { $this->content->text .= $coursetagscontent; }
224             if ($commtags) { $this->content->text .= $commtagscontent; }
226             // add the input form section (allowing a user to tag the current course) and navigation, or loggin message
227             if ($loggedin) {
228                 // only show the input form on course pages for those allowed (or not barred)
229                 if ($coursepage && $canedit) {
230                     //$this->content->footer .= coursetag_get_jscript();
231                     $tagthisunit = get_string('tagthisunit', $tagslang);
232                     $buttonadd = get_string('add', $tagslang);
233                     $arrowtitle = get_string('arrowtitle', $tagslang);
234                     $coursetaghelpbutton = $OUTPUT->help_icon('addtags', 'adding tags', $tagslang);
235                     $sesskey = sesskey();
236                     $arrowright = $OUTPUT->pix_url('t/arrow_left');
237                     $this->content->footer .= <<<EOT
238                         <hr />
239                         <form action="{$CFG->wwwroot}/tag/coursetags_add.php" method="post" id="coursetag"
240                                 onsubmit="return ctags_checkinput(this.coursetag_new_tag.value)">
241                             <div style="display: none;">
242                                 <input type="hidden" name="entryid" value="$COURSE->id" />
243                                 <input type="hidden" name="userid" value="$USER->id" />
244                                 <input type="hidden" name="sesskey" value="$sesskey" />
245                             </div>
246                             <div><label for="coursetag_new_tag">$tagthisunit</label></div>
247                             <div class="coursetag_form_wrapper">
248                             <div class="coursetag_form_positioner">
249                                 <div class="coursetag_form_input1">
250                                     <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
251                                 </div>
252                                 <div class="coursetag_form_input2">
253                                     <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
254                                         onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
255                                 </div>
256                                 <div class="coursetag_form_input3" id="coursetag_sug_btn">
257                                     <a title="$arrowtitle">
258                                         <img src="$arrowright" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
259                                     </a>
260                                 </div>
261                             </div>
262                             <div style="display: inline;">
263                                 <button type="submit">$buttonadd</button>
264                                 $coursetaghelpbutton
265                             </div>
266                             </div>
267                         </form>
268 EOT;
269                     // add the edit link
270                     $this->content->footer .= '
271                         <div>
272                         <a href="'.$CFG->wwwroot.'/tag/coursetags_edit.php?courseid='.$this->page->course->id.'"
273                         title="'.get_string('edittags', $tagslang).'">'.get_string('edittags', $tagslang).'</a>
274                         </div>';
275                 }
277                 // Navigation elements at the bottom of the block
278                 // show the alternative displays options if available
279                 if ($mytags or $officialtags or $commtags or $coursetags) {
280                     $this->content->footer .= '<div id="coursetagslinks"></div>';
281                 }
282                 // This section sets the order of the links
283                 $coursetagslinks = array();
284                 if ($mytags) {
285                     $coursetagslinks['my'] = array('title'=>get_string('mytags2', $tagslang),
286                                                     'onclick'=>'f_mytags',
287                                                     'text'=>get_string('mytags1', $tagslang));
288                 }
289                 // because alltags is always present, only show link if there is something else as well
290                 if ($alltags and ($mytags or $officialtags or $commtags or $coursetags)) {
291                     $coursetagslinks['all'] = array('title'=>get_string('alltags2', $tagslang),
292                                                     'onclick'=>'f_alltags',
293                                                     'text'=>get_string('alltags1', $tagslang));
294                 }
295                 if ($officialtags) {
296                     $coursetagslinks['off'] = array('title'=>get_string('officialtags2', $tagslang),
297                                                     'onclick'=>'f_officialtags',
298                                                     'text'=>get_string('officialtags1', $tagslang));
299                 }
300                 //if ($commtags) {
301                 //    $coursetagslinks['com'] = array('title'=>get_string('communitytags2', $tagslang),
302                 //                                    'onclick'=>'f_commtags',
303                 //                                    'text'=>get_string('communitytags1', $tagslang));
304                 //}
305                 if ($coursetags) {
306                     $coursetagslinks['crs'] = array('title'=>get_string('coursetags2', $tagslang),
307                                                     'onclick'=>'f_coursetags',
308                                                     'text'=>get_string('coursetags1', $tagslang));
309                 }
310                 coursetag_get_jscript_links($coursetagslinks);
312             } else {
313                 //if not logged in
314                 $this->content->footer = '<hr />'.get_string('please', $tagslang).'
315                     <a href="'.get_login_url().'">'.get_string('login', $tagslang).'
316                         </a> '.get_string('tagunits', $tagslang);
317             }
318         }
319         // end of show course tags section
321         return $this->content;
322     }