Skip to content

Commit

Permalink
MDL-35939 mod_quiz: Add quiz attempt generator function
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Aug 15, 2019
1 parent ab44453 commit ee6c9e9
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion mod/quiz/tests/attempt_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,48 @@ public function test_is_participant() {
$this->assertEquals(true, $quizobj->is_participant($USER->id),
'Admin is enrolled, suspended and can participate');
}
}

/**
* Create quiz and attempt data with layout.
*
* @param string $layout layout to set. Like quiz attempt.layout. E.g. '1,2,0,3,4,0,'.
* @return quiz_attempt the new quiz_attempt object
*/
protected function create_quiz_and_attempt_with_layout($layout) {
$this->resetAfterTest(true);

// Make a user to do the quiz.
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
// Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(['course' => $course->id,
'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);

$quizobj = quiz::create($quiz->id, $user->id);

$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);

$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();

$page = 1;
foreach (explode(',', $layout) as $slot) {
if ($slot == 0) {
$page += 1;
continue;
}

$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $quiz, $page);
}

$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);

return quiz_attempt::create($attempt->id);
}
}

0 comments on commit ee6c9e9

Please sign in to comment.