From 29b25b04988a7ea8e0716105594926be8d9180f6 Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 27 Oct 2003 13:38:08 +0000 Subject: [PATCH] Much better updating of access times to make the accurate straight away --- lib/db/mysql.php | 43 ++++++++++++++++++++++++++++++++++++++----- lib/db/postgres7.php | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 5c8b496270..3d06b99aef 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -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; } diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index 9dc4ec9544..92783c14fe 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -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; } -- 2.39.5