MDL-13579 Corrected a bug in default value selection for the grader report preference...
[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);
17if ($tag_name) {
dee71a16 18 $tag = tag_get_id($tag_name, TAG_RETURN_OBJECT);
3d535996 19} elseif ( $tag_id ) {
20 $tag = tag_get_tag_by_id($tag_id);
21} else {
22 error('A required parameter was missing');
23}
9c9d3076 24$tagname = tag_display_name($tag);
c933a060 25
4f81c51d 26//Editing a tag requires moodle/tag:edit capability
c933a060 27$systemcontext = get_context_instance(CONTEXT_SYSTEM);
4f81c51d 28require_capability('moodle/tag:edit', $systemcontext);
c933a060 29
30// set the relatedtags field of the $tag object that will be passed to the form
0f039283 31// need to use html_entity_decode because formslib does it for us later on.
bbb4ba59 32$tag->relatedtags = html_entity_decode(tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT));
c933a060 33
d4be9d6d 34if (can_use_html_editor()) {
35 $options = new object();
36 $options->smiley = false;
37 $options->filter = false;
38
39 // convert and remove any XSS
40 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
41 $tag->descriptionformat = FORMAT_HTML;
42}
43
7e6e76a6 44$errorstring = '';
45
c933a060 46$tagform = new tag_edit_form();
47$tagform->set_data($tag);
48
7e6e76a6 49// If new data has been sent, update the tag record
c933a060 50if ($tagnew = $tagform->get_data()) {
d4be9d6d 51
7e6e76a6 52 if (!has_capability('moodle/tag:manage', $systemcontext)) {
53 unset($tagnew->name);
54 unset($tagnew->rawname);
55
56 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
57 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
58
59 if (!$tagold = tag_get_tag_by_id($tag_id)) { // For doing checks
ffcd3023 60 error('Error finding tag record');
7e6e76a6 61 }
c933a060 62
7e6e76a6 63 if ($tagold->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
64 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
65 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
66 }
67 }
68 }
c933a060 69
7e6e76a6 70 if (empty($errorstring)) { // All is OK, let's save it
71 $tagnew->timemodified = time();
72
071cda6c 73 // rename tag if needed
ffcd3023 74 if (tag_rename($tag_id, $tagnew->rawname)) {
7e6e76a6 75 error('Error updating tag record');
76 }
77
78 //updated related tags
79 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 80 //print_object($tagnew); die();
7e6e76a6 81
82 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
83 }
c933a060 84}
85
86
87$navlinks = array();
88$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
89$navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
90
91$navigation = build_navigation($navlinks);
92print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
93
94print_heading($tagname, '', 2);
95
7e6e76a6 96if (!empty($errorstring)) {
97 notify($errorstring);
98}
99
c933a060 100$tagform->display();
101
9c9d3076 102if (ajaxenabled()) {
9ee998a5 103?>
c933a060 104
105<script type="text/javascript">
106
107// An XHR DataSource
108var myServer = "./tag_autocomplete.php";
109var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
110myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
111myDataSource.maxCacheEntries = 60;
112myDataSource.queryMatchSubset = true;
113
114var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
115myAutoComp.delimChar = ",";
116myAutoComp.maxResultsDisplayed = 20;
117myAutoComp.minQueryLength = 2;
118myAutoComp.allowBrowserAutocomplete = false;
16803c57 119myAutoComp.formatResult = function(aResultItem, sQuery) {
120 return aResultItem[1];
121}
c933a060 122</script>
123
124<?php
9c9d3076 125}
c933a060 126print_footer();
127
128?>