]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10105 idnumber uniqueness tested before insert in UI now - both modedit and item...
authorskodak <skodak>
Thu, 2 Aug 2007 10:38:18 +0000 (10:38 +0000)
committerskodak <skodak>
Thu, 2 Aug 2007 10:38:18 +0000 (10:38 +0000)
course/modedit.php
course/moodleform_mod.php
grade/edit/tree/item_form.php
grade/edit/tree/outcomeitem_form.php
lang/en_utf8/moodle.php
lib/gradelib.php
mod/choice/mod_form.php
mod/lesson/mod_form.php
mod/quiz/mod_form.php
mod/scorm/mod_form.php

index 88b114ec671ce36a3fb7c8a687338567b344a303..6ffdd9222850d3559537987fb0d8955b012bd7f6 100644 (file)
             error("Data submitted is invalid.");
         }
 
+        //sync idnumber with grade_item
+        if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
+                     'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
+            if ($grade_item->idnumber != $fromform->idnumber) {
+                $grade_item->idnumber = $fromform->idnumber;
+                $grade_item->update();
+            }
+        }
+
         // add outcomes if requested
         if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
             foreach($outcomes as $outcome) {
index 7304549c60b931af4370249885921a409bc98f87..82937aec652c4615a29803e569ce7ff58fa91bb9 100644 (file)
@@ -66,6 +66,32 @@ class moodleform_mod extends moodleform {
         }
     }
 
+    // form verification
+    function validation($data) {
+        global $COURSE;
+
+        $errors = array();
+
+        $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
+                     'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
+        if ($data['coursemodule']) {
+            $cm = get_record('course_modules', 'id', $data['coursemodule']);
+        } else {
+            $cm = null;
+        }
+
+        // verify the idnumber
+        if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
+            $errors['cmidnumber'] = get_string('idnumbertaken');
+        }
+
+        if (count($errors) == 0) {
+            return true;
+        } else {
+            return $errors;
+        }
+    }
+
     /**
      * Load in existing data as form defaults. Usually new entry defaults are stored directly in
      * form definition (new entry form); this function is used to load in data where values
index 9f4e04709a211f28d50510354763832f4f665452..7c45f022b4dd14f0436b82ff809cc397ea3879b7 100644 (file)
@@ -171,7 +171,24 @@ class edit_item_form extends moodleform {
 
 /// perform extra validation before submission
     function validation($data){
-        $errors= array();
+        $errors = array();
+
+        if (array_key_exists('idnumber', $data)) {
+            if ($data['id']) {
+                $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
+                if ($grade_item->itemtype == 'mod') {
+                    $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
+                } else {
+                    $cm = null;
+                }
+            } else {
+                $grade_item = null;
+                $cm = null;
+            }
+            if (!grade_verify_idnumber($data['idnumber'], $grade_item, $cm)) {
+                $errors['idnumber'] = get_string('idnumbertaken');
+            }
+        }
 
         if (array_key_exists('calculation', $data) and $data['calculation'] != '') {
             $grade_item = new grade_item(array('id'=>$data['id'], 'itemtype'=>$data['itemtype'], 'courseid'=>$data['courseid']));
index 49e70ed7066ef0e3d91f6a2155f4f1f2569b3d9a..2f16173afaea8eca38a3f21e65e8403aa53762ca 100644 (file)
@@ -104,6 +104,17 @@ class edit_outcomeitem_form extends moodleform {
     function validation($data){
         $errors= array();
 
+        if (array_key_exists('idnumber', $data)) {
+            if ($data['id']) {
+                $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
+            } else {
+                $grade_item = null;
+            }
+            if (!grade_verify_idnumber($data['idnumber'], $grade_item, null)) {
+                $errors['idnumber'] = get_string('idnumbertaken');
+            }
+        }
+
         if (0 == count($errors)){
             return true;
         } else {
index f0fb257fd01858c077a4060740bc6ba36f766b6f..bc9026a3fb2f8abedd35552bb2245f9528ea9270 100644 (file)
@@ -752,6 +752,7 @@ $string['htmlformat'] = 'Pretty HTML format';
 $string['icqnumber'] = 'ICQ number';
 $string['idnumber'] = 'ID number';
 $string['idnumbercourse'] = 'Course ID number';
+$string['idnumbertaken'] = 'This ID number is already taken';
 $string['imagealt'] = 'Picture description';
 $string['import'] = 'Import';
 $string['importactivities'] = 'Import activities from another course';
index 1b9783adf2b4a7b968083b0d5ba0fd920ec69999..72f6d0d91dbd6229d37f0c7a27994b5bd5830135 100644 (file)
@@ -360,6 +360,42 @@ function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $item
 
 /***** END OF PUBLIC API *****/
 
