From: moodler Date: Mon, 27 Oct 2003 12:38:00 +0000 (+0000) Subject: Added a new column to user_students and user_teachers called "timeaccess" X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4d744a220ac8d984c874f281d2406af9fec87fcb;p=moodle.git Added a new column to user_students and user_teachers called "timeaccess" for recording the last access to the COURSE. This is updated at the same time as add_to_log and is now used instead of user->lastaccess when course user listings are required. This means course listings now show what you expect and open up the way for a "current users" listing and instant messaging etc ... --- diff --git a/course/lib.php b/course/lib.php index afee28df2b..384ccb8649 100644 --- a/course/lib.php +++ b/course/lib.php @@ -29,7 +29,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate="today" $users = array(); if ($course->category) { - $courseusers = get_course_users($course->id, "u.lastaccess DESC"); + $courseusers = get_course_users($course->id); } else { $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname"); } diff --git a/lib/datalib.php b/lib/datalib.php index 8baa197b70..3e7512e444 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1110,11 +1110,9 @@ function get_recent_enrolments($courseid, $timestart) { /** * Returns list of all students in this course * -* if courseid = 0 then return ALL students in all courses -* * @param type description */ -function get_course_students($courseid, $sort="u.lastaccess", $dir="", $page=0, $recordsperpage=99999) { +function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0, $recordsperpage=99999) { global $CFG; @@ -1130,7 +1128,7 @@ function get_course_students($courseid, $sort="u.lastaccess", $dir="", $page=0, } return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, - u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture + u.email, u.city, u.country, u.lastlogin, u.picture, s.timeaccess as lastaccess FROM {$CFG->prefix}user u, {$CFG->prefix}user_students s WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' @@ -1140,15 +1138,15 @@ function get_course_students($courseid, $sort="u.lastaccess", $dir="", $page=0, /** * Returns list of all teachers in this course * -* if courseid = 0 then return ALL teachers in all courses -* * @param type description */ function get_course_teachers($courseid, $sort="t.authority ASC") { global $CFG; - return get_records_sql("SELECT u.*,t.authority,t.role,t.editall + return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, + u.email, u.city, u.country, u.lastlogin, u.picture, + t.authority,t.role,t.editall,t.timeaccess as lastaccess FROM {$CFG->prefix}user u, {$CFG->prefix}user_teachers t WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' @@ -1162,7 +1160,7 @@ function get_course_teachers($courseid, $sort="t.authority ASC") { * * @param type description */ -function get_course_users($courseid, $sort="u.lastaccess DESC") { +function get_course_users($courseid, $sort="timeaccess DESC") { $site = get_site(); @@ -1884,7 +1882,7 @@ function instance_is_visible($moduletype, $module) { * @param string $url the file and parameters used to see the results of the action * @param string $info additional description information */ -function add_to_log($course, $module, $action, $url="", $info="") { +function add_to_log($courseid, $module, $action, $url="", $info="") { global $db, $CFG, $USER, $REMOTE_ADDR; @@ -1907,7 +1905,7 @@ function add_to_log($course, $module, $action, $url="", $info="") { info) VALUES ('$timenow', '$userid', - '$course', + '$courseid', '$REMOTE_ADDR', '$module', '$action', @@ -1917,6 +1915,16 @@ function add_to_log($course, $module, $action, $url="", $info="") { if (!$result and ($CFG->debug > 7)) { echo "

Error: Could not insert a new entry to the Moodle log

"; // Don't throw an error } + + if (isstudent($courseid)) { + $db->Execute("UPDATE {$CFG->prefix}user_students SET timeaccess = '$timenow' ". + "WHERE course = '$courseid' AND userid = '$userid'"); + } + + if (isteacher($courseid, false, false)) { + $db->Execute("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$timenow' ". + "WHERE course = '$courseid' AND userid = '$userid'"); + } } diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 3152c56f18..5c8b496270 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -19,7 +19,7 @@ function main_upgrade($oldversion=0) { - global $CFG, $THEME; + global $CFG, $THEME, $db; $result = true; @@ -509,6 +509,20 @@ function main_upgrade($oldversion=0) { table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); } + if ($oldversion < 2003102700) { + 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); + } + $db->debug = true; + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index d9da0b3d58..47cfb27e37 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -262,6 +262,7 @@ CREATE TABLE `prefix_user_students` ( `timestart` int(10) unsigned NOT NULL default '0', `timeend` int(10) unsigned NOT NULL default '0', `time` int(10) unsigned NOT NULL default '0', + `timeaccess` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), KEY `courseuserid` (course,userid) @@ -280,6 +281,7 @@ CREATE TABLE `prefix_user_teachers` ( `role` varchar(40) NOT NULL default '', `editall` int(1) unsigned NOT NULL default '1', `timemodified` int(10) unsigned NOT NULL default '0', + `timeaccess` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), KEY `courseuserid` (course,userid) diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index e7e09b7ce1..9dc4ec9544 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -19,7 +19,7 @@ function main_upgrade($oldversion=0) { - global $CFG, $THEME; + global $CFG, $THEME, $db; $result = true; @@ -258,6 +258,21 @@ function main_upgrade($oldversion=0) { table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); } + if ($oldversion < 2003102700) { + 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); + } + $db->debug = true; + } + + return $result; } ?> diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 30a5f4141e..e7a1b49c24 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -167,7 +167,8 @@ CREATE TABLE prefix_user_students ( course integer NOT NULL default '0', timestart integer NOT NULL default '0', timeend integer NOT NULL default '0', - time integer NOT NULL default '0' + time integer NOT NULL default '0', + timeaccess integer NOT NULL default '0' ); CREATE INDEX prefix_user_students_courseuserid_idx ON prefix_user_students (course,userid); @@ -179,7 +180,8 @@ CREATE TABLE prefix_user_teachers ( authority integer NOT NULL default '3', role varchar(40) NOT NULL default '', editall integer NOT NULL default '1', - timemodified integer NOT NULL default '0' + timemodified integer NOT NULL default '0', + timeaccess integer NOT NULL default '0' ); CREATE INDEX prefix_user_teachers_courseuserid_idx ON prefix_user_teachers (course,userid); diff --git a/user/index.php b/user/index.php index 4ce1a9c556..970e9ccdbb 100644 --- a/user/index.php +++ b/user/index.php @@ -72,7 +72,11 @@ } } - $dsort = "u.$sort"; + if ($sort = lastaccess) { + $dsort = "s.timeaccess"; + } else { + $dsort = "u.$sort"; + } $totalcount = count_records("user_students", "course", $course->id); diff --git a/version.php b/version.php index aca3bf61a2..4dfd7d8e73 100644 --- a/version.php +++ b/version.php @@ -5,7 +5,7 @@ // database to determine whether upgrades should // be performed (see lib/db/*.php) -$version = 2003102200; // The current version is a date (YYYYMMDDXX) +$version = 2003102700; // The current version is a date (YYYYMMDDXX) $release = "1.2 development"; // User-friendly version number