/** * Create a quiz add questions to it, walk through quiz attempts and then check results. * * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv", * "stepsXX.csv" and "resultsXX.csv". * @dataProvider get_data_for_walkthrough */ public function test_walkthrough_from_csv($quizsettings, $csvdata) { // CSV data files for these tests were generated using : // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator $this->resetAfterTest(true); question_bank::get_qtype('random')->clear_caches_before_testing(); $this->create_quiz($quizsettings, $csvdata['questions']); $attemptids = $this->walkthrough_attempts($csvdata['steps']); $this->check_attempts_results($csvdata['results'], $attemptids); $this->report = new quiz_statistics_report(); $whichattempts = QUIZ_GRADEAVERAGE; $groupstudents = array(); $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz); list($quizstats, $questionstats, $subquestionstats) = $this->report->get_quiz_and_questions_stats($this->quiz, $whichattempts, $groupstudents, $questions); $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts); // We will create some quiz and question stat calculator instances and some response analyser instances, just in order // to check the time of the $quizcalc = new quiz_statistics_calculator(); // Should not be a delay of more than one second between the calculation of stats above and here. $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids)); $qcalc = new \core_question\statistics\questions\calculator($questions); $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids)); foreach ($questions as $question) { if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) { continue; } $responesstats = new \core_question\statistics\responses\analyser($question); $this->assertTimeCurrent($responesstats->get_last_analysed_time($qubaids)); } // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is // available in open document or excel format here : // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet $quizstatsexpected = array('median' => 4.5, 'firstattemptsavg' => 4.617333332, 'allattemptsavg' => 4.617333332, 'firstattemptscount' => 25, 'allattemptscount' => 25, 'standarddeviation' => 0.8117265554, 'skewness' => -0.092502502, 'kurtosis' => -0.7073968557, 'cic' => -87.22309355420001, 'errorratio' => 136.8294900795, 'standarderror' => 1.1106813066); foreach ($quizstatsexpected as $statname => $statvalue) { $this->assertEquals($statvalue, $quizstats->{$statname}, $quizstats->{$statname}, abs($statvalue) * 1.5E-5); } for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) { $slotqstats = $csvdata['qstats']->getRow($rowno); foreach ($slotqstats as $statname => $slotqstat) { if ($statname !== 'slot') { switch ($statname) { case 'covariance': case 'discriminationindex': case 'discriminativeefficiency': case 'effectiveweight': $precision = 1.0E-5; break; default: $precision = 1.0E-6; } $slot = $slotqstats['slot']; $delta = abs($slotqstat) * $precision; $actual = $questionstats[$slot]->{$statname}; $this->assertEquals(floatval($slotqstat), $actual, "{$statname} for slot {$slot}", $delta); } } } }
/** * Check the question stats and the response counts used in the statistics report. If the appropriate files exist in fixtures/. * * @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata Data loaded from csv files for this test. * @param string $whichattempts * @param string $whichtries * @param \core\dml\sql_join $groupstudentsjoins * @return array with contents 0 => $questions, 1 => $quizstats, 2=> $questionstats, 3=> $qubaids Might be needed for further * testing. */ protected function check_stats_calculations_and_response_analysis($csvdata, $whichattempts, $whichtries, \core\dml\sql_join $groupstudentsjoins) { $this->report = new quiz_statistics_report(); $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz); list($quizstats, $questionstats) = $this->report->get_all_stats_and_analysis($this->quiz, $whichattempts, $whichtries, $groupstudentsjoins, $questions); $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudentsjoins, $whichattempts); // We will create some quiz and question stat calculator instances and some response analyser instances, just in order // to check the last analysed time then returned. $quizcalc = new \quiz_statistics\calculator(); // Should not be a delay of more than one second between the calculation of stats above and here. $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids)); $qcalc = new \core_question\statistics\questions\calculator($questions); $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids)); if (isset($csvdata['responsecounts'])) { $this->check_response_counts($csvdata['responsecounts'], $qubaids, $questions, $whichtries); } if (isset($csvdata['qstats'])) { $this->check_question_stats($csvdata['qstats'], $questionstats); return array($questions, $quizstats, $questionstats, $qubaids); } return array($questions, $quizstats, $questionstats, $qubaids); }