Browse files

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

…OODLE_20_STABLE
  • Loading branch information...
2 parents 31bb03f + d5d16e1 commit 96083d8068751732110e21851ddbdc1fc066dc8b 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
@@ -3417,6 +3417,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
@@ -459,7 +459,7 @@ function quiz_stats($nostudentsingroup, $quizid, $currentgroup, $groupstudents,
$sql = 'SELECT (CASE WHEN attempt=1 THEN 1 ELSE 0 END) AS isfirst, COUNT(1) AS countrecs, SUM(sumgrades) AS total ' .
'FROM '.$fromqa.
'WHERE ' .$whereqa.
- 'GROUP BY (attempt=1)';
+ 'GROUP BY CASE WHEN attempt=1 THEN 1 ELSE 0 END';
if (!$attempttotals = $DB->get_records_sql($sql, $qaparams)){
$s = 0;

0 comments on commit 96083d8

Please sign in to comment.