Merge branch 'MDL-70114-39' of git://github.com/bmbrands/moodle into MOODLE_39_STABLE
[moodle.git] / tag / edit.php
blobcbd53134fc95f7b56faabfe336d8a64bddcbd906
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 $tagid = optional_param('id', 0, PARAM_INT);
30 $tagname = 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 ($tagname) {
44 $tagcollid = optional_param('tc', 0, PARAM_INT);
45 if (!$tagcollid) {
46 // Tag name specified but tag collection was not. Try to guess it.
47 $tags = core_tag_tag::guess_by_name($tagname, '*');
48 if (count($tags) > 1) {
49 // This tag was found in more than one collection, redirect to search.
50 redirect(new moodle_url('/tag/search.php', array('tag' => $tagname)));
51 } else if (count($tags) == 1) {
52 $tag = reset($tags);
54 } else {
55 if (!$tag = core_tag_tag::get_by_name($tagcollid, $tagname, '*')) {
56 redirect(new moodle_url('/tag/search.php', array('tagcollid' => $tagcollid)));
59 } else if ($tagid) {
60 $tag = core_tag_tag::get($tagid, '*');
63 if (empty($tag)) {
64 redirect(new moodle_url('/tag/search.php'));
67 $PAGE->set_url($tag->get_view_url());
68 $PAGE->set_subpage($tag->id);
69 $PAGE->set_context($systemcontext);
70 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
71 $PAGE->set_pagelayout('standard');
73 $tagname = $tag->get_display_name();
74 $tagcollid = $tag->tagcollid;
76 // set the relatedtags field of the $tag object that will be passed to the form
77 $data = $tag->to_object();
78 $data->relatedtags = core_tag_tag::get_item_tags_array('core', 'tag', $tag->id);
80 $options = new stdClass();
81 $options->smiley = false;
82 $options->filter = false;
84 // convert and remove any XSS
85 $data->description = format_text($tag->description, $tag->descriptionformat, $options);
86 $data->descriptionformat = FORMAT_HTML;
88 $errorstring = '';
90 $editoroptions = array(
91 'maxfiles' => EDITOR_UNLIMITED_FILES,
92 'maxbytes' => $CFG->maxbytes,
93 'trusttext' => false,
94 'context' => $systemcontext,
95 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id),
97 $data = file_prepare_standard_editor($data, 'description', $editoroptions, $systemcontext, 'tag', 'description', $data->id);
99 $tagform = new tag_edit_form(null, array('editoroptions' => $editoroptions, 'tag' => $tag));
100 $data->returnurl = $returnurl;
102 $tagform->set_data($data);
104 if ($tagform->is_cancelled()) {
105 redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
106 } else if ($tagnew = $tagform->get_data()) {
107 // If new data has been sent, update the tag record.
108 $updatedata = array();
110 if (has_capability('moodle/tag:manage', $systemcontext)) {
111 $updatedata['isstandard'] = empty($tagnew->isstandard) ? 0 : 1;
112 $updatedata['rawname'] = $tagnew->rawname;
115 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions,
116 $systemcontext, 'tag', 'description', $tag->id);
117 $updatedata['description'] = $tagnew->description;
118 $updatedata['descriptionformat'] = $tagnew->descriptionformat;
120 // Update name, description and whether it is a standard tag.
121 $tag->update($updatedata);
123 // Updated related tags.
124 $tag->set_related_tags($tagnew->relatedtags);
126 redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
129 navigation_node::override_active_url(new moodle_url('/tag/search.php'));
130 $PAGE->navbar->add($tagname);
131 $PAGE->navbar->add(get_string('edit'));
132 $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
133 $PAGE->set_heading($COURSE->fullname);
134 echo $OUTPUT->header();
135 echo $OUTPUT->heading($tagname, 2);
137 if (!empty($errorstring)) {
138 echo $OUTPUT->notification($errorstring);
141 $tagform->display();
143 echo $OUTPUT->footer();