Updated the HEAD build version to 20080428
[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)) {
5a2a5331 12 print_error('tagsaredisabled', 'tag');
c933a060 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();
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
b50d2245 61 tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
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
7e6e76a6 72 if (!has_capability('moodle/tag:manage', $systemcontext)) {
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
071cda6c 90 // rename tag if needed
2f4b82f4 91 if (!tag_rename($tag->id, $tagnew->rawname)) {
5a2a5331 92 print_error('Error updating tag record');
7e6e76a6 93 }
94
95 //updated related tags
96 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 97 //print_object($tagnew); die();
7e6e76a6 98
99 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
100 }
c933a060 101}
102
103
104$navlinks = array();
105$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
106$navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
107
108$navigation = build_navigation($navlinks);
109print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
110
111print_heading($tagname, '', 2);
112
7e6e76a6 113if (!empty($errorstring)) {
114 notify($errorstring);
115}
116
c933a060 117$tagform->display();
118
9c9d3076 119if (ajaxenabled()) {
9ee998a5 120?>
c933a060 121
122<script type="text/javascript">
123
124// An XHR DataSource
125var myServer = "./tag_autocomplete.php";
126var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
127myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
128myDataSource.maxCacheEntries = 60;
129myDataSource.queryMatchSubset = true;
130
131var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
132myAutoComp.delimChar = ",";
133myAutoComp.maxResultsDisplayed = 20;
134myAutoComp.minQueryLength = 2;
135myAutoComp.allowBrowserAutocomplete = false;
16803c57 136myAutoComp.formatResult = function(aResultItem, sQuery) {
137 return aResultItem[1];
138}
c933a060 139</script>
140
141<?php
9c9d3076 142}
c933a060 143print_footer();
144
145?>