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'));
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
);
19 $tag = tag_get('name', $tag_name, '*');
21 $tag = tag_get('id', $tag_id, '*');
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
;
49 $tagform = new tag_edit_form();
50 if ( $tag->tagtype
== 'official' ) {
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)) {
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)) {
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
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);
133 <script type
="text/javascript">
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];