|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00025 require_once('../config.php'); 00026 require_once('lib.php'); 00027 require_once('edit_form.php'); 00028 00029 $tag_id = optional_param('id', 0, PARAM_INT); 00030 $tag_name = optional_param('tag', '', PARAM_TAG); 00031 00032 require_login(); 00033 00034 if (empty($CFG->usetags)) { 00035 print_error('tagsaredisabled', 'tag'); 00036 } 00037 00038 //Editing a tag requires moodle/tag:edit capability 00039 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00040 require_capability('moodle/tag:edit', $systemcontext); 00041 00042 if ($tag_name) { 00043 $tag = tag_get('name', $tag_name, '*'); 00044 } else if ($tag_id) { 00045 $tag = tag_get('id', $tag_id, '*'); 00046 } 00047 00048 if (empty($tag)) { 00049 redirect($CFG->wwwroot.'/tag/search.php'); 00050 } 00051 00052 $PAGE->set_url('/tag/index.php', array('id' => $tag->id)); 00053 $PAGE->set_subpage($tag->id); 00054 $PAGE->set_context($systemcontext); 00055 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks'); 00056 $PAGE->set_pagelayout('base'); 00057 00058 $PAGE->requires->yui2_lib('connection'); 00059 $PAGE->requires->yui2_lib('animation'); 00060 $PAGE->requires->yui2_lib('datasource'); 00061 $PAGE->requires->yui2_lib('autocomplete'); 00062 00063 $tagname = tag_display_name($tag); 00064 00065 // set the relatedtags field of the $tag object that will be passed to the form 00066 $tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT); 00067 00068 if (can_use_html_editor()) { 00069 $options = new stdClass(); 00070 $options->smiley = false; 00071 $options->filter = false; 00072 00073 // convert and remove any XSS 00074 $tag->description = format_text($tag->description, $tag->descriptionformat, $options); 00075 $tag->descriptionformat = FORMAT_HTML; 00076 } 00077 00078 $errorstring = ''; 00079 00080 $editoroptions = array( 00081 'maxfiles' => EDITOR_UNLIMITED_FILES, 00082 'maxbytes' => $CFG->maxbytes, 00083 'trusttext' => false, 00084 'context' => $systemcontext 00085 ); 00086 $tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); 00087 00088 $tagform = new tag_edit_form(null, compact('editoroptions')); 00089 if ( $tag->tagtype == 'official' ) { 00090 $tag->tagtype = '1'; 00091 } else { 00092 $tag->tagtype = '0'; 00093 } 00094 00095 $tagform->set_data($tag); 00096 00097 // If new data has been sent, update the tag record 00098 if ($tagnew = $tagform->get_data()) { 00099 00100 if (has_capability('moodle/tag:manage', $systemcontext)) { 00101 if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) { 00102 tag_type_set($tag->id, 'default'); 00103 00104 } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) { 00105 tag_type_set($tag->id, 'official'); 00106 } 00107 } 00108 00109 if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) { 00110 unset($tagnew->name); 00111 unset($tagnew->rawname); 00112 00113 } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name 00114 $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER)); 00115 00116 if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag 00117 if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error 00118 $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag'); 00119 } 00120 } 00121 } 00122 00123 if (empty($errorstring)) { // All is OK, let's save it 00124 00125 $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); 00126 00127 tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); 00128 00129 $tagnew->timemodified = time(); 00130 00131 if (has_capability('moodle/tag:manage', $systemcontext)) { 00132 // rename tag 00133 if(!tag_rename($tag->id, $tagnew->rawname)) { 00134 print_error('errorupdatingrecord', 'tag'); 00135 } 00136 } 00137 00138 //log tag changes activity 00139 //if tag name exist from form, renaming is allow. record log action as rename 00140 //otherwise, record log action as update 00141 if (isset($tagnew->name) && ($tag->name != $tagnew->name)){ 00142 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name); 00143 00144 } elseif ($tag->description != $tagnew->description) { 00145 add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name); 00146 } 00147 00148 //updated related tags 00149 tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags))); 00150 //print_object($tagnew); die(); 00151 00152 redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form 00153 } 00154 } 00155 00156 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php')); 00157 $PAGE->navbar->add($tagname); 00158 $PAGE->navbar->add(get_string('edit')); 00159 $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname); 00160 $PAGE->set_heading($COURSE->fullname); 00161 echo $OUTPUT->header(); 00162 echo $OUTPUT->heading($tagname, 2); 00163 00164 if (!empty($errorstring)) { 00165 echo $OUTPUT->notification($errorstring); 00166 } 00167 00168 $tagform->display(); 00169 00170 if (ajaxenabled()) { 00171 $PAGE->requires->js('/tag/tag.js'); 00172 $PAGE->requires->js_function_call('init_tag_autocomplete', null, true); 00173 } 00174 echo $OUTPUT->footer();