<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for (some of) mod/quiz/locallib.php.
*
* @package mod_quiz
* @category test
* @copyright 2008 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
> namespace mod_quiz;
> use core_external\external_api;
> use mod_quiz\quiz_settings;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/lib.php');
/**
* @copyright 2008 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
< class mod_quiz_lib_testcase extends advanced_testcase {
> class lib_test extends \advanced_testcase {
public function test_quiz_has_grades() {
< $quiz = new stdClass();
> $quiz = new \stdClass();
$quiz->grade = '100.0000';
$quiz->sumgrades = '100.0000';
$this->assertTrue(quiz_has_grades($quiz));
$quiz->sumgrades = '0.0000';
$this->assertFalse(quiz_has_grades($quiz));
$quiz->grade = '0.0000';
$this->assertFalse(quiz_has_grades($quiz));
$quiz->sumgrades = '100.0000';
$this->assertFalse(quiz_has_grades($quiz));
}
public function test_quiz_format_grade() {
< $quiz = new stdClass();
> $quiz = new \stdClass();
$quiz->decimalpoints = 2;
$this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2));
$this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2));
$this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2));
$quiz->decimalpoints = 0;
$this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0');
}
public function test_quiz_get_grade_format() {
< $quiz = new stdClass();
> $quiz = new \stdClass();
$quiz->decimalpoints = 2;
$this->assertEquals(quiz_get_grade_format($quiz), 2);
$this->assertEquals($quiz->questiondecimalpoints, -1);
$quiz->questiondecimalpoints = 2;
$this->assertEquals(quiz_get_grade_format($quiz), 2);
$quiz->decimalpoints = 3;
$quiz->questiondecimalpoints = -1;
$this->assertEquals(quiz_get_grade_format($quiz), 3);
$quiz->questiondecimalpoints = 4;
$this->assertEquals(quiz_get_grade_format($quiz), 4);
}
public function test_quiz_format_question_grade() {
< $quiz = new stdClass();
> $quiz = new \stdClass();
$quiz->decimalpoints = 2;
$quiz->questiondecimalpoints = 2;
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2));
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2));
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2));
$quiz->decimalpoints = 3;
$quiz->questiondecimalpoints = -1;
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3));
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3));
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3));
$quiz->questiondecimalpoints = 4;
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4));
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4));
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4));
}
/**
* Test deleting a quiz instance.
*/
public function test_quiz_delete_instance() {
global $SITE, $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup a quiz with 1 standard and 1 random question.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
< $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
> $quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
< $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
> $standardq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
quiz_add_quiz_question($standardq->id, $quiz);
quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
// Get the random question.
< $randomq = $DB->get_record('question', array('qtype' => 'random'));
> $randomq = $DB->get_record('question', ['qtype' => 'random']);
quiz_delete_instance($quiz->id);
// Check that the random question was deleted.
< $count = $DB->count_records('question', array('id' => $randomq->id));
> if ($randomq) {
> $count = $DB->count_records('question', ['id' => $randomq->id]);
$this->assertEquals(0, $count);
> }
// Check that the standard question was not deleted.
< $count = $DB->count_records('question', array('id' => $standardq->id));
> $count = $DB->count_records('question', ['id' => $standardq->id]);
$this->assertEquals(1, $count);
// Check that all the slots were removed.
< $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id));
> $count = $DB->count_records('quiz_slots', ['quizid' => $quiz->id]);
$this->assertEquals(0, $count);
// Check that the quiz was removed.
< $count = $DB->count_records('quiz', array('id' => $quiz->id));
> $count = $DB->count_records('quiz', ['id' => $quiz->id]);
$this->assertEquals(0, $count);
}
/**
< * Test checking the completion state of a quiz.
> * Setup function for all test_quiz_get_completion_state_* tests.
> *
> * @param array $completionoptions ['nbstudents'] => int, ['qtype'] => string, ['quizoptions'] => array
> * @throws dml_exception
> * @return array [$course, $students, $quiz, $cm]
*/
< public function test_quiz_get_completion_state() {
> private function setup_quiz_for_testing_completion(array $completionoptions) {
global $CFG, $DB;
>
$this->resetAfterTest(true);
// Enable completion before creating modules, otherwise the completion data is not written in DB.
$CFG->enablecompletion = true;
< // Create a course and student.
< $course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
< $passstudent = $this->getDataGenerator()->create_user();
< $failstudent = $this->getDataGenerator()->create_user();
< $studentrole = $DB->get_record('role', array('shortname' => 'student'));
< $this->assertNotEmpty($studentrole);
<
< // Enrol students.
< $this->assertTrue($this->getDataGenerator()->enrol_user($passstudent->id, $course->id, $studentrole->id));
< $this->assertTrue($this->getDataGenerator()->enrol_user($failstudent->id, $course->id, $studentrole->id));
<
< // Make a scale and an outcome.
< $scale = $this->getDataGenerator()->create_scale();
< $data = array('courseid' => $course->id,
< 'fullname' => 'Team work',
< 'shortname' => 'Team work',
< 'scaleid' => $scale->id);
< $outcome = $this->getDataGenerator()->create_grade_outcome($data);
> // Create a course and students.
> $studentrole = $DB->get_record('role', ['shortname' => 'student']);
> $course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
> $students = [];
> for ($i = 0; $i < $completionoptions['nbstudents']; $i++) {
> $students[$i] = $this->getDataGenerator()->create_user();
> $this->assertTrue($this->getDataGenerator()->enrol_user($students[$i]->id, $course->id, $studentrole->id));
> }
< // Make a quiz with the outcome on.
> // Make a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
< $data = array('course' => $course->id,
< 'outcome_'.$outcome->id => 1,
> $data = array_merge([
> 'course' => $course->id,
'grade' => 100.0,
'questionsperpage' => 0,
'sumgrades' => 1,
< 'completion' => COMPLETION_TRACKING_AUTOMATIC,
< 'completionusegrade' => 1,
< 'completionpass' => 1);
> 'completion' => COMPLETION_TRACKING_AUTOMATIC
> ], $completionoptions['quizoptions']);
$quiz = $quizgenerator->create_instance($data);
$cm = get_coursemodule_from_id('quiz', $quiz->cmid);
< // Create a couple of questions.
> // Create a question.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
< $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
> $question = $questiongenerator->create_question($completionoptions['qtype'], null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $quiz);
< $quizobj = quiz::create($quiz->id, $passstudent->id);
<
// Set grade to pass.
< $item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod',
< 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null));
> $item = \grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz',
> 'iteminstance' => $quiz->id, 'outcomeid' => null]);
$item->gradepass = 80;
$item->update();
> return [
// Start the passing attempt.
> $course,
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
> $students,
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
> $quiz,
> $cm
$timenow = time();
> ];
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $passstudent->id);
> }
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
>
quiz_attempt_save_started($quizobj, $quba, $attempt);
> /**
> * Helper function for all test_quiz_get_completion_state_* tests.
// Process some responses from the student.
> * Starts an attempt, processes responses and finishes the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
> *
$tosubmit = array(1 => array('answer' => '3.14'));
> * @param $attemptoptions ['quiz'] => object, ['student'] => object, ['tosubmit'] => array, ['attemptnumber'] => int
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
> */
> private function do_attempt_quiz($attemptoptions) {
// Finish the attempt.
> $quizobj = quiz_settings::create($attemptoptions['quiz']->id);
$attemptobj = quiz_attempt::create($attempt->id);
>
< $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
> $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
< $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $passstudent->id);
< quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
> $attempt = quiz_create_attempt($quizobj, $attemptoptions['attemptnumber'], false, $timenow, false,
> $attemptoptions['student']->id);
> quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptoptions['attemptnumber'], $timenow);
< // Process some responses from the student.
> // Process responses from the student.
< $tosubmit = array(1 => array('answer' => '3.14'));
< $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
> $attemptobj->process_submitted_actions($timenow, false, $attemptoptions['tosubmit']);
> }
< // Start the failing attempt.
< $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
< $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
> /**
> * Test checking the completion state of a quiz.
> * The quiz requires a passing grade to be completed.
> */
> public function test_quiz_get_completion_state_completionpass() {
< $timenow = time();
< $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $failstudent->id);
< quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
< quiz_attempt_save_started($quizobj, $quba, $attempt);
> list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
> 'nbstudents' => 2,
> 'qtype' => 'numerical',
> 'quizoptions' => [
> 'completionusegrade' => 1,
> 'completionpassgrade' => 1
> ]
> ]);
< // Process some responses from the student.
< $attemptobj = quiz_attempt::create($attempt->id);
< $tosubmit = array(1 => array('answer' => '0'));
< $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
> list($passstudent, $failstudent) = $students;
< // Finish the attempt.
< $attemptobj = quiz_attempt::create($attempt->id);
< $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
< $attemptobj->process_finish($timenow, false);
> // Do a passing attempt.
> $this->do_attempt_quiz([
> 'quiz' => $quiz,
> 'student' => $passstudent,
> 'attemptnumber' => 1,
> 'tosubmit' => [1 => ['answer' => '3.14']]
> ]);
// Check the results.
$this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
>
$this->assertFalse(quiz_get_completion_state($course, $cm, $failstudent->id, 'return'));
> // Do a failing attempt.
}
> $this->do_attempt_quiz([
> 'quiz' => $quiz,
public function test_quiz_get_user_attempts() {
> 'student' => $failstudent,
global $DB;
> 'attemptnumber' => 1,
$this->resetAfterTest();
> 'tosubmit' => [1 => ['answer' => '0']]
> ]);
$dg = $this->getDataGenerator();
>
$quizgen = $dg->get_plugin_generator('mod_quiz');
> // Check the results.
$course = $dg->create_course();
>
$u1 = $dg->create_user();
> $this->assertDebuggingCalledCount(3, [
$u2 = $dg->create_user();
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
$u3 = $dg->create_user();
> 'quiz_completion_check_min_attempts has been deprecated.',
$u4 = $dg->create_user();
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
$role = $DB->get_record('role', ['shortname' => 'student']);
> ]);
> }
$dg->enrol_user($u1->id, $course->id, $role->id);
>
$dg->enrol_user($u2->id, $course->id, $role->id);
> /**
$dg->enrol_user($u3->id, $course->id, $role->id);
> * Test checking the completion state of a quiz.
$dg->enrol_user($u4->id, $course->id, $role->id);
> * To be completed, this quiz requires either a passing grade or for all attempts to be used up.
> */
$quiz1 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
> public function test_quiz_get_completion_state_completionexhausted() {
$quiz2 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
>
> list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
// Questions.
> 'nbstudents' => 2,
$questgen = $dg->get_plugin_generator('core_question');
> 'qtype' => 'numerical',
$quizcat = $questgen->create_question_category();
> 'quizoptions' => [
$question = $questgen->create_question('numerical', null, ['category' => $quizcat->id]);
> 'attempts' => 2,
quiz_add_quiz_question($question->id, $quiz1);
> 'completionusegrade' => 1,
quiz_add_quiz_question($question->id, $quiz2);
> 'completionpassgrade' => 1,
> 'completionattemptsexhausted' => 1
$quizobj1a = quiz::create($quiz1->id, $u1->id);
> ]
$quizobj1b = quiz::create($quiz1->id, $u2->id);
> ]);
$quizobj1c = quiz::create($quiz1->id, $u3->id);
>
$quizobj1d = quiz::create($quiz1->id, $u4->id);
> list($passstudent, $exhauststudent) = $students;
$quizobj2a = quiz::create($quiz2->id, $u1->id);
>
> // Start a passing attempt.
// Set attempts.
> $this->do_attempt_quiz([
$quba1a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1a->get_context());
> 'quiz' => $quiz,
$quba1a->set_preferred_behaviour($quizobj1a->get_quiz()->preferredbehaviour);
> 'student' => $passstudent,
$quba1b = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1b->get_context());
> 'attemptnumber' => 1,
$quba1b->set_preferred_behaviour($quizobj1b->get_quiz()->preferredbehaviour);
> 'tosubmit' => [1 => ['answer' => '3.14']]
$quba1c = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1c->get_context());
> ]);
$quba1c->set_preferred_behaviour($quizobj1c->get_quiz()->preferredbehaviour);
>
$quba1d = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1d->get_context());
> // Check the results. Quiz is completed by $passstudent because of passing grade.
$quba1d->set_preferred_behaviour($quizobj1d->get_quiz()->preferredbehaviour);
> $this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
$quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
>
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
> // Do a failing attempt.
> $this->do_attempt_quiz([
$timenow = time();
> 'quiz' => $quiz,
> 'student' => $exhauststudent,
// User 1 passes quiz 1.
> 'attemptnumber' => 1,
$attempt = quiz_create_attempt($quizobj1a, 1, false, $timenow, false, $u1->id);
> 'tosubmit' => [1 => ['answer' => '0']]
quiz_start_new_attempt($quizobj1a, $quba1a, $attempt, 1, $timenow);
> ]);
quiz_attempt_save_started($quizobj1a, $quba1a, $attempt);
>
$attemptobj = quiz_attempt::create($attempt->id);
> // Check the results. Quiz is not completed by $exhauststudent yet because of failing grade and of remaining attempts.
$attemptobj->process_submitted_actions($timenow, false, [1 => ['answer' => '3.14']]);
> $this->assertFalse(quiz_get_completion_state($course, $cm, $exhauststudent->id, 'return'));
$attemptobj->process_finish($timenow, false);
>
> // Do a second failing attempt.
// User 2 goes overdue in quiz 1.
> $this->do_attempt_quiz([
$attempt = quiz_create_attempt($quizobj1b, 1, false, $timenow, false, $u2->id);
> 'quiz' => $quiz,
quiz_start_new_attempt($quizobj1b, $quba1b, $attempt, 1, $timenow);
> 'student' => $exhauststudent,
quiz_attempt_save_started($quizobj1b, $quba1b, $attempt);
> 'attemptnumber' => 2,
$attemptobj = quiz_attempt::create($attempt->id);
> 'tosubmit' => [1 => ['answer' => '0']]
$attemptobj->process_going_overdue($timenow, true);
> ]);
>
// User 3 does not finish quiz 1.
> // Check the results. Quiz is completed by $exhauststudent because there are no remaining attempts.
$attempt = quiz_create_attempt($quizobj1c, 1, false, $timenow, false, $u3->id);
> $this->assertTrue(quiz_get_completion_state($course, $cm, $exhauststudent->id, 'return'));
quiz_start_new_attempt($quizobj1c, $quba1c, $attempt, 1, $timenow);
>
quiz_attempt_save_started($quizobj1c, $quba1c, $attempt);
> $this->assertDebuggingCalledCount(5, [
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
// User 4 abandons the quiz 1.
> 'quiz_completion_check_min_attempts has been deprecated.',
$attempt = quiz_create_attempt($quizobj1d, 1, false, $timenow, false, $u4->id);
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
quiz_start_new_attempt($quizobj1d, $quba1d, $attempt, 1, $timenow);
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
quiz_attempt_save_started($quizobj1d, $quba1d, $attempt);
> 'quiz_completion_check_min_attempts has been deprecated.',
$attemptobj = quiz_attempt::create($attempt->id);
> ]);
$attemptobj->process_abandon($timenow, true);
> }
>
// User 1 attempts the quiz three times (abandon, finish, in progress).
> /**
$quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> * Test checking the completion state of a quiz.
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
> * To be completed, this quiz requires a minimum number of attempts.
> */
$attempt = quiz_create_attempt($quizobj2a, 1, false, $timenow, false, $u1->id);
> public function test_quiz_get_completion_state_completionminattempts() {
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 1, $timenow);
>
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
> list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
$attemptobj = quiz_attempt::create($attempt->id);
> 'nbstudents' => 1,
$attemptobj->process_abandon($timenow, true);
> 'qtype' => 'essay',
> 'quizoptions' => [
$quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> 'completionminattemptsenabled' => 1,
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
> 'completionminattempts' => 2
> ]
$attempt = quiz_create_attempt($quizobj2a, 2, false, $timenow, false, $u1->id);
> ]);
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 2, $timenow);
>
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
> list($student) = $students;
$attemptobj = quiz_attempt::create($attempt->id);
>
$attemptobj->process_finish($timenow, false);
> // Do a first attempt.
> $this->do_attempt_quiz([
$quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> 'quiz' => $quiz,
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
> 'student' => $student,
> 'attemptnumber' => 1,
$attempt = quiz_create_attempt($quizobj2a, 3, false, $timenow, false, $u1->id);
> 'tosubmit' => [1 => ['answer' => 'Lorem ipsum.', 'answerformat' => '1']]
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 3, $timenow);
> ]);
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
>
> // Check the results. Quiz is not completed yet because only one attempt was done.
// Check for user 1.
> $this->assertFalse(quiz_get_completion_state($course, $cm, $student->id, 'return'));
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'all');
>
$this->assertCount(1, $attempts);
> // Do a second attempt.
$attempt = array_shift($attempts);
> $this->do_attempt_quiz([
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
> 'quiz' => $quiz,
$this->assertEquals($u1->id, $attempt->userid);
> 'student' => $student,
$this->assertEquals($quiz1->id, $attempt->quiz);
> 'attemptnumber' => 2,
> 'tosubmit' => [1 => ['answer' => 'Lorem ipsum.', 'answerformat' => '1']]
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'finished');
> ]);
$this->assertCount(1, $attempts);
>
$attempt = array_shift($attempts);
> // Check the results. Quiz is completed by $student because two attempts were done.
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
> $this->assertTrue(quiz_get_completion_state($course, $cm, $student->id, 'return'));
$this->assertEquals($u1->id, $attempt->userid);
>
$this->assertEquals($quiz1->id, $attempt->quiz);
> $this->assertDebuggingCalledCount(4, [
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'unfinished');
> 'quiz_completion_check_min_attempts has been deprecated.',
$this->assertCount(0, $attempts);
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
> 'quiz_completion_check_min_attempts has been deprecated.',
// Check for user 2.
> ]);
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'all');
> }
$this->assertCount(1, $attempts);
>
$attempt = array_shift($attempts);
> /**
$this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
> * Test checking the completion state of a quiz.
$this->assertEquals($u2->id, $attempt->userid);
> * To be completed, this quiz requires a minimum number of attempts AND a passing grade.
$this->assertEquals($quiz1->id, $attempt->quiz);
> * This is somewhat of an edge case as it is hard to imagine a scenario in which these precise settings are useful.
> * Nevertheless, this test makes sure these settings interact as intended.
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'finished');
> */
$this->assertCount(0, $attempts);
> public function test_quiz_get_completion_state_completionminattempts_pass() {
>
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'unfinished');
> list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
$this->assertCount(1, $attempts);
> 'nbstudents' => 1,
$attempt = array_shift($attempts);
> 'qtype' => 'numerical',
$this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
> 'quizoptions' => [
$this->assertEquals($u2->id, $attempt->userid);
> 'attempts' => 2,
$this->assertEquals($quiz1->id, $attempt->quiz);
> 'completionusegrade' => 1,
> 'completionpassgrade' => 1,
// Check for user 3.
> 'completionminattemptsenabled' => 1,
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'all');
> 'completionminattempts' => 2
$this->assertCount(1, $attempts);
> ]
$attempt = array_shift($attempts);
> ]);
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
>
$this->assertEquals($u3->id, $attempt->userid);
> list($student) = $students;
$this->assertEquals($quiz1->id, $attempt->quiz);
>
> // Start a first attempt.
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'finished');
> $this->do_attempt_quiz([
$this->assertCount(0, $attempts);
> 'quiz' => $quiz,
> 'student' => $student,
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'unfinished');
> 'attemptnumber' => 1,
$this->assertCount(1, $attempts);
> 'tosubmit' => [1 => ['answer' => '3.14']]
$attempt = array_shift($attempts);
> ]);
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
>
$this->assertEquals($u3->id, $attempt->userid);
> // Check the results. Even though one requirement is met (passing grade) quiz is not completed yet because only
$this->assertEquals($quiz1->id, $attempt->quiz);
> // one attempt was done.
> $this->assertFalse(quiz_get_completion_state($course, $cm, $student->id, 'return'));
// Check for user 4.
>
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'all');
> // Start a second attempt.
$this->assertCount(1, $attempts);
> $this->do_attempt_quiz([
$attempt = array_shift($attempts);
> 'quiz' => $quiz,
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
> 'student' => $student,
$this->assertEquals($u4->id, $attempt->userid);
> 'attemptnumber' => 2,
$this->assertEquals($quiz1->id, $attempt->quiz);
> 'tosubmit' => [1 => ['answer' => '42']]
> ]);
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'finished');
>
$this->assertCount(1, $attempts);
> // Check the results. Quiz is completed by $student because two attempts were done AND a passing grade was obtained.
$attempt = array_shift($attempts);
> $this->assertTrue(quiz_get_completion_state($course, $cm, $student->id, 'return'));
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
>
$this->assertEquals($u4->id, $attempt->userid);
> $this->assertDebuggingCalledCount(4, [
$this->assertEquals($quiz1->id, $attempt->quiz);
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
> 'quiz_completion_check_min_attempts has been deprecated.',
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'unfinished');
> 'quiz_completion_check_passing_grade_or_all_attempts has been deprecated.',
$this->assertCount(0, $attempts);
> 'quiz_completion_check_min_attempts has been deprecated.',
> ]);
< $quizobj1a = quiz::create($quiz1->id, $u1->id);
< $quizobj1b = quiz::create($quiz1->id, $u2->id);
< $quizobj1c = quiz::create($quiz1->id, $u3->id);
< $quizobj1d = quiz::create($quiz1->id, $u4->id);
< $quizobj2a = quiz::create($quiz2->id, $u1->id);
> $quizobj1a = quiz_settings::create($quiz1->id, $u1->id);
> $quizobj1b = quiz_settings::create($quiz1->id, $u2->id);
> $quizobj1c = quiz_settings::create($quiz1->id, $u3->id);
> $quizobj1d = quiz_settings::create($quiz1->id, $u4->id);
> $quizobj2a = quiz_settings::create($quiz2->id, $u1->id);
< $quba1a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1a->get_context());
> $quba1a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1a->get_context());
< $quba1b = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1b->get_context());
> $quba1b = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1b->get_context());
< $quba1c = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1c->get_context());
> $quba1c = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1c->get_context());
< $quba1d = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1d->get_context());
> $quba1d = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1d->get_context());
< $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> $quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
< $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> $quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
< $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> $quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
< $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
> $quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
$this->assertEquals($u1->id, $attempt->userid);
$this->assertEquals($quiz2->id, $attempt->quiz);
$attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'finished');
$this->assertCount(2, $attempts);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
$attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'unfinished');
$this->assertCount(1, $attempts);
$attempt = array_shift($attempts);
// Multiple quiz attempts fetched at once.
$attempts = quiz_get_user_attempts([$quiz1->id, $quiz2->id], $u1->id, 'all');
$this->assertCount(4, $attempts);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
$this->assertEquals($u1->id, $attempt->userid);
$this->assertEquals($quiz1->id, $attempt->quiz);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
$this->assertEquals($u1->id, $attempt->userid);
$this->assertEquals($quiz2->id, $attempt->quiz);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
$this->assertEquals($u1->id, $attempt->userid);
$this->assertEquals($quiz2->id, $attempt->quiz);
$attempt = array_shift($attempts);
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
$this->assertEquals($u1->id, $attempt->userid);
$this->assertEquals($quiz2->id, $attempt->quiz);
}
/**
* Test for quiz_get_group_override_priorities().
*/
public function test_quiz_get_group_override_priorities() {
global $DB;
$this->resetAfterTest();
$dg = $this->getDataGenerator();
$quizgen = $dg->get_plugin_generator('mod_quiz');
$course = $dg->create_course();
$quiz = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
$this->assertNull(quiz_get_group_override_priorities($quiz->id));
< $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
< $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
> $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
> $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
$now = 100;
$override1 = (object)[
'quiz' => $quiz->id,
'groupid' => $group1->id,
'timeopen' => $now,
'timeclose' => $now + 20
];
$DB->insert_record('quiz_overrides', $override1);
$override2 = (object)[
'quiz' => $quiz->id,
'groupid' => $group2->id,
'timeopen' => $now - 10,
'timeclose' => $now + 10
];
$DB->insert_record('quiz_overrides', $override2);
$priorities = quiz_get_group_override_priorities($quiz->id);
$this->assertNotEmpty($priorities);
$openpriorities = $priorities['open'];
// Override 2's time open has higher priority since it is sooner than override 1's.
$this->assertEquals(2, $openpriorities[$override1->timeopen]);
$this->assertEquals(1, $openpriorities[$override2->timeopen]);
$closepriorities = $priorities['close'];
// Override 1's time close has higher priority since it is later than override 2's.
$this->assertEquals(1, $closepriorities[$override1->timeclose]);
$this->assertEquals(2, $closepriorities[$override2->timeclose]);
}
public function test_quiz_core_calendar_provide_event_action_open() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student and enrol into the course.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Now, log in as student.
$this->setUser($student);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
public function test_quiz_core_calendar_provide_event_action_open_for_user() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student and enrol into the course.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
public function test_quiz_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeclose' => time() - DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeclose' => time() - DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Confirm the result was null.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
}
public function test_quiz_core_calendar_provide_event_action_closed_for_user() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeclose' => time() - DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeclose' => time() - DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Confirm the result was null.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
}
public function test_quiz_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student and enrol into the course.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeopen' => time() + DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeopen' => time() + DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
// Now, log in as student.
$this->setUser($student);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
public function test_quiz_core_calendar_provide_event_action_open_in_future_for_user() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student and enrol into the course.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'timeopen' => time() + DAYSECS));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'timeopen' => time() + DAYSECS]);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
public function test_quiz_core_calendar_provide_event_action_no_capability() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_user();
< $studentrole = $DB->get_record('role', array('shortname' => 'student'));
> $studentrole = $DB->get_record('role', ['shortname' => 'student']);
// Enrol student.
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
// Remove the permission to attempt or review the quiz for the student role.
< $coursecontext = context_course::instance($course->id);
> $coursecontext = \context_course::instance($course->id);
assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Set current user to the student.
$this->setUser($student);
// Confirm null is returned.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
}
public function test_quiz_core_calendar_provide_event_action_no_capability_for_user() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_user();
< $studentrole = $DB->get_record('role', array('shortname' => 'student'));
> $studentrole = $DB->get_record('role', ['shortname' => 'student']);
// Enrol student.
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
// Remove the permission to attempt or review the quiz for the student role.
< $coursecontext = context_course::instance($course->id);
> $coursecontext = \context_course::instance($course->id);
assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Confirm null is returned.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
}
public function test_quiz_core_calendar_provide_event_action_already_finished() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_user();
< $studentrole = $DB->get_record('role', array('shortname' => 'student'));
> $studentrole = $DB->get_record('role', ['shortname' => 'student']);
// Enrol student.
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'sumgrades' => 1));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'sumgrades' => 1]);
// Add a question to the quiz.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
< $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
> $question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $quiz);
// Get the quiz object.
< $quizobj = quiz::create($quiz->id, $student->id);
> $quizobj = quiz_settings::create($quiz->id, $student->id);
// Create an attempt for the student in the quiz.
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
< $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
> $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_finish($timenow, false);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Set current user to the student.
$this->setUser($student);
// Confirm null is returned.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
}
public function test_quiz_core_calendar_provide_event_action_already_finished_for_user() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_user();
< $studentrole = $DB->get_record('role', array('shortname' => 'student'));
> $studentrole = $DB->get_record('role', ['shortname' => 'student']);
// Enrol student.
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
// Create a quiz.
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
< 'sumgrades' => 1));
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
> 'sumgrades' => 1]);
// Add a question to the quiz.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
< $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
> $question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
quiz_add_quiz_question($question->id, $quiz);
// Get the quiz object.
< $quizobj = quiz::create($quiz->id, $student->id);
> $quizobj = quiz_settings::create($quiz->id, $student->id);
// Create an attempt for the student in the quiz.
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
< $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
> $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
// Finish the attempt.
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_finish($timenow, false);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Confirm null is returned.
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
}
public function test_quiz_core_calendar_provide_event_action_already_completed() {
$this->resetAfterTest();
set_config('enablecompletion', 1);
$this->setAdminUser();
// Create the activity.
< $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
< array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
> $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id],
> ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]);
// Get some additional data.
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
// Mark the activity as completed.
< $completion = new completion_info($course);
> $completion = new \completion_info($course);
$completion->set_module_viewed($cm);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
// Ensure result was null.
$this->assertNull($actionevent);
}
public function test_quiz_core_calendar_provide_event_action_already_completed_for_user() {
$this->resetAfterTest();
set_config('enablecompletion', 1);
$this->setAdminUser();
// Create the activity.
< $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
< $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
< array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
> $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id],
> ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]);
// Enrol a student in the course.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Get some additional data.
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
// Create a calendar event.
$event = $this->create_action_event($course->id, $quiz->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
// Mark the activity as completed for the student.
< $completion = new completion_info($course);
> $completion = new \completion_info($course);
$completion->set_module_viewed($cm, $student->id);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
// Ensure result was null.
$this->assertNull($actionevent);
}
/**
* Creates an action event.
*
* @param int $courseid
* @param int $instanceid The quiz id.
* @param string $eventtype The event type. eg. QUIZ_EVENT_TYPE_OPEN.
* @return bool|calendar_event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
< $event = new stdClass();
> $event = new \stdClass();
$event->name = 'Calendar event';
$event->modulename = 'quiz';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();
< return calendar_event::create($event);
> return \calendar_event::create($event);
}
/**
* Test the callback responsible for returning the completion rule descriptions.
* This function should work given either an instance of the module (cm_info), such as when checking the active rules,
* or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
*/
public function test_mod_quiz_completion_get_active_rule_descriptions() {
$this->resetAfterTest();
$this->setAdminUser();
// Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
$quiz1 = $this->getDataGenerator()->create_module('quiz', [
'course' => $course->id,
'completion' => 2,
'completionusegrade' => 1,
> 'completionpassgrade' => 1,
'completionattemptsexhausted' => 1,
< 'completionpass' => 1
]);
$quiz2 = $this->getDataGenerator()->create_module('quiz', [
'course' => $course->id,
'completion' => 2,
'completionusegrade' => 0
]);
< $cm1 = cm_info::create(get_coursemodule_from_instance('quiz', $quiz1->id));
< $cm2 = cm_info::create(get_coursemodule_from_instance('quiz', $quiz2->id));
> $cm1 = \cm_info::create(get_coursemodule_from_instance('quiz', $quiz1->id));
> $cm2 = \cm_info::create(get_coursemodule_from_instance('quiz', $quiz2->id));
// Data for the stdClass input type.
// This type of input would occur when checking the default completion rules for an activity type, where we don't have
// any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
< $moddefaults = new stdClass();
> $moddefaults = new \stdClass();
$moddefaults->customdata = ['customcompletionrules' => [
'completionattemptsexhausted' => 1,
< 'completionpass' => 1
]];
$moddefaults->completion = 2;
$activeruledescriptions = [
< get_string('completionattemptsexhausteddesc', 'quiz'),
< get_string('completionpassdesc', 'quiz'),
> get_string('completionpassorattemptsexhausteddesc', 'quiz'),
];
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm2), []);
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
< $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions(new stdClass()), []);
> $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions(new \stdClass()), []);
}
/**
* A user who does not have capabilities to add events to the calendar should be able to create a quiz.
*/
public function test_creation_with_no_calendar_capabilities() {
$this->resetAfterTest();
$course = self::getDataGenerator()->create_course();
< $context = context_course::instance($course->id);
> $context = \context_course::instance($course->id);
$user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
$roleid = self::getDataGenerator()->create_role();
self::getDataGenerator()->role_assign($roleid, $user->id, $context->id);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
$generator = self::getDataGenerator()->get_plugin_generator('mod_quiz');
// Create an instance as a user without the calendar capabilities.
$this->setUser($user);
$time = time();
< $params = array(
> $params = [
'course' => $course->id,
'timeopen' => $time + 200,
'timeclose' => $time + 2000,
< );
> ];
$generator->create_instance($params);
> }
}
>
}
> /**
> * Data provider for summarise_response() test cases.
> *
> * @return array List of data sets (test cases)
> */
> public function mod_quiz_inplace_editable_provider(): array {
> return [
> 'set to A1' => [1, 'A1'],
> 'set with HTML characters' => [2, 'A & & <-:'],
> 'set to integer' => [3, '3'],
> 'set to blank' => [4, ''],
> 'set with Unicode characters' => [1, 'L\'Aina Lluís^'],
> 'set with Unicode at the truncation point' => [1, '123456789012345碁'],
> 'set with HTML Char at the truncation point' => [1, '123456789012345>'],
> ];
> }
>
> /**
> * Test customised and automated question numbering for a given slot number and customised value.
> *
> * @dataProvider mod_quiz_inplace_editable_provider
> * @param int $slotnumber
> * @param string $newvalue
> * @covers ::mod_quiz_inplace_editable
> */
> public function test_mod_quiz_inplace_editable(int $slotnumber, string $newvalue): void {
> global $CFG;
> require_once($CFG->dirroot . '/lib/external/externallib.php');
> $this->resetAfterTest();
>
> $this->setAdminUser();
> $course = self::getDataGenerator()->create_course();
> $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'sumgrades' => 1]);
> $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
>
> // Add few questions to the quiz.
> $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
> $cat = $questiongenerator->create_question_category();
>
> $question = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
> quiz_add_quiz_question($question->id, $quiz);
>
> $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
> quiz_add_quiz_question($question->id, $quiz);
>
> $question = $questiongenerator->create_question('multichoice', null, ['category' => $cat->id]);
> quiz_add_quiz_question($question->id, $quiz);
>
> $question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
> quiz_add_quiz_question($question->id, $quiz);
>
> // Create the quiz object.
> $quizobj = new quiz_settings($quiz, $cm, $course);
> $structure = $quizobj->get_structure();
>
> $slots = $structure->get_slots();
> $this->assertEquals(4, count($slots));
>
> $slotid = $structure->get_slot_id_for_slot($slotnumber);
> $inplaceeditable = mod_quiz_inplace_editable('slotdisplaynumber', $slotid, $newvalue);
> $result = \core_external::update_inplace_editable('mod_quiz', 'slotdisplaynumber', $slotid, $newvalue);
> $result = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $result);
>
> $this->assertEquals(count((array) $inplaceeditable), count($result));
> $this->assertEquals($slotid, $result['itemid']);
> if ($newvalue === '' || is_null($newvalue)) {
> // Check against default.
> $this->assertEquals($slotnumber, $result['displayvalue']);
> $this->assertEquals($slotnumber, $result['value']);
> } else {
> // Check against the custom number.
> $this->assertEquals(s($newvalue), $result['displayvalue']);
> $this->assertEquals($newvalue, $result['value']);
> }