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