diff --git a/grade/grading/form/lib.php b/grade/grading/form/lib.php index 8e1ebe904cb99..d66ae077c4e0e 100644 --- a/grade/grading/form/lib.php +++ b/grade/grading/form/lib.php @@ -368,10 +368,10 @@ protected function get_instance($instance) { * @param int $itemid * @return gradingform_instance */ - public function create_instance($raterid, $itemid) { + public function create_instance($raterid, $itemid = null) { global $DB; // first find if there is already an active instance for this itemid - if ($current = $this->get_current_instance($raterid, $itemid)) { + if ($itemid && $current = $this->get_current_instance($raterid, $itemid)) { return $this->get_instance($current->copy($raterid, $itemid)); } else { $class = 'gradingform_'. $this->get_method_name(). '_instance'; @@ -588,6 +588,9 @@ protected function make_active() { // already active return; } + if (empty($this->data->itemid)) { + throw new coding_exception('You cannot mark active the grading instance without itemid'); + } $currentid = $this->get_controller()->get_current_instance($this->data->raterid, $this->data->itemid, true); if ($currentid) { if ($currentid != $this->get_id()) { @@ -618,9 +621,22 @@ public function cancel() { * Updates the instance with the data received from grading form. This function may be * called via AJAX when grading is not yet completed, so it does not change the * status of the instance. + * + * @param array $elementvalue */ public function update($elementvalue) { - // TODO update timemodified at least + global $DB; + $newdata = new stdClass(); + $newdata->id = $this->get_id(); + $newdata->timemodified = time(); + if (isset($elementvalue['itemid']) && $elementvalue['itemid'] != $this->data->itemid) { + $newdata->itemid = $elementvalue['itemid']; + } + // TODO also update: rawgrade, feedback, feedbackformat + $DB->update_record('grading_instances', $newdata); + foreach ($newdata as $key => $value) { + $this->data->$key = $value; + } } /** @@ -632,10 +648,16 @@ abstract public function get_grade(); /** * 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 + * updates the instance in DB, marks it as ACTIVE and returns the grade to be pushed to the gradebook. + * $itemid must be specified here (it was not required when the instance was + * created, because it might not existed in draft) + * + * @param array $elementvalue + * @param int $itemid * @return int the grade on 0-100 scale */ - public function submit_and_get_grade($elementvalue) { + public function submit_and_get_grade($elementvalue, $itemid) { + $elementvalue['itemid'] = $itemid; $this->update($elementvalue); $this->make_active(); return $this->get_grade(); diff --git a/grade/grading/form/rubric/lib.php b/grade/grading/form/rubric/lib.php index f788b8bfdac1c..0be5fa62b1405 100644 --- a/grade/grading/form/rubric/lib.php +++ b/grade/grading/form/rubric/lib.php @@ -457,11 +457,13 @@ public function get_rubric_filling($force = false) { * Updates the instance with the data received from grading form. This function may be * called via AJAX when grading is not yet completed, so it does not change the * status of the instance. + * + * @param array $data */ public function update($data) { global $DB; $currentgrade = $this->get_rubric_filling(); - parent::update($data); // TODO ? +timemodified + parent::update($data); foreach ($data['criteria'] as $criterionid => $record) { if (!array_key_exists($criterionid, $currentgrade['criteria'])) { $newrecord = array('forminstanceid' => $this->get_id(), 'criterionid' => $criterionid, diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 0a30eb4464f87..ec602d8f502ce 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -1067,11 +1067,11 @@ function display_submission($offset=-1,$userid =-1, $display=true) { $mformdata->fileui_options = array('subdirs'=>0, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); } if ($controller = get_grading_manager($this->context, 'mod_assignment', 'submission')->get_active_controller()) { - if (!isset($submission->id)) { - // we create a submission if it does not exist yet because we need submission->id for grading - $mformdata->submission = $this->get_submission($user->id, true); + $itemid = null; + if (!empty($submission->id)) { + $itemid = $submission->id; } - $mformdata->advancedgradinginstance = $controller->create_instance($USER->id, $mformdata->submission->id); + $mformdata->advancedgradinginstance = $controller->create_instance($USER->id, $itemid); } $submitform = new mod_assignment_grading_form( null, $mformdata ); @@ -1613,7 +1613,9 @@ function validate_and_preprocess_feedback() { // preprocess advanced grading here if ($gradinginstance) { $data = $mform->get_data(); - $_POST['xgrade'] = $gradinginstance->submit_and_get_grade($data->advancedgrading); + // create submission if it did not exist yet because we need submission->id for storing the grading instance + $submission = $this->get_submission($userid, true); + $_POST['xgrade'] = $gradinginstance->submit_and_get_grade($data->advancedgrading, $submission->id); } } return true;