Skip to content

Commit

Permalink
mod/glossary, mod/hotpot: use sql_concat() and sql_concat_join()
Browse files Browse the repository at this point in the history
Cleaned up several dbtype conditionals too.
  • Loading branch information
martinlanghoff committed Sep 26, 2006
1 parent 38e02f4 commit d2715c7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions mod/glossary/sql.php
Expand Up @@ -92,9 +92,9 @@
break; break;
case 'mysql': case 'mysql':
if ( $sqlsortkey == 'FIRSTNAME' ) { if ( $sqlsortkey == 'FIRSTNAME' ) {
$usernamefield = "CONCAT(CONCAT(u.firstname,' '), u.lastname)"; $usernamefield = sql_fullname('u.firstname' , 'u.lastname');
} else { } else {
$usernamefield = "CONCAT(CONCAT(u.lastname,' '), u.firstname)"; $usernamefield = sql_fullname('u.lastname' , 'u.firsttname');
} }
$where = "AND left(ucase($usernamefield)," . $textlib->strlen($hook, current_charset()) . ") = '$hook'"; $where = "AND left(ucase($usernamefield)," . $textlib->strlen($hook, current_charset()) . ") = '$hook'";
break; break;
Expand Down
10 changes: 2 additions & 8 deletions mod/hotpot/index.php
Expand Up @@ -203,14 +203,8 @@
$questions = false; $questions = false;
$regradehotpots = array(); $regradehotpots = array();


switch (strtolower($CFG->dbtype)) { $field = sql_concat('hotpot', "'_'", 'name');
case 'mysql' :
$field = "CONCAT(hotpot, '_', name)";
break;
case 'postgres7' :
$field = "hotpot||'_'||name";
break;
}
if ($field) { if ($field) {
$questions = get_records_sql(" $questions = get_records_sql("
SELECT $field, COUNT(*), hotpot, name SELECT $field, COUNT(*), hotpot, name
Expand Down
22 changes: 18 additions & 4 deletions mod/hotpot/lib.php
Expand Up @@ -1089,17 +1089,31 @@ function hotpot_get_grades($hotpot, $user_ids='') {
break; break;
case HOTPOT_GRADEMETHOD_FIRST: case HOTPOT_GRADEMETHOD_FIRST:
if ($CFG->dbtype=='postgres7') { if ($CFG->dbtype=='postgres7') {
$grade = "MIN(timestart||'_'||(CASE WHEN (score IS NULL) THEN '' ELSE TRIM(ROUND(score * $weighting, $precision)) END)) AS grade"; $grade = "(CASE WHEN (score IS NULL)
THEN ''
ELSE TRIM(ROUND(score * $weighting, $precision))
END)";
} else { } else {
$grade = "MIN(CONCAT(timestart, '_', IF(score IS NULL, '', ROUND(score * $weighting, $precision)))) AS grade"; $grade = "IF(score IS NULL,
'',
ROUND(score * $weighting, $precision))";
} }
$grade = sql_concat('timestart', "'_'", $grade);
$grade = "MIN($grade) AS grade";
break; break;
case HOTPOT_GRADEMETHOD_LAST: case HOTPOT_GRADEMETHOD_LAST:
if ($CFG->dbtype=='postgres7') { if ($CFG->dbtype=='postgres7') {
$grade = "MAX(timestart||'_'||(CASE WHEN (score IS NULL) THEN '' ELSE TRIM(ROUND(score * $weighting, $precision)) END)) AS grade"; $grade = "(CASE WHEN (score IS NULL)
THEN ''
ELSE TRIM(ROUND(score * $weighting, $precision))
END)";
} else { } else {
$grade = "MAX(CONCAT(timestart, '_', IF(score IS NULL, '', ROUND(score * $weighting, $precision)))) AS grade"; $grade = "IF(score IS NULL,
'',
ROUND(score * $weighting, $precision))";
} }
$grade = sql_concat('timestart', "'_'", $grade);
$grade = "MAX($grade) AS grade";
break; break;
} }


Expand Down
14 changes: 2 additions & 12 deletions mod/hotpot/report.php
Expand Up @@ -594,18 +594,8 @@ function hotpot_get_records_groupby($function, $fieldnames, $table, $select, $gr
// $function is an SQL aggregate function (MAX or MIN) // $function is an SQL aggregate function (MAX or MIN)


global $CFG; global $CFG;

$fields = sql_concat_join("'_'", $fieldnames);
switch (strtolower($CFG->dbtype)) { $fields = "$groupby, $function($fields) AS joinedvalues";
case 'mysql':
$fields = "$groupby, $function(CONCAT(".join(",'_',", $fieldnames).")) AS joinedvalues";
break;
case 'postgres7':
$fields = "$groupby, $function(".join("||'_'||", $fieldnames).") AS joinedvalues";
break;
default:
$fields = "";
break;
}


if ($fields) { if ($fields) {
$records = get_records_sql("SELECT $fields FROM $table WHERE $select GROUP BY $groupby"); $records = get_records_sql("SELECT $fields FROM $table WHERE $select GROUP BY $groupby");
Expand Down

0 comments on commit d2715c7

Please sign in to comment.