]> git.mjollnir.org Git - moodle.git/commitdiff
Much better updating of access times to make the accurate straight away
authormoodler <moodler>
Mon, 27 Oct 2003 13:38:08 +0000 (13:38 +0000)
committermoodler <moodler>
Mon, 27 Oct 2003 13:38:08 +0000 (13:38 +0000)
lib/db/mysql.php
lib/db/postgres7.php

index 5c8b496270db13a8a4ce181d9689b4e164e4936d..3d06b99aef62b0638cef82b58b7c2abb8685be6a 100644 (file)
@@ -513,13 +513,46 @@ function main_upgrade($oldversion=0) {
         table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
         table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
 
-        $users = get_records_select("user", "id > 0", "", "id, lastaccess");
-
         $db->debug = false;
-        foreach ($users as $user) {
-            execute_sql("UPDATE {$CFG->prefix}user_students SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
-            execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
+        $CFG->debug = 0;
+        notify("Calculating access times.  Please wait - this may take a long time on big sites...", "green");
+        flush();
+
+        if ($courses = get_records_select("course", "category > 0")) {
+            foreach ($courses as $course) {
+                notify("Processing $course->fullname ...", "green");
+                flush();
+                if ($users = get_records_select("user_teachers", "course = '$course->id'", 
+                                                "id", "id, userid, timeaccess")) {
+                    foreach ($users as $user) {
+                        $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log                                                                                  WHERE course = '$course->id' and userid = '$user->userid'                                                               ORDER by time DESC");
+                        if (empty($loginfo->time)) {
+                            $loginfo->time = 0;
+                        }
+                        execute_sql("UPDATE {$CFG->prefix}user_teachers                                                                                      SET timeaccess = '$loginfo->time' 
+                                     WHERE userid = '$user->userid' AND course = '$course->id'", false);
+                        
+                    }
+                }
+
+                if ($users = get_records_select("user_students", "course = '$course->id'", 
+                                                "id", "id, userid, timeaccess")) {
+                    foreach ($users as $user) {
+                        $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log 
+                                                   WHERE course = '$course->id' and userid = '$user->userid' 
+                                                   ORDER by time DESC");
+                        if (empty($loginfo->time)) {
+                            $loginfo->time = 0;
+                        }
+                        execute_sql("UPDATE {$CFG->prefix}user_students 
+                                     SET timeaccess = '$loginfo->time' 
+                                     WHERE userid = '$user->userid' AND course = '$course->id'", false);
+                        
+                    }
+                }
+            }
         }
+        notify("All courses complete.", "green");
         $db->debug = true;
     }
 
index 9dc4ec954428d673f74bac3a775ef1be75995299..92783c14fe670304c7e8c7996bcf475ffc07968d 100644 (file)
@@ -262,13 +262,46 @@ function main_upgrade($oldversion=0) {
         table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
         table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
 
-        $users = get_records_select("user", "id > 0", "", "id, lastaccess");
-
         $db->debug = false;
-        foreach ($users as $user) {
-            execute_sql("UPDATE {$CFG->prefix}user_students SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
-            execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
+        $CFG->debug = 0;
+        notify("Calculating access times.  Please wait - this may take a long time on big sites...", "green");
+        flush();
+
+        if ($courses = get_records_select("course", "category > 0")) {
+            foreach ($courses as $course) {
+                notify("Processing $course->fullname ...", "green");
+                flush();
+                if ($users = get_records_select("user_teachers", "course = '$course->id'", 
+                                                "id", "id, userid, timeaccess")) {
+                    foreach ($users as $user) {
+                        $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log                                                                                  WHERE course = '$course->id' and userid = '$user->userid'                                                               ORDER by time DESC");
+                        if (empty($loginfo->time)) {
+                            $loginfo->time = 0;
+                        }
+                        execute_sql("UPDATE {$CFG->prefix}user_teachers                                                                                      SET timeaccess = '$loginfo->time' 
+                                     WHERE userid = '$user->userid' AND course = '$course->id'", false);
+                        
+                    }
+                }
+
+                if ($users = get_records_select("user_students", "course = '$course->id'", 
+                                                "id", "id, userid, timeaccess")) {
+                    foreach ($users as $user) {
+                        $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log 
+                                                   WHERE course = '$course->id' and userid = '$user->userid' 
+                                                   ORDER by time DESC");
+                        if (empty($loginfo->time)) {
+                            $loginfo->time = 0;
+                        }
+                        execute_sql("UPDATE {$CFG->prefix}user_students 
+                                     SET timeaccess = '$loginfo->time' 
+                                     WHERE userid = '$user->userid' AND course = '$course->id'", false);
+                        
+                    }
+                }
+            }
         }
+        notify("All courses complete.", "green");
         $db->debug = true;
     }