+
+/**
+ * Verify nwe value of idnumber - checks for uniqueness of new idnubmers, old are kept intact
+ * @param string idnumber string (with magic quotes)
+ * @param object $cm used for course module idnumbers and items attached to modules
+ * @param object $gradeitem is item idnumber
+ * @return boolean true means idnumber ok
+ */
+function grade_verify_idnumber($idnumber, $grade_item=null, $cm=null) {
+    if ($idnumber == '') {
+        //we allow empty idnumbers
+        return true;
+    }
+
+    // keep existing even when not unique
+    if ($cm and $cm->idnumber == $idnumber) {
+        return true;
+    } else if ($grade_item and $grade_item->idnumber == $idnumber) {
+        return true;
+    }
+
+    if (get_records('course_modules', 'idnumber', $idnumber)) {
+        return false;
+    }
+
+    if (get_records('grade_items', 'idnumber', $idnumber)) {
+        return false;
+    }
+
+    return true;
+}
+
+/**
+ * Force final grade recalculation in all course items
+ * @param int $courseid
+ */
 function grade_force_full_regrading($courseid) {
     set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
 }
index 0760054af863ae6b33eac621cf1d8154a3a83d40..f7adbd9653e93f8ce4140b20ed2275899e6d3bcf 100644 (file)
@@ -115,20 +115,31 @@ class mod_choice_mod_form extends moodleform_mod {
     }
 
     function validation($data){
-        $choices=0;
+        $errors = parent::validation($data);
+        if ($errors === true) {
+            $errors = array();
+        }
+
+        $choices = 0;
         foreach ($data['option'] as $option){
-            if (trim($option)!=''){
+            if (trim($option) != ''){
                 $choices++;
             }
         }
-        if ($choices>1){
-           return true;
-        } elseif ($choices==0) {
-           return array('option[0]'=>get_string('fillinatleastoneoption', 'choice'));
-        } else  {
-           return array('option[1]'=>get_string('fillinatleastoneoption', 'choice'));
+
+        if ($choices < 1) {
+           $errors['option[0]'] = get_string('fillinatleastoneoption', 'choice');
         }
 
+        if ($choices < 2) {
+           $errors['option[1]'] = get_string('fillinatleastoneoption', 'choice');
+        }
+
+        if (count($errors) == 0) {
+            return true;
+        } else {
+            return $errors;
+        }
 
     }
 
index a0f677694d7c426865021ce91d0159be8f15bd04..9ddeab79ca2770848d7cfca16ef7057297981d49 100644 (file)
@@ -324,13 +324,20 @@ class mod_lesson_mod_form extends moodleform_mod {
      * @return array
      **/
     function validation($data) {
-        $errors = array();
+        $errors = parent::validation($data);
+        if ($errors === true) {
+            $errors = array();
+        }
 
         if (empty($data['maxtime']) and !empty($data['timed'])) {
             $errors['timedgrp'] = get_string('err_numeric', 'form');
         }
 
-        return $errors;
+        if (count($errors) == 0) {
+            return true;
+        } else {
+            return $errors;
+        }
     }
 }
 ?>
index c34deb476baccd6d315d58df8e0ebf0054e6d1fe..b197e37462bee8193e2a37783c0ccf3e1d907616 100644 (file)
@@ -304,7 +304,11 @@ class mod_quiz_mod_form extends moodleform_mod {
     }
 
     function validation($data){
-        $errors = array();
+        $errors = parent::validation($data);
+        if ($errors === true) {
+            $errors = array();
+        }
+
         // Check open and close times are consistent.
         if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
             $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
@@ -344,7 +348,12 @@ class mod_quiz_mod_form extends moodleform_mod {
                 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
             }
         }
-        return $errors;
+
+        if (count($errors) == 0) {
+            return true;
+        } else {
+            return $errors;
+        }
     }
 
 }
index 6f2dc98e637fdb249fc58e69d33b3428c3540d21..95bc3a4a856873c8760c130882b0c44f3988014d 100644 (file)
@@ -232,12 +232,21 @@ class mod_scorm_mod_form extends moodleform_mod {
     }
 
     function validation($data) {
+        $errors = parent::validation($data);
+        if ($errors === true) {
+            $errors = array();
+        }
+
         $validate = scorm_validate($data);
 
-        if ($validate->result) {
+        if (!$validate->result) {
+            $errors = $errors + $validate->errors;
+        }
+
+        if (count($errors) == 0) {
             return true;
         } else {
-            return $validate->errors;
+            return $errors;
         }
     }