Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for (some of) mod/quiz/locallib.php.
  19   *
  20   * @package    mod_quiz
  21   * @category   test
  22   * @copyright  2008 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/mod/quiz/lib.php');
  31  
  32  /**
  33   * @copyright  2008 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  35   */
  36  class mod_quiz_lib_testcase extends advanced_testcase {
  37      public function test_quiz_has_grades() {
  38          $quiz = new stdClass();
  39          $quiz->grade = '100.0000';
  40          $quiz->sumgrades = '100.0000';
  41          $this->assertTrue(quiz_has_grades($quiz));
  42          $quiz->sumgrades = '0.0000';
  43          $this->assertFalse(quiz_has_grades($quiz));
  44          $quiz->grade = '0.0000';
  45          $this->assertFalse(quiz_has_grades($quiz));
  46          $quiz->sumgrades = '100.0000';
  47          $this->assertFalse(quiz_has_grades($quiz));
  48      }
  49  
  50      public function test_quiz_format_grade() {
  51          $quiz = new stdClass();
  52          $quiz->decimalpoints = 2;
  53          $this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2));
  54          $this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2));
  55          $this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2));
  56          $quiz->decimalpoints = 0;
  57          $this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0');
  58      }
  59  
  60      public function test_quiz_get_grade_format() {
  61          $quiz = new stdClass();
  62          $quiz->decimalpoints = 2;
  63          $this->assertEquals(quiz_get_grade_format($quiz), 2);
  64          $this->assertEquals($quiz->questiondecimalpoints, -1);
  65          $quiz->questiondecimalpoints = 2;
  66          $this->assertEquals(quiz_get_grade_format($quiz), 2);
  67          $quiz->decimalpoints = 3;
  68          $quiz->questiondecimalpoints = -1;
  69          $this->assertEquals(quiz_get_grade_format($quiz), 3);
  70          $quiz->questiondecimalpoints = 4;
  71          $this->assertEquals(quiz_get_grade_format($quiz), 4);
  72      }
  73  
  74      public function test_quiz_format_question_grade() {
  75          $quiz = new stdClass();
  76          $quiz->decimalpoints = 2;
  77          $quiz->questiondecimalpoints = 2;
  78          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2));
  79          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2));
  80          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2));
  81          $quiz->decimalpoints = 3;
  82          $quiz->questiondecimalpoints = -1;
  83          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3));
  84          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3));
  85          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3));
  86          $quiz->questiondecimalpoints = 4;
  87          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4));
  88          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4));
  89          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4));
  90      }
  91  
  92      /**
  93       * Test deleting a quiz instance.
  94       */
  95      public function test_quiz_delete_instance() {
  96          global $SITE, $DB;
  97          $this->resetAfterTest(true);
  98          $this->setAdminUser();
  99  
 100          // Setup a quiz with 1 standard and 1 random question.
 101          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 102          $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
 103  
 104          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 105          $cat = $questiongenerator->create_question_category();
 106          $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 107  
 108          quiz_add_quiz_question($standardq->id, $quiz);
 109          quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
 110  
 111          // Get the random question.
 112          $randomq = $DB->get_record('question', array('qtype' => 'random'));
 113  
 114          quiz_delete_instance($quiz->id);
 115  
 116          // Check that the random question was deleted.
 117          $count = $DB->count_records('question', array('id' => $randomq->id));
 118          $this->assertEquals(0, $count);
 119          // Check that the standard question was not deleted.
 120          $count = $DB->count_records('question', array('id' => $standardq->id));
 121          $this->assertEquals(1, $count);
 122  
 123          // Check that all the slots were removed.
 124          $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id));
 125          $this->assertEquals(0, $count);
 126  
 127          // Check that the quiz was removed.
 128          $count = $DB->count_records('quiz', array('id' => $quiz->id));
 129          $this->assertEquals(0, $count);
 130      }
 131  
 132      /**
 133       * Setup function for all test_quiz_get_completion_state_* tests.
 134       *
 135       * @param array $completionoptions ['nbstudents'] => int, ['qtype'] => string, ['quizoptions'] => array
 136       * @throws dml_exception
 137       * @return array [$course, $students, $quiz, $cm]
 138       */
 139      private function setup_quiz_for_testing_completion(array $completionoptions) {
 140          global $CFG, $DB;
 141  
 142          $this->resetAfterTest(true);
 143  
 144          // Enable completion before creating modules, otherwise the completion data is not written in DB.
 145          $CFG->enablecompletion = true;
 146  
 147          // Create a course and students.
 148          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 149          $course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
 150          $students = [];
 151          for ($i = 0; $i < $completionoptions['nbstudents']; $i++) {
 152              $students[$i] = $this->getDataGenerator()->create_user();
 153              $this->assertTrue($this->getDataGenerator()->enrol_user($students[$i]->id, $course->id, $studentrole->id));
 154          }
 155  
 156          // Make a quiz.
 157          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 158          $data = array_merge([
 159              'course' => $course->id,
 160              'grade' => 100.0,
 161              'questionsperpage' => 0,
 162              'sumgrades' => 1,
 163              'completion' => COMPLETION_TRACKING_AUTOMATIC
 164          ], $completionoptions['quizoptions']);
 165          $quiz = $quizgenerator->create_instance($data);
 166          $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
 167  
 168          // Create a question.
 169          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 170  
 171          $cat = $questiongenerator->create_question_category();
 172          $question = $questiongenerator->create_question($completionoptions['qtype'], null, ['category' => $cat->id]);
 173          quiz_add_quiz_question($question->id, $quiz);
 174  
 175          // Set grade to pass.
 176          $item = grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz',
 177              'iteminstance' => $quiz->id, 'outcomeid' => null]);
 178          $item->gradepass = 80;
 179          $item->update();
 180  
 181          return [
 182              $course,
 183              $students,
 184              $quiz,
 185              $cm
 186          ];
 187      }
 188  
 189      /**
 190       * Helper function for all test_quiz_get_completion_state_* tests.
 191       * Starts an attempt, processes responses and finishes the attempt.
 192       *
 193       * @param $attemptoptions ['quiz'] => object, ['student'] => object, ['tosubmit'] => array, ['attemptnumber'] => int
 194       */
 195      private function do_attempt_quiz($attemptoptions) {
 196          $quizobj = quiz::create($attemptoptions['quiz']->id);
 197  
 198          // Start the passing attempt.
 199          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 200          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 201  
 202          $timenow = time();
 203          $attempt = quiz_create_attempt($quizobj, $attemptoptions['attemptnumber'], false, $timenow, false,
 204              $attemptoptions['student']->id);
 205          quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptoptions['attemptnumber'], $timenow);
 206          quiz_attempt_save_started($quizobj, $quba, $attempt);
 207  
 208          // Process responses from the student.
 209          $attemptobj = quiz_attempt::create($attempt->id);
 210          $attemptobj->process_submitted_actions($timenow, false, $attemptoptions['tosubmit']);
 211  
 212          // Finish the attempt.
 213          $attemptobj = quiz_attempt::create($attempt->id);
 214          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 215          $attemptobj->process_finish($timenow, false);
 216      }
 217  
 218      /**
 219       * Test checking the completion state of a quiz.
 220       * The quiz requires a passing grade to be completed.
 221       */
 222      public function test_quiz_get_completion_state_completionpass() {
 223  
 224          list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
 225              'nbstudents' => 2,
 226              'qtype' => 'numerical',
 227              'quizoptions' => [
 228                  'completionusegrade' => 1,
 229                  'completionpass' => 1
 230              ]
 231          ]);
 232  
 233          list($passstudent, $failstudent) = $students;
 234  
 235          // Do a passing attempt.
 236          $this->do_attempt_quiz([
 237             'quiz' => $quiz,
 238             'student' => $passstudent,
 239             'attemptnumber' => 1,
 240             'tosubmit' => [1 => ['answer' => '3.14']]
 241          ]);
 242  
 243          // Check the results.
 244          $this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
 245  
 246          // Do a failing attempt.
 247          $this->do_attempt_quiz([
 248              'quiz' => $quiz,
 249              'student' => $failstudent,
 250              'attemptnumber' => 1,
 251              'tosubmit' => [1 => ['answer' => '0']]
 252          ]);
 253  
 254          // Check the results.
 255          $this->assertFalse(quiz_get_completion_state($course, $cm, $failstudent->id, 'return'));
 256      }
 257  
 258      /**
 259       * Test checking the completion state of a quiz.
 260       * To be completed, this quiz requires either a passing grade or for all attempts to be used up.
 261       */
 262      public function test_quiz_get_completion_state_completionexhausted() {
 263  
 264          list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
 265              'nbstudents' => 2,
 266              'qtype' => 'numerical',
 267              'quizoptions' => [
 268                  'attempts' => 2,
 269                  'completionusegrade' => 1,
 270                  'completionpass' => 1,
 271                  'completionattemptsexhausted' => 1
 272              ]
 273          ]);
 274  
 275          list($passstudent, $exhauststudent) = $students;
 276  
 277          // Start a passing attempt.
 278          $this->do_attempt_quiz([
 279              'quiz' => $quiz,
 280              'student' => $passstudent,
 281              'attemptnumber' => 1,
 282              'tosubmit' => [1 => ['answer' => '3.14']]
 283          ]);
 284  
 285          // Check the results. Quiz is completed by $passstudent because of passing grade.
 286          $this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
 287  
 288          // Do a failing attempt.
 289          $this->do_attempt_quiz([
 290              'quiz' => $quiz,
 291              'student' => $exhauststudent,
 292              'attemptnumber' => 1,
 293              'tosubmit' => [1 => ['answer' => '0']]
 294          ]);
 295  
 296          // Check the results. Quiz is not completed by $exhauststudent yet because of failing grade and of remaining attempts.
 297          $this->assertFalse(quiz_get_completion_state($course, $cm, $exhauststudent->id, 'return'));
 298  
 299          // Do a second failing attempt.
 300          $this->do_attempt_quiz([
 301              'quiz' => $quiz,
 302              'student' => $exhauststudent,
 303              'attemptnumber' => 2,
 304              'tosubmit' => [1 => ['answer' => '0']]
 305          ]);
 306  
 307          // Check the results. Quiz is completed by $exhauststudent because there are no remaining attempts.
 308          $this->assertTrue(quiz_get_completion_state($course, $cm, $exhauststudent->id, 'return'));
 309      }
 310  
 311      /**
 312       * Test checking the completion state of a quiz.
 313       * To be completed, this quiz requires a minimum number of attempts.
 314       */
 315      public function test_quiz_get_completion_state_completionminattempts() {
 316  
 317          list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
 318              'nbstudents' => 1,
 319              'qtype' => 'essay',
 320              'quizoptions' => [
 321                  'completionminattemptsenabled' => 1,
 322                  'completionminattempts' => 2
 323              ]
 324          ]);
 325  
 326          list($student) = $students;
 327  
 328          // Do a first attempt.
 329          $this->do_attempt_quiz([
 330              'quiz' => $quiz,
 331              'student' => $student,
 332              'attemptnumber' => 1,
 333              'tosubmit' => [1 => ['answer' => 'Lorem ipsum.', 'answerformat' => '1']]
 334          ]);
 335  
 336          // Check the results. Quiz is not completed yet because only one attempt was done.
 337          $this->assertFalse(quiz_get_completion_state($course, $cm, $student->id, 'return'));
 338  
 339          // Do a second attempt.
 340          $this->do_attempt_quiz([
 341              'quiz' => $quiz,
 342              'student' => $student,
 343              'attemptnumber' => 2,
 344              'tosubmit' => [1 => ['answer' => 'Lorem ipsum.', 'answerformat' => '1']]
 345          ]);
 346  
 347          // Check the results. Quiz is completed by $student because two attempts were done.
 348          $this->assertTrue(quiz_get_completion_state($course, $cm, $student->id, 'return'));
 349      }
 350  
 351      /**
 352       * Test checking the completion state of a quiz.
 353       * To be completed, this quiz requires a minimum number of attempts AND a passing grade.
 354       * This is somewhat of an edge case as it is hard to imagine a scenario in which these precise settings are useful.
 355       * Nevertheless, this test makes sure these settings interact as intended.
 356       */
 357      public function  test_quiz_get_completion_state_completionminattempts_pass() {
 358  
 359          list($course, $students, $quiz, $cm) = $this->setup_quiz_for_testing_completion([
 360              'nbstudents' => 1,
 361              'qtype' => 'numerical',
 362              'quizoptions' => [
 363                  'attempts' => 2,
 364                  'completionusegrade' => 1,
 365                  'completionpass' => 1,
 366                  'completionminattemptsenabled' => 1,
 367                  'completionminattempts' => 2
 368              ]
 369          ]);
 370  
 371          list($student) = $students;
 372  
 373          // Start a first attempt.
 374          $this->do_attempt_quiz([
 375              'quiz' => $quiz,
 376              'student' => $student,
 377              'attemptnumber' => 1,
 378              'tosubmit' => [1 => ['answer' => '3.14']]
 379          ]);
 380  
 381          // Check the results. Even though one requirement is met (passing grade) quiz is not completed yet because only
 382          // one attempt was done.
 383          $this->assertFalse(quiz_get_completion_state($course, $cm, $student->id, 'return'));
 384  
 385          // Start a second attempt.
 386          $this->do_attempt_quiz([
 387              'quiz' => $quiz,
 388              'student' => $student,
 389              'attemptnumber' => 2,
 390              'tosubmit' => [1 => ['answer' => '42']]
 391          ]);
 392  
 393          // Check the results. Quiz is completed by $student because two attempts were done AND a passing grade was obtained.
 394          $this->assertTrue(quiz_get_completion_state($course, $cm, $student->id, 'return'));
 395      }
 396  
 397      public function test_quiz_get_user_attempts() {
 398          global $DB;
 399          $this->resetAfterTest();
 400  
 401          $dg = $this->getDataGenerator();
 402          $quizgen = $dg->get_plugin_generator('mod_quiz');
 403          $course = $dg->create_course();
 404          $u1 = $dg->create_user();
 405          $u2 = $dg->create_user();
 406          $u3 = $dg->create_user();
 407          $u4 = $dg->create_user();
 408          $role = $DB->get_record('role', ['shortname' => 'student']);
 409  
 410          $dg->enrol_user($u1->id, $course->id, $role->id);
 411          $dg->enrol_user($u2->id, $course->id, $role->id);
 412          $dg->enrol_user($u3->id, $course->id, $role->id);
 413          $dg->enrol_user($u4->id, $course->id, $role->id);
 414  
 415          $quiz1 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
 416          $quiz2 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
 417  
 418          // Questions.
 419          $questgen = $dg->get_plugin_generator('core_question');
 420          $quizcat = $questgen->create_question_category();
 421          $question = $questgen->create_question('numerical', null, ['category' => $quizcat->id]);
 422          quiz_add_quiz_question($question->id, $quiz1);
 423          quiz_add_quiz_question($question->id, $quiz2);
 424  
 425          $quizobj1a = quiz::create($quiz1->id, $u1->id);
 426          $quizobj1b = quiz::create($quiz1->id, $u2->id);
 427          $quizobj1c = quiz::create($quiz1->id, $u3->id);
 428          $quizobj1d = quiz::create($quiz1->id, $u4->id);
 429          $quizobj2a = quiz::create($quiz2->id, $u1->id);
 430  
 431          // Set attempts.
 432          $quba1a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1a->get_context());
 433          $quba1a->set_preferred_behaviour($quizobj1a->get_quiz()->preferredbehaviour);
 434          $quba1b = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1b->get_context());
 435          $quba1b->set_preferred_behaviour($quizobj1b->get_quiz()->preferredbehaviour);
 436          $quba1c = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1c->get_context());
 437          $quba1c->set_preferred_behaviour($quizobj1c->get_quiz()->preferredbehaviour);
 438          $quba1d = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1d->get_context());
 439          $quba1d->set_preferred_behaviour($quizobj1d->get_quiz()->preferredbehaviour);
 440          $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
 441          $quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
 442  
 443          $timenow = time();
 444  
 445          // User 1 passes quiz 1.
 446          $attempt = quiz_create_attempt($quizobj1a, 1, false, $timenow, false, $u1->id);
 447          quiz_start_new_attempt($quizobj1a, $quba1a, $attempt, 1, $timenow);
 448          quiz_attempt_save_started($quizobj1a, $quba1a, $attempt);
 449          $attemptobj = quiz_attempt::create($attempt->id);
 450          $attemptobj->process_submitted_actions($timenow, false, [1 => ['answer' => '3.14']]);
 451          $attemptobj->process_finish($timenow, false);
 452  
 453          // User 2 goes overdue in quiz 1.
 454          $attempt = quiz_create_attempt($quizobj1b, 1, false, $timenow, false, $u2->id);
 455          quiz_start_new_attempt($quizobj1b, $quba1b, $attempt, 1, $timenow);
 456          quiz_attempt_save_started($quizobj1b, $quba1b, $attempt);
 457          $attemptobj = quiz_attempt::create($attempt->id);
 458          $attemptobj->process_going_overdue($timenow, true);
 459  
 460          // User 3 does not finish quiz 1.
 461          $attempt = quiz_create_attempt($quizobj1c, 1, false, $timenow, false, $u3->id);
 462          quiz_start_new_attempt($quizobj1c, $quba1c, $attempt, 1, $timenow);
 463          quiz_attempt_save_started($quizobj1c, $quba1c, $attempt);
 464  
 465          // User 4 abandons the quiz 1.
 466          $attempt = quiz_create_attempt($quizobj1d, 1, false, $timenow, false, $u4->id);
 467          quiz_start_new_attempt($quizobj1d, $quba1d, $attempt, 1, $timenow);
 468          quiz_attempt_save_started($quizobj1d, $quba1d, $attempt);
 469          $attemptobj = quiz_attempt::create($attempt->id);
 470          $attemptobj->process_abandon($timenow, true);
 471  
 472          // User 1 attempts the quiz three times (abandon, finish, in progress).
 473          $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
 474          $quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
 475  
 476          $attempt = quiz_create_attempt($quizobj2a, 1, false, $timenow, false, $u1->id);
 477          quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 1, $timenow);
 478          quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
 479          $attemptobj = quiz_attempt::create($attempt->id);
 480          $attemptobj->process_abandon($timenow, true);
 481  
 482          $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
 483          $quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
 484  
 485          $attempt = quiz_create_attempt($quizobj2a, 2, false, $timenow, false, $u1->id);
 486          quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 2, $timenow);
 487          quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
 488          $attemptobj = quiz_attempt::create($attempt->id);
 489          $attemptobj->process_finish($timenow, false);
 490  
 491          $quba2a = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
 492          $quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
 493  
 494          $attempt = quiz_create_attempt($quizobj2a, 3, false, $timenow, false, $u1->id);
 495          quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 3, $timenow);
 496          quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
 497  
 498          // Check for user 1.
 499          $attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'all');
 500          $this->assertCount(1, $attempts);
 501          $attempt = array_shift($attempts);
 502          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 503          $this->assertEquals($u1->id, $attempt->userid);
 504          $this->assertEquals($quiz1->id, $attempt->quiz);
 505  
 506          $attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'finished');
 507          $this->assertCount(1, $attempts);
 508          $attempt = array_shift($attempts);
 509          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 510          $this->assertEquals($u1->id, $attempt->userid);
 511          $this->assertEquals($quiz1->id, $attempt->quiz);
 512  
 513          $attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'unfinished');
 514          $this->assertCount(0, $attempts);
 515  
 516          // Check for user 2.
 517          $attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'all');
 518          $this->assertCount(1, $attempts);
 519          $attempt = array_shift($attempts);
 520          $this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
 521          $this->assertEquals($u2->id, $attempt->userid);
 522          $this->assertEquals($quiz1->id, $attempt->quiz);
 523  
 524          $attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'finished');
 525          $this->assertCount(0, $attempts);
 526  
 527          $attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'unfinished');
 528          $this->assertCount(1, $attempts);
 529          $attempt = array_shift($attempts);
 530          $this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
 531          $this->assertEquals($u2->id, $attempt->userid);
 532          $this->assertEquals($quiz1->id, $attempt->quiz);
 533  
 534          // Check for user 3.
 535          $attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'all');
 536          $this->assertCount(1, $attempts);
 537          $attempt = array_shift($attempts);
 538          $this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
 539          $this->assertEquals($u3->id, $attempt->userid);
 540          $this->assertEquals($quiz1->id, $attempt->quiz);
 541  
 542          $attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'finished');
 543          $this->assertCount(0, $attempts);
 544  
 545          $attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'unfinished');
 546          $this->assertCount(1, $attempts);
 547          $attempt = array_shift($attempts);
 548          $this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
 549          $this->assertEquals($u3->id, $attempt->userid);
 550          $this->assertEquals($quiz1->id, $attempt->quiz);
 551  
 552          // Check for user 4.
 553          $attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'all');
 554          $this->assertCount(1, $attempts);
 555          $attempt = array_shift($attempts);
 556          $this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
 557          $this->assertEquals($u4->id, $attempt->userid);
 558          $this->assertEquals($quiz1->id, $attempt->quiz);
 559  
 560          $attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'finished');
 561          $this->assertCount(1, $attempts);
 562          $attempt = array_shift($attempts);
 563          $this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
 564          $this->assertEquals($u4->id, $attempt->userid);
 565          $this->assertEquals($quiz1->id, $attempt->quiz);
 566  
 567          $attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'unfinished');
 568          $this->assertCount(0, $attempts);
 569  
 570          // Multiple attempts for user 1 in quiz 2.
 571          $attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'all');
 572          $this->assertCount(3, $attempts);
 573          $attempt = array_shift($attempts);
 574          $this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
 575          $this->assertEquals($u1->id, $attempt->userid);
 576          $this->assertEquals($quiz2->id, $attempt->quiz);
 577          $attempt = array_shift($attempts);
 578          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 579          $this->assertEquals($u1->id, $attempt->userid);
 580          $this->assertEquals($quiz2->id, $attempt->quiz);
 581          $attempt = array_shift($attempts);
 582          $this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
 583          $this->assertEquals($u1->id, $attempt->userid);
 584          $this->assertEquals($quiz2->id, $attempt->quiz);
 585  
 586          $attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'finished');
 587          $this->assertCount(2, $attempts);
 588          $attempt = array_shift($attempts);
 589          $this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
 590          $attempt = array_shift($attempts);
 591          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 592  
 593          $attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'unfinished');
 594          $this->assertCount(1, $attempts);
 595          $attempt = array_shift($attempts);
 596  
 597          // Multiple quiz attempts fetched at once.
 598          $attempts = quiz_get_user_attempts([$quiz1->id, $quiz2->id], $u1->id, 'all');
 599          $this->assertCount(4, $attempts);
 600          $attempt = array_shift($attempts);
 601          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 602          $this->assertEquals($u1->id, $attempt->userid);
 603          $this->assertEquals($quiz1->id, $attempt->quiz);
 604          $attempt = array_shift($attempts);
 605          $this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
 606          $this->assertEquals($u1->id, $attempt->userid);
 607          $this->assertEquals($quiz2->id, $attempt->quiz);
 608          $attempt = array_shift($attempts);
 609          $this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
 610          $this->assertEquals($u1->id, $attempt->userid);
 611          $this->assertEquals($quiz2->id, $attempt->quiz);
 612          $attempt = array_shift($attempts);
 613          $this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
 614          $this->assertEquals($u1->id, $attempt->userid);
 615          $this->assertEquals($quiz2->id, $attempt->quiz);
 616      }
 617  
 618      /**
 619       * Test for quiz_get_group_override_priorities().
 620       */
 621      public function test_quiz_get_group_override_priorities() {
 622          global $DB;
 623          $this->resetAfterTest();
 624  
 625          $dg = $this->getDataGenerator();
 626          $quizgen = $dg->get_plugin_generator('mod_quiz');
 627          $course = $dg->create_course();
 628  
 629          $quiz = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
 630  
 631          $this->assertNull(quiz_get_group_override_priorities($quiz->id));
 632  
 633          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 634          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 635  
 636          $now = 100;
 637          $override1 = (object)[
 638              'quiz' => $quiz->id,
 639              'groupid' => $group1->id,
 640              'timeopen' => $now,
 641              'timeclose' => $now + 20
 642          ];
 643          $DB->insert_record('quiz_overrides', $override1);
 644  
 645          $override2 = (object)[
 646              'quiz' => $quiz->id,
 647              'groupid' => $group2->id,
 648              'timeopen' => $now - 10,
 649              'timeclose' => $now + 10
 650          ];
 651          $DB->insert_record('quiz_overrides', $override2);
 652  
 653          $priorities = quiz_get_group_override_priorities($quiz->id);
 654          $this->assertNotEmpty($priorities);
 655  
 656          $openpriorities = $priorities['open'];
 657          // Override 2's time open has higher priority since it is sooner than override 1's.
 658          $this->assertEquals(2, $openpriorities[$override1->timeopen]);
 659          $this->assertEquals(1, $openpriorities[$override2->timeopen]);
 660  
 661          $closepriorities = $priorities['close'];
 662          // Override 1's time close has higher priority since it is later than override 2's.
 663          $this->assertEquals(1, $closepriorities[$override1->timeclose]);
 664          $this->assertEquals(2, $closepriorities[$override2->timeclose]);
 665      }
 666  
 667      public function test_quiz_core_calendar_provide_event_action_open() {
 668          $this->resetAfterTest();
 669  
 670          $this->setAdminUser();
 671  
 672          // Create a course.
 673          $course = $this->getDataGenerator()->create_course();
 674          // Create a student and enrol into the course.
 675          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 676          // Create a quiz.
 677          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 678              'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
 679  
 680          // Create a calendar event.
 681          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
 682          // Now, log in as student.
 683          $this->setUser($student);
 684          // Create an action factory.
 685          $factory = new \core_calendar\action_factory();
 686  
 687          // Decorate action event.
 688          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
 689  
 690          // Confirm the event was decorated.
 691          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 692          $this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
 693          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 694          $this->assertEquals(1, $actionevent->get_item_count());
 695          $this->assertTrue($actionevent->is_actionable());
 696      }
 697  
 698      public function test_quiz_core_calendar_provide_event_action_open_for_user() {
 699          $this->resetAfterTest();
 700  
 701          $this->setAdminUser();
 702  
 703          // Create a course.
 704          $course = $this->getDataGenerator()->create_course();
 705          // Create a student and enrol into the course.
 706          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 707          // Create a quiz.
 708          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 709              'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
 710  
 711          // Create a calendar event.
 712          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
 713  
 714          // Create an action factory.
 715          $factory = new \core_calendar\action_factory();
 716  
 717          // Decorate action event for the student.
 718          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
 719  
 720          // Confirm the event was decorated.
 721          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 722          $this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
 723          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 724          $this->assertEquals(1, $actionevent->get_item_count());
 725          $this->assertTrue($actionevent->is_actionable());
 726      }
 727  
 728      public function test_quiz_core_calendar_provide_event_action_closed() {
 729          $this->resetAfterTest();
 730  
 731          $this->setAdminUser();
 732  
 733          // Create a course.
 734          $course = $this->getDataGenerator()->create_course();
 735  
 736          // Create a quiz.
 737          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 738              'timeclose' => time() - DAYSECS));
 739  
 740          // Create a calendar event.
 741          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
 742  
 743          // Create an action factory.
 744          $factory = new \core_calendar\action_factory();
 745  
 746          // Confirm the result was null.
 747          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
 748      }
 749  
 750      public function test_quiz_core_calendar_provide_event_action_closed_for_user() {
 751          $this->resetAfterTest();
 752  
 753          $this->setAdminUser();
 754  
 755          // Create a course.
 756          $course = $this->getDataGenerator()->create_course();
 757  
 758          // Create a student.
 759          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 760  
 761          // Create a quiz.
 762          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 763              'timeclose' => time() - DAYSECS));
 764  
 765          // Create a calendar event.
 766          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
 767  
 768          // Create an action factory.
 769          $factory = new \core_calendar\action_factory();
 770  
 771          // Confirm the result was null.
 772          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
 773      }
 774  
 775      public function test_quiz_core_calendar_provide_event_action_open_in_future() {
 776          $this->resetAfterTest();
 777  
 778          $this->setAdminUser();
 779  
 780          // Create a course.
 781          $course = $this->getDataGenerator()->create_course();
 782          // Create a student and enrol into the course.
 783          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 784          // Create a quiz.
 785          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 786              'timeopen' => time() + DAYSECS));
 787  
 788          // Create a calendar event.
 789          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
 790          // Now, log in as student.
 791          $this->setUser($student);
 792          // Create an action factory.
 793          $factory = new \core_calendar\action_factory();
 794  
 795          // Decorate action event.
 796          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
 797  
 798          // Confirm the event was decorated.
 799          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 800          $this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
 801          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 802          $this->assertEquals(1, $actionevent->get_item_count());
 803          $this->assertFalse($actionevent->is_actionable());
 804      }
 805  
 806      public function test_quiz_core_calendar_provide_event_action_open_in_future_for_user() {
 807          $this->resetAfterTest();
 808  
 809          $this->setAdminUser();
 810  
 811          // Create a course.
 812          $course = $this->getDataGenerator()->create_course();
 813          // Create a student and enrol into the course.
 814          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 815          // Create a quiz.
 816          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 817              'timeopen' => time() + DAYSECS));
 818  
 819          // Create a calendar event.
 820          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
 821  
 822          // Create an action factory.
 823          $factory = new \core_calendar\action_factory();
 824  
 825          // Decorate action event for the student.
 826          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
 827  
 828          // Confirm the event was decorated.
 829          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 830          $this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
 831          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 832          $this->assertEquals(1, $actionevent->get_item_count());
 833          $this->assertFalse($actionevent->is_actionable());
 834      }
 835  
 836      public function test_quiz_core_calendar_provide_event_action_no_capability() {
 837          global $DB;
 838  
 839          $this->resetAfterTest();
 840          $this->setAdminUser();
 841  
 842          // Create a course.
 843          $course = $this->getDataGenerator()->create_course();
 844  
 845          // Create a student.
 846          $student = $this->getDataGenerator()->create_user();
 847          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 848  
 849          // Enrol student.
 850          $this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
 851  
 852          // Create a quiz.
 853          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
 854  
 855          // Remove the permission to attempt or review the quiz for the student role.
 856          $coursecontext = context_course::instance($course->id);
 857          assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
 858          assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
 859  
 860          // Create a calendar event.
 861          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
 862  
 863          // Create an action factory.
 864          $factory = new \core_calendar\action_factory();
 865  
 866          // Set current user to the student.
 867          $this->setUser($student);
 868  
 869          // Confirm null is returned.
 870          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
 871      }
 872  
 873      public function test_quiz_core_calendar_provide_event_action_no_capability_for_user() {
 874          global $DB;
 875  
 876          $this->resetAfterTest();
 877          $this->setAdminUser();
 878  
 879          // Create a course.
 880          $course = $this->getDataGenerator()->create_course();
 881  
 882          // Create a student.
 883          $student = $this->getDataGenerator()->create_user();
 884          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 885  
 886          // Enrol student.
 887          $this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
 888  
 889          // Create a quiz.
 890          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
 891  
 892          // Remove the permission to attempt or review the quiz for the student role.
 893          $coursecontext = context_course::instance($course->id);
 894          assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
 895          assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
 896  
 897          // Create a calendar event.
 898          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
 899  
 900          // Create an action factory.
 901          $factory = new \core_calendar\action_factory();
 902  
 903          // Confirm null is returned.
 904          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
 905      }
 906  
 907      public function test_quiz_core_calendar_provide_event_action_already_finished() {
 908          global $DB;
 909  
 910          $this->resetAfterTest();
 911  
 912          $this->setAdminUser();
 913  
 914          // Create a course.
 915          $course = $this->getDataGenerator()->create_course();
 916  
 917          // Create a student.
 918          $student = $this->getDataGenerator()->create_user();
 919          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 920  
 921          // Enrol student.
 922          $this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
 923  
 924          // Create a quiz.
 925          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 926              'sumgrades' => 1));
 927  
 928          // Add a question to the quiz.
 929          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 930          $cat = $questiongenerator->create_question_category();
 931          $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
 932          quiz_add_quiz_question($question->id, $quiz);
 933  
 934          // Get the quiz object.
 935          $quizobj = quiz::create($quiz->id, $student->id);
 936  
 937          // Create an attempt for the student in the quiz.
 938          $timenow = time();
 939          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
 940          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 941          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 942          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
 943          quiz_attempt_save_started($quizobj, $quba, $attempt);
 944  
 945          // Finish the attempt.
 946          $attemptobj = quiz_attempt::create($attempt->id);
 947          $attemptobj->process_finish($timenow, false);
 948  
 949          // Create a calendar event.
 950          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
 951  
 952          // Create an action factory.
 953          $factory = new \core_calendar\action_factory();
 954  
 955          // Set current user to the student.
 956          $this->setUser($student);
 957  
 958          // Confirm null is returned.
 959          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
 960      }
 961  
 962      public function test_quiz_core_calendar_provide_event_action_already_finished_for_user() {
 963          global $DB;
 964  
 965          $this->resetAfterTest();
 966  
 967          $this->setAdminUser();
 968  
 969          // Create a course.
 970          $course = $this->getDataGenerator()->create_course();
 971  
 972          // Create a student.
 973          $student = $this->getDataGenerator()->create_user();
 974          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 975  
 976          // Enrol student.
 977          $this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
 978  
 979          // Create a quiz.
 980          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
 981              'sumgrades' => 1));
 982  
 983          // Add a question to the quiz.
 984          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 985          $cat = $questiongenerator->create_question_category();
 986          $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
 987          quiz_add_quiz_question($question->id, $quiz);
 988  
 989          // Get the quiz object.
 990          $quizobj = quiz::create($quiz->id, $student->id);
 991  
 992          // Create an attempt for the student in the quiz.
 993          $timenow = time();
 994          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
 995          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 996          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 997          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
 998          quiz_attempt_save_started($quizobj, $quba, $attempt);
 999  
