Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [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 mod_quiz;
  18  
  19  use question_engine;
  20  use quiz;
  21  use quiz_attempt;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  27  
  28  /**
  29   * Quiz attempt walk through using data from csv file.
  30   *
  31   * @package    mod_quiz
  32   * @category   test
  33   * @copyright  2013 The Open University
  34   * @author     Jamie Pratt <me@jamiep.org>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class attempt_walkthrough_from_csv_test extends \advanced_testcase {
  38  
  39      protected $files = array('questions', 'steps', 'results');
  40  
  41      /**
  42       * @var stdClass the quiz record we create.
  43       */
  44      protected $quiz;
  45  
  46      /**
  47       * @var array with slot no => question name => questionid. Question ids of questions created in the same category as random q.
  48       */
  49      protected $randqids;
  50  
  51      /**
  52       * The only test in this class. This is run multiple times depending on how many sets of files there are in fixtures/
  53       * directory.
  54       *
  55       * @param array $quizsettings of settings read from csv file quizzes.csv
  56       * @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "resultsXX.csv".
  57       * @dataProvider get_data_for_walkthrough
  58       */
  59      public function test_walkthrough_from_csv($quizsettings, $csvdata) {
  60  
  61          // CSV data files for these tests were generated using :
  62          // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
  63  
  64          $this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);
  65      }
  66  
  67      public function create_quiz($quizsettings, $qs) {
  68          global $SITE, $DB;
  69          $this->setAdminUser();
  70  
  71          /** @var core_question_generator $questiongenerator */
  72          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  73          $slots = array();
  74          $qidsbycat = array();
  75          $sumofgrades = 0;
  76          foreach ($qs as $qsrow) {
  77              $q = $this->explode_dot_separated_keys_to_make_subindexs($qsrow);
  78  
  79              $catname = array('name' => $q['cat']);
  80              if (!$cat = $DB->get_record('question_categories', array('name' => $q['cat']))) {
  81                  $cat = $questiongenerator->create_question_category($catname);
  82              }
  83              $q['catid'] = $cat->id;
  84              foreach (array('which' => null, 'overrides' => array()) as $key => $default) {
  85                  if (empty($q[$key])) {
  86                      $q[$key] = $default;
  87                  }
  88              }
  89  
  90              if ($q['type'] !== 'random') {
  91                  // Don't actually create random questions here.
  92                  $overrides = array('category' => $cat->id, 'defaultmark' => $q['mark']) + $q['overrides'];
  93                  if ($q['type'] === 'truefalse') {
  94                      // True/false question can never have hints, but sometimes we need to put them
  95                      // in the CSV file, to keep it rectangular.
  96                      unset($overrides['hint']);
  97                  }
  98                  $question = $questiongenerator->create_question($q['type'], $q['which'], $overrides);
  99                  $q['id'] = $question->id;
 100  
 101                  if (!isset($qidsbycat[$q['cat']])) {
 102                      $qidsbycat[$q['cat']] = array();
 103                  }
 104                  if (!empty($q['which'])) {
 105                      $name = $q['type'].'_'.$q['which'];
 106                  } else {
 107                      $name = $q['type'];
 108                  }
 109                  $qidsbycat[$q['catid']][$name] = $q['id'];
 110              }
 111              if (!empty($q['slot'])) {
 112                  $slots[$q['slot']] = $q;
 113                  $sumofgrades += $q['mark'];
 114              }
 115          }
 116  
 117          ksort($slots);
 118  
 119          // Make a quiz.
 120          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 121  
 122          // Settings from param override defaults.
 123          $aggregratedsettings = $quizsettings + array('course' => $SITE->id,
 124                                                       'questionsperpage' => 0,
 125                                                       'grade' => 100.0,
 126                                                       'sumgrades' => $sumofgrades);
 127  
 128          $this->quiz = $quizgenerator->create_instance($aggregratedsettings);
 129  
 130          $this->randqids = array();
 131          foreach ($slots as $slotno => $slotquestion) {
 132              if ($slotquestion['type'] !== 'random') {
 133                  quiz_add_quiz_question($slotquestion['id'], $this->quiz, 0, $slotquestion['mark']);
 134              } else {
 135                  quiz_add_random_questions($this->quiz, 0, $slotquestion['catid'], 1, 0);
 136                  $this->randqids[$slotno] = $qidsbycat[$slotquestion['catid']];
 137              }
 138          }
 139      }
 140  
 141      /**
 142       * Create quiz, simulate attempts and check results (if resultsXX.csv exists).
 143       *
 144       * @param array $quizsettings Quiz overrides for this quiz.
 145       * @param array $csvdata Data loaded from csv files for this test.
 146       */
 147      protected function create_quiz_simulate_attempts_and_check_results(array $quizsettings, array $csvdata) {
 148          $this->resetAfterTest();
 149  
 150          $this->create_quiz($quizsettings, $csvdata['questions']);
 151  
 152          $attemptids = $this->walkthrough_attempts($csvdata['steps']);
 153  
 154          if (isset($csvdata['results'])) {
 155              $this->check_attempts_results($csvdata['results'], $attemptids);
 156          }
 157      }
 158  
 159      /**
 160       * Get full path of CSV file.
 161       *
 162       * @param string $setname
 163       * @param string $test
 164       * @return string full path of file.
 165       */
 166      protected function get_full_path_of_csv_file(string $setname, string $test): string {
 167          return  __DIR__."/fixtures/{$setname}{$test}.csv";
 168      }
 169  
 170      /**
 171       * Load dataset from CSV file "{$setname}{$test}.csv".
 172       *
 173       * @param string $setname
 174       * @param string $test
 175       * @return array
 176       */
 177      protected function load_csv_data_file(string $setname, string $test = ''): array {
 178          $files = array($setname => $this->get_full_path_of_csv_file($setname, $test));
 179          return $this->dataset_from_files($files)->get_rows([$setname]);
 180      }
 181  
 182      /**
 183       * Break down row of csv data into sub arrays, according to column names.
 184       *
 185       * @param array $row from csv file with field names with parts separate by '.'.
 186       * @return array the row with each part of the field name following a '.' being a separate sub array's index.
 187       */
 188      protected function explode_dot_separated_keys_to_make_subindexs(array $row): array {
 189          $parts = array();
 190          foreach ($row as $columnkey => $value) {
 191              $newkeys = explode('.', trim($columnkey));
 192              $placetoputvalue =& $parts;
 193              foreach ($newkeys as $newkeydepth => $newkey) {
 194                  if ($newkeydepth + 1 === count($newkeys)) {
 195                      $placetoputvalue[$newkey] = $value;
 196                  } else {
 197                      // Going deeper down.
 198                      if (!isset($placetoputvalue[$newkey])) {
 199                          $placetoputvalue[$newkey] = array();
 200                      }
 201                      $placetoputvalue =& $placetoputvalue[$newkey];
 202                  }
 203              }
 204          }
 205          return $parts;
 206      }
 207  
 208      /**
 209       * Data provider method for test_walkthrough_from_csv. Called by PHPUnit.
 210       *
 211       * @return array One array element for each run of the test. Each element contains an array with the params for
 212       *                  test_walkthrough_from_csv.
 213       */
 214      public function get_data_for_walkthrough(): array {
 215          $quizzes = $this->load_csv_data_file('quizzes')['quizzes'];
 216          $datasets = array();
 217          foreach ($quizzes as $quizsettings) {
 218              $dataset = array();
 219              foreach ($this->files as $file) {
 220                  if (file_exists($this->get_full_path_of_csv_file($file, $quizsettings['testnumber']))) {
 221                      $dataset[$file] = $this->load_csv_data_file($file, $quizsettings['testnumber'])[$file];
 222                  }
 223              }
 224              $datasets[] = array($quizsettings, $dataset);
 225          }
 226          return $datasets;
 227      }
 228  
 229      /**
 230       * @param array $steps the step data from the csv file.
 231       * @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
 232       */
 233      protected function walkthrough_attempts(array $steps): array {
 234          global $DB;
 235          $attemptids = array();
 236          foreach ($steps as $steprow) {
 237  
 238              $step = $this->explode_dot_separated_keys_to_make_subindexs($steprow);
 239              // Find existing user or make a new user to do the quiz.
 240              $username = array('firstname' => $step['firstname'],
 241                                'lastname'  => $step['lastname']);
 242  
 243              if (!$user = $DB->get_record('user', $username)) {
 244                  $user = $this->getDataGenerator()->create_user($username);
 245              }
 246  
 247              if (!isset($attemptids[$step['quizattempt']])) {
 248                  // Start the attempt.
 249                  $quizobj = quiz::create($this->quiz->id, $user->id);
 250                  $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 251                  $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 252  
 253                  $prevattempts = quiz_get_user_attempts($this->quiz->id, $user->id, 'all', true);
 254                  $attemptnumber = count($prevattempts) + 1;
 255                  $timenow = time();
 256                  $attempt = quiz_create_attempt($quizobj, $attemptnumber, null, $timenow, false, $user->id);
 257                  // Select variant and / or random sub question.
 258                  if (!isset($step['variants'])) {
 259                      $step['variants'] = array();
 260                  }
 261                  if (isset($step['randqs'])) {
 262                      // Replace 'names' with ids.
 263                      foreach ($step['randqs'] as $slotno => $randqname) {
 264                          $step['randqs'][$slotno] = $this->randqids[$slotno][$randqname];
 265                      }
 266                  } else {
 267                      $step['randqs'] = array();
 268                  }
 269  
 270                  quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow, $step['randqs'], $step['variants']);
 271                  quiz_attempt_save_started($quizobj, $quba, $attempt);
 272                  $attemptid = $attemptids[$step['quizattempt']] = $attempt->id;
 273              } else {
 274                  $attemptid = $attemptids[$step['quizattempt']];
 275              }
 276  
 277              // Process some responses from the student.
 278              $attemptobj = quiz_attempt::create($attemptid);
 279              $attemptobj->process_submitted_actions($timenow, false, $step['responses']);
 280  
 281              // Finish the attempt.
 282              if (!isset($step['finished']) || ($step['finished'] == 1)) {
 283                  $attemptobj = quiz_attempt::create($attemptid);
 284                  $attemptobj->process_finish($timenow, false);
 285              }
 286          }
 287          return $attemptids;
 288      }
 289  
 290      /**
 291       * @param array $results the results data from the csv file.
 292       * @param array $attemptids attempt no as in csv file => the id of the quiz_attempt as stored in the db.
 293       */
 294      protected function check_attempts_results(array $results, array $attemptids) {
 295          foreach ($results as $resultrow) {
 296              $result = $this->explode_dot_separated_keys_to_make_subindexs($resultrow);
 297              // Re-load quiz attempt data.
 298              $attemptobj = quiz_attempt::create($attemptids[$result['quizattempt']]);
 299              $this->check_attempt_results($result, $attemptobj);
 300          }
 301      }
 302  
 303      /**
 304       * Check that attempt results are as specified in $result.
 305       *
 306       * @param array        $result             row of data read from csv file.
 307       * @param quiz_attempt $attemptobj         the attempt object loaded from db.
 308       */
 309      protected function check_attempt_results(array $result, quiz_attempt $attemptobj) {
 310          foreach ($result as $fieldname => $value) {
 311              if ($value === '!NULL!') {
 312                  $value = null;
 313              }
 314              switch ($fieldname) {
 315                  case 'quizattempt' :
 316                      break;
 317                  case 'attemptnumber' :
 318                      $this->assertEquals($value, $attemptobj->get_attempt_number());
 319                      break;
 320                  case 'slots' :
 321                      foreach ($value as $slotno => $slottests) {
 322                          foreach ($slottests as $slotfieldname => $slotvalue) {
 323                              switch ($slotfieldname) {
 324                                  case 'mark' :
 325                                      $this->assertEquals(round($slotvalue, 2), $attemptobj->get_question_mark($slotno),
 326                                                          "Mark for slot $slotno of attempt {$result['quizattempt']}.");
 327                                      break;
 328                                  default :
 329                                      throw new \coding_exception('Unknown slots sub field column in csv file '
 330                                                                 .s($slotfieldname));
 331                              }
 332                          }
 333                      }
 334                      break;
 335                  case 'finished' :
 336                      $this->assertEquals((bool)$value, $attemptobj->is_finished());
 337                      break;
 338                  case 'summarks' :
 339                      $this->assertEquals((float)$value, $attemptobj->get_sum_marks(),
 340                          "Sum of marks of attempt {$result['quizattempt']}.");
 341                      break;
 342                  case 'quizgrade' :
 343                      // Check quiz grades.
 344                      $grades = quiz_get_user_grades($attemptobj->get_quiz(), $attemptobj->get_userid());
 345                      $grade = array_shift($grades);
 346                      $this->assertEquals($value, $grade->rawgrade, "Quiz grade for attempt {$result['quizattempt']}.");
 347                      break;
 348                  case 'gradebookgrade' :
 349                      // Check grade book.
 350                      $gradebookgrades = grade_get_grades($attemptobj->get_courseid(),
 351                                                          'mod', 'quiz',
 352                                                          $attemptobj->get_quizid(),
 353                                                          $attemptobj->get_userid());
 354                      $gradebookitem = array_shift($gradebookgrades->items);
 355                      $gradebookgrade = array_shift($gradebookitem->grades);
 356                      $this->assertEquals($value, $gradebookgrade->grade, "Gradebook grade for attempt {$result['quizattempt']}.");
 357                      break;
 358                  default :
 359                      throw new \coding_exception('Unknown column in csv file '.s($fieldname));
 360              }
 361          }
 362      }
 363  }