Merge branch 'wip-MDL-25125-MOODLE_21_STABLE' of github.com:marinaglancy/moodle into...
[moodle.git] / tag / edit.php
blob306d5576a59f5c7928550ff0a977bc8fde53bea0
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * @package core
20 * @subpackage 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);
32 require_login();
34 if (empty($CFG->usetags)) {
35 print_error('tagsaredisabled', 'tag');
38 //Editing a tag requires moodle/tag:edit capability
39 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
40 require_capability('moodle/tag:edit', $systemcontext);
42 if ($tag_name) {
43 $tag = tag_get('name', $tag_name, '*');
44 } else if ($tag_id) {
45 $tag = tag_get('id', $tag_id, '*');
48 if (empty($tag)) {
49 redirect($CFG->wwwroot.'/tag/search.php');
52 $PAGE->set_url('/tag/index.php', array('id' => $tag->id));
53 $PAGE->set_subpage($tag->id);
54 $PAGE->set_context($systemcontext);
55 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
56 $PAGE->set_pagelayout('base');
58 $PAGE->requires->yui2_lib('connection');
59 $PAGE->requires->yui2_lib('animation');
60 $PAGE->requires->yui2_lib('datasource');
61 $PAGE->requires->yui2_lib('autocomplete');
63 $tagname = tag_display_name($tag);
65 // set the relatedtags field of the $tag object that will be passed to the form
66 $tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
68 if (can_use_html_editor()) {
69 $options = new stdClass();
70 $options->smiley = false;
71 $options->filter = false;
73 // convert and remove any XSS
74 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
75 $tag->descriptionformat = FORMAT_HTML;
78 $errorstring = '';
80 $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false);
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 $tagform->set_data($tag);
92 // If new data has been sent, update the tag record
93 if ($tagnew = $tagform->get_data()) {
95 if (has_capability('moodle/tag:manage', $systemcontext)) {
96 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
97 tag_type_set($tag->id, 'default');
99 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
100 tag_type_set($tag->id, 'official');
104 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
105 unset($tagnew->name);
106 unset($tagnew->rawname);
108 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
109 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
111 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
112 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
113 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
118 if (empty($errorstring)) { // All is OK, let's save it
120 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id);
122 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
124 $tagnew->timemodified = time();
126 if (has_capability('moodle/tag:manage', $systemcontext)) {
127 // rename tag
128 if(!tag_rename($tag->id, $tagnew->rawname)) {
129 print_error('errorupdatingrecord', 'tag');
133 //log tag changes activity
134 //if tag name exist from form, renaming is allow. record log action as rename
135 //otherwise, record log action as update
136 if (isset($tagnew->name) && ($tag->name != $tagnew->name)){
137 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name);
139 } elseif ($tag->description != $tagnew->description) {
140 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name);
143 //updated related tags
144 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
145 //print_object($tagnew); die();
147 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
151 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
152 $PAGE->navbar->add($tagname);
153 $PAGE->navbar->add(get_string('edit'));
154 $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
155 $PAGE->set_heading($COURSE->fullname);
156 echo $OUTPUT->header();
157 echo $OUTPUT->heading($tagname, 2);
159 if (!empty($errorstring)) {
160 echo $OUTPUT->notification($errorstring);
163 $tagform->display();
165 if (ajaxenabled()) {
166 $PAGE->requires->js('/tag/tag.js');
167 $PAGE->requires->js_function_call('init_tag_autocomplete', null, true);
169 echo $OUTPUT->footer();