MDL-18011 - removing reference to loancalc block from core
[moodle.git] / tag / edit.php
CommitLineData
6bfe7aac 1<?php
c933a060 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
a6855934 30$PAGE->set_url('/tag/index.php', array('id' => $tag->id));
0edd6535 31$PAGE->set_subpage($tag->id);
32$PAGE->set_context($systemcontext);
33$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
191b267b 34$PAGE->set_pagelayout('base');
c933a060 35
f44b10ed
PS
36$PAGE->requires->yui2_lib('animation');
37$PAGE->requires->yui2_lib('autocomplete');
0edd6535 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
8bdc9cac
SH
56$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false);
57$tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag_description', $tag->id);
58
59$tagform = new tag_edit_form(null, compact('editoroptions'));
603443b5 60if ( $tag->tagtype == 'official' ) {
61 $tag->tagtype = '1';
62} else {
63 $tag->tagtype = '0';
64}
8bdc9cac 65
c933a060 66$tagform->set_data($tag);
67
7e6e76a6 68// If new data has been sent, update the tag record
c933a060 69if ($tagnew = $tagform->get_data()) {
d4be9d6d 70
603443b5 71 if (has_capability('moodle/tag:manage', $systemcontext)) {
28f64040 72 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
603443b5 73 tag_type_set($tag->id, 'default');
74
75 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
76 tag_type_set($tag->id, 'official');
77 }
78 }
79
3efae234 80 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
7e6e76a6 81 unset($tagnew->name);
82 unset($tagnew->rawname);
83
84 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
85 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
86
2f4b82f4 87 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
7e6e76a6 88 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
89 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
90 }
91 }
92 }
c933a060 93
7e6e76a6 94 if (empty($errorstring)) { // All is OK, let's save it
2f4b82f4 95
8bdc9cac 96 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag_description', $tag->id);
03221650 97
8bdc9cac
SH
98 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat);
99
7e6e76a6 100 $tagnew->timemodified = time();
101
3efae234 102 if (has_capability('moodle/tag:manage', $systemcontext)) {
103 // rename tag
104 if(!tag_rename($tag->id, $tagnew->rawname)) {
38fb8190 105 print_error('errorupdatingrecord', 'tag');
3efae234 106 }
7e6e76a6 107 }
6bfe7aac 108
b89e4ad8 109 //log tag changes activity
110 //if tag name exist from form, renaming is allow. record log action as rename
6bfe7aac 111 //otherwise, record log action as update
b89e4ad8 112 if (isset($tagnew->name) && ($tag->name != $tagnew->name)){
113 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name);
114
6bfe7aac 115 } elseif ($tag->description != $tagnew->description) {
b89e4ad8 116 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name);
117 }
6bfe7aac 118
7e6e76a6 119 //updated related tags
120 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
0d626493 121 //print_object($tagnew); die();
6bfe7aac 122
7e6e76a6 123 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
124 }
c933a060 125}
126
a6855934 127$PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
968583af 128$PAGE->navbar->add($tagname);
440e1f1a 129$PAGE->navbar->add(get_string('edit'));
968583af 130$PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
440e1f1a 131$PAGE->set_heading($COURSE->fullname);
968583af 132echo $OUTPUT->header();
73aec3a7 133echo $OUTPUT->heading($tagname, 2);
c933a060 134
7e6e76a6 135if (!empty($errorstring)) {
8f6e7e6c 136 echo $OUTPUT->notification($errorstring);
7e6e76a6 137}
138
c933a060 139$tagform->display();
140
9c9d3076 141if (ajaxenabled()) {
9dec75db 142 $PAGE->requires->js('/tag/tag.js');
593f9b87 143 $PAGE->requires->js_function_call('init_tag_autocomplete', null, true);
9c9d3076 144}
1dd2ba54 145echo $OUTPUT->footer();