From 235d8a4196ed7e65a52cfce035e04b957cd544fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 1 Sep 2015 22:57:04 +0200 Subject: [PATCH 1/2] MDL-48165 workshop: Add support for assessment forms validation --- mod/workshop/form/edit_form.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mod/workshop/form/edit_form.php b/mod/workshop/form/edit_form.php index 89c31fbc716..708cdc4f118 100644 --- a/mod/workshop/form/edit_form.php +++ b/mod/workshop/form/edit_form.php @@ -80,6 +80,23 @@ class workshop_edit_strategy_form extends moodleform { $mform->closeHeaderBefore('buttonar'); } + /** + * Validate the submitted form data. + * + * Grading strategy plugins can provide their own validation rules by + * overriding the {@link self::validation_inner()} method. + * + * @param array $data + * @param array $files + * @return array + */ + final public function validation($data, $files) { + return array_merge( + parent::validation($data, $files), + $this->validation_inner($data, $files) + ); + } + /** * Add any strategy specific form fields. * @@ -89,4 +106,14 @@ class workshop_edit_strategy_form extends moodleform { // By default, do nothing. } + /** + * Add strategy specific validation rules. + * + * @param array $data + * @param array $files + * @return array + */ + protected function validation_inner($data, $files) { + return array(); + } } From fa36f1d1ab78605442d2e2730f78aacb80f52c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 1 Sep 2015 22:57:59 +0200 Subject: [PATCH 2/2] MDL-48165 workshop: Validate the rubric editing form --- mod/workshop/form/rubric/edit_form.php | 44 +++++++++++++++++++ .../rubric/lang/en/workshopform_rubric.php | 1 + 2 files changed, 45 insertions(+) diff --git a/mod/workshop/form/rubric/edit_form.php b/mod/workshop/form/rubric/edit_form.php index 79acc423383..ccd4c29c25c 100644 --- a/mod/workshop/form/rubric/edit_form.php +++ b/mod/workshop/form/rubric/edit_form.php @@ -103,4 +103,48 @@ class workshop_edit_rubric_strategy_form extends workshop_edit_strategy_form { $mform->setDefault('config_layout', 'list'); $this->set_data($current); } + + /** + * Provide validation rules for the rubric editor form. + * + * @param array $data + * @param array $files + * @return array + */ + protected function validation_inner($data, $files) { + + $errors = array(); + + // Iterate over all submitted dimensions (criteria). + for ($i = 0; isset($data['dimensionid__idx_'.$i]); $i++) { + + $dimgrades = array(); + + if (0 == strlen(trim($data['description__idx_'.$i.'_editor']['text']))) { + // The description text is empty and this criterion will be deleted. + continue; + } + + // Make sure the levels grades are unique within the criterion. + for ($j = 0; isset($data['levelid__idx_'.$i.'__idy_'.$j]); $j++) { + if (0 == strlen(trim($data['definition__idx_'.$i.'__idy_'.$j]))) { + // The level definition is empty and will not be saved. + continue; + } + + $levelgrade = $data['grade__idx_'.$i.'__idy_'.$j]; + + if (isset($dimgrades[$levelgrade])) { + // This grade has already been set for another level. + $k = $dimgrades[$levelgrade]; + $errors['level__idx_'.$i.'__idy_'.$j] = $errors['level__idx_'.$i.'__idy_'.$k] = get_string('mustbeunique', + 'workshopform_rubric'); + } else { + $dimgrades[$levelgrade] = $j; + } + } + } + + return $errors; + } } diff --git a/mod/workshop/form/rubric/lang/en/workshopform_rubric.php b/mod/workshop/form/rubric/lang/en/workshopform_rubric.php index 9c2b5e30178..3785c4f94ad 100644 --- a/mod/workshop/form/rubric/lang/en/workshopform_rubric.php +++ b/mod/workshop/form/rubric/lang/en/workshopform_rubric.php @@ -33,5 +33,6 @@ $string['layoutgrid'] = 'Grid'; $string['layoutlist'] = 'List'; $string['levelgroup'] = 'Level grade and definition'; $string['levels'] = 'Levels'; +$string['mustbeunique'] = 'Level grades must be unique within a criterion'; $string['mustchooseone'] = 'You have to select one of these items'; $string['pluginname'] = 'Rubric';