Updated the HEAD build version to 20090909
[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
0edd6535 7$tag_id = optional_param('id', 0, PARAM_INT);
8$tag_name = optional_param('tag', '', PARAM_TAG);
9c9d3076 9
c933a060 10require_login();
11
9c9d3076 12if (empty($CFG->usetags)) {
5a2a5331 13 print_error('tagsaredisabled', 'tag');
c933a060 14}
15
0edd6535 16//Editing a tag requires moodle/tag:edit capability
17$systemcontext = get_context_instance(CONTEXT_SYSTEM);
18require_capability('moodle/tag:edit', $systemcontext);
8479c2e0 19
3d535996 20if ($tag_name) {
8479c2e0 21 $tag = tag_get('name', $tag_name, '*');
22} else if ($tag_id) {
23 $tag = tag_get('id', $tag_id, '*');
24}
25
26if (empty($tag)) {
27 redirect($CFG->wwwroot.'/tag/search.php');
3d535996 28}
8479c2e0 29
0edd6535 30$PAGE->set_url('tag/index.php', array('id' => $tag->id));
31$PAGE->set_subpage($tag->id);
32$PAGE->set_context($systemcontext);
33$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
34$PAGE->set_generaltype('form');
c933a060 35
0edd6535 36$PAGE->requires->yui_lib('animation');
37$PAGE->requires->yui_lib('autocomplete');
38
39$tagname = tag_display_name($tag);
c933a060 40
41// set the relatedtags field of the $tag object that will be passed to the form
e7134583 42$tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
c933a060 43
d4be9d6d 44if (can_use_html_editor()) {
45 $options = new object();
46 $options->smiley = false;
47 $options->filter = false;
48
49 // convert and remove any XSS
50 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
51 $tag->descriptionformat = FORMAT_HTML;
52}
53
7e6e76a6 54$errorstring = '';
55
c933a060 56$tagform = new tag_edit_form();
603443b5 57if ( $tag->tagtype == 'official' ) {
58 $tag->tagtype = '1';
59} else {
60 $tag->tagtype = '0';
61}
c933a060 62$tagform->set_data($tag);
63
7e6e76a6 64// If new data has been sent, update the tag record
c933a060 65if ($tagnew = $tagform->get_data()) {
d4be9d6d 66
294ce987 67 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
b50d2245 68
603443b5 69 if (has_capability('moodle/tag:manage', $systemcontext)) {
28f64040 70 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
603443b5 71 tag_type_set($tag->id, 'default');
72
73 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
74 tag_type_set($tag->id, 'official');
75 }
76 }
77
3efae234 78 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
7e6e76a6 79 unset($tagnew->name);
80 unset($tagnew->rawname);
81
82 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
83 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
84
2f4b82f4 85 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
7e6e76a6 86 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
87 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
88 }
89 }
90 }
c933a060 91
7e6e76a6 92 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 93
7e6e76a6 94 $tagnew->timemodified = time();
95
3efae234 96 if (has_capability('moodle/tag:manage', $systemcontext)) {
97 // rename tag
98 if(!tag_rename($tag->id, $tagnew->rawname)) {
38fb8190 99 print_error('errorupdatingrecord', 'tag');
3efae234 100 }
7e6e76a6 101 }
102
103 //updated related tags
104 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 105 //print_object($tagnew); die();
7e6e76a6 106
107 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
108 }
c933a060 109}
110
968583af 111$PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url($CFG->wwwroot.'/tag/search.php'));
112$PAGE->navbar->add($tagname);
113$PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
114echo $OUTPUT->header();
73aec3a7 115echo $OUTPUT->heading($tagname, 2);
c933a060 116
7e6e76a6 117if (!empty($errorstring)) {
8f6e7e6c 118 echo $OUTPUT->notification($errorstring);
7e6e76a6 119}
120
c933a060 121$tagform->display();
122
9c9d3076 123if (ajaxenabled()) {
9ee998a5 124?>
c933a060 125
126<script type="text/javascript">
127
128// An XHR DataSource
129var myServer = "./tag_autocomplete.php";
130var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
131myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
132myDataSource.maxCacheEntries = 60;
133myDataSource.queryMatchSubset = true;
134
135var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
136myAutoComp.delimChar = ",";
137myAutoComp.maxResultsDisplayed = 20;
138myAutoComp.minQueryLength = 2;
139myAutoComp.allowBrowserAutocomplete = false;
16803c57 140myAutoComp.formatResult = function(aResultItem, sQuery) {
141 return aResultItem[1];
142}
c933a060 143</script>
144
145<?php
9c9d3076 146}
1dd2ba54 147echo $OUTPUT->footer();
c933a060 148
149?>