gradebook MDL-25358 improved validation of scales
authorAndrew Davis <andrew@affinitysoftware.net>
Mon, 22 Nov 2010 05:52:13 +0000 (05:52 +0000)
committerAndrew Davis <andrew@affinitysoftware.net>
Mon, 22 Nov 2010 05:52:13 +0000 (05:52 +0000)
grade/edit/scale/edit_form.php

index 186fb0a..db75e8e 100644 (file)
@@ -124,22 +124,30 @@ class edit_scale_form extends moodleform {
         }
 
         if (array_key_exists('scale', $data)) {
         }
 
         if (array_key_exists('scale', $data)) {
-            $count = $DB->count_records('scale', array('courseid'=>$courseid, 'scale'=>$data['scale']));
+            $scalearray = explode(',', $data['scale']);
+            $scalearray = array_map('trim', $scalearray);
+            $scaleoptioncount = count($scalearray);
 
 
-            if (empty($old->id) or $old->courseid != $courseid) {
-                if ($count) {
-                    $errors['scale'] = get_string('duplicatescale', 'grades');
-                }
+            if (count($scalearray) < 2) {
+                $errors['scale'] = get_string('badlyformattedscale', 'grades');
+            } else {
+                   $thescale = implode(',',$scalearray);
 
 
-            } else if ($old->scale != $data['scale']) {
-                if ($count) {
-                    $errors['scale'] = get_string('duplicatescale', 'grades');
-                }
-            }
+                   $textlib = textlib_get_instance();
+                //this check strips out whitespace from the scale we're validating but not from those already in the DB
+                   $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', $textlib->strlen($thescale)).'=:scale',
+                       array('courseid'=>$courseid, 'scale'=>$thescale));
 
 
-            $options = explode(',', $data['scale']);
-            if (count($options) < 2) {
-                $errors['scale'] = get_string('badlyformattedscale', 'grades');
+                if ($count) {
+                    //if this is a new scale but we found a duplice in the DB
+                    //or we found a duplicate in another course report the error
+                    if (empty($old->id) or $old->courseid != $courseid) {
+                           $errors['scale'] = get_string('duplicatescale', 'grades');
+                    } else if ($old->scale !== $thescale and $old->scale !== $data['scale']) {
+                        //if the old scale from DB is different but we found a duplicate then we're trying to modify a scale to be a duplicate
+                           $errors['scale'] = get_string('duplicatescale', 'grades');
+                       }
+                   }
             }
         }
 
             }
         }