Skip to content

Commit

Permalink
Fixed invalid use of GROUP BY and (mostly) fixed sorting by question …
Browse files Browse the repository at this point in the history
…grade.

There was a compatibility break before/after the Big Quiz Refactor it seems
that won't allow before/after attempts to coexist in the same table sorted
as they should. Will open a bug.
  • Loading branch information
defacer committed May 22, 2005
1 parent 53d23b3 commit 254e5a2
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions mod/quiz/attempts.php
Expand Up @@ -220,43 +220,50 @@
// Start working -- this is necessary as soon as the niceties are over
$table->setup();

// Construct the SQL

/// Construct the SQL
$select = 'SELECT qa.id AS attempt, u.id AS userid, u.firstname, u.lastname, u.picture, '.
'qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
$from = 'FROM mdl_user u LEFT JOIN mdl_quiz_attempts qa ON u.id = qa.userid ';
$where = 'WHERE u.id IN ('.implode(',', array_keys($users)).') AND ('.($noattempts ? sql_isnull('qa.quiz').' OR ' : '') . 'qa.quiz = '.$quiz->id.') ';

if($where = $table->get_sql_where()) {
$where .= ' AND ';
// Count the records NOW, before funky question grade sorting messes up $from
$total = count_records_sql('SELECT COUNT(DISTINCT('.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from);


// Add extra limits due to initials bar
if($table->get_sql_where()) {
$where .= ' AND '.$table->get_sql_where();
}


// Add extra limits due to sorting by question grade
if($sort = $table->get_sql_sort()) {
$sortparts = explode(',', $sort);
$newsort = array();
$firsttime = true;
$sortparts = explode(',', $sort);
$newsort = array();
$questionsort = false;
foreach($sortparts as $sortpart) {
$sortpart = trim($sortpart);
if(substr($sortpart, 0, 1) == '$') {
if($firsttime) {
$qnum = intval(substr($sortpart, 1));
$where .= '('.sql_isnull('qr.question').' OR qr.question = '.$qnum.') AND ';
$newsort[] = 'grade '.(strpos($sortpart, 'ASC')? 'ASC' : 'DESC');
$firsttime = false;
if(!$questionsort) {
$qid = intval(substr($sortpart, 1));
$select .= ', grade ';
$from .= 'LEFT JOIN mdl_quiz_newest_states qns ON qns.attemptid = qa.id '.
'LEFT JOIN mdl_quiz_states qs ON qs.id = qns.newgraded ';
$where .= ' AND ('.sql_isnull('qns.questionid').' OR qns.questionid = '.$qid.')';
$newsort[] = 'grade '.(strpos($sortpart, 'ASC')? 'ASC' : 'DESC');
$questionsort = true;
}
}
else {
$newsort[] = $sortpart;
}
}

// Reconstruct the sort string
$sort = ' ORDER BY '.implode(', ', $newsort);
}

$select = 'SELECT '.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS uvsa, u.id AS userid, u.firstname, u.lastname, u.picture, qa.id AS attempt, qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
$group = 'GROUP BY uvsa';
$sql = 'FROM '.$CFG->prefix.'user u '.
'LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid '.
'LEFT JOIN '.$CFG->prefix.'quiz_states qr ON qr.attempt = qa.id '.
'WHERE '.$where.'u.id IN ('.implode(',', array_keys($users)).') AND ('.($noattempts ? sql_isnull('qa.quiz').' OR ' : '') . 'qa.quiz = '.$quiz->id.') ';


$total = count_records_sql('SELECT COUNT(DISTINCT('.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$sql);
$table->pagesize(10, $total);

if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
Expand All @@ -268,7 +275,7 @@

/// Fetch the attempts

$attempts = get_records_sql($select.$sql.$group.$sort.$limit);
$attempts = get_records_sql($select.$from.$where.$sort.$limit);

/// Build table rows

Expand Down

0 comments on commit 254e5a2

Please sign in to comment.