Merge branch 'MDL-48255-27' of git://github.com/lameze/moodle into MOODLE_27_STABLE
[moodle.git] / tag / coursetags_edit.php
blobcf53bcafceccec7a271a8344564ff5f28b941845
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/>.
18 /**
19 * Displays personal tags for a course with some editing facilites
21 * @package core_tag
22 * @category tag
23 * @copyright 2007 j.beedell@open.ac.uk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../config.php');
28 require_once($CFG->dirroot.'/tag/coursetagslib.php');
29 require_once($CFG->dirroot.'/tag/lib.php');
31 $courseid = optional_param('courseid', 0, PARAM_INT);
32 $keyword = optional_param('coursetag_new_tag', '', PARAM_TEXT);
33 $deltag = optional_param('del_tag', 0, PARAM_INT);
35 $url = new moodle_url('/tag/coursetags_edit.php');
36 if ($courseid !== 0) {
37 $url->param('courseid', $courseid);
39 if ($keyword !== '') {
40 $url->param('coursetag_new_tag', $keyword);
42 if ($deltag !== 0) {
43 $url->param('del_tag', $deltag);
45 $PAGE->set_url($url);
47 require_login();
49 if (empty($CFG->usetags)) {
50 print_error('tagsaredisabled', 'tag');
53 if ($courseid != SITEID) {
54 if (! ($course = $DB->get_record('course', array('id' => $courseid), '*')) ) {
55 print_error('invalidcourse');
57 } else {
58 print_error('errortagfrontpage', 'tag');
61 // Permissions
62 $sitecontext = context_system::instance();
63 require_login($course);
64 $canedit = has_capability('moodle/tag:create', $sitecontext);
66 // Language strings
67 $tagslang = 'block_tags';
69 // Store data
70 if ($data = data_submitted()) {
71 if (confirm_sesskey() and $courseid > 0 and $USER->id > 0 and $canedit) {
72 // store personal tag
73 if (trim(strip_tags($keyword))) {
74 $myurl = 'tag/search.php';
75 $keywords = explode(',', $keyword);
76 coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl);
78 // delete personal tag
79 if ($deltag > 0) {
80 coursetag_delete_keyword($deltag, $USER->id, $courseid);
85 // The title and breadcrumb
86 $title = get_string('edittitle', $tagslang);
87 $coursefullname = format_string($course->fullname);
88 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
89 $PAGE->navbar->add($title);
90 $PAGE->set_title($title);
91 $PAGE->set_heading($course->fullname);
92 $PAGE->set_cacheable(false);
93 echo $OUTPUT->header();
95 // Print personal tags for all courses
96 $title = get_string('edittitle', $tagslang);
97 echo $OUTPUT->heading($title, 2, 'mdl-align');
99 $mytags = tag_print_cloud(coursetag_get_tags(0, $USER->id, 'default'), 150, true);
100 $outstr = '
101 <div class="coursetag_edit_centered">
102 <div>
103 '.get_string('editmytags', $tagslang).'
104 </div>
105 <div>';
107 if ($mytags) {
108 $outstr .= $mytags;
109 } else {
110 $outstr .= get_string('editnopersonaltags', $tagslang);
113 $outstr .= '
114 </div>
115 </div>';
116 echo $outstr;
118 // Personal tag editing
119 if ($canedit) {
120 $title = get_string('editmytagsfor', $tagslang, '"'.$coursefullname.' ('.$courseshortname.')"');
121 echo $OUTPUT->heading($title, 2, 'main mdl-align');
123 // Deletion here is open to the users own tags for this course only
124 $selectoptions = '<option value="0">'.get_string('select', $tagslang).'</option>';
125 $coursetabs = '';
126 if ($options = coursetag_get_records($courseid, $USER->id)) {
127 $coursetabs = '"';
128 foreach ($options as $option) {
129 $selectoptions .= '<option value="'.$option->id.'">'.$option->rawname.'</option>';
130 $coursetabs .= $option->rawname . ', ';
132 $coursetabs = rtrim($coursetabs, ', ');
133 $coursetabs .= '"';
135 if ($coursetabs) {
136 $outstr = '
137 <div class="coursetag_edit_centered">
138 '.get_string('editthiscoursetags', $tagslang, $coursetabs).'
139 </div>';
140 } else {
141 $outstr = '
142 <div class="coursetag_edit_centered">
143 '.get_string('editnopersonaltags', $tagslang).'
144 </div>';
147 // Print the add and delete form
148 coursetag_get_jscript();
149 $edittagthisunit = get_string('edittagthisunit', $tagslang);
150 $suggestedtagthisunit = get_string('suggestedtagthisunit', $tagslang);
151 $arrowtitle = get_string('arrowtitle', $tagslang);
152 $sesskey = sesskey();
153 $leftarrow = $OUTPUT->pix_url('t/arrow_left');
154 $outstr .= <<<EOT
155 <form action="$CFG->wwwroot/tag/coursetags_edit.php" method="post" id="coursetag">
156 <div style="display: none;">
157 <input type="hidden" name="courseid" value="$course->id" />
158 <input type="hidden" name="sesskey" value="$sesskey" />
159 </div>
160 <div class="coursetag_edit_centered">
161 <div class="coursetag_edit_row">
162 <div class="coursetag_edit_left">
163 <label class="accesshide" for="coursetag_sug_keyword">$suggestedtagthisunit</label>
164 </div>
165 <div class="coursetag_edit_right">
166 <div class="coursetag_form_input1">
167 <input type="text" name="coursetag_sug_keyword" id="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
168 </div>
169 </div>
170 </div>
171 <div class="coursetag_edit_row">
172 <div class="coursetag_edit_left">
173 <label for="coursetag_new_tag">$edittagthisunit</label>
174 </div>
175 <div class="coursetag_edit_right">
176 <div class="coursetag_form_input2">
177 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
178 onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
179 </div>
180 <div class="coursetag_edit_input3" id="coursetag_sug_btn">
181 <a title="$arrowtitle">
182 <img src="$leftarrow" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
183 </a>
184 </div>
185 </div>
186 </div>
187 EOT;
188 if ($coursetabs) {
189 $editdeletemytag = get_string('editdeletemytag', $tagslang);
190 $outstr .= <<<EOT1
191 <div class="coursetag_edit_row">
192 <div class="coursetag_edit_left">
193 <label for="del_tag">
194 $editdeletemytag
195 </label>
196 </div>
197 <div class="coursetag_edit_right">
198 <select id="del_tag" name="del_tag">
199 $selectoptions
200 </select>
201 </div>
202 </div>
203 EOT1;
205 $submitstr = get_string('submit');
206 $outstr .= <<<EOT2
207 <div class="clearer"></div>
208 <div class="coursetag_edit_row">
209 <button type="submit">$submitstr</button>
210 </div>
211 </div>
212 </form>
213 EOT2;
214 echo $outstr;
217 echo $OUTPUT->footer();