Skip to content

Commit

Permalink
MDL-29483: allow the grading instance to exist without itemid until s…
Browse files Browse the repository at this point in the history
…ubmitting (submission in assignment might not yet existed)
  • Loading branch information
marinaglancy committed Oct 24, 2011
1 parent ed70941 commit fc05f22
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
32 changes: 27 additions & 5 deletions grade/grading/form/lib.php
Expand Up @@ -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';
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
}

/**
Expand All @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion grade/grading/form/rubric/lib.php
Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions mod/assignment/lib.php
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fc05f22

Please sign in to comment.