1000          // Finish the attempt.
1001          $attemptobj = quiz_attempt::create($attempt->id);
1002          $attemptobj->process_finish($timenow, false);
1003  
1004          // Create a calendar event.
1005          $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
1006  
1007          // Create an action factory.
1008          $factory = new \core_calendar\action_factory();
1009  
1010          // Confirm null is returned.
1011          $this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
1012      }
1013  
1014      public function test_quiz_core_calendar_provide_event_action_already_completed() {
1015          $this->resetAfterTest();
1016          set_config('enablecompletion', 1);
1017          $this->setAdminUser();
1018  
1019          // Create the activity.
1020          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
1021          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1022              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
1023  
1024          // Get some additional data.
1025          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
1026  
1027          // Create a calendar event.
1028          $event = $this->create_action_event($course->id, $quiz->id,
1029              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
1030  
1031          // Mark the activity as completed.
1032          $completion = new completion_info($course);
1033          $completion->set_module_viewed($cm);
1034  
1035          // Create an action factory.
1036          $factory = new \core_calendar\action_factory();
1037  
1038          // Decorate action event.
1039          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
1040  
1041          // Ensure result was null.
1042          $this->assertNull($actionevent);
1043      }
1044  
1045      public function test_quiz_core_calendar_provide_event_action_already_completed_for_user() {
1046          $this->resetAfterTest();
1047          set_config('enablecompletion', 1);
1048          $this->setAdminUser();
1049  
1050          // Create the activity.
1051          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
1052          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1053              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
1054  
1055          // Enrol a student in the course.
1056          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
1057  
1058          // Get some additional data.
1059          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
1060  
1061          // Create a calendar event.
1062          $event = $this->create_action_event($course->id, $quiz->id,
1063              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
1064  
1065          // Mark the activity as completed for the student.
1066          $completion = new completion_info($course);
1067          $completion->set_module_viewed($cm, $student->id);
1068  
1069          // Create an action factory.
1070          $factory = new \core_calendar\action_factory();
1071  
1072          // Decorate action event for the student.
1073          $actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
1074  
1075          // Ensure result was null.
1076          $this->assertNull($actionevent);
1077      }
1078  
1079      /**
1080       * Creates an action event.
1081       *
1082       * @param int $courseid
1083       * @param int $instanceid The quiz id.
1084       * @param string $eventtype The event type. eg. QUIZ_EVENT_TYPE_OPEN.
1085       * @return bool|calendar_event
1086       */
1087      private function create_action_event($courseid, $instanceid, $eventtype) {
1088          $event = new stdClass();
1089          $event->name = 'Calendar event';
1090          $event->modulename  = 'quiz';
1091          $event->courseid = $courseid;
1092          $event->instance = $instanceid;
1093          $event->type = CALENDAR_EVENT_TYPE_ACTION;
1094          $event->eventtype = $eventtype;
1095          $event->timestart = time();
1096  
1097          return calendar_event::create($event);
1098      }
1099  
1100      /**
1101       * Test the callback responsible for returning the completion rule descriptions.
1102       * This function should work given either an instance of the module (cm_info), such as when checking the active rules,
1103       * or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
1104       */
1105      public function test_mod_quiz_completion_get_active_rule_descriptions() {
1106          $this->resetAfterTest();
1107          $this->setAdminUser();
1108  
1109          // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
1110          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
1111          $quiz1 = $this->getDataGenerator()->create_module('quiz', [
1112              'course' => $course->id,
1113              'completion' => 2,
1114              'completionusegrade' => 1,
1115              'completionattemptsexhausted' => 1,
1116              'completionpass' => 1
1117          ]);
1118          $quiz2 = $this->getDataGenerator()->create_module('quiz', [
1119              'course' => $course->id,
1120              'completion' => 2,
1121              'completionusegrade' => 0
1122          ]);
1123          $cm1 = cm_info::create(get_coursemodule_from_instance('quiz', $quiz1->id));
1124          $cm2 = cm_info::create(get_coursemodule_from_instance('quiz', $quiz2->id));
1125  
1126          // Data for the stdClass input type.
1127          // This type of input would occur when checking the default completion rules for an activity type, where we don't have
1128          // any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
1129          $moddefaults = new stdClass();
1130          $moddefaults->customdata = ['customcompletionrules' => [
1131              'completionattemptsexhausted' => 1,
1132              'completionpass' => 1
1133          ]];
1134          $moddefaults->completion = 2;
1135  
1136          $activeruledescriptions = [
1137              get_string('completionattemptsexhausteddesc', 'quiz'),
1138              get_string('completionpassdesc', 'quiz'),
1139          ];
1140          $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
1141          $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm2), []);
1142          $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
1143          $this->assertEquals(mod_quiz_get_completion_active_rule_descriptions(new stdClass()), []);
1144      }
1145  
1146      /**
1147       * A user who does not have capabilities to add events to the calendar should be able to create a quiz.
1148       */
1149      public function test_creation_with_no_calendar_capabilities() {
1150          $this->resetAfterTest();
1151          $course = self::getDataGenerator()->create_course();
1152          $context = context_course::instance($course->id);
1153          $user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
1154          $roleid = self::getDataGenerator()->create_role();
1155          self::getDataGenerator()->role_assign($roleid, $user->id, $context->id);
1156          assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
1157          $generator = self::getDataGenerator()->get_plugin_generator('mod_quiz');
1158          // Create an instance as a user without the calendar capabilities.
1159          $this->setUser($user);
1160          $time = time();
1161          $params = array(
1162              'course' => $course->id,
1163              'timeopen' => $time + 200,
1164              'timeclose' => $time + 2000,
1165          );
1166          $generator->create_instance($params);
1167      }
1168  }