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 ...
This commit is contained in:
moodler 2003-10-27 12:38:00 +00:00
parent ecd25101f5
commit 4d744a220a
8 changed files with 62 additions and 17 deletions

View file

@ -29,7 +29,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate="today"
$users = array(); $users = array();
if ($course->category) { if ($course->category) {
$courseusers = get_course_users($course->id, "u.lastaccess DESC"); $courseusers = get_course_users($course->id);
} else { } else {
$courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname"); $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname");
} }

View file

@ -1110,11 +1110,9 @@ function get_recent_enrolments($courseid, $timestart) {
/** /**
* Returns list of all students in this course * Returns list of all students in this course
* *
* if courseid = 0 then return ALL students in all courses
*
* @param type description * @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; 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, 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, FROM {$CFG->prefix}user u,
{$CFG->prefix}user_students s {$CFG->prefix}user_students s
WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' 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 * Returns list of all teachers in this course
* *
* if courseid = 0 then return ALL teachers in all courses
*
* @param type description * @param type description
*/ */
function get_course_teachers($courseid, $sort="t.authority ASC") { function get_course_teachers($courseid, $sort="t.authority ASC") {
global $CFG; 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, FROM {$CFG->prefix}user u,
{$CFG->prefix}user_teachers t {$CFG->prefix}user_teachers t
WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' 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 * @param type description
*/ */
function get_course_users($courseid, $sort="u.lastaccess DESC") { function get_course_users($courseid, $sort="timeaccess DESC") {
$site = get_site(); $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 $url the file and parameters used to see the results of the action
* @param string $info additional description information * @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; global $db, $CFG, $USER, $REMOTE_ADDR;
@ -1907,7 +1905,7 @@ function add_to_log($course, $module, $action, $url="", $info="") {
info) info)
VALUES ('$timenow', VALUES ('$timenow',
'$userid', '$userid',
'$course', '$courseid',
'$REMOTE_ADDR', '$REMOTE_ADDR',
'$module', '$module',
'$action', '$action',
@ -1917,6 +1915,16 @@ function add_to_log($course, $module, $action, $url="", $info="") {
if (!$result and ($CFG->debug > 7)) { if (!$result and ($CFG->debug > 7)) {
echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // Don't throw an error echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // 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'");
}
} }

View file

@ -19,7 +19,7 @@
function main_upgrade($oldversion=0) { function main_upgrade($oldversion=0) {
global $CFG, $THEME; global $CFG, $THEME, $db;
$result = true; $result = true;
@ -509,6 +509,20 @@ function main_upgrade($oldversion=0) {
table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); 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; return $result;
} }

View file

@ -262,6 +262,7 @@ CREATE TABLE `prefix_user_students` (
`timestart` int(10) unsigned NOT NULL default '0', `timestart` int(10) unsigned NOT NULL default '0',
`timeend` int(10) unsigned NOT NULL default '0', `timeend` int(10) unsigned NOT NULL default '0',
`time` 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`), PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`), UNIQUE KEY `id` (`id`),
KEY `courseuserid` (course,userid) KEY `courseuserid` (course,userid)
@ -280,6 +281,7 @@ CREATE TABLE `prefix_user_teachers` (
`role` varchar(40) NOT NULL default '', `role` varchar(40) NOT NULL default '',
`editall` int(1) unsigned NOT NULL default '1', `editall` int(1) unsigned NOT NULL default '1',
`timemodified` int(10) unsigned NOT NULL default '0', `timemodified` int(10) unsigned NOT NULL default '0',
`timeaccess` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`), UNIQUE KEY `id` (`id`),
KEY `courseuserid` (course,userid) KEY `courseuserid` (course,userid)

View file

@ -19,7 +19,7 @@
function main_upgrade($oldversion=0) { function main_upgrade($oldversion=0) {
global $CFG, $THEME; global $CFG, $THEME, $db;
$result = true; $result = true;
@ -258,6 +258,21 @@ function main_upgrade($oldversion=0) {
table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); 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; return $result;
} }
?> ?>

View file

@ -167,7 +167,8 @@ CREATE TABLE prefix_user_students (
course integer NOT NULL default '0', course integer NOT NULL default '0',
timestart integer NOT NULL default '0', timestart integer NOT NULL default '0',
timeend 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); 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', authority integer NOT NULL default '3',
role varchar(40) NOT NULL default '', role varchar(40) NOT NULL default '',
editall integer NOT NULL default '1', 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); CREATE INDEX prefix_user_teachers_courseuserid_idx ON prefix_user_teachers (course,userid);

View file

@ -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); $totalcount = count_records("user_students", "course", $course->id);

View file

@ -5,7 +5,7 @@
// database to determine whether upgrades should // database to determine whether upgrades should
// be performed (see lib/db/*.php) // 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 $release = "1.2 development"; // User-friendly version number