diff --git a/mod/quiz/report/statistics/statisticslib.php b/mod/quiz/report/statistics/statisticslib.php index b66b83db7bc..26539790985 100644 --- a/mod/quiz/report/statistics/statisticslib.php +++ b/mod/quiz/report/statistics/statisticslib.php @@ -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 index 00000000000..edfb33bf205 --- /dev/null +++ b/mod/quiz/report/statistics/tests/statisticslib_test.php @@ -0,0 +1,52 @@ +. + +/** + * 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); + } +}