if (!empty($instanceid) && !empty($roleid)) { if (!($cm = get_coursemodule_from_instance($module->name, $instanceid, $course->id))) { print_error('cmunknown'); } require_once $CFG->dirroot . '/lib/tablelib.php'; $table = new flexible_table('course-participation-' . $course->id . '-' . $cm->id . '-' . $roleid); $table->course = $course; $table->define_columns(array('fullname', 'count', '')); $table->define_headers(array(get_string('user'), !empty($action) ? get_string($action) : get_string('allactions'), get_string('select'))); $table->define_baseurl($baseurl); $table->set_attribute('cellpadding', '5'); $table->set_attribute('class', 'generaltable generalbox reporttable'); $table->sortable(true, 'lastname', 'ASC'); $table->set_control_variables(array(TABLE_VAR_SORT => 'ssort', TABLE_VAR_HIDE => 'shide', TABLE_VAR_SHOW => 'sshow', TABLE_VAR_IFIRST => 'sifirst', TABLE_VAR_ILAST => 'silast', TABLE_VAR_PAGE => 'spage')); $table->setup(); $primary_roles = sql_primary_role_subselect(); // In dmllib.php $sql = 'SELECT DISTINCT prs.userid, u.firstname,u.lastname,u.idnumber,count(l.action) as count FROM (' . $primary_roles . ') prs' . ' JOIN ' . $CFG->prefix . 'user u ON u.id = prs.userid LEFT JOIN ' . $CFG->prefix . 'log l ON prs.userid = l.userid ' . ' AND prs.courseid = l.course AND l.time > ' . $timefrom . ' AND l.course = ' . $course->id . ' AND l.module = \'' . $module->name . '\' ' . ' AND l.cmid = ' . $cm->id; switch ($action) { case 'view': $sql .= ' AND action IN (\'' . implode('\',\'', $viewnames) . '\' )'; break; case 'post': $sql .= ' AND action IN (\'' . implode('\',\'', $postnames) . '\' )'; break; default: // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here. $sql .= ' AND action IN (\'' . implode('\',\'', array_merge($viewnames, $postnames)) . '\' )'; } $sql .= ' WHERE prs.courseid = ' . $course->id . ' AND prs.primary_roleid = ' . $roleid . ' AND prs.contextlevel = ' . CONTEXT_COURSE . ' AND prs.courseid = ' . $course->id; if ($table->get_sql_where()) {
function stats_cron_daily() { global $CFG; if (empty($CFG->enablestats)) { return STATS_RUN_ABORTED; } if (!($timestart = stats_get_start_from('daily'))) { return STATS_RUN_ABORTED; } $midnight = stats_getmidnight(time()); // check to make sure we're due to run, at least one day after last run if (isset($CFG->statslastdaily) and time() - 24 * 60 * 60 < $CFG->statslastdaily) { return STATS_RUN_ABORTED; } mtrace("Running daily statistics gathering..."); set_config('statslastdaily', time()); $return = STATS_RUN_COMPLETE; // optimistic static $daily_modules; if (empty($daily_modules)) { $daily_modules = array(); $mods = get_records("modules"); foreach ($mods as $mod) { $file = $CFG->dirroot . '/mod/' . $mod->name . '/lib.php'; if (!is_readable($file)) { continue; } require_once $file; $fname = $mod->name . '_get_daily_stats'; if (function_exists($fname)) { $daily_modules[$mod] = $fname; } } } $nextmidnight = stats_get_next_dayend($timestart); if (!($courses = get_records('course', '', '', '', 'id,1'))) { return STATS_RUN_ABORTED; } $days = 0; mtrace("starting at {$timestart}"); while ($midnight > $nextmidnight && $timestart < $nextmidnight) { $timesql = " (l.time > {$timestart} AND l.time < {$nextmidnight}) "; begin_sql(); foreach ($courses as $course) { //do this first. if ($course->id == SITEID) { $stat = new StdClass(); $stat->courseid = $course->id; $stat->timeend = $nextmidnight; $stat->roleid = 0; // all users $stat->stattype = 'logins'; $sql = 'SELECT count(l.id) FROM ' . $CFG->prefix . 'log l WHERE l.action = \'login\' AND ' . $timesql; $stat->stat1 = count_records_sql($sql); $sql = 'SELECT COUNT(DISTINCT(l.userid)) FROM ' . $CFG->prefix . 'log l WHERE l.action = \'login\' AND ' . $timesql; $stat->stat2 = count_records_sql($sql); insert_record('stats_daily', $stat, false); // don't worry about the return id, we don't need it. // and now user logins... $sql = 'SELECT l.userid,count(l.id) as count FROM ' . $CFG->prefix . 'log l WHERE action = \'login\' AND ' . $timesql . ' GROUP BY userid'; if ($logins = get_records_sql($sql)) { foreach ($logins as $l) { $stat->statsreads = $l->count; $stat->userid = $l->userid; $stat->timeend = $nextmidnight; $stat->courseid = SITEID; $stat->statswrites = 0; $stat->stattype = 'logins'; $stat->roleid = 0; insert_record('stats_user_daily', $stat, false); } } } $context = get_context_instance(CONTEXT_COURSE, $course->id); if (!($roles = get_roles_on_exact_context($context))) { // no roles.. nothing to log. continue; } $primary_roles = sql_primary_role_subselect(); // In dmllib.php foreach ($roles as $role) { // ENROLMENT FIRST.... // ALL users with this role... $stat = new StdClass(); $stat->courseid = $course->id; $stat->roleid = $role->id; $stat->timeend = $nextmidnight; $stat->stattype = 'enrolments'; $sql = 'SELECT COUNT(DISTINCT prs.userid) FROM (' . $primary_roles . ') prs WHERE prs.primary_roleid=' . $role->id . ' AND prs.courseid=' . $course->id . ' AND prs.contextlevel = ' . CONTEXT_COURSE; $stat->stat1 = count_records_sql($sql); $sql = 'SELECT COUNT(DISTINCT prs.userid) FROM (' . $primary_roles . ') prs INNER JOIN ' . $CFG->prefix . 'log l ON (prs.userid=l.userid AND l.course=prs.courseid) WHERE prs.primary_roleid=' . $role->id . ' AND prs.courseid=' . $course->id . ' AND prs.contextlevel = ' . CONTEXT_COURSE . ' AND ' . $timesql; $stat->stat2 = count_records_sql($sql); insert_record('stats_daily', $stat, false); // don't worry about the return id, we don't need it. // ACTIVITY $stat = new StdClass(); $stat->courseid = $course->id; $stat->roleid = $role->id; $stat->timeend = $nextmidnight; $stat->stattype = 'activity'; $sql = 'SELECT COUNT(DISTINCT l.id) FROM (' . $primary_roles . ') prs INNER JOIN ' . $CFG->prefix . 'log l ON (prs.userid=l.userid AND l.course=prs.courseid) WHERE prs.primary_roleid=' . $role->id . ' AND prs.courseid=' . $course->id . ' AND prs.contextlevel = ' . CONTEXT_COURSE . ' AND ' . $timesql . ' ' . stats_get_action_sql_in('view'); $stat->stat1 = count_records_sql($sql); $sql = 'SELECT COUNT(DISTINCT l.id) FROM (' . $primary_roles . ') prs INNER JOIN ' . $CFG->prefix . 'log l ON (prs.userid=l.userid AND l.course=prs.courseid) WHERE prs.primary_roleid=' . $role->id . ' AND prs.courseid=' . $course->id . ' AND prs.contextlevel = ' . CONTEXT_COURSE . ' AND ' . $timesql . ' ' . stats_get_action_sql_in('post'); $stat->stat2 = count_records_sql($sql); insert_record('stats_daily', $stat, false); // don't worry about the return id, we don't need it. } $users = stats_get_course_users($course, $timesql); foreach ($users as $user) { stats_do_daily_user_cron($course, $user, $user->primaryrole, $timesql, $nextmidnight, $daily_modules); } } commit_sql(); $timestart = $nextmidnight; $nextmidnight = stats_get_next_dayend($nextmidnight); $days++; if (!stats_check_runtime()) { mtrace("Stopping early! reached maxruntime"); $return = STATS_RUN_ABORTED; break; } } mtrace("got up to " . $timestart); mtrace("Completed {$days} days"); return $return; }