MDL-40543 convert from simulated responses

to post data
This commit is contained in:
Jamie Pratt 2013-07-14 18:23:48 +07:00 committed by Dan Poltawski
parent 6064e5cc00
commit 388f047345
9 changed files with 381 additions and 16 deletions

View File

@ -1330,15 +1330,26 @@ class quiz_attempt {
/**
* Process all the actions that were submitted as part of the current request.
*
* @param int $timestamp the timestamp that should be stored as the modifed
* time in the database for these actions. If null, will use the current time.
* @param int $timestamp the timestamp that should be stored as the modifed
* time in the database for these actions. If null, will use the current time.
* @param bool $becomingoverdue
* @param array|null $simulatedresponses If not null, then we are testing, and this is an array of simulated data, keys are slot
* nos and values are arrays representing student responses which will be passed to
* question_definition::prepare_simulated_post_data method and then have the
* appropriate prefix added.
*/
public function process_submitted_actions($timestamp, $becomingoverdue = false, $postdata = null) {
public function process_submitted_actions($timestamp, $becomingoverdue = false, $simulatedresponses = null) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$this->quba->process_all_actions($timestamp, $postdata);
if ($simulatedresponses !== null) {
$simulatedpostdata = $this->quba->prepare_simulated_post_data($simulatedresponses);
} else {
$simulatedpostdata = null;
}
$this->quba->process_all_actions($timestamp, $simulatedpostdata);
question_engine::save_questions_usage_by_activity($this->quba);
$this->attempt->timemodified = $timestamp;

View File

@ -88,13 +88,9 @@ class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
$prefix1 = $quba->get_field_prefix(1);
$prefix2 = $quba->get_field_prefix(2);
$tosubmit = array();
$tosubmit['slots'] = '1,2';
$tosubmit[$prefix1.':sequencecheck'] = 1;
$tosubmit[$prefix1.'answer'] = 'frog';
$tosubmit[$prefix2.':sequencecheck'] = 1;
$tosubmit[$prefix2.'answer'] = '3.14';
$tosubmit = array(1 => array('answer' => 'frog'),
2 => array('answer' => '3.14'));
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
@ -123,4 +119,107 @@ class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
$gradebookgrade = array_shift($gradebookitem->grades);
$this->assertEquals(100, $gradebookgrade->grade);
}
/**
* Create a second quiz with questions and walk through a quiz attempt this time with a random question.
*/
public function test_quiz_with_random_question_attempt_walkthrough() {
global $SITE;
$this->resetAfterTest(true);
question_bank::get_qtype('random')->clear_caches_before_testing();
$this->setAdminUser();
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
'sumgrades' => 4));
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
// Add two questions to question category.
$cat = $questiongenerator->create_question_category();
$saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
// Add random question to the quiz.
quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
// Make another category.
$cat2 = $questiongenerator->create_question_category();
$match = $questiongenerator->create_question('match', null, array('category' => $cat->id));
quiz_add_quiz_question($match->id, $quiz, 0);
$multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', array('category' => $cat->id));
quiz_add_quiz_question($multichoicemulti->id, $quiz, 0);
$multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', array('category' => $cat->id));
quiz_add_quiz_question($multichoicesingle->id, $quiz, 0);
// Make a user to do the quiz.
$user1 = $this->getDataGenerator()->create_user();
$this->setUser($user1);
$quizobj = quiz::create($quiz->id, $user1->id);
// Start the attempt.
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
// Process some responses from the student.
$attemptobj = quiz_attempt::create($attempt->id);
$tosubmit = array();
$selectedquestionid = $quba->get_question_attempt(1)->get_question()->id;
if ($selectedquestionid == $numq->id) {
$tosubmit[1] = array('answer' => '3.14');
} else {
$tosubmit[1] = array('answer' => 'frog');
}
$tosubmit[2] = array(
0 => 'amphibian',
1 => 'mammal',
2 => 'amphibian');
$tosubmit[3] = array('1', '0', '1', '0'); // First and third choice.
$tosubmit[4] = array('answer' => 0); // The first choice.
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_finish($timenow, false);
// Re-load quiz attempt data.
$attemptobj = quiz_attempt::create($attempt->id);
// Check that results are stored as expected.
$this->assertEquals(1, $attemptobj->get_attempt_number());
$this->assertEquals(4, $attemptobj->get_sum_marks());
$this->assertEquals(true, $attemptobj->is_finished());
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
$this->assertEquals($user1->id, $attemptobj->get_userid());
// Check quiz grades.
$grades = quiz_get_user_grades($quiz, $user1->id);
$grade = array_shift($grades);
$this->assertEquals(100.0, $grade->rawgrade);
// Check grade book.
$gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
$gradebookitem = array_shift($gradebookgrades->items);
$gradebookgrade = array_shift($gradebookitem->grades);
$this->assertEquals(100, $gradebookgrade->grade);
}
}

