MDL-47618 quiz statistics: fatal error with groups.

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.
This commit is contained in:
Tim Hunt 2014-10-10 15:17:10 +01:00
parent 683d7de4d1
commit bae1d7d9e8
2 changed files with 80 additions and 1 deletions

View 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}.
*

View File

@ -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);
}
}