MDL-19875 tested patch and added some more echos, thanks Tim!
[moodle.git] / tag / edit.php
CommitLineData
c933a060 1<?php // $Id$
2
3require_once('../config.php');
4require_once('lib.php');
5require_once('edit_form.php');
c933a060 6
25ddb7ef 7$PAGE->requires->yui_lib('animation');
8$PAGE->requires->yui_lib('autocomplete');
9c9d3076 9
c933a060 10require_login();
11
9c9d3076 12if (empty($CFG->usetags)) {
5a2a5331 13 print_error('tagsaredisabled', 'tag');
c933a060 14}
15
3d535996 16$tag_id = optional_param('id', 0, PARAM_INT);
17$tag_name = optional_param('tag', '', PARAM_TAG);
8479c2e0 18
3d535996 19if ($tag_name) {
8479c2e0 20 $tag = tag_get('name', $tag_name, '*');
21} else if ($tag_id) {
22 $tag = tag_get('id', $tag_id, '*');
23}
24
25if (empty($tag)) {
26 redirect($CFG->wwwroot.'/tag/search.php');
3d535996 27}
8479c2e0 28
9c9d3076 29$tagname = tag_display_name($tag);
c933a060 30
4f81c51d 31//Editing a tag requires moodle/tag:edit capability
c933a060 32$systemcontext = get_context_instance(CONTEXT_SYSTEM);
4f81c51d 33require_capability('moodle/tag:edit', $systemcontext);
c933a060 34
35// set the relatedtags field of the $tag object that will be passed to the form
e7134583 36$tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
c933a060 37
d4be9d6d 38if (can_use_html_editor()) {
39 $options = new object();
40 $options->smiley = false;
41 $options->filter = false;
42
43 // convert and remove any XSS
44 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
45 $tag->descriptionformat = FORMAT_HTML;
46}
47
7e6e76a6 48$errorstring = '';
49
c933a060 50$tagform = new tag_edit_form();
603443b5 51if ( $tag->tagtype == 'official' ) {
52 $tag->tagtype = '1';
53} else {
54 $tag->tagtype = '0';
55}
c933a060 56$tagform->set_data($tag);
57
7e6e76a6 58// If new data has been sent, update the tag record
c933a060 59if ($tagnew = $tagform->get_data()) {
d4be9d6d 60
294ce987 61 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
b50d2245 62
603443b5 63 if (has_capability('moodle/tag:manage', $systemcontext)) {
28f64040 64 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
603443b5 65 tag_type_set($tag->id, 'default');
66
67 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
68 tag_type_set($tag->id, 'official');
69 }
70 }
71
3efae234 72 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
7e6e76a6 73 unset($tagnew->name);
74 unset($tagnew->rawname);
75
76 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
77 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
78
2f4b82f4 79 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
7e6e76a6 80 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
81 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
82 }
83 }
84 }
c933a060 85
7e6e76a6 86 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 87
7e6e76a6 88 $tagnew->timemodified = time();
89
3efae234 90 if (has_capability('moodle/tag:manage', $systemcontext)) {
91 // rename tag
92 if(!tag_rename($tag->id, $tagnew->rawname)) {
38fb8190 93 print_error('errorupdatingrecord', 'tag');
3efae234 94 }
7e6e76a6 95 }
96
97 //updated related tags
98 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 99 //print_object($tagnew); die();
7e6e76a6 100
101 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
102 }
c933a060 103}
104
105
106$navlinks = array();
107$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
108$navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
109
110$navigation = build_navigation($navlinks);
111print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
112
113print_heading($tagname, '', 2);
114
7e6e76a6 115if (!empty($errorstring)) {
116 notify($errorstring);
117}
118
c933a060 119$tagform->display();
120
9c9d3076 121if (ajaxenabled()) {
9ee998a5 122?>
c933a060 123
124<script type="text/javascript">
125
126// An XHR DataSource
127var myServer = "./tag_autocomplete.php";
128var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
129myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
130myDataSource.maxCacheEntries = 60;
131myDataSource.queryMatchSubset = true;
132
133var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
134myAutoComp.delimChar = ",";
135myAutoComp.maxResultsDisplayed = 20;
136myAutoComp.minQueryLength = 2;
137myAutoComp.allowBrowserAutocomplete = false;
16803c57 138myAutoComp.formatResult = function(aResultItem, sQuery) {
139 return aResultItem[1];
140}
c933a060 141</script>
142
143<?php
9c9d3076 144}
c933a060 145print_footer();
146
147?>