Moodle release 4.4beta
[moodle.git] / tag / edit.php
blobe159bed5d8f147f9238c33a0f245f8681a209c4d
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 throw new \moodle_exception('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->filter = false;
83 // convert and remove any XSS
84 $data->description = format_text($tag->description, $tag->descriptionformat, $options);
85 $data->descriptionformat = FORMAT_HTML;
87 $errorstring = '';
89 $editoroptions = array(
90 'maxfiles' => EDITOR_UNLIMITED_FILES,
91 'maxbytes' => $CFG->maxbytes,
92 'trusttext' => false,
93 'context' => $systemcontext,
94 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id),
96 $data = file_prepare_standard_editor($data, 'description', $editoroptions, $systemcontext, 'tag', 'description', $data->id);
98 $tagform = new tag_edit_form(null, array('editoroptions' => $editoroptions, 'tag' => $tag));
99 $data->returnurl = $returnurl;
101 $tagform->set_data($data);
103 if ($tagform->is_cancelled()) {
104 redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
105 } else if ($tagnew = $tagform->get_data()) {
106 // If new data has been sent, update the tag record.
107 $updatedata = array();
109 if (has_capability('moodle/tag:manage', $systemcontext)) {
110 $updatedata['isstandard'] = empty($tagnew->isstandard) ? 0 : 1;
111 $updatedata['rawname'] = $tagnew->rawname;
114 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions,
115 $systemcontext, 'tag', 'description', $tag->id);
116 $updatedata['description'] = $tagnew->description;
117 $updatedata['descriptionformat'] = $tagnew->descriptionformat;
119 // Update name, description and whether it is a standard tag.
120 $tag->update($updatedata);
122 // Updated related tags.
123 $tag->set_related_tags($tagnew->relatedtags);
125 redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
128 navigation_node::override_active_url(new moodle_url('/tag/search.php'));
129 $PAGE->navbar->add($tagname);
130 $PAGE->navbar->add(get_string('edit'));
131 $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
132 $PAGE->set_heading($COURSE->fullname);
133 echo $OUTPUT->header();
134 echo $OUTPUT->heading($tagname, 2);
136 if (!empty($errorstring)) {
137 echo $OUTPUT->notification($errorstring);
140 $tagform->display();
142 echo $OUTPUT->footer();