View File

@ -584,6 +584,28 @@ class question_usage_by_activity {
return $this->get_question_attempt($slot)->get_submitted_data($postdata);
}
/**
* Transform an array of response data for slots to an array of post data as you would get from quiz attempt form.
*
* @param $simulatedresponses array keys are slot nos => contains arrays representing student
* responses which will be passed to question_definition::prepare_simulated_post_data method
* and then have the appropriate prefix added.
* @return array simulated post data
*/
public function prepare_simulated_post_data($simulatedresponses) {
$simulatedpostdata = array();
$simulatedpostdata['slots'] = implode(',', array_keys($simulatedresponses));
foreach ($simulatedresponses as $slot => $responsedata) {
$prefix = $this->get_field_prefix($slot);
$slotresponse = $this->get_question($slot)->prepare_simulated_post_data($responsedata);
$slotresponse[':sequencecheck'] = $this->get_question_attempt($slot)->get_sequence_check_count();
foreach ($slotresponse as $key => $value) {
$simulatedpostdata[$prefix.$key] = $value;
}
}
return $simulatedpostdata;
}
/**
* Process a specific action on a specific question.
* @param int $slot the number used to identify this question within this usage.

View File

@ -188,6 +188,17 @@ class qtype_match_question extends question_graded_automatically_with_countback
return $response;
}
public function prepare_simulated_post_data($simulatedresponse) {
$postdata = array();
$stemnos = array_flip($this->stemorder);
$choicetochoiceno = array_flip($this->choices);
$choicenotochoiceselectvalue = array_flip($this->choiceorder);
foreach ($simulatedresponse as $subquestion => $choice) {
$postdata[$this->field($stemnos[$subquestion + 1])] = $choicenotochoiceselectvalue[$choicetochoiceno[$choice]];
}
return $postdata;
}
public function get_right_choice_for($stemid) {
foreach ($this->choiceorder as $choicekey => $choiceid) {
if ($this->right[$stemid] == $choiceid) {

View File

@ -65,7 +65,7 @@ class qtype_match_test_helper extends question_test_helper {
$q->modifiedby = $USER->id;
$q->options = new stdClass();
$q->options->shuffleanswers = 0;
$q->options->shuffleanswers = 1;
test_question_maker::set_standard_combined_feedback_fields($q->options);
$q->options->subquestions = array(
@ -107,7 +107,7 @@ class qtype_match_test_helper extends question_test_helper {
$q->defaultmark = 1;
$q->penalty = 0.3333333;
$q->shuffleanswers = 0;
$q->shuffleanswers = 1;
test_question_maker::set_standard_combined_feedback_form_data($q);
$q->subquestions = array(

View File

@ -194,6 +194,13 @@ class qtype_multichoice_single_question extends qtype_multichoice_base {
return array();
}
public function prepare_simulated_post_data($simulatedresponse) {
$ansnumbertoanswerid = array_keys($this->answers);
$ansid = $ansnumbertoanswerid[$simulatedresponse['answer']];
return array('answer' => array_search($ansid, $this->order));
}
public function is_same_response(array $prevresponse, array $newresponse) {
return question_utils::arrays_same_at_key($prevresponse, $newresponse, 'answer');
}
@ -335,6 +342,17 @@ class qtype_multichoice_multi_question extends qtype_multichoice_base {
return $response;
}
public function prepare_simulated_post_data($simulatedresponse) {
$postdata = array();
$ansidtochoiceno = array_flip($this->order);
ksort($ansidtochoiceno, SORT_NUMERIC);
$ansnotochoiceno = array_values($ansidtochoiceno);
foreach ($simulatedresponse as $ansno => $checked) {
$postdata[$this->field($ansnotochoiceno[$ansno])] = $checked;
}
return $postdata;
}
public function is_same_response(array $prevresponse, array $newresponse) {
foreach ($this->order as $key => $notused) {
$fieldname = $this->field($key);

View File

@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die();
*/
class qtype_multichoice_test_helper extends question_test_helper {
public function get_test_questions() {
return array('two_of_four');
return array('two_of_four', 'one_of_four');
}
/**
@ -216,4 +216,185 @@ class qtype_multichoice_test_helper extends question_test_helper {
return $qdata;
}
/**
* Get the question data, as it would be loaded by get_question_options.
* @return object
*/
public static function get_multichoice_question_data_one_of_four() {
global $USER;
$qdata = new stdClass();
$qdata->createdby = $USER->id;
$qdata->modifiedby = $USER->id;
$qdata->qtype = 'multichoice';
$qdata->name = 'Multiple choice question';
$qdata->questiontext = 'Which is the oddest number?';
$qdata->questiontextformat = FORMAT_HTML;
$qdata->generalfeedback = 'The oddest number is One.'; // Arguable possibly but it is a quick way to make a variation on
//this question with one correct answer.
$qdata->generalfeedbackformat = FORMAT_HTML;
$qdata->defaultmark = 1;
$qdata->length = 1;
$qdata->penalty = 0.3333333;
$qdata->hidden = 0;
$qdata->options = new stdClass();
$qdata->options->shuffleanswers = 1;
$qdata->options->answernumbering = '123';
$qdata->options->layout = 0;
$qdata->options->single = 1;
$qdata->options->correctfeedback =
test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK;
$qdata->options->correctfeedbackformat = FORMAT_HTML;
$qdata->options->partiallycorrectfeedback =
test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
$qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
$qdata->options->shownumcorrect = 1;
$qdata->options->incorrectfeedback =
test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
$qdata->options->incorrectfeedbackformat = FORMAT_HTML;
$qdata->options->answers = array(
13 => (object) array(
'id' => 13,
'answer' => 'One',
'answerformat' => FORMAT_PLAIN,
'fraction' => '1',
'feedback' => 'One is the oddest.',
'feedbackformat' => FORMAT_HTML,
),
14 => (object) array(
'id' => 14,
'answer' => 'Two',
'answerformat' => FORMAT_PLAIN,
'fraction' => '0.0',
'feedback' => 'Two is even.',
'feedbackformat' => FORMAT_HTML,
),
15 => (object) array(
'id' => 15,
'answer' => 'Three',
'answerformat' => FORMAT_PLAIN,
'fraction' => '0',
'feedback' => 'Three is odd.',
'feedbackformat' => FORMAT_HTML,
),
16 => (object) array(
'id' => 16,
'answer' => 'Four',
'answerformat' => FORMAT_PLAIN,
'fraction' => '0.0',
'feedback' => 'Four is even.',
'feedbackformat' => FORMAT_HTML,
),
);
$qdata->hints = array(
1 => (object) array(
'hint' => 'Hint 1.',
'hintformat' => FORMAT_HTML,
'shownumcorrect' => 1,
'clearwrong' => 0,
'options' => 0,
),
2 => (object) array(
'hint' => 'Hint 2.',
'hintformat' => FORMAT_HTML,
'shownumcorrect' => 1,
'clearwrong' => 1,
'options' => 1,
),
);
return $qdata;
}
/**
* Get the question data, as it would be loaded by get_question_options.
* @return object
*/
public static function get_multichoice_question_form_data_one_of_four() {
$qdata = new stdClass();
$qdata->name = 'multiple choice question';
$qdata->questiontext = array('text' => 'Which is the oddest number?', 'format' => FORMAT_HTML);
$qdata->generalfeedback = array('text' => 'The oddest number is One.', 'format' => FORMAT_HTML);
$qdata->defaultmark = 1;
$qdata->noanswers = 5;
$qdata->numhints = 2;
$qdata->penalty = 0.3333333;
$qdata->shuffleanswers = 1;
$qdata->answernumbering = '123';
$qdata->single = '1';
$qdata->correctfeedback = array('text' => test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK,
'format' => FORMAT_HTML);
$qdata->partiallycorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
'format' => FORMAT_HTML);
$qdata->shownumcorrect = 1;
$qdata->incorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK,
'format' => FORMAT_HTML);
$qdata->fraction = array('1.0', '0.0', '0.0', '0.0', '0.0');
$qdata->answer = array(
0 => array(
'text' => 'One',
'format' => FORMAT_PLAIN
),
1 => array(
'text' => 'Two',
'format' => FORMAT_PLAIN
),
2 => array(
'text' => 'Three',
'format' => FORMAT_PLAIN
),
3 => array(
'text' => 'Four',
'format' => FORMAT_PLAIN
),
4 => array(
'text' => '',
'format' => FORMAT_PLAIN
)
);
$qdata->feedback = array(
0 => array(
'text' => 'One is the oddest.',
'format' => FORMAT_HTML
),
1 => array(
'text' => 'Two is even.',
'format' => FORMAT_HTML
),
2 => array(
'text' => 'Three is odd.',
'format' => FORMAT_HTML
),
3 => array(
'text' => 'Four is even.',
'format' => FORMAT_HTML
),
4 => array(
'text' => '',
'format' => FORMAT_HTML
)
);
$qdata->hint = array(
0 => array(
'text' => 'Hint 1.',
'format' => FORMAT_HTML
),
1 => array(
'text' => 'Hint 2.',
'format' => FORMAT_HTML
)
);
$qdata->hintclearwrong = array(0, 1);
$qdata->hintshownumcorrect = array(1, 1);
return $qdata;
}
}

