From 5ed4dd5a76f687c460c99c3104b83702fafd48ed Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Tue, 11 Oct 2005 01:20:37 +0000 Subject: [PATCH] Stats: many bug fixes found while running over a very large dataset... --- course/stats.php | 3 ++- lib/statslib.php | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/course/stats.php b/course/stats.php index 495537c0f9a..c2b37f91e5e 100644 --- a/course/stats.php +++ b/course/stats.php @@ -95,8 +95,9 @@ $sql = 'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber,u.nickname FROM '.$CFG->prefix.'stats_user_'.$param->table.' s JOIN '.$CFG->prefix.'user u ON u.id = s.userid ' .'WHERE courseid = '.$course->id.' AND timeend > '.$param->timeafter . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : ''); if (!isadmin()) { - $sql .= ' AND s.roleid = 1 OR s.userid = '.$USER->id; + $sql .= ' AND (s.roleid = 1 OR s.userid = '.$USER->id .")"; } + $sql .= " ORDER BY s.roleid "; } else { $sql = 'SELECT s.userid,u.firstname,u.lastname,u.idnumber,u.nickname,1 AS roleid FROM '.$CFG->prefix.'user_students s JOIN '.$CFG->prefix.'user u ON u.id = s.userid WHERE course = '.$course->id; } diff --git a/lib/statslib.php b/lib/statslib.php index 94da980e6cc..054e609ba79 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -160,10 +160,10 @@ function stats_cron_daily () { stats_get_course_users($course,$timesql,$students,$teachers); foreach ($students as $user) { - stats_do_daily_user_cron($course,$user,1,$timesql,$nextmidnight,'daily',$daily_modules); + stats_do_daily_user_cron($course,$user,1,$timesql,$nextmidnight,$daily_modules); } foreach ($teachers as $user) { - stats_do_daily_user_cron($course,$user,2,$timesql,$nextmidnight,'daily',$daily_modules); + stats_do_daily_user_cron($course,$user,2,$timesql,$nextmidnight,$daily_modules); } } $timestart = $nextmidnight; @@ -255,7 +255,7 @@ function stats_cron_weekly () { foreach (array_keys((array)$stat) as $key) { if (!isset($stat->$key)) { - $stat->key = 0; // we don't want nulls , so avoid them. + $stat->$key = 0; // we don't want nulls , so avoid them. } } @@ -345,6 +345,8 @@ function stats_cron_monthly () { $sql = 'SELECT ceil(avg(students)) as students, ceil(avg(teachers)) as teachers, ceil(avg(activestudents)) as activestudents,ceil(avg(activeteachers)) as activeteachers, sum(studentreads) as studentreads, sum(studentwrites) as studentwrites, sum(teacherreads) as teacherreads, sum(teacherwrites) as teacherwrites, sum(logins) as logins FROM '.$CFG->prefix.'stats_daily WHERE courseid = '.$course->id.' AND '.$timesql; + + $stat = get_record_sql($sql); $stat->uniquelogins = 0; if ($course->id == SITEID) { @@ -357,7 +359,7 @@ function stats_cron_monthly () { foreach (array_keys((array)$stat) as $key) { if (!isset($stat->$key)) { - $stat->key = 0; // we don't want nulls , so avoid them. + $stat->$key = 0; // we don't want nulls , so avoid them. } } @@ -635,7 +637,7 @@ function stats_do_aggregate_user_cron($course,$user,$roleid,$timesql,$timeend,$t $stat->timeend = $timeend; $sql = 'SELECT sum(reads) as reads, sum(writes) as writes FROM '.$CFG->prefix.'stats_user_daily WHERE courseid = '.$course->id.' AND '.$timesql - ." AND roleid=2 AND userid = ".$stat->userid." AND stattype='activity'"; // add on roleid in case they have teacher and student records. + ." AND roleid=".$roleid." AND userid = ".$stat->userid." AND stattype='activity'"; // add on roleid in case they have teacher and student records. $r = get_record_sql($sql); $stat->reads = (empty($r->reads)) ? 0 : $r->reads; @@ -645,7 +647,7 @@ function stats_do_aggregate_user_cron($course,$user,$roleid,$timesql,$timeend,$t if ($course->id == SITEID) { $sql = 'SELECT sum(reads) as reads, sum(writes) as writes FROM '.$CFG->prefix.'stats_user_daily WHERE courseid = '.$course->id.' AND '.$timesql - ." AND roleid=2 AND userid = ".$stat->userid." AND stattype='logins'"; // add on roleid in case they have teacher and student records. + ." AND roleid=".$roleid." AND userid = ".$stat->userid." AND stattype='logins'"; // add on roleid in case they have teacher and student records. $r = get_record_sql($sql); $stat->reads = (empty($r->reads)) ? 0 : $r->reads; @@ -751,7 +753,7 @@ function stats_fix_zeros($stats,$timeafter,$timestr,$line2=true,$line3=false) { $timestr = str_replace('user_','',$timestr); // just in case. $fun = 'stats_get_base_'.$timestr; - + $now = $fun(); $times = array();