Updated the 19 build version to 20100904
[moodle.git] / tag / edit.php
blob7fa361ad518e5b0fc079267f07a875022032ca1b
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 $tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
37 if (can_use_html_editor()) {
38 $options = new object();
39 $options->smiley = false;
40 $options->filter = false;
42 // convert and remove any XSS
43 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
44 $tag->descriptionformat = FORMAT_HTML;
47 $errorstring = '';
49 $tagform = new tag_edit_form();
50 if ( $tag->tagtype == 'official' ) {
51 $tag->tagtype = '1';
52 } else {
53 $tag->tagtype = '0';
55 $tagform->set_data($tag);
57 // If new data has been sent, update the tag record
58 if ($tagnew = $tagform->get_data()) {
60 tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
62 if (has_capability('moodle/tag:manage', $systemcontext)) {
63 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
64 tag_type_set($tag->id, 'default');
66 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
67 tag_type_set($tag->id, 'official');
71 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
72 unset($tagnew->name);
73 unset($tagnew->rawname);
75 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
76 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
78 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
79 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
80 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
85 if (empty($errorstring)) { // All is OK, let's save it
87 $tagnew->timemodified = time();
89 if (has_capability('moodle/tag:manage', $systemcontext)) {
90 // rename tag
91 if(!tag_rename($tag->id, $tagnew->rawname)) {
92 error('Error updating tag record');
96 //log tag changes activity
97 //if tag name exist from form, renaming is allow. record log action as rename
98 //otherwise, record log action as update
99 if (isset($tagnew->name) && ($tag->name != $tagnew->name)){
100 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name);
102 } elseif ($tag->description != $tagnew->description) {
103 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name);
106 //updated related tags
107 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
108 //print_object($tagnew); die();
110 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
115 $navlinks = array();
116 $navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
117 $navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
119 $navigation = build_navigation($navlinks);
120 print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
122 print_heading($tagname, '', 2);
124 if (!empty($errorstring)) {
125 notify($errorstring);
128 $tagform->display();
130 if (ajaxenabled()) {
133 <script type="text/javascript">
135 // An XHR DataSource
136 var myServer = "./tag_autocomplete.php";
137 var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
138 myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
139 myDataSource.maxCacheEntries = 60;
140 myDataSource.queryMatchSubset = true;
142 var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
143 myAutoComp.delimChar = ",";
144 myAutoComp.maxResultsDisplayed = 20;
145 myAutoComp.minQueryLength = 2;
146 myAutoComp.allowBrowserAutocomplete = false;
147 myAutoComp.formatResult = function(aResultItem, sQuery) {
148 return aResultItem[1];
150 </script>
152 <?php
154 print_footer();