MDL-50919 tags: new UI for managing tags
[moodle.git] / tag / edit.php
CommitLineData
6bfe7aac 1<?php
000ebd5c
DM
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
bcdcee90 17
000ebd5c 18/**
bcdcee90
GGC
19 * @package core_tag
20 * @category tag
000ebd5c
DM
21 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
c933a060 25require_once('../config.php');
26require_once('lib.php');
27require_once('edit_form.php');
c933a060 28
0edd6535 29$tag_id = optional_param('id', 0, PARAM_INT);
30$tag_name = optional_param('tag', '', PARAM_TAG);
8e355853 31$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
9c9d3076 32
c933a060 33require_login();
34
9c9d3076 35if (empty($CFG->usetags)) {
5a2a5331 36 print_error('tagsaredisabled', 'tag');
c933a060 37}
38
0edd6535 39//Editing a tag requires moodle/tag:edit capability
43731030 40$systemcontext = context_system::instance();
0edd6535 41require_capability('moodle/tag:edit', $systemcontext);
8479c2e0 42
3d535996 43if ($tag_name) {
8479c2e0 44 $tag = tag_get('name', $tag_name, '*');
45} else if ($tag_id) {
46 $tag = tag_get('id', $tag_id, '*');
47}
48
49if (empty($tag)) {
50 redirect($CFG->wwwroot.'/tag/search.php');
3d535996 51}
8479c2e0 52
a6855934 53$PAGE->set_url('/tag/index.php', array('id' => $tag->id));
0edd6535 54$PAGE->set_subpage($tag->id);
55$PAGE->set_context($systemcontext);
56$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
8e355853 57$PAGE->set_pagelayout('standard');
c933a060 58
0edd6535 59$tagname = tag_display_name($tag);
c933a060 60
61// set the relatedtags field of the $tag object that will be passed to the form
e7134583 62$tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
c933a060 63
3d27180e
DW
64$options = new stdClass();
65$options->smiley = false;
66$options->filter = false;
67
68// convert and remove any XSS
69$tag->description = format_text($tag->description, $tag->descriptionformat, $options);
70$tag->descriptionformat = FORMAT_HTML;
d4be9d6d 71
7e6e76a6 72$errorstring = '';
73
e9de1cf4
RT
74$editoroptions = array(
75 'maxfiles' => EDITOR_UNLIMITED_FILES,
76 'maxbytes' => $CFG->maxbytes,
77 'trusttext' => false,
5e95223e
PS
78 'context' => $systemcontext,
79 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id),
e9de1cf4 80);
64f93798 81$tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id);
8bdc9cac
SH
82
83$tagform = new tag_edit_form(null, compact('editoroptions'));
603443b5 84if ( $tag->tagtype == 'official' ) {
85 $tag->tagtype = '1';
86} else {
87 $tag->tagtype = '0';
88}
8bdc9cac 89
8e355853 90$tag->returnurl = $returnurl;
c933a060 91$tagform->set_data($tag);
92
7e6e76a6 93// If new data has been sent, update the tag record
8e355853
MG
94if ($tagform->is_cancelled()) {
95 redirect($returnurl ? new moodle_url($returnurl) :
96 new moodle_url('/tag/index.php', array('tag' => $tag->name)));
97} else if ($tagnew = $tagform->get_data()) {
d4be9d6d 98
603443b5 99 if (has_capability('moodle/tag:manage', $systemcontext)) {
28f64040 100 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
603443b5 101 tag_type_set($tag->id, 'default');
102
103 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
104 tag_type_set($tag->id, 'official');
105 }
106 }
107
867c44c3 108 if (!has_capability('moodle/tag:manage', $systemcontext)) {
7e6e76a6 109 unset($tagnew->name);
110 unset($tagnew->rawname);
111
112 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
7c2cc9c8
PS
113 $norm = tag_normalize($tagnew->rawname, TAG_CASE_LOWER);
114 $tagnew->name = array_shift($norm);
7e6e76a6 115
8e355853
MG
116 if ($tag->rawname !== $tagnew->rawname) { // The name has changed, let's make sure it's not another existing tag
117 if (($id = tag_get_id($tagnew->name)) && $id != $tag->id) { // Something exists already, so flag an error.
7e6e76a6 118 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
119 }
120 }
121 }
c933a060 122
7e6e76a6 123 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 124
64f93798 125 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id);
03221650 126
09fce434
MN
127 if ($tag->description != $tagnew->description) {
128 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
129 }
8bdc9cac 130
7e6e76a6 131 $tagnew->timemodified = time();
132
3efae234 133 if (has_capability('moodle/tag:manage', $systemcontext)) {
09fce434 134 // Check if we need to rename the tag.
8e355853 135 if (isset($tagnew->name) && ($tag->rawname != $tagnew->rawname)) {
09fce434
MN
136 // Rename the tag.
137 if (!tag_rename($tag->id, $tagnew->rawname)) {
138 print_error('errorupdatingrecord', 'tag');
139 }
3efae234 140 }
7e6e76a6 141 }
6bfe7aac 142
7e6e76a6 143 //updated related tags
a9282e41 144 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)), 'core', $systemcontext->id);
0d626493 145 //print_object($tagnew); die();
6bfe7aac 146
8e355853
MG
147 $tagname = isset($tagnew->rawname) ? $tagnew->rawname : $tag->rawname;
148 redirect($returnurl ? new moodle_url($returnurl) :
149 new moodle_url('/tag/index.php', array('tag' => $tagname)));
7e6e76a6 150 }
c933a060 151}
152
8e355853 153navigation_node::override_active_url(new moodle_url('/tag/search.php'));
968583af 154$PAGE->navbar->add($tagname);
440e1f1a 155$PAGE->navbar->add(get_string('edit'));
968583af 156$PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
440e1f1a 157$PAGE->set_heading($COURSE->fullname);
968583af 158echo $OUTPUT->header();
73aec3a7 159echo $OUTPUT->heading($tagname, 2);
c933a060 160
7e6e76a6 161if (!empty($errorstring)) {
8f6e7e6c 162 echo $OUTPUT->notification($errorstring);
7e6e76a6 163}
164
c933a060 165$tagform->display();
166
6538153b
FM
167$PAGE->requires->js('/tag/tag.js');
168$PAGE->requires->js_function_call('init_tag_autocomplete', null, true);
169
1dd2ba54 170echo $OUTPUT->footer();