Merged MDL-13676 Fixed some mistakes I just made
[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
9c9d3076 7require_js(array('yui_dom-event', 'yui_connection', 'yui_animation', 'yui_autocomplete'));
8
c933a060 9require_login();
10
9c9d3076 11if (empty($CFG->usetags)) {
c933a060 12 error(get_string('tagsaredisabled', 'tag'));
13}
14
3d535996 15$tag_id = optional_param('id', 0, PARAM_INT);
16$tag_name = optional_param('tag', '', PARAM_TAG);
8479c2e0 17
3d535996 18if ($tag_name) {
8479c2e0 19 $tag = tag_get('name', $tag_name, '*');
20} else if ($tag_id) {
21 $tag = tag_get('id', $tag_id, '*');
22}
23
24if (empty($tag)) {
25 redirect($CFG->wwwroot.'/tag/search.php');
3d535996 26}
8479c2e0 27
9c9d3076 28$tagname = tag_display_name($tag);
c933a060 29
4f81c51d 30//Editing a tag requires moodle/tag:edit capability
c933a060 31$systemcontext = get_context_instance(CONTEXT_SYSTEM);
4f81c51d 32require_capability('moodle/tag:edit', $systemcontext);
c933a060 33
34// set the relatedtags field of the $tag object that will be passed to the form
0f039283 35// need to use html_entity_decode because formslib does it for us later on.
bbb4ba59 36$tag->relatedtags = html_entity_decode(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();
51$tagform->set_data($tag);
52
7e6e76a6 53// If new data has been sent, update the tag record
c933a060 54if ($tagnew = $tagform->get_data()) {
d4be9d6d 55
b50d2245 56 tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
57
7e6e76a6 58 if (!has_capability('moodle/tag:manage', $systemcontext)) {
59 unset($tagnew->name);
60 unset($tagnew->rawname);
61
62 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
63 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
64
2f4b82f4 65 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
7e6e76a6 66 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
67 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
68 }
69 }
70 }
c933a060 71
7e6e76a6 72 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 73
7e6e76a6 74 $tagnew->timemodified = time();
75
071cda6c 76 // rename tag if needed
2f4b82f4 77 if (!tag_rename($tag->id, $tagnew->rawname)) {
7e6e76a6 78 error('Error updating tag record');
79 }
80
81 //updated related tags
82 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 83 //print_object($tagnew); die();
7e6e76a6 84
85 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
86 }
c933a060 87}
88
89
90$navlinks = array();
91$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
92$navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
93
94$navigation = build_navigation($navlinks);
95print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
96
97print_heading($tagname, '', 2);
98
7e6e76a6 99if (!empty($errorstring)) {
100 notify($errorstring);
101}
102
c933a060 103$tagform->display();
104
9c9d3076 105if (ajaxenabled()) {
9ee998a5 106?>
c933a060 107
108<script type="text/javascript">
109
110// An XHR DataSource
111var myServer = "./tag_autocomplete.php";
112var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
113myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
114myDataSource.maxCacheEntries = 60;
115myDataSource.queryMatchSubset = true;
116
117var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
118myAutoComp.delimChar = ",";
119myAutoComp.maxResultsDisplayed = 20;
120myAutoComp.minQueryLength = 2;
121myAutoComp.allowBrowserAutocomplete = false;
16803c57 122myAutoComp.formatResult = function(aResultItem, sQuery) {
123 return aResultItem[1];
124}
c933a060 125</script>
126
127<?php
9c9d3076 128}
c933a060 129print_footer();
130
131?>