Revert "Merge branch 'MDL-48591_m27' of https://github.com/jrchamp/moodle into MOODLE...
[moodle.git] / blocks / tags / block_tags.php
blob66a4be8fa4cd36f33717151553b67955c2e7230d
1 <?php
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.
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/>.
17 /**
18 * Tags block.
20 * @package block_tags
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 class block_tags extends block_base {
26 public function init() {
27 $this->title = get_string('pluginname', 'block_tags');
30 public function instance_allow_multiple() {
31 return true;
34 public function has_config() {
35 return true;
38 public function applicable_formats() {
39 return array('all' => true);
42 public function instance_allow_config() {
43 return true;
46 public function specialization() {
48 // Load userdefined title and make sure it's never empty.
49 if (empty($this->config->title)) {
50 $this->title = get_string('pluginname', 'block_tags');
51 } else {
52 $this->title = $this->config->title;
56 public function get_content() {
58 global $CFG, $COURSE, $USER, $SCRIPT, $OUTPUT;
60 if (empty($CFG->usetags)) {
61 $this->content = new stdClass();
62 $this->content->text = '';
63 if ($this->page->user_is_editing()) {
64 $this->content->text = get_string('disabledtags', 'block_tags');
66 return $this->content;
69 if (!isset($this->config)) {
70 $this->config = new stdClass();
73 if (empty($this->config->numberoftags)) {
74 $this->config->numberoftags = 80;
77 if (empty($this->config->tagtype)) {
78 $this->config->tagtype = '';
81 if ($this->content !== NULL) {
82 return $this->content;
85 if (empty($this->instance)) {
86 $this->content = '';
87 return $this->content;
90 $this->content = new stdClass;
91 $this->content->text = '';
92 $this->content->footer = '';
94 // Get a list of tags.
96 require_once($CFG->dirroot.'/tag/locallib.php');
98 if (empty($CFG->block_tags_showcoursetags) or !$CFG->block_tags_showcoursetags) {
100 $this->content->text = tag_print_cloud(null, $this->config->numberoftags, true);
102 } else {
103 // Start of show course tags section.
104 require_once($CFG->dirroot.'/tag/coursetagslib.php');
106 // Page awareness.
107 $tagtype = 'all';
108 if ($SCRIPT == '/my/index.php') {
109 $tagtype = 'my';
110 } else if (isset($this->page->course->id)) {
111 if ($this->page->course->id != SITEID) {
112 $tagtype = 'course';
116 // DB hits to get groups of marked up tags (if available).
117 // TODO check whether time limited personal tags are required.
118 $content = '';
119 $moretags = new moodle_url('/tag/coursetags_more.php', array('show'=>$tagtype));
120 if ($tagtype == 'all') {
121 $tags = coursetag_get_tags(0, 0, $this->config->tagtype, $this->config->numberoftags);
122 } else if ($tagtype == 'course') {
123 $tags = coursetag_get_tags($this->page->course->id, 0, $this->config->tagtype, $this->config->numberoftags);
124 $moretags->param('courseid', $this->page->course->id);
125 } else if ($tagtype == 'my') {
126 $tags = coursetag_get_tags(0, $USER->id, $this->config->tagtype, $this->config->numberoftags);
128 $tagcloud = tag_print_cloud($tags, 150, true);
129 if (!$tagcloud) {
130 $tagcloud = get_string('notagsyet', 'block_tags');
133 // Prepare the divs that display the groups of tags.
134 $content = get_string($tagtype."tags", 'block_tags').
135 '<div class="coursetag_list">'.$tagcloud.'</div>
136 <div class="coursetag_morelink">
137 <a href="'.$moretags->out().'" title="'.get_string('moretags', 'block_tags').'">'
138 .get_string('more', 'block_tags').'</a>
139 </div>';
140 // Add javascript.
141 coursetag_get_jscript();
143 // Add the divs (containing the tags) to the block's content.
144 $this->content->text .= $content;
146 // Add the input form section (allowing a user to tag the current course) and navigation, or login message.
147 if (isloggedin() && !isguestuser()) {
148 // Only show the input form on course pages for those allowed (or not barred).
149 if ($tagtype == 'course' &&
150 has_capability('moodle/tag:create', context_course::instance($this->page->course->id))) {
151 $buttonadd = get_string('add', 'block_tags');
152 $arrowtitle = get_string('arrowtitle', 'block_tags');
153 $edittags = get_string('edittags', 'block_tags');
154 $sesskey = sesskey();
155 $arrowright = $OUTPUT->pix_url('t/arrow_left');
156 $redirect = $this->page->url->out();
157 $this->content->footer .= <<<EOT
158 <hr />
159 <form action="{$CFG->wwwroot}/tag/coursetags_add.php" method="post" id="coursetag"
160 onsubmit="return ctags_checkinput(this.coursetag_new_tag.value)">
161 <div style="display: none;">
162 <input type="hidden" name="entryid" value="$COURSE->id" />
163 <input type="hidden" name="userid" value="$USER->id" />
164 <input type="hidden" name="sesskey" value="$sesskey" />
165 <input type="hidden" name="returnurl" value="$redirect" />
166 </div>
167 <div class="coursetag_form_wrapper">
168 <div class="coursetag_form_positioner">
169 <div class="coursetag_form_input1">
170 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
171 </div>
172 <div class="coursetag_form_input2">
173 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag"
174 class="coursetag_form_input2a" onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
175 </div>
176 <div class="coursetag_form_input3" id="coursetag_sug_btn">
177 <a title="$arrowtitle">
178 <img src="$arrowright" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
179 </a>
180 </div>
181 </div>
182 <div style="display: inline;">
183 <button type="submit">$buttonadd</button>
184 <a href="$CFG->wwwroot/tag/coursetags_edit.php?courseid=$COURSE->id" title="$edittags">$edittags</a>
185 </div>
186 </div>
187 </form>
188 EOT;
190 } else {
191 // If not logged in.
192 $this->content->footer = '<hr />'.get_string('please', 'block_tags').'
193 <a href="'.get_login_url().'">'.get_string('login', 'block_tags').'
194 </a> '.get_string('tagunits', 'block_tags');
197 // End of show course tags section.
199 return $this->content;