Skip to content

Commit

Permalink
Merge branch 'MDL-38538_clean' of git://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Apr 3, 2013
2 parents c985d12 + d122fe3 commit 89cc981
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
20 changes: 20 additions & 0 deletions question/engine/questionusage.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,26 @@ public function validate_sequence_number($slot, $postdata = null) {
return true;
}
}

/**
* Check, based on the sequence number, whether this auto-save is still required.
* @param int $slot the number used to identify this question within this usage.
* @param array $submitteddata the submitted data that constitutes the action.
* @return bool true if the check variable is present and correct, otherwise false.
*/
public function is_autosave_required($slot, $postdata = null) {
$qa = $this->get_question_attempt($slot);
$sequencecheck = $qa->get_submitted_var(
$qa->get_control_field_name('sequencecheck'), PARAM_INT, $postdata);
if (is_null($sequencecheck)) {
return false;
} else if ($sequencecheck != $qa->get_num_steps()) {
return false;
} else {
return true;
}
}

/**
* Update the flagged state for all question_attempts in this usage, if their
* flagged state was changed in the request.
Expand Down
27 changes: 18 additions & 9 deletions question/engine/tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,13 +684,12 @@ protected function start_attempt_at_question($question, $preferredbehaviour,
$this->quba->start_question($this->slot, $variant);
}

protected function process_submission($data) {
// Backwards compatibility.
reset($data);
if (count($data) == 1 && key($data) === '-finish') {
$this->finish();
}

/**
* Convert an array of data destined for one question to the equivalent POST data.
* @param array $data the data for the quetsion.
* @return array the complete post data.
*/
protected function response_data_to_post($data) {
$prefix = $this->quba->get_field_prefix($this->slot);
$fulldata = array(
'slots' => $this->slot,
Expand All @@ -699,11 +698,21 @@ protected function process_submission($data) {
foreach ($data as $name => $value) {
$fulldata[$prefix . $name] = $value;
}
$this->quba->process_all_actions(time(), $fulldata);
return $fulldata;
}

protected function process_submission($data) {
// Backwards compatibility.
reset($data);
if (count($data) == 1 && key($data) === '-finish') {
$this->finish();
}

$this->quba->process_all_actions(time(), $this->response_data_to_post($data));
}

protected function process_autosave($data) {
$this->quba->process_autosave($this->slot, $data);
$this->quba->process_all_autosaves(null, $this->response_data_to_post($data));
}

protected function finish() {
Expand Down
45 changes: 45 additions & 0 deletions question/engine/tests/questionusage_autosave_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,49 @@ public function test_concurrent_autosaves() {

$DB2->dispose();
}

public function test_autosave_with_wrong_seq_number_ignored() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $generator->create_question_category();
$question = $generator->create_question('shortanswer', null,
array('category' => $cat->id));

// Start attempt at a shortanswer question.
$q = question_bank::load_question($question->id);
$this->start_attempt_at_question($q, 'deferredfeedback', 1);

$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_step_count(1);

// Process a response and check the expected result.
$this->process_submission(array('answer' => 'first response'));

$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_step_count(2);
$this->save_quba();

// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');

// Process an autosave with a sequence number 1 to small (so from the past).
$this->load_quba();
$postdata = $this->response_data_to_post(array('answer' => 'obsolete response'));
$postdata[$this->quba->get_field_prefix($this->slot) . ':sequencecheck'] = $this->get_question_attempt()->get_num_steps() - 1;
$this->quba->process_all_autosaves(null, $postdata);
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_step_count(2);
$this->save_quba();

// Now check how that is re-displayed.
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'first response');

$this->delete_quba();
}
}

0 comments on commit 89cc981

Please sign in to comment.