]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13635 name editing merged from stable
authormoodler <moodler>
Mon, 25 Feb 2008 06:29:12 +0000 (06:29 +0000)
committermoodler <moodler>
Mon, 25 Feb 2008 06:29:12 +0000 (06:29 +0000)
tag/edit.php
tag/edit_form.php

index 768bd8c08eaaa702ee880b29bb51bcda6c9b2829..79497dfdbf6476eb59021c4d4d21253f3aed2be4 100644 (file)
@@ -41,23 +41,46 @@ if (can_use_html_editor()) {
     $tag->descriptionformat = FORMAT_HTML;
 }
 
+$errorstring = '';
+
 $tagform = new tag_edit_form();
 $tagform->set_data($tag);
 
-// if new data has been sent, update the tag record
+// If new data has been sent, update the tag record
 if ($tagnew = $tagform->get_data()) {
 
-    $tagnew->timemodified = time();
 
-    if (!update_record('tag', $tagnew)) {
-        error('Error updating tag record');
-    }
+    if (!has_capability('moodle/tag:manage', $systemcontext)) {
+        unset($tagnew->name);
+        unset($tagnew->rawname);
+
+    } else {  // They might be trying to change the rawname, make sure it's a change that doesn't affect name
+        $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
+
+        if (!$tagold = tag_get_tag_by_id($tag_id)) {  // For doing checks
+            error('Error updating tag record');
+        }
 
-    //updated related tags
-    tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
-    //var_dump($tagnew); die();
+        if ($tagold->name != $tagnew->name) {  // The name has changed, let's make sure it's not another existing tag
+            if (tag_get_id($tagnew->name)) {   // Something exists already, so flag an error
+                $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
+            }
+        }
+    }
 
-    redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
+    if (empty($errorstring)) {    // All is OK, let's save it
+        $tagnew->timemodified = time();
+
+        if (!update_record('tag', $tagnew)) {
+            error('Error updating tag record');
+        }
+    
+        //updated related tags
+        tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
+        //var_dump($tagnew); die();
+    
+        redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
+    }
 }
 
 
@@ -70,6 +93,10 @@ print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation)
 
 print_heading($tagname, '', 2);
 
+if (!empty($errorstring)) {
+    notify($errorstring);
+}
+
 $tagform->display();
 
 if (ajaxenabled()) {
index e96429439425fb7144e02cd81dd42a9b3c128feb..43c657fbce2e2cc6e4ede8b2a5676bfe7a736580 100644 (file)
@@ -4,14 +4,21 @@ require_once($CFG->dirroot.'/lib/formslib.php');
 
 class tag_edit_form extends moodleform {
 
-function definition () {
+    function definition () {
 
-    $mform =& $this->_form;
+        $mform =& $this->_form;
 
         $mform->addElement('header', 'tag', get_string('description','tag'));
 
         $mform->addElement('hidden', 'id');
 
+        $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
+
+        if (has_capability('moodle/tag:manage', $systemcontext)) {
+            $mform->addElement('text', 'rawname', get_string('name', 'tag'), 
+                    'maxlength="'.TAG_MAX_LENGTH.'" size="'.TAG_MAX_LENGTH.'"');
+        }
+
         $mform->addElement('htmleditor', 'description', get_string('description', 'tag'), array('rows'=>20));
 
         $mform->addElement('format', 'descriptionformat', get_string('format'));