diff --git a/grade/edit/scale/edit.php b/grade/edit/scale/edit.php index eb2b5b30f86..4e8023e84e0 100644 --- a/grade/edit/scale/edit.php +++ b/grade/edit/scale/edit.php @@ -33,7 +33,7 @@ if ($id) { } $scale_rec->standard = 1; $scale_rec->courseid = $courseid; - require_login(); + require_login($courseid); require_capability('moodle/course:managescales', $systemcontext); } diff --git a/grade/edit/scale/edit_form.php b/grade/edit/scale/edit_form.php index e749cdfc827..e97bc24a38b 100644 --- a/grade/edit/scale/edit_form.php +++ b/grade/edit/scale/edit_form.php @@ -84,21 +84,44 @@ class edit_scale_form extends moodleform { /// perform extra validation before submission function validation($data){ + global $CFG, $COURSE; - global $CFG; - - $errors= array(); + $errors = array(); // we can not allow 2 scales with the same exact scale as this creates // problems for backup/restore - $courseid = empty($data['courseid'])?0:$data['courseid']; - if (count_records('scale', 'courseid', $courseid, 'scale', $data['scale'])) { - $errors['scale'] = get_string('duplicatescale', 'grades'); + + $old = grade_scale::fetch(array('id'=>$data['id'])); + + if (array_key_exists('standard', $data)) { + if (empty($data['standard'])) { + $courseid = $COURSE->id; + } else { + $courseid = 0; + } + + } else { + $courseid = $old->courseid; } - $options = explode(',', $data['scale']); - if (count($options) < 2) { - $errors['scale'] = get_string('error'); + if (array_key_exists('scale', $data)) { + $count = count_records('scale', 'courseid', $courseid, 'scale', $data['scale']); + + if (empty($old->id) or $old->courseid != $courseid) { + if ($count) { + $errors['scale'] = get_string('duplicatescale', 'grades'); + } + + } else if ($old->scale != $data['scale']) { + if ($count) { + $errors['scale'] = get_string('duplicatescale', 'grades'); + } + } + + $options = explode(',', $data['scale']); + if (count($options) < 2) { + $errors['scale'] = get_string('error'); + } } if (0 == count($errors)){