Merge branch 'MDL-51582-30' of git://github.com/danpoltawski/moodle into MOODLE_30_STABLE
[moodle.git] / tag / edit.php
blob16bf9e7b04e411af32e5dca46d1c76e33e688847
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 * @package core_tag
20 * @category tag
21 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once('edit_form.php');
29 $tag_id = optional_param('id', 0, PARAM_INT);
30 $tag_name = optional_param('tag', '', PARAM_TAG);
31 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
33 require_login();
35 if (empty($CFG->usetags)) {
36 print_error('tagsaredisabled', 'tag');
39 //Editing a tag requires moodle/tag:edit capability
40 $systemcontext = context_system::instance();
41 require_capability('moodle/tag:edit', $systemcontext);
43 if ($tag_name) {
44 $tag = tag_get('name', $tag_name, '*');
45 } else if ($tag_id) {
46 $tag = tag_get('id', $tag_id, '*');
49 if (empty($tag)) {
50 redirect($CFG->wwwroot.'/tag/search.php');
53 $PAGE->set_url('/tag/index.php', array('id' => $tag->id));
54 $PAGE->set_subpage($tag->id);
55 $PAGE->set_context($systemcontext);
56 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
57 $PAGE->set_pagelayout('standard');
59 $tagname = tag_display_name($tag);
61 // set the relatedtags field of the $tag object that will be passed to the form
62 $tag->relatedtags = tag_get_tags_array('tag', $tag->id);
64 $options = new stdClass();
65 $options->smiley = false;
66 $options->filter = false;
68 // convert and remove any XSS
69 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
70 $tag->descriptionformat = FORMAT_HTML;
72 $errorstring = '';
74 $editoroptions = array(
75 'maxfiles' => EDITOR_UNLIMITED_FILES,
76 'maxbytes' => $CFG->maxbytes,
77 'trusttext' => false,
78 'context' => $systemcontext,
79 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id),
81 $tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id);
83 $tagform = new tag_edit_form(null, compact('editoroptions'));
84 if ( $tag->tagtype == 'official' ) {
85 $tag->tagtype = '1';
86 } else {
87 $tag->tagtype = '0';
90 $tag->returnurl = $returnurl;
91 $tagform->set_data($tag);
93 // If new data has been sent, update the tag record
94 if ($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()) {
99 if (has_capability('moodle/tag:manage', $systemcontext)) {
100 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
101 tag_type_set($tag->id, 'default');
103 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
104 tag_type_set($tag->id, 'official');
108 if (!has_capability('moodle/tag:manage', $systemcontext)) {
109 unset($tagnew->name);
110 unset($tagnew->rawname);
112 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
113 $norm = tag_normalize($tagnew->rawname, TAG_CASE_LOWER);
114 $tagnew->name = array_shift($norm);
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.
118 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
123 if (empty($errorstring)) { // All is OK, let's save it
125 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id);
127 if ($tag->description != $tagnew->description) {
128 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
131 $tagnew->timemodified = time();
133 if (has_capability('moodle/tag:manage', $systemcontext)) {
134 // Check if we need to rename the tag.
135 if (isset($tagnew->name) && ($tag->rawname != $tagnew->rawname)) {
136 // Rename the tag.
137 if (!tag_rename($tag->id, $tagnew->rawname)) {
138 print_error('errorupdatingrecord', 'tag');
143 //updated related tags
144 tag_set('tag', $tagnew->id, $tagnew->relatedtags, 'core', $systemcontext->id);
145 //print_object($tagnew); die();
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)));
153 navigation_node::override_active_url(new moodle_url('/tag/search.php'));
154 $PAGE->navbar->add($tagname);
155 $PAGE->navbar->add(get_string('edit'));
156 $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
157 $PAGE->set_heading($COURSE->fullname);
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading($tagname, 2);
161 if (!empty($errorstring)) {
162 echo $OUTPUT->notification($errorstring);
165 $tagform->display();
167 echo $OUTPUT->footer();