MDL-8188, add approve.gif to chameleon theme
[moodle-linuxchix.git] / tag / edit.php
blob0ce61b4a5e7d63b194d3f485f661f1971cde3c23
1 <?php // $Id$
3 require_once('../config.php');
4 require_once('lib.php');
5 require_once('edit_form.php');
7 require_js(array('yui_dom-event', 'yui_connection', 'yui_animation', 'yui_autocomplete'));
9 require_login();
11 if (empty($CFG->usetags)) {
12 print_error('tagsaredisabled', 'tag');
15 $tag_id = optional_param('id', 0, PARAM_INT);
16 $tag_name = optional_param('tag', '', PARAM_TAG);
18 if ($tag_name) {
19 $tag = tag_get('name', $tag_name, '*');
20 } else if ($tag_id) {
21 $tag = tag_get('id', $tag_id, '*');
24 if (empty($tag)) {
25 redirect($CFG->wwwroot.'/tag/search.php');
28 $tagname = tag_display_name($tag);
30 //Editing a tag requires moodle/tag:edit capability
31 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
32 require_capability('moodle/tag:edit', $systemcontext);
34 // set the relatedtags field of the $tag object that will be passed to the form
35 // need to use html_entity_decode because formslib does it for us later on.
36 $tag->relatedtags = html_entity_decode(tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT));
38 if (can_use_html_editor()) {
39 $options = new object();
40 $options->smiley = false;
41 $options->filter = false;
43 // convert and remove any XSS
44 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
45 $tag->descriptionformat = FORMAT_HTML;
48 $errorstring = '';
50 $tagform = new tag_edit_form();
51 if ( $tag->tagtype == 'official' ) {
52 $tag->tagtype = '1';
53 } else {
54 $tag->tagtype = '0';
56 $tagform->set_data($tag);
58 // If new data has been sent, update the tag record
59 if ($tagnew = $tagform->get_data()) {
61 tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
63 if (has_capability('moodle/tag:manage', $systemcontext)) {
64 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
65 tag_type_set($tag->id, 'default');
67 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
68 tag_type_set($tag->id, 'official');
72 if (!has_capability('moodle/tag:manage', $systemcontext)) {
73 unset($tagnew->name);
74 unset($tagnew->rawname);
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));
79 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
80 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
81 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
86 if (empty($errorstring)) { // All is OK, let's save it
88 $tagnew->timemodified = time();
90 // rename tag if needed
91 if (!tag_rename($tag->id, $tagnew->rawname)) {
92 error('Error updating tag record');
95 //updated related tags
96 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
97 //print_object($tagnew); die();
99 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
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' => '');
108 $navigation = build_navigation($navlinks);
109 print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
111 print_heading($tagname, '', 2);
113 if (!empty($errorstring)) {
114 notify($errorstring);
117 $tagform->display();
119 if (ajaxenabled()) {
122 <script type="text/javascript">
124 // An XHR DataSource
125 var myServer = "./tag_autocomplete.php";
126 var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
127 myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
128 myDataSource.maxCacheEntries = 60;
129 myDataSource.queryMatchSubset = true;
131 var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
132 myAutoComp.delimChar = ",";
133 myAutoComp.maxResultsDisplayed = 20;
134 myAutoComp.minQueryLength = 2;
135 myAutoComp.allowBrowserAutocomplete = false;
136 myAutoComp.formatResult = function(aResultItem, sQuery) {
137 return aResultItem[1];
139 </script>
141 <?php
143 print_footer();