From 58ffdbb883f24f18b37243d0486fc9a019a01dd1 Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Wed, 23 Apr 2008 06:45:11 +0000 Subject: [PATCH] MDL-14471 - when attempt data is generated by a script then sometimes quiz_get_latest_attempt_by_user fetches the wrong attempt since timestart for the attempt is not a second apart from the last attempt - "patch to quiz/locallib.php function quiz_get_latest_attempt_by_user which makes the query simpler and also makes it more robust. In the case that there are two attempts started in the same second, which can happen if you are generating random attempt data then the id field is used to tell which is the later attempt." --- mod/quiz/locallib.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 3de18bf029..5e537dcd6f 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -137,14 +137,18 @@ function quiz_get_user_attempt_unfinished($quizid, $userid) { * @param integer $quizid the id of the quiz. * @param integer $userid the id of the user. * - * @return mixed the unfinished attempt if there is one, false if not. + * @return mixed the attempt if there is one, false if not. */ function quiz_get_latest_attempt_by_user($quizid, $userid) { global $CFG; - return get_record_sql('SELECT qa.* FROM ' . $CFG->prefix . 'quiz_attempts qa - WHERE qa.quiz=' . $quizid . ' AND qa.userid=' . $userid . ' AND qa.timestart = ( - SELECT MAX(timestart) FROM ' . $CFG->prefix . 'quiz_attempts ssqa - WHERE ssqa.quiz=' . $quizid . ' AND ssqa.userid=' . $userid . ')'); + $attempt = get_records_sql('SELECT qa.* FROM ' . $CFG->prefix . 'quiz_attempts qa + WHERE qa.quiz=' . $quizid . ' AND qa.userid=' . $userid . + ' ORDER BY qa.timestart DESC, qa.id DESC', 0, 1); + if ($attempt) { + return array_shift($attempt); + } else { + return false; + } } /** -- 2.39.5