From af4e440888f26bb58b4aec2269d10c3028f74146 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 2 Dec 2011 16:16:41 +0000 Subject: [PATCH] MDL-30559 conditional availability: missing validation of grade rules. --- course/moodleform_mod.php | 31 +++++++++++++++++++++++++++++-- lang/en/condition.php | 4 ++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index a531061cd5b..75e08749990 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -313,6 +313,35 @@ abstract class moodleform_mod extends moodleform { $errors['availablefrom'] = get_string('badavailabledates', 'condition'); } + // Conditions: Verify that the grade conditions are numbers, and make sense. + if (array_key_exists('conditiongradegroup', $data)) { + foreach ($data['conditiongradegroup'] as $i => $gradedata) { + if ($gradedata['conditiongrademin'] !== '' && !is_numeric($gradedata['conditiongrademin'])) { + $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition'); + continue; + } + if ($gradedata['conditiongrademax'] !== '' && !is_numeric($gradedata['conditiongrademax'])) { + $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition'); + continue; + } + if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' && + $gradedata['conditiongrademax'] < $gradedata['conditiongrademin']) { + $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition'); + continue; + } + if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' && + $gradedata['conditiongradeitemid']) { + $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition'); + continue; + } + if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') && + !$gradedata['conditiongradeitemid']) { + $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition'); + continue; + } + } + } + return $errors; } @@ -471,8 +500,6 @@ abstract class moodleform_mod extends moodleform { $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' '); $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3)); $grouparray[] =& $mform->createElement('static', '', '','%'); - $mform->setType('conditiongrademin',PARAM_FLOAT); - $mform->setType('conditiongrademax',PARAM_FLOAT); $group = $mform->createElement('group','conditiongradegroup', get_string('gradecondition', 'condition'),$grouparray); diff --git a/lang/en/condition.php b/lang/en/condition.php index ed05e324f2b..9a1c764c7c0 100644 --- a/lang/en/condition.php +++ b/lang/en/condition.php @@ -32,6 +32,7 @@ $string['availablefrom_help'] = 'Access from/to dates determine when students ca The difference between access from/to dates and availability settings for the activity is that outside the set dates the latter allows students to view the activity description, whereas access from/to dates prevent access completely.'; $string['availableuntil'] = 'Allow access until'; $string['badavailabledates'] = 'Invalid dates. If you set both dates, the \'Allow access from\' date should be before the \'until\' date.'; +$string['badgradelimits'] = 'If you set both an upper and lower grade limit, the upper limit must be higher than the lower limit.'; $string['completion_complete'] = 'must be marked complete'; $string['completioncondition'] = 'Activity completion condition'; $string['completioncondition_help'] = 'This setting determines any activity completion conditions which must be met in order to access the activity. Note that completion tracking must first be set before an activity completion condition can be set. @@ -48,6 +49,9 @@ $string['gradecondition_help'] = 'This setting determines any grade conditions w Multiple grade conditions may be set if desired. If so, the activity will only allow access when ALL grade conditions are met.'; $string['grade_upto'] = 'and less than'; +$string['gradeitembutnolimits'] = 'You must enter an upper or lower limit, or both.'; +$string['gradelimitsbutnoitem'] = 'You must choose a grade item.'; +$string['gradesmustbenumeric'] = 'The minimum and maximum grades must be numeric (or blank).'; $string['none'] = '(none)'; $string['notavailableyet'] = 'Not available yet'; $string['requires_completion_0'] = 'Not available unless the activity {$a} is incomplete.';