View File

@ -102,12 +102,19 @@ class qtype_multichoice_test extends advanced_testcase {
), $this->qtype->get_possible_responses($q));
}
public function test_question_saving_two_of_four() {
public function get_question_saving_which() {
return array(array('two_of_four'), array('one_of_four'));
}
/**
* @dataProvider get_question_saving_which
*/
public function test_question_saving_two_of_four($which) {
$this->resetAfterTest(true);
$this->setAdminUser();
$questiondata = test_question_maker::get_question_data('multichoice');
$formdata = test_question_maker::get_question_form_data('multichoice');
$questiondata = test_question_maker::get_question_data('multichoice', $which);
$formdata = test_question_maker::get_question_form_data('multichoice', $which);
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $generator->create_question_category(array());

View File

@ -287,6 +287,22 @@ abstract class question_definition {
*/
public abstract function get_correct_response();
/**
* Passed an array of data representing a student response this function transforms the array to a response array as would be
* returned from the html form for this question instance.
*
* In most cases the array will just be returned as is. Some question types will need to transform the keys of the array in
* as the meaning of the keys in the html form is deliberately obfuscated so that someone looking at the html does not get an
* advantage.
*
* @param array $simulatedresponse an array of data representing a student response
* @return array a response array as would be returned from the html form (but without prefixes)
*/
public function prepare_simulated_post_data($simulatedresponse) {
return $simulatedresponse;
}
/**
* Apply {@link format_text()} to some content with appropriate settings for
* this question.