Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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  namespace quiz_overview;
  18  
  19  use core_question\local\bank\question_version_status;
  20  use mod_quiz\external\submit_question_version;
  21  use mod_quiz\quiz_attempt;
  22  use question_engine;
  23  use mod_quiz\quiz_settings;
  24  use mod_quiz\local\reports\attempts_report;
  25  use quiz_overview_options;
  26  use quiz_overview_report;
  27  use quiz_overview_table;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  33  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  34  require_once($CFG->dirroot . '/mod/quiz/report/overview/report.php');
  35  require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_form.php');
  36  require_once($CFG->dirroot . '/mod/quiz/report/overview/tests/helpers.php');
  37  require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
  38  
  39  
  40  /**
  41   * Tests for the quiz overview report.
  42   *
  43   * @package    quiz_overview
  44   * @copyright  2014 The Open University
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class report_test extends \advanced_testcase {
  48      use \quiz_question_helper_test_trait;
  49  
  50      /**
  51       * Data provider for test_report_sql.
  52       *
  53       * @return array the data for the test sub-cases.
  54       */
  55      public function report_sql_cases(): array {
  56          return [[null], ['csv']]; // Only need to test on or off, not all download types.
  57      }
  58  
  59      /**
  60       * Test how the report queries the database.
  61       *
  62       * @param string|null $isdownloading a download type, or null.
  63       * @dataProvider report_sql_cases
  64       */
  65      public function test_report_sql(?string $isdownloading): void {
  66          global $DB;
  67          $this->resetAfterTest();
  68  
  69          // Create a course and a quiz.
  70          $generator = $this->getDataGenerator();
  71          $course = $generator->create_course();
  72          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
  73          $quiz = $quizgenerator->create_instance(['course' => $course->id,
  74                  'grademethod' => QUIZ_GRADEHIGHEST, 'grade' => 100.0, 'sumgrades' => 10.0,
  75                  'attempts' => 10]);
  76  
  77          // Add one question.
  78          /** @var core_question_generator $questiongenerator */
  79          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  80          $cat = $questiongenerator->create_question_category();
  81          $q = $questiongenerator->create_question('essay', 'plain', ['category' => $cat->id]);
  82          quiz_add_quiz_question($q->id, $quiz, 0 , 10);
  83  
  84          // Create some students and enrol them in the course.
  85          $student1 = $generator->create_user();
  86          $student2 = $generator->create_user();
  87          $student3 = $generator->create_user();
  88          $generator->enrol_user($student1->id, $course->id);
  89          $generator->enrol_user($student2->id, $course->id);
  90          $generator->enrol_user($student3->id, $course->id);
  91          // This line is not really necessary for the test asserts below,
  92          // but what it does is add an extra user row returned by
  93          // get_enrolled_with_capabilities_join because of a second enrolment.
  94          // The extra row returned used to make $table->query_db complain
  95          // about duplicate records. So this is really a test that an extra
  96          // student enrolment does not cause duplicate records in this query.
  97          $generator->enrol_user($student2->id, $course->id, null, 'self');
  98  
  99          // Also create a user who should not appear in the reports,
 100          // because they have a role with neither 'mod/quiz:attempt'
 101          // nor 'mod/quiz:reviewmyattempts'.
 102          $tutor = $generator->create_user();
 103          $generator->enrol_user($tutor->id, $course->id, 'teacher');
 104  
 105          // The test data.
 106          $timestamp = 1234567890;
 107          $attempts = [
 108              [$quiz, $student1, 1, 0.0,  quiz_attempt::FINISHED],
 109              [$quiz, $student1, 2, 5.0,  quiz_attempt::FINISHED],
 110              [$quiz, $student1, 3, 8.0,  quiz_attempt::FINISHED],
 111              [$quiz, $student1, 4, null, quiz_attempt::ABANDONED],
 112              [$quiz, $student1, 5, null, quiz_attempt::IN_PROGRESS],
 113              [$quiz, $student2, 1, null, quiz_attempt::ABANDONED],
 114              [$quiz, $student2, 2, null, quiz_attempt::ABANDONED],
 115              [$quiz, $student2, 3, 7.0,  quiz_attempt::FINISHED],
 116              [$quiz, $student2, 4, null, quiz_attempt::ABANDONED],
 117              [$quiz, $student2, 5, null, quiz_attempt::ABANDONED],
 118          ];
 119  
 120          // Load it in to quiz attempts table.
 121          foreach ($attempts as $attemptdata) {
 122              list($quiz, $student, $attemptnumber, $sumgrades, $state) = $attemptdata;
 123              $timestart = $timestamp + $attemptnumber * 3600;
 124  
 125              $quizobj = quiz_settings::create($quiz->id, $student->id);
 126              $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 127              $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 128  
 129              // Create the new attempt and initialize the question sessions.
 130              $attempt = quiz_create_attempt($quizobj, $attemptnumber, null, $timestart, false, $student->id);
 131  
 132              $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timestamp);
 133              $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
 134  
 135              // Process some responses from the student.
 136              $attemptobj = quiz_attempt::create($attempt->id);
 137              switch ($state) {
 138                  case quiz_attempt::ABANDONED:
 139                      $attemptobj->process_abandon($timestart + 300, false);
 140                      break;
 141  
 142                  case quiz_attempt::IN_PROGRESS:
 143                      // Do nothing.
 144                      break;
 145  
 146                  case quiz_attempt::FINISHED:
 147                      // Save answer and finish attempt.
 148                      $attemptobj->process_submitted_actions($timestart + 300, false, [
 149                              1 => ['answer' => 'My essay by ' . $student->firstname, 'answerformat' => FORMAT_PLAIN]]);
 150                      $attemptobj->process_finish($timestart + 600, false);
 151  
 152                      // Manually grade it.
 153                      $quba = $attemptobj->get_question_usage();
 154                      $quba->get_question_attempt(1)->manual_grade(
 155                              'Comment', $sumgrades, FORMAT_HTML, $timestart + 1200);
 156                      question_engine::save_questions_usage_by_activity($quba);
 157                      $update = new \stdClass();
 158                      $update->id = $attemptobj->get_attemptid();
 159                      $update->timemodified = $timestart + 1200;
 160                      $update->sumgrades = $quba->get_total_mark();
 161                      $DB->update_record('quiz_attempts', $update);
 162                      $attemptobj->get_quizobj()->get_grade_calculator()->recompute_final_grade($student->id);
 163                      break;
 164              }
 165          }
 166  
 167          // Actually getting the SQL to run is quite hard. Do a minimal set up of
 168          // some objects.
 169          $context = \context_module::instance($quiz->cmid);
 170          $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
 171          $qmsubselect = quiz_report_qm_filter_select($quiz);
 172          $studentsjoins = get_enrolled_with_capabilities_join($context, '',
 173                  ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
 174          $empty = new \core\dml\sql_join();
 175  
 176          // Set the options.
 177          $reportoptions = new quiz_overview_options('overview', $quiz, $cm, null);
 178          $reportoptions->attempts = attempts_report::ENROLLED_ALL;
 179          $reportoptions->onlygraded = true;
 180          $reportoptions->states = [quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE, quiz_attempt::FINISHED];
 181  
 182          // Now do a minimal set-up of the table class.
 183          $q->slot = 1;
 184          $q->maxmark = 10;
 185          $table = new quiz_overview_table($quiz, $context, $qmsubselect, $reportoptions,
 186                  $empty, $studentsjoins, [1 => $q], null);
 187          $table->download = $isdownloading; // Cannot call the is_downloading API, because it gives errors.
 188          $table->define_columns(['fullname']);
 189          $table->sortable(true, 'uniqueid');
 190          $table->define_baseurl(new \moodle_url('/mod/quiz/report.php'));
 191          $table->setup();
 192  
 193          // Run the query.
 194          $table->setup_sql_queries($studentsjoins);
 195          $table->query_db(30, false);
 196  
 197          // Should be 4 rows, matching count($table->rawdata) tested below.
 198          // The count is only done if not downloading.
 199          if (!$isdownloading) {
 200              $this->assertEquals(4, $table->totalrows);
 201          }
 202  
 203          // Verify what was returned: Student 1's best and in progress attempts.
 204          // Student 2's finshed attempt, and Student 3 with no attempt.
 205          // The array key is {student id}#{attempt number}.
 206          $this->assertEquals(4, count($table->rawdata));
 207          $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
 208          $this->assertEquals(1, $table->rawdata[$student1->id . '#3']->gradedattempt);
 209          $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
 210          $this->assertEquals(0, $table->rawdata[$student1->id . '#5']->gradedattempt);
 211          $this->assertArrayHasKey($student2->id . '#3', $table->rawdata);
 212          $this->assertEquals(1, $table->rawdata[$student2->id . '#3']->gradedattempt);
 213          $this->assertArrayHasKey($student3->id . '#0', $table->rawdata);
 214          $this->assertEquals(0, $table->rawdata[$student3->id . '#0']->gradedattempt);
 215  
 216          // Check the calculation of averages.
 217          $averagerow = $table->compute_average_row('overallaverage', $studentsjoins);
 218          $this->assertStringContainsString('75.00', $averagerow['sumgrades']);
 219          $this->assertStringContainsString('75.00', $averagerow['qsgrade1']);
 220          if (!$isdownloading) {
 221              $this->assertStringContainsString('(2)', $averagerow['sumgrades']);
 222              $this->assertStringContainsString('(2)', $averagerow['qsgrade1']);
 223          }
 224  
 225          // Ensure that filtering by initial does not break it.
 226          // This involves setting a private properly of the base class, which is
 227          // only really possible using reflection :-(.
 228          $reflectionobject = new \ReflectionObject($table);
 229          while ($parent = $reflectionobject->getParentClass()) {
 230              $reflectionobject = $parent;
 231          }
 232          $prefsproperty = $reflectionobject->getProperty('prefs');
 233          $prefsproperty->setAccessible(true);
 234          $prefs = $prefsproperty->getValue($table);
 235          $prefs['i_first'] = 'A';
 236          $prefsproperty->setValue($table, $prefs);
 237  
 238          list($fields, $from, $where, $params) = $table->base_sql($studentsjoins);
 239          $table->set_count_sql("SELECT COUNT(1) FROM (SELECT $fields FROM $from WHERE $where) temp WHERE 1 = 1", $params);
 240          $table->set_sql($fields, $from, $where, $params);
 241          $table->query_db(30, false);
 242          // Just verify that this does not cause a fatal error.
 243      }
 244  
 245      /**
 246       * Bands provider.
 247       * @return array
 248       */
 249      public function get_bands_count_and_width_provider(): array {
 250          return [
 251              [10, [20, .5]],
 252              [20, [20, 1]],
 253              [30, [15, 2]],
 254              // TODO MDL-55068 Handle bands better when grade is 50.
 255              // [50, [10, 5]],
 256              [100, [20, 5]],
 257              [200, [20, 10]],
 258          ];
 259      }
 260  
 261      /**
 262       * Test bands.
 263       *
 264       * @dataProvider get_bands_count_and_width_provider
 265       * @param int $grade grade
 266       * @param array $expected
 267       */
 268      public function test_get_bands_count_and_width(int $grade, array $expected): void {
 269          $this->resetAfterTest();
 270          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 271          $quiz = $quizgenerator->create_instance(['course' => SITEID, 'grade' => $grade]);
 272          $this->assertEquals($expected, quiz_overview_report::get_bands_count_and_width($quiz));
 273      }
 274  
 275      /**
 276       * Test delete_selected_attempts function.
 277       */
 278      public function test_delete_selected_attempts(): void {
 279          $this->resetAfterTest();
 280  
 281          $timestamp = 1234567890;
 282          $timestart = $timestamp + 3600;
 283  
 284          // Create a course and a quiz.
 285          $generator = $this->getDataGenerator();
 286          $course = $generator->create_course();
 287          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 288          $quiz = $quizgenerator->create_instance([
 289                  'course' => $course->id,
 290                  'grademethod' => QUIZ_GRADEHIGHEST,
 291                  'grade' => 100.0,
 292                  'sumgrades' => 10.0,
 293                  'attempts' => 10
 294          ]);
 295  
 296          // Add one question.
 297          /** @var core_question_generator $questiongenerator */
 298          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 299          $cat = $questiongenerator->create_question_category();
 300          $q = $questiongenerator->create_question('essay', 'plain', ['category' => $cat->id]);
 301          quiz_add_quiz_question($q->id, $quiz, 0 , 10);
 302  
 303          // Create student and enrol them in the course.
 304          // Note: we create two enrolments, to test the problem reported in MDL-67942.
 305          $student = $generator->create_user();
 306          $generator->enrol_user($student->id, $course->id);
 307          $generator->enrol_user($student->id, $course->id, null, 'self');
 308  
 309          $context = \context_module::instance($quiz->cmid);
 310          $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
 311          $allowedjoins = get_enrolled_with_capabilities_join($context, '', ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
 312          $quizattemptsreport = new \testable_quiz_attempts_report();
 313  
 314          // Create the new attempt and initialize the question sessions.
 315          $quizobj = quiz_settings::create($quiz->id, $student->id);
 316          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 317          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 318          $attempt = quiz_create_attempt($quizobj, 1, null, $timestart, false, $student->id);
 319          $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timestamp);
 320          $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
 321  
 322          // Delete the student's attempt.
 323          $quizattemptsreport->delete_selected_attempts($quiz, $cm, [$attempt->id], $allowedjoins);
 324      }
 325  
 326      /**
 327       * Test question regrade for selected versions.
 328       *
 329       * @covers ::regrade_question
 330       */
 331      public function test_regrade_question() {
 332          global $DB;
 333          $this->resetAfterTest();
 334          $this->setAdminUser();
 335  
 336          $course = $this->getDataGenerator()->create_course();
 337          $quiz = $this->create_test_quiz($course);
 338          $cm = get_fast_modinfo($course->id)->get_cm($quiz->cmid);
 339          $context = \context_module::instance($quiz->cmid);
 340  
 341          /** @var core_question_generator $questiongenerator */
 342          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 343          // Create a couple of questions.
 344          $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
 345          $q = $questiongenerator->create_question('shortanswer', null,
 346                  ['category' => $cat->id, 'name' => 'Toad scores 0.8']);
 347  
 348          // Create a version, the last one draft.
 349          // Sadly, update_question is a bit dodgy, so it can't handle updating the answer score.
 350          $q2 = $questiongenerator->update_question($q, null,
 351                  ['name' => 'Toad now scores 1.0']);
 352          $toadanswer = $DB->get_record_select('question_answers',
 353                  'question = ? AND ' . $DB->sql_compare_text('answer') . ' = ?',
 354                  [$q2->id, 'toad'], '*', MUST_EXIST);
 355          $DB->set_field('question_answers', 'fraction', 1, ['id' => $toadanswer->id]);
 356  
 357          // Add the question to the quiz.
 358          quiz_add_quiz_question($q2->id, $quiz, 0, 10);
 359  
 360          // Attempt the quiz, submitting response 'toad'.
 361          $quizobj = quiz_settings::create($quiz->id);
 362          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
 363          $attemptobj = quiz_attempt::create($attempt->id);
 364          $attemptobj->process_submitted_actions(time(), false, [1 => ['answer' => 'toad']]);
 365          $attemptobj->process_finish(time(), false);
 366  
 367          // We should be using 'always latest' version, which is currently v2, so should be right.
 368          $this->assertEquals(10, $attemptobj->get_question_usage()->get_total_mark());
 369  
 370          // Now change the quiz to use fixed version 1.
 371          $slot = $quizobj->get_question($q2->id);
 372          submit_question_version::execute($slot->slotid, 1);
 373  
 374          // Regrade.
 375          $report = new quiz_overview_report();
 376          $report->init('overview', 'quiz_overview_settings_form', $quiz, $cm, $course);
 377          $report->regrade_attempt($attempt);
 378  
 379          // The mark should now be 8.
 380          $attemptobj = quiz_attempt::create($attempt->id);
 381          $this->assertEquals(8, $attemptobj->get_question_usage()->get_total_mark());
 382  
 383          // Now add two more versions, the second of which is draft.
 384          $q3 = $questiongenerator->update_question($q, null,
 385                  ['name' => 'Toad now scores 0.5']);
 386          $toadanswer = $DB->get_record_select('question_answers',
 387                  'question = ? AND ' . $DB->sql_compare_text('answer') . ' = ?',
 388                  [$q3->id, 'toad'], '*', MUST_EXIST);
 389          $DB->set_field('question_answers', 'fraction', 0.5, ['id' => $toadanswer->id]);
 390  
 391          $q4 = $questiongenerator->update_question($q, null,
 392                  ['name' => 'Toad now scores 0.3',
 393                      'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 394          $toadanswer = $DB->get_record_select('question_answers',
 395                  'question = ? AND ' . $DB->sql_compare_text('answer') . ' = ?',
 396                  [$q4->id, 'toad'], '*', MUST_EXIST);
 397          $DB->set_field('question_answers', 'fraction', 0.3, ['id' => $toadanswer->id]);
 398  
 399          // Now change the quiz back to always latest and regrade again.
 400          submit_question_version::execute($slot->slotid, 0);
 401          $report->clear_regrade_date_cache();
 402          $report->regrade_attempt($attempt);
 403  
 404          // Score should now be 5, because v3 is the latest non-draft version.
 405          $attemptobj = quiz_attempt::create($attempt->id);
 406          $this->assertEquals(5, $attemptobj->get_question_usage()->get_total_mark());
 407      }
 408  }