diff --git a/course/stats.php b/course/stats.php index c2b37f91e5e..4d861159c85 100644 --- a/course/stats.php +++ b/course/stats.php @@ -91,9 +91,9 @@ if ($mode == STATS_MODE_DETAILED) { if (!empty($time)) { - $param = stats_get_parameters($time,null); // we only care about the table and the time string. + $param = stats_get_parameters($time,null,$course->id); // we only care about the table and the time string. $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.'\'' : ''); + .'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 .")"; } @@ -153,14 +153,18 @@ if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { error("This type of report is only available for the site course"); } - $param = stats_get_parameters($time,$report); + $param = stats_get_parameters($time,$report,$course->id); if ($mode == STATS_MODE_DETAILED) { $param->table = 'user_'.$param->table; } - $sql = 'SELECT timeend,'.$param->fields.',id FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE courseid = '.$course->id.((!empty($userid)) ? ' AND userid = '.$userid : '') - . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '') - .' AND timeend > '.$param->timeafter + $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE ' + .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') + .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') + . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') + .' timeend >= '.$param->timeafter + .$param->extras .' ORDER BY timeend DESC'; + $stats = get_records_sql($sql); if (empty($stats)) { diff --git a/course/statsgraph.php b/course/statsgraph.php index 3959c6c251d..029d2b6980b 100644 --- a/course/statsgraph.php +++ b/course/statsgraph.php @@ -26,15 +26,18 @@ stats_check_uptodate($course->id); - $param = stats_get_parameters($time,$report); + $param = stats_get_parameters($time,$report,$course->id); if (!empty($userid)) { $param->table = 'user_'.$param->table; } - $sql = 'SELECT timeend,'.$param->fields.',id FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE courseid = '.$course->id.((!empty($userid)) ? ' AND userid = '.$userid : '') - . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '') - .' AND timeend > '.$param->timeafter + $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE ' + . (($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') + . ((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') + . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') + .' timeend >= '.$param->timeafter + .$param->extras .' ORDER BY timeend DESC'; $stats = get_records_sql($sql); diff --git a/course/user.php b/course/user.php index 1906839701a..9bb48090c81 100644 --- a/course/user.php +++ b/course/user.php @@ -107,20 +107,30 @@ } // use the earliest. - $time = array_pop($timeoptions); - - echo '
'; + $time = array_pop(array_keys($timeoptions)); - $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW); + $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id); $param->table = 'user_'.$param->table; - $sql = 'SELECT timeend,'.$param->fields.',id FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE courseid = '.$course->id.' AND userid = '.$user->id + $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE ' + .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') + .' userid = '.$user->id .' AND stattype = \''.$param->stattype.'\'' - .' AND timeend > '.$param->timeafter + .' AND timeend >= '.$param->timeafter + .$param->extras .' ORDER BY timeend DESC'; + $stats = get_records_sql($sql); + + if (empty($stats)) { + error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline'); + } + echo '
'; + + $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3))); + $table = new object(); $table->align = array('left','center','center','center'); $param->table = str_replace('user_','',$param->table); diff --git a/lib/statslib.php b/lib/statslib.php index 054e609ba79..a0388386ed1 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -464,24 +464,23 @@ function stats_clean_old() { // don't delete monthlies } -function stats_get_parameters($time,$report) { +function stats_get_parameters($time,$report,$courseid) { if ($time < 10) { // dailies // number of days to go back = 7* time - $param->limit = 7*$time; $param->table = 'daily'; $param->timeafter = strtotime("-".($time*7)." days",stats_get_base_daily()); } elseif ($time < 20) { // weeklies // number of weeks to go back = time - 10 * 4 (weeks) + base week - $param->limit = ($time - 10) * 4; $param->table = 'weekly'; $param->timeafter = strtotime("-".(($time - 10)*4)." weeks",stats_get_base_weekly()); } else { // monthlies. // number of months to go back = time - 20 * months + base month - $param->limit = $time - 20; $param->table = 'monthly'; $param->timeafter = strtotime("-".($time - 20)." months",stats_get_base_monthly()); } + $param->extras = ''; + switch ($report) { case STATS_REPORT_LOGINS: $param->fields = 'logins as line1,uniquelogins as line2'; @@ -537,6 +536,11 @@ function stats_get_parameters($time,$report) { $param->stattype = 'activity'; break; } + + if ($courseid == SITEID) { // just aggregate all courses. + $param->fields = preg_replace('/([a-zA-Z0-9+_]*)\W+as\W+([a-zA-Z0-9_]*)/','sum($1) as $2',$param->fields); + $param->extras = ' GROUP BY timeend'; + } return $param; } @@ -751,6 +755,10 @@ function stats_get_report_options($courseid,$mode) { function stats_fix_zeros($stats,$timeafter,$timestr,$line2=true,$line3=false) { + if (empty($stats)) { + return; + } + $timestr = str_replace('user_','',$timestr); // just in case. $fun = 'stats_get_base_'.$timestr;