]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14471 - when attempt data is generated by a script then sometimes quiz_get_latest...
authorjamiesensei <jamiesensei>
Wed, 23 Apr 2008 06:45:11 +0000 (06:45 +0000)
committerjamiesensei <jamiesensei>
Wed, 23 Apr 2008 06:45:11 +0000 (06:45 +0000)
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

index 3de18bf029ef65e744ad86e00156694ea8df4532..5e537dcd6f24b13e33e5139702ab75b5b2f9137d 100644 (file)
@@ -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;
+    }
 }
 
 /**