files MDL-20635 Added support for cleaning up files associated with a context when...
[moodle.git] / tag / edit.php
CommitLineData
6bfe7aac 1<?php
c933a060 2
3require_once('../config.php');
4require_once('lib.php');
5require_once('edit_form.php');
c933a060 6
0edd6535 7$tag_id = optional_param('id', 0, PARAM_INT);
8$tag_name = optional_param('tag', '', PARAM_TAG);
9c9d3076 9
c933a060 10require_login();
11
9c9d3076 12if (empty($CFG->usetags)) {
5a2a5331 13 print_error('tagsaredisabled', 'tag');
c933a060 14}
15
0edd6535 16//Editing a tag requires moodle/tag:edit capability
17$systemcontext = get_context_instance(CONTEXT_SYSTEM);
18require_capability('moodle/tag:edit', $systemcontext);
8479c2e0 19
3d535996 20if ($tag_name) {
8479c2e0 21 $tag = tag_get('name', $tag_name, '*');
22} else if ($tag_id) {
23 $tag = tag_get('id', $tag_id, '*');
24}
25
26if (empty($tag)) {
27 redirect($CFG->wwwroot.'/tag/search.php');
3d535996 28}
8479c2e0 29
0edd6535 30$PAGE->set_url('tag/index.php', array('id' => $tag->id));
31$PAGE->set_subpage($tag->id);
32$PAGE->set_context($systemcontext);
33$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
34$PAGE->set_generaltype('form');
c933a060 35
0edd6535 36$PAGE->requires->yui_lib('animation');
37$PAGE->requires->yui_lib('autocomplete');
38
39$tagname = tag_display_name($tag);
c933a060 40
41// set the relatedtags field of the $tag object that will be passed to the form
e7134583 42$tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
c933a060 43
d4be9d6d 44if (can_use_html_editor()) {
45 $options = new object();
46 $options->smiley = false;
47 $options->filter = false;
48
49 // convert and remove any XSS
50 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
51 $tag->descriptionformat = FORMAT_HTML;
52}
53
7e6e76a6 54$errorstring = '';
55
c933a060 56$tagform = new tag_edit_form();
603443b5 57if ( $tag->tagtype == 'official' ) {
58 $tag->tagtype = '1';
59} else {
60 $tag->tagtype = '0';
61}
c933a060 62$tagform->set_data($tag);
63
7e6e76a6 64// If new data has been sent, update the tag record
c933a060 65if ($tagnew = $tagform->get_data()) {
d4be9d6d 66
294ce987 67 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
b50d2245 68
603443b5 69 if (has_capability('moodle/tag:manage', $systemcontext)) {
28f64040 70 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
603443b5 71 tag_type_set($tag->id, 'default');
72
73 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
74 tag_type_set($tag->id, 'official');
75 }
76 }
77
3efae234 78 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
7e6e76a6 79 unset($tagnew->name);
80 unset($tagnew->rawname);
81
82 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
83 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
84
2f4b82f4 85 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
7e6e76a6 86 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
87 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
88 }
89 }
90 }
c933a060 91
7e6e76a6 92 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 93
7e6e76a6 94 $tagnew->timemodified = time();
95
3efae234 96 if (has_capability('moodle/tag:manage', $systemcontext)) {
97 // rename tag
98 if(!tag_rename($tag->id, $tagnew->rawname)) {
38fb8190 99 print_error('errorupdatingrecord', 'tag');
3efae234 100 }
7e6e76a6 101 }
6bfe7aac 102
b89e4ad8 103 //log tag changes activity
104 //if tag name exist from form, renaming is allow. record log action as rename
6bfe7aac 105 //otherwise, record log action as update
b89e4ad8 106 if (isset($tagnew->name) && ($tag->name != $tagnew->name)){
107 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name);
108
6bfe7aac 109 } elseif ($tag->description != $tagnew->description) {
b89e4ad8 110 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name);
111 }
6bfe7aac 112
7e6e76a6 113 //updated related tags
114 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 115 //print_object($tagnew); die();
6bfe7aac 116
7e6e76a6 117 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
118 }
c933a060 119}
120
968583af 121$PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url($CFG->wwwroot.'/tag/search.php'));
122$PAGE->navbar->add($tagname);
123$PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
124echo $OUTPUT->header();
73aec3a7 125echo $OUTPUT->heading($tagname, 2);
c933a060 126
7e6e76a6 127if (!empty($errorstring)) {
8f6e7e6c 128 echo $OUTPUT->notification($errorstring);
7e6e76a6 129}
130
c933a060 131$tagform->display();
132
9c9d3076 133if (ajaxenabled()) {
1cc02867 134 $PAGE->requires->js('tag/tag.js');
135 $PAGE->requires->js_function_call('init_tag_autocomplete')->on_dom_ready();
9c9d3076 136}
1dd2ba54 137echo $OUTPUT->footer();