weekly release 2.7dev
[moodle.git] / blocks / tags / block_tags.php
CommitLineData
4ca6cfbf 1<?php
7dd764b8
JG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d797e50f 17
18class block_tags extends block_base {
7dd764b8 19 public function init() {
8d4904be 20 $this->title = get_string('pluginname', 'block_tags');
d797e50f 21 }
22
7dd764b8 23 public function instance_allow_multiple() {
d797e50f 24 return true;
25 }
26
7dd764b8 27 public function has_config() {
38fb8190 28 return true;
d797e50f 29 }
30
7dd764b8 31 public function applicable_formats() {
cdcaa9f2 32 return array('all' => true);
d797e50f 33 }
34
7dd764b8 35 public function instance_allow_config() {
d797e50f 36 return true;
37 }
38
7dd764b8 39 public function specialization() {
d797e50f 40
7dd764b8 41 // Load userdefined title and make sure it's never empty.
d797e50f 42 if (empty($this->config->title)) {
7dd764b8 43 $this->title = get_string('pluginname', 'block_tags');
d797e50f 44 } else {
45 $this->title = $this->config->title;
46 }
47 }
48
7dd764b8 49 public function get_content() {
d797e50f 50
7dd764b8 51 global $CFG, $COURSE, $USER, $SCRIPT, $OUTPUT;
d797e50f 52
53 if (empty($CFG->usetags)) {
7c2cc9c8 54 $this->content = new stdClass();
d797e50f 55 $this->content->text = '';
b58961ac 56 if ($this->page->user_is_editing()) {
57 $this->content->text = get_string('disabledtags', 'block_tags');
58 }
d797e50f 59 return $this->content;
60 }
61
b85b25eb
PS
62 if (!isset($this->config)) {
63 $this->config = new stdClass();
64 }
65
d797e50f 66 if (empty($this->config->numberoftags)) {
67 $this->config->numberoftags = 80;
68 }
69
7dd764b8
JG
70 if (empty($this->config->tagtype)) {
71 $this->config->tagtype = '';
72 }
73
d797e50f 74 if ($this->content !== NULL) {
75 return $this->content;
76 }
77
78 if (empty($this->instance)) {
79 $this->content = '';
80 return $this->content;
81 }
82
83 $this->content = new stdClass;
c891a73d 84 $this->content->text = '';
d797e50f 85 $this->content->footer = '';
86
7dd764b8 87 // Get a list of tags.
d797e50f 88
7b2b1a91 89 require_once($CFG->dirroot.'/tag/locallib.php');
d797e50f 90
38fb8190 91 if (empty($CFG->block_tags_showcoursetags) or !$CFG->block_tags_showcoursetags) {
92
7dd764b8 93 $this->content->text = tag_print_cloud(null, $this->config->numberoftags, true);
38fb8190 94
38fb8190 95 } else {
7dd764b8 96 // Start of show course tags section.
38fb8190 97 require_once($CFG->dirroot.'/tag/coursetagslib.php');
98
7dd764b8
JG
99 // Page awareness.
100 $tagtype = 'all';
101 if ($SCRIPT == '/my/index.php') {
102 $tagtype = 'my';
103 } else if (isset($this->page->course->id)) {
104 if ($this->page->course->id != SITEID) {
105 $tagtype = 'course';
38fb8190 106 }
107 }
38fb8190 108
7dd764b8
JG
109 // DB hits to get groups of marked up tags (if available).
110 // TODO check whether time limited personal tags are required.
111 $content = '';
112 $moretags = new moodle_url('/tag/coursetags_more.php', array('show'=>$tagtype));
113 if ($tagtype == 'all') {
740c4ada 114 $tags = coursetag_get_tags(0, 0, $this->config->tagtype, $this->config->numberoftags);
d0dff189 115 } else if ($tagtype == 'course') {
740c4ada 116 $tags = coursetag_get_tags($this->page->course->id, 0, $this->config->tagtype, $this->config->numberoftags);
7dd764b8 117 $moretags->param('courseid', $this->page->course->id);
d0dff189 118 } else if ($tagtype == 'my') {
740c4ada 119 $tags = coursetag_get_tags(0, $USER->id, $this->config->tagtype, $this->config->numberoftags);
38fb8190 120 }
7dd764b8
JG
121 $tagcloud = tag_print_cloud($tags, 150, true);
122 if (!$tagcloud) {
123 $tagcloud = get_string('notagsyet', 'block_tags');
38fb8190 124 }
38fb8190 125
7dd764b8
JG
126 // Prepare the divs that display the groups of tags.
127 $content = get_string($tagtype."tags", 'block_tags').
128 '<div class="coursetag_list">'.$tagcloud.'</div>
129 <div class="coursetag_morelink">
130 <a href="'.$moretags->out().'" title="'.get_string('moretags', 'block_tags').'">'
131 .get_string('more', 'block_tags').'</a>
132 </div>';
133 // Add javascript.
134 coursetag_get_jscript();
135
136 // Add the divs (containing the tags) to the block's content.
137 $this->content->text .= $content;
138
139 // Add the input form section (allowing a user to tag the current course) and navigation, or login message.
140 if (isloggedin() && !isguestuser()) {
141 // Only show the input form on course pages for those allowed (or not barred).
142 if ($tagtype == 'course' &&
143 has_capability('moodle/tag:create', context_course::instance($this->page->course->id))) {
144 $buttonadd = get_string('add', 'block_tags');
145 $arrowtitle = get_string('arrowtitle', 'block_tags');
d0dff189 146 $edittags = get_string('edittags', 'block_tags');
973d2660 147 $sesskey = sesskey();
b5d0cafc 148 $arrowright = $OUTPUT->pix_url('t/arrow_left');
7dd764b8 149 $redirect = $this->page->url->out();
38fb8190 150 $this->content->footer .= <<<EOT
151 <hr />
152 <form action="{$CFG->wwwroot}/tag/coursetags_add.php" method="post" id="coursetag"
153 onsubmit="return ctags_checkinput(this.coursetag_new_tag.value)">
154 <div style="display: none;">
cae83708 155 <input type="hidden" name="entryid" value="$COURSE->id" />
38fb8190 156 <input type="hidden" name="userid" value="$USER->id" />
973d2660 157 <input type="hidden" name="sesskey" value="$sesskey" />
7dd764b8 158 <input type="hidden" name="returnurl" value="$redirect" />
38fb8190 159 </div>
7dd764b8
JG
160 <div class="coursetag_form_wrapper">
161 <div class="coursetag_form_positioner">
162 <div class="coursetag_form_input1">
163 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
164 </div>
165 <div class="coursetag_form_input2">
166 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag"
167 class="coursetag_form_input2a" onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
168 </div>
169 <div class="coursetag_form_input3" id="coursetag_sug_btn">
170 <a title="$arrowtitle">
171 <img src="$arrowright" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
172 </a>
173 </div>
38fb8190 174 </div>
7dd764b8
JG
175 <div style="display: inline;">
176 <button type="submit">$buttonadd</button>
d0dff189 177 <a href="$CFG->wwwroot/tag/coursetags_edit.php?courseid=$COURSE->id" title="$edittags">$edittags</a>
38fb8190 178 </div>
179 </div>
38fb8190 180 </form>
181EOT;
38fb8190 182 }
38fb8190 183 } else {
7dd764b8
JG
184 // If not logged in.
185 $this->content->footer = '<hr />'.get_string('please', 'block_tags').'
186 <a href="'.get_login_url().'">'.get_string('login', 'block_tags').'
187 </a> '.get_string('tagunits', 'block_tags');
38fb8190 188 }
189 }
7dd764b8 190 // End of show course tags section.
d797e50f 191
192 return $this->content;
193 }
d0dff189 194}