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;
}
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}.
*
--- /dev/null
+<?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);
+ }
+}