Browse files

Merge branch 'MDL-26819_21' of git://github.com/stronk7/moodle into M…

…OODLE_21_STABLE
  • Loading branch information...
2 parents 6870b5d + c24ccde commit a4ad64950b4d44ff0238a79104924f2def15feaf Sam Hemelryk committed Aug 29, 2011
Showing with 18 additions and 1 deletion.
  1. +17 −0 lib/dml/simpletest/testdml.php
  2. +1 −1 mod/quiz/report/statistics/report.php
View
17 lib/dml/simpletest/testdml.php
@@ -3497,6 +3497,23 @@ public function test_get_records_sql_complicated() {
$DB->insert_record($tablename, array('course' => 5, 'content' => 'hello', 'name'=>'def'));
$DB->insert_record($tablename, array('course' => 2, 'content' => 'universe', 'name'=>'abc'));
+ // test grouping by expressions in the query. MDL-26819. Note that there are 4 ways:
+ // - By column position (GROUP by 1) - Not supported by mssql & oracle
+ // - By column name (GROUP by course) - Supported by all, but leading to wrong results
+ // - By column alias (GROUP by casecol) - Not supported by mssql & oracle
+ // - By complete expression (GROUP BY CASE ...) - 100% cross-db, this test checks it
+ $sql = "SELECT (CASE WHEN course = 3 THEN 1 ELSE 0 END) AS casecol,
+ COUNT(1) AS countrecs,
+ MAX(name) AS maxname
+ FROM {{$tablename}}
+ GROUP BY CASE WHEN course = 3 THEN 1 ELSE 0 END
+ ORDER BY casecol DESC";
+ $result = array(
+ 1 => (object)array('casecol' => 1, 'countrecs' => 2, 'maxname' => 'xyz'),
+ 0 => (object)array('casecol' => 0, 'countrecs' => 2, 'maxname' => 'def'));
+ $records = $DB->get_records_sql($sql, null);
+ $this->assertEqual($result, $records);
+
// test limits in queries with DISTINCT/ALL clauses and multiple whitespace. MDL-25268
$sql = "SELECT DISTINCT course
FROM {{$tablename}}
View
2 mod/quiz/report/statistics/report.php
@@ -620,7 +620,7 @@ protected function compute_stats($quizid, $currentgroup, $nostudentsingroup,
SUM(sumgrades) AS total
FROM $fromqa
WHERE $whereqa
- GROUP BY attempt = 1", $qaparams);
+ GROUP BY CASE WHEN attempt = 1 THEN 1 ELSE 0 END", $qaparams);
if (!$attempttotals) {
return $this->get_emtpy_stats($questions);

0 comments on commit a4ad649

Please sign in to comment.