MDL-47618 quiz statistics: fatal error with groups.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 10 Oct 2014 14:17:10 +0000 (15:17 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 11 Nov 2014 18:01:35 +0000 (18:01 +0000)
The problem was that the names of query placeholders were changing. The
fix was to do things ourself, rather than using get_in_or_equal.

mod/quiz/report/statistics/statisticslib.php
mod/quiz/report/statistics/tests/statisticslib_test.php [new file with mode: 0644]

index b66b83d..2653979 100644 (file)
@@ -46,7 +46,9 @@ function quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts =
     if ($groupstudents) {
         ksort($groupstudents);
         list($grpsql, $grpparams) = $DB->get_in_or_equal(array_keys($groupstudents),
-                                                         SQL_PARAMS_NAMED, 'u');
+                SQL_PARAMS_NAMED, 'statsuser');
+        list($grpsql, $grpparams) = quiz_statistics_renumber_placeholders(
+                $grpsql, $grpparams, 'statsuser');
         $whereqa .= " AND quiza.userid $grpsql";
         $qaparams += $grpparams;
     }
@@ -63,6 +65,31 @@ function quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts =
     return array($fromqa, $whereqa, $qaparams);
 }
 
+/**
+ * Re-number all the params beginning with $paramprefix in a fragment of SQL.
+ *
+ * @param string $sql the SQL.
+ * @param array $params the params.
+ * @param string $paramprefix the parameter prefix.
+ * @return array with two elements, the modified SQL, and the modified params.
+ */
+function quiz_statistics_renumber_placeholders($sql, $params, $paramprefix) {
+    $basenumber = null;
+    $newparams = array();
+    $newsql = preg_replace_callback('~:' . preg_quote($paramprefix, '~') . '(\d+)\b~',
+            function($match) use ($paramprefix, $params, &$newparams, &$basenumber) {
+                if ($basenumber === null) {
+                    $basenumber = $match[1] - 1;
+                }
+                $oldname = $paramprefix . $match[1];
+                $newname = $paramprefix . ($match[1] - $basenumber);
+                $newparams[$newname] = $params[$oldname];
+                return ':' . $newname;
+            }, $sql);
+
+    return array($newsql, $newparams);
+}
+
 /**
  * Return a {@link qubaid_condition} from the values returned by {@link quiz_statistics_attempts_sql}.
  *
diff --git a/mod/quiz/report/statistics/tests/statisticslib_test.php b/mod/quiz/report/statistics/tests/statisticslib_test.php
new file mode 100644 (file)
index 0000000..edfb33b
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Unit tests for (some of) statisticslib.php.
+ *
+ * @package   quiz_statistics
+ * @category  test
+ * @copyright 2014 The Open University
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
+
+/**
+ * Unit tests for (some of) statisticslib.php.
+ *
+ * @copyright 2014 The Open University
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class quiz_statistics_statisticslib_testcase extends basic_testcase {
+
+    public function test_quiz_statistics_renumber_placeholders_no_op() {
+        list($sql, $params) = quiz_statistics_renumber_placeholders(
+                ' IN (:u1, :u2)', array('u1' => 1, 'u2' => 2), 'u');
+        $this->assertEquals(' IN (:u1, :u2)', $sql);
+        $this->assertEquals(array('u1' => 1, 'u2' => 2), $params);
+    }
+
+    public function test_quiz_statistics_renumber_placeholders_work_to_do() {
+        list($sql, $params) = quiz_statistics_renumber_placeholders(
+                'frog1 IN (:frog100 , :frog101)', array('frog100' => 1, 'frog101' => 2), 'frog');
+        $this->assertEquals('frog1 IN (:frog1 , :frog2)', $sql);
+        $this->assertEquals(array('frog1' => 1, 'frog2' => 2), $params);
+    }
+}