diff --git a/grade/grading/form/guide/lib.php b/grade/grading/form/guide/lib.php index 8199efa6bef..3565ef60734 100644 --- a/grade/grading/form/guide/lib.php +++ b/grade/grading/form/guide/lib.php @@ -756,6 +756,26 @@ class gradingform_guide_instance extends gradingform_instance { return $instanceid; } + /** + * Determines whether the submitted form was empty. + * + * @param array $elementvalue value of element submitted from the form + * @return boolean true if the form is empty + */ + public function is_empty_form($elementvalue) { + $criteria = $this->get_controller()->get_definition()->guide_criteria; + foreach ($criteria as $id => $criterion) { + $score = $elementvalue['criteria'][$id]['score']; + $remark = $elementvalue['criteria'][$id]['remark']; + + if ((isset($score) && $score !== '') + || ((isset($remark) && $remark !== ''))) { + return false; + } + } + return true; + } + /** * Validates that guide is fully completed and contains valid grade on each criterion * @@ -843,6 +863,19 @@ class gradingform_guide_instance extends gradingform_instance { $this->get_guide_filling(true); } + /** + * Removes the attempt from the gradingform_guide_fillings table + * @param array $data the attempt data + */ + public function clear_attempt($data) { + global $DB; + + foreach ($data['criteria'] as $criterionid => $record) { + $DB->delete_records('gradingform_guide_fillings', + array('criterionid' => $criterionid, 'instanceid' => $this->get_id())); + } + } + /** * Calculates the grade to be pushed to the gradebook * diff --git a/grade/grading/form/lib.php b/grade/grading/form/lib.php index 6e926d58a25..b79f5c77362 100644 --- a/grade/grading/form/lib.php +++ b/grade/grading/form/lib.php @@ -935,6 +935,28 @@ abstract class gradingform_instance { */ abstract public function get_grade(); + /** + * Determines whether the submitted form was empty. + * + * @param array $elementvalue value of element submitted from the form + * @return boolean true if the form is empty + */ + public function is_empty_form($elementvalue) { + return false; + } + + /** + * Removes the attempt from the gradingform_*_fillings table. + * This function is not abstract as to not break plugins that might + * use advanced grading. + * @param array $data the attempt data + */ + public function clear_attempt($data) { + // This function is empty because the way to clear a grade + // attempt will be different depending on the grading method. + return; + } + /** * Called when teacher submits the grading form: * updates the instance in DB, marks it as ACTIVE and returns the grade to be pushed to the gradebook. @@ -947,12 +969,16 @@ abstract class gradingform_instance { */ public function submit_and_get_grade($elementvalue, $itemid) { $elementvalue['itemid'] = $itemid; + if ($this->is_empty_form($elementvalue)) { + $this->clear_attempt($elementvalue); + $this->make_active(); + return -1; + } $this->update($elementvalue); $this->make_active(); return $this->get_grade(); } - /** * Returns html for form element of type 'grading'. If there is a form input element * it must have the name $gradingformelement->getName(). diff --git a/grade/grading/form/rubric/lib.php b/grade/grading/form/rubric/lib.php index 36d366d9ed3..d4d4b6f8c15 100644 --- a/grade/grading/form/rubric/lib.php +++ b/grade/grading/form/rubric/lib.php @@ -756,6 +756,37 @@ class gradingform_rubric_instance extends gradingform_instance { return $instanceid; } + /** + * Determines whether the submitted form was empty. + * + * @param array $elementvalue value of element submitted from the form + * @return boolean true if the form is empty + */ + public function is_empty_form($elementvalue) { + $criteria = $this->get_controller()->get_definition()->rubric_criteria; + + foreach ($criteria as $id => $criterion) { + if (isset($elementvalue['criteria'][$id]['levelid']) + || !empty($elementvalue['criteria'][$id]['remark'])) { + return false; + } + } + return true; + } + + /** + * Removes the attempt from the gradingform_guide_fillings table + * @param array $data the attempt data + */ + public function clear_attempt($data) { + global $DB; + + foreach ($data['criteria'] as $criterionid => $record) { + $DB->delete_records('gradingform_rubric_fillings', + array('criterionid' => $criterionid, 'instanceid' => $this->get_id())); + } + } + /** * Validates that rubric is fully completed and contains valid grade on each criterion * diff --git a/lib/form/grading.php b/lib/form/grading.php index 00674218849..18191dd6d40 100644 --- a/lib/form/grading.php +++ b/lib/form/grading.php @@ -138,11 +138,14 @@ class MoodleQuickForm_grading extends HTML_QuickForm_input{ * Function registered as rule for this element and is called when this element is being validated. * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element * - * @param mixed $elementValue value of element to be validated + * @param mixed $elementvalue value of element to be validated * @param array $attributes element attributes * @return MoodleQuickForm_grading */ - static function _validate($elementValue, $attributes = null) { - return $attributes['gradinginstance']->validate_grading_element($elementValue); + public static function _validate($elementvalue, $attributes = null) { + if (!$attributes['gradinginstance']->is_empty_form($elementvalue)) { + return $attributes['gradinginstance']->validate_grading_element($elementvalue); + } + return true; } }