MDL-42957 (2) quiz statistics : Hardcoded sql LIMIT clause
authorJames Pratt <me@jamiep.org>
Thu, 28 Nov 2013 10:13:26 +0000 (17:13 +0700)
committerJames Pratt <me@jamiep.org>
Thu, 28 Nov 2013 10:13:26 +0000 (17:13 +0700)
Fixing some typos and adding some tests.

mod/quiz/report/statistics/report.php
mod/quiz/report/statistics/tests/stats_from_steps_walkthrough_test.php
question/classes/statistics/questions/calculator.php
question/classes/statistics/responses/analyser.php

index 4945b2d..8609335 100644 (file)
@@ -500,7 +500,7 @@ class quiz_statistics_report extends quiz_default_report {
      *     - $questionstats array of \core_question\statistics\questions\calculated objects keyed by slot.
      *     - $subquestionstats array of \core_question\statistics\questions\calculated_for_subquestion objects keyed by question id.
      */
-    protected function get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions) {
+    public function get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions) {
 
         $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
 
index c9aae05..bb2c2d7 100644 (file)
@@ -31,22 +31,7 @@ require_once($CFG->dirroot . '/mod/quiz/tests/attempt_walkthrough_from_csv_test.
 require_once($CFG->dirroot . '/mod/quiz/report/default.php');
 require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
-/**
- * Test helper subclass of quiz_statistics_report
- *
- * @copyright  2013 The Open University
- * @author     Jamie Pratt <me@jamiep.org>
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class testable_quiz_statistics_report extends quiz_statistics_report {
 
-    public function get_stats($quiz, $whichattempts = QUIZ_GRADEAVERAGE, $groupstudents = array()) {
-        $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
-        $this->clear_cached_data($qubaids);
-        $questions = $this->load_and_initialise_questions_for_calculations($quiz);
-        return $this->get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions);
-    }
-}
 
 /**
  * Quiz attempt walk through using data from csv file.
@@ -92,8 +77,31 @@ class quiz_report_statistics_from_steps extends mod_quiz_attempt_walkthrough_fro
 
         $this->check_attempts_results($csvdata['results'], $attemptids);
 
-        $this->report = new testable_quiz_statistics_report();
-        list($quizstats, $questionstats, $subquestionstats) = $this->report->get_stats($this->quiz);
+        $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->assertEquals(time(), $quizcalc->get_last_calculated_time($qubaids), '', 1);
+
+        $qcalc = new \core_question\statistics\questions\calculator($questions);
+        $this->assertEquals(time(), $qcalc->get_last_calculated_time($qubaids), '', 1);
+
+        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->assertEquals(time(), $responesstats->get_last_analysed_time($qubaids), '', 1);
+        }
 
         // 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 :
index 78ab82e..e1919e9 100644 (file)
@@ -251,7 +251,7 @@ class calculator {
 
         $timemodified = time() - self::TIME_TO_CACHE;
         return $DB->get_field_select('question_statistics', 'timemodified', 'hashcode = ? AND timemodified > ?',
-                                     array($qubaids->get_hash_code(), $timemodified));
+                                     array($qubaids->get_hash_code(), $timemodified), IGNORE_MULTIPLE);
     }
 
     /** @var integer Time after which statistics are automatically recomputed. */
index a42d2f0..bfaa26f 100644 (file)
@@ -161,7 +161,8 @@ class analyser {
         global $DB;
 
         $timemodified = time() - self::TIME_TO_CACHE;
-        return $DB->get_field_select('question_response_analysis', 'hashcode = ? AND questionid = ? AND timemodified > ?',
+        return $DB->get_field_select('question_response_analysis', 'timemodified',
+                                     'hashcode = ? AND questionid = ? AND timemodified > ?',
                                      array($qubaids->get_hash_code(), $this->questiondata->id, $timemodified), IGNORE_MULTIPLE);
     }
 }