MDL-37250 mod_lesson: All actual attempts on the lesson are displayed.
authorAdrian Greeve <adrian@moodle.com>
Tue, 21 Jun 2016 06:50:33 +0000 (14:50 +0800)
committerAdrian Greeve <adrian@moodle.com>
Mon, 11 Jul 2016 00:18:41 +0000 (08:18 +0800)
The lesson overview report now shows all attempts made including hitting
the page and immediately navigating away.

mod/lesson/report.php

index 4f4a3b3..9273e55 100644 (file)
@@ -123,7 +123,8 @@ if ($action === 'delete') {
     // Only load students if there attempts for this lesson.
     $attempts = $DB->record_exists('lesson_attempts', array('lessonid' => $lesson->id));
     $branches = $DB->record_exists('lesson_branch', array('lessonid' => $lesson->id));
-    if ($attempts or $branches) {
+    $timer = $DB->record_exists('lesson_timer', array('lessonid' => $lesson->id));
+    if ($attempts or $branches or $timer) {
         list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
         list($sort, $sortparams) = users_order_by_sql('u');
 
@@ -131,8 +132,13 @@ if ($action === 'delete') {
         $ufields = user_picture::fields('u');
         $sql = "SELECT DISTINCT $ufields
                 FROM {user} u
-                JOIN (SELECT userid, lessonid FROM {lesson_attempts} a1 UNION
-                SELECT userid, lessonid FROM {lesson_branch} b1) a ON u.id = a.userid
+                JOIN (
+                    SELECT userid, lessonid FROM {lesson_attempts} a1
+                        UNION
+                    SELECT userid, lessonid FROM {lesson_branch} b1
+                        UNION
+                    SELECT userid, lessonid FROM {lesson_timer} c1
+                    ) a ON u.id = a.userid
                 JOIN ($esql) ue ON ue.id = a.userid
                 WHERE a.lessonid = :lessonid
                 ORDER BY $sort";
@@ -271,6 +277,43 @@ if ($action === 'delete') {
     }
     $branches->close();
 
+    // Need the same thing for timed entries that were not completed.
+    foreach ($times as $time) {
+        $endoflesson = $time->completed;
+        // If the time start is the same with another record then we shouldn't be adding another item to this array.
+        if (isset($studentdata[$time->userid])) {
+            $foundmatch = false;
+            $n = 0;
+            foreach ($studentdata[$time->userid] as $key => $value) {
+                if ($value['timestart'] == $time->starttime) {
+                    // Don't add this to the array.
+                    $foundmatch = true;
+                    break;
+                }
+            }
+            $n = count($studentdata[$time->userid]) + 1;
+            if (!$foundmatch) {
+                // Add a record.
+                $studentdata[$time->userid][] = array(
+                                "timestart" => $time->starttime,
+                                "timeend" => $time->lessontime,
+                                "grade" => null,
+                                "end" => $endoflesson,
+                                "try" => $n,
+                                "userid" => $time->userid
+                            );
+            }
+        } else {
+            $studentdata[$time->userid][] = array(
+                                "timestart" => $time->starttime,
+                                "timeend" => $time->lessontime,
+                                "grade" => null,
+                                "end" => $endoflesson,
+                                "try" => 0,
+                                "userid" => $time->userid
+                            );
+        }
+    }
     // Determine if lesson should have a score.
     if ($branchcount > 0 AND $questioncount == 0) {
         // This lesson only contains content pages and is not graded.