Trivial code shuffle merged
[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)) {
c933a060 12 error(get_string('tagsaredisabled', 'tag'));
13}
14
3d535996 15$tag_id = optional_param('id', 0, PARAM_INT);
16$tag_name = optional_param('tag', '', PARAM_TAG);
17if ($tag_name) {
dee71a16 18 $tag = tag_get_id($tag_name, TAG_RETURN_OBJECT);
3d535996 19} elseif ( $tag_id ) {
20 $tag = tag_get_tag_by_id($tag_id);
21} else {
22 error('A required parameter was missing');
23}
9c9d3076 24$tagname = tag_display_name($tag);
c933a060 25
4f81c51d 26//Editing a tag requires moodle/tag:edit capability
c933a060 27$systemcontext = get_context_instance(CONTEXT_SYSTEM);
4f81c51d 28require_capability('moodle/tag:edit', $systemcontext);
c933a060 29
30// set the relatedtags field of the $tag object that will be passed to the form
0f039283 31// need to use html_entity_decode because formslib does it for us later on.
32$tag->relatedtags = html_entity_decode(tag_get_related_tags_csv(tag_get_related_tags($tag->id), TAG_RETURN_TEXT));
c933a060 33
d4be9d6d 34if (can_use_html_editor()) {
35 $options = new object();
36 $options->smiley = false;
37 $options->filter = false;
38
39 // convert and remove any XSS
40 $tag->description = format_text($tag->description, $tag->descriptionformat, $options);
41 $tag->descriptionformat = FORMAT_HTML;
42}
43
c933a060 44$tagform = new tag_edit_form();
45$tagform->set_data($tag);
46
47// if new data has been sent, update the tag record
48if ($tagnew = $tagform->get_data()) {
d4be9d6d 49
c933a060 50 $tagnew->timemodified = time();
d4be9d6d 51
c933a060 52 if (!update_record('tag', $tagnew)) {
53 error('Error updating tag record');
54 }
55
56 //updated related tags
3d535996 57 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
58 //var_dump($tagnew); die();
c933a060 59
3d535996 60 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
c933a060 61}
62
63
64$navlinks = array();
65$navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
66$navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
67
68$navigation = build_navigation($navlinks);
69print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
70
71print_heading($tagname, '', 2);
72
73$tagform->display();
74
9c9d3076 75if (ajaxenabled()) {
9ee998a5 76?>
c933a060 77
78<script type="text/javascript">
79
80// An XHR DataSource
81var myServer = "./tag_autocomplete.php";
82var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
83myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
84myDataSource.maxCacheEntries = 60;
85myDataSource.queryMatchSubset = true;
86
87var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
88myAutoComp.delimChar = ",";
89myAutoComp.maxResultsDisplayed = 20;
90myAutoComp.minQueryLength = 2;
91myAutoComp.allowBrowserAutocomplete = false;
92</script>
93
94<?php
9c9d3076 95}
c933a060 96print_footer();
97
98?>