Skip to content

Commit

Permalink
MDL-13192 major stats cleanup patch - see tracker for more details; m…
Browse files Browse the repository at this point in the history
…erged from MOODLE_19_STABLE
  • Loading branch information
skodak committed Feb 16, 2008
1 parent 46d5b31 commit 61460dd
Show file tree
Hide file tree
Showing 16 changed files with 1,123 additions and 732 deletions.
45 changes: 9 additions & 36 deletions admin/cron.php
Expand Up @@ -430,47 +430,20 @@
}

if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {

require_once($CFG->dirroot.'/lib/statslib.php');
// check we're not before our runtime
$timetocheck = strtotime("today $CFG->statsruntimestarthour:$CFG->statsruntimestartminute");
$timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;

if (time() > $timetocheck) {
$time = 60*60*20; // set it to 20 here for first run... (overridden by $CFG)
$clobber = true;
if (!empty($CFG->statsmaxruntime)) {
$time = $CFG->statsmaxruntime+(60*30); // add on half an hour just to make sure (it could take that long to break out of the loop)
}
if (!get_field_sql('SELECT id FROM '.$CFG->prefix.'stats_daily')) {
// first run, set another lock. we'll check for this in subsequent runs to set the timeout to later for the normal lock.
set_cron_lock('statsfirstrunlock',true,$time,true);
$firsttime = true;
}
$time = 60*60*2; // this time set to 2.. (overridden by $CFG)
if (!empty($CFG->statsmaxruntime)) {
$time = $CFG->statsmaxruntime+(60*30); // add on half an hour to make sure (it could take that long to break out of the loop)
}
if ($config = get_record('config','name','statsfirstrunlock')) {
if (!empty($config->value)) {
$clobber = false; // if we're on the first run, just don't clobber it.
}
}
if (set_cron_lock('statsrunning',true,$time, $clobber)) {
require_once($CFG->dirroot.'/lib/statslib.php');
$return = stats_cron_daily();
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
stats_cron_weekly();
}
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
$return = $return && stats_cron_monthly();
}
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
stats_clean_old();
}
set_cron_lock('statsrunning',false);
if (!empty($firsttime)) {
set_cron_lock('statsfirstrunlock',false);
// process max 31 days per cron execution
if (stats_cron_daily(31)) {
if (stats_cron_weekly()) {
if (stats_cron_monthly()) {
stats_clean_old();
}
}
}
@set_time_limit(0);
}
}

Expand Down
15 changes: 8 additions & 7 deletions admin/report/courseoverview/index.php
Expand Up @@ -18,7 +18,6 @@
redirect("$CFG->wwwroot/$CFG->admin/settings.php?section=stats", get_string('mustenablestats', 'admin'), 3);
}


$course = get_site();
stats_check_uptodate($course->id);

Expand Down Expand Up @@ -48,7 +47,7 @@
}

echo '<form action="index.php" method="post">'."\n";
echo '<fieldset class="invisiblefieldset">';
echo '<div>';

$table->width = '*';
$table->align = array('left','left','left','left','left','left');
Expand All @@ -58,17 +57,19 @@
'<input type="submit" value="'.get_string('view').'" />') ;

print_table($table);
echo '</fieldset>';
echo '</div>';
echo '</form>';

print_heading($reportoptions[$report]);


if (!empty($report) && !empty($time)) {
$param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);

if (!empty($param->sql)) {
$sql = $param->sql;
} else {
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
." GROUP BY courseid "
.$param->extras
." ORDER BY ".$param->orderby;
Expand All @@ -82,9 +83,9 @@

} else {
if (empty($CFG->gdversion)) {
echo '<div class="boxaligncenter">(' . get_string("gdneed") .')</div>';
echo '<div class="graph">(' . get_string("gdneed") .')</div>';
} else {
echo '<div class="boxaligncenter"><img alt="'.get_string('courseoverviewgraph').'" src="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/courseoverview/reportsgraph.php?time='.$time.'&report='.$report.'&numcourses='.$numcourses.'" /></div>';
echo '<div class="graph"><img alt="'.get_string('courseoverviewgraph').'" src="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/courseoverview/reportsgraph.php?time='.$time.'&report='.$report.'&numcourses='.$numcourses.'" /></div>';
}

$table = new StdClass;
Expand Down
2 changes: 1 addition & 1 deletion admin/report/courseoverview/reportsgraph.php
Expand Up @@ -20,7 +20,7 @@
$sql = $param->sql;
} else {
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
." GROUP BY courseid "
.$param->extras
." ORDER BY ".$param->orderby;
Expand Down
9 changes: 7 additions & 2 deletions admin/settings/server.php
Expand Up @@ -123,7 +123,9 @@
60*60*24*140 => get_string('nummonths','moodle',5),
60*60*24*168 => get_string('nummonths','moodle',6),
'all' => get_string('all') )));
$temp->add(new admin_setting_configselect('statsmaxruntime', get_string('statsmaxruntime', 'admin'), get_string('configstatsmaxruntime', 'admin'), 0, array(0 => get_string('untilcomplete'),
$temp->add(new admin_setting_configselect('statsmaxruntime', get_string('statsmaxruntime', 'admin'), get_string('configstatsmaxruntime2', 'admin'), 0, array(0 => get_string('untilcomplete'),
60*30 => '10 '.get_string('minutes'),
60*30 => '30 '.get_string('minutes'),
60*60 => '1 '.get_string('hour'),
60*60*2 => '2 '.get_string('hours'),
60*60*3 => '3 '.get_string('hours'),
Expand All @@ -134,6 +136,9 @@
60*60*8 => '8 '.get_string('hours') )));
$temp->add(new admin_setting_configtime('statsruntimestarthour', 'statsruntimestartminute', get_string('statsruntimestart', 'admin'), get_string('configstatsruntimestart', 'admin'), array('h' => 0, 'm' => 0)));
$temp->add(new admin_setting_configtext('statsuserthreshold', get_string('statsuserthreshold', 'admin'), get_string('configstatsuserthreshold', 'admin'), 0, PARAM_INT));

$options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6);
$temp->add(new admin_setting_configselect('statscatdepth', get_string('statscatdepth', 'admin'), get_string('configstatscatdepth', 'admin'), 1, $options));
$ADMIN->add('server', $temp);


Expand Down Expand Up @@ -189,7 +194,7 @@
120 => get_string('numdays', '', 120),
90 => get_string('numdays', '', 90),
60 => get_string('numdays', '', 60),
30 => get_string('numdays', '', 30))));
35 => get_string('numdays', '', 35))));


$temp->add(new admin_setting_configcheckbox('disablegradehistory', get_string('disablegradehistory', 'grades'),
Expand Down
46 changes: 26 additions & 20 deletions course/report/stats/graph.php
Expand Up @@ -60,8 +60,6 @@
$graph->parameter['title'] = false; // moodle will do a nicer job.
$graph->y_tick_labels = null;

$c = array_keys($graph->colour);

if (empty($param->crosstab)) {
foreach ($stats as $stat) {
$graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
Expand All @@ -74,16 +72,17 @@
}
}
$graph->y_order = array('line1');
$graph->y_format['line1'] = array('colour' => $c[1],'line' => 'line','legend' => $param->line1);
$graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
if (!empty($param->line2)) {
$graph->y_order[] = 'line2';
$graph->y_format['line2'] = array('colour' => $c[2],'line' => 'line','legend' => $param->line2);
$graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
}
if (!empty($param->line3)) {
$graph->y_order[] = 'line3';
$graph->y_format['line3'] = array('colour' => $c[3],'line' => 'line','legend' => $param->line3);
$graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
}
$graph->y_tick_labels = false;

} else {
$data = array();
$times = array();
Expand All @@ -98,6 +97,10 @@
if (!array_key_exists($stat->roleid,$roles)) {
$roles[$stat->roleid] = get_field('role','name','id',$stat->roleid);
}
} else {
if (!array_key_exists($stat->roleid,$roles)) {
$roles[$stat->roleid] = get_string('all');
}
}
if (!array_key_exists($stat->timeend,$times)) {
$times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
Expand All @@ -110,27 +113,30 @@
}
}
}
foreach ($data as $role => $stuff) {
ksort($data[$role]);
}
$nonzeroroleid = 0;
foreach (array_keys($data) as $roleid) {
if ($roleid == 0) {
continue;

$roleid = 0;
krsort($roles); // the same sorting as in table bellow graph

$colors = array('green', 'blue', 'red', 'purple', 'yellow', 'olive', 'navy', 'maroon', 'gray', 'ltred', 'ltltred', 'ltgreen', 'ltltgreen', 'orange', 'ltorange', 'ltltorange', 'lime', 'ltblue', 'ltltblue', 'fuchsia', 'aqua', 'grayF0', 'grayEE', 'grayDD', 'grayCC', 'gray33', 'gray66', 'gray99');
$colorindex = 0;

foreach ($roles as $roleid=>$rname) {
ksort($data[$roleid]);
$graph->y_order[] = $roleid+1;
if ($roleid) {
$color = $colors[$colorindex++];
$colorindex = $colorindex % count($colors);
} else {
$color = 'black';
}
$graph->y_order[] = $roleid;
$graph->y_format[$roleid] = array('colour' => $c[$roleid], 'line' => 'line','legend' => $roles[$roleid]);
$nonzeroroleid = $roleid;
$graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
}
foreach (array_keys($data[$nonzeroroleid]) as $time) {
foreach (array_keys($data[$roleid]) as $time) {
$graph->x_data[] = $times[$time];
}
foreach ($data as $roleid => $t) {
if ($roleid == 0) {
continue;
}
foreach ($t as $time => $data) {
$graph->y_data[$roleid][] = $data;
$graph->y_data[$roleid+1][] = $data;
}
}
}
Expand Down
44 changes: 26 additions & 18 deletions course/report/stats/report.php
Expand Up @@ -17,26 +17,25 @@
error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/view.php?id='.$course->id);
}

$table->width = '*';
$table->width = 'auto';

if ($mode == STATS_MODE_DETAILED) {
$param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time)

$sql = 'SELECT DISTINCT s.userid,s.roleid,r.name AS rolename,r.sortorder,u.firstname,u.lastname,u.idnumber
$sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber
FROM '.$CFG->prefix.'stats_user_'.$param->table.' s
JOIN '.$CFG->prefix.'user u ON u.id = s.userid
JoIN '.$CFG->prefix.'role r ON s.roleid = r.id
WHERE courseid = '.$course->id
. ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '')
. ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
.' ORDER BY r.sortorder';
.' ORDER BY u.lastname, u.firstname ASC';

if (!$us = get_records_sql($sql)) {
error('Cannot enter detailed view: No users found for this course.');
}

foreach ($us as $u) {
$users[$u->userid] = $u->rolename.' - '.fullname($u,true);
$users[$u->userid] = fullname($u, true);
}

$table->align = array('left','left','left','left','left','left','left','left');
Expand All @@ -59,24 +58,25 @@
}

echo '<form action="index.php" method="post">'."\n"
.'<fieldset class="invisiblefieldset">'."\n"
.'<div>'."\n"
.'<input type="hidden" name="mode" value="'.$mode.'" />'."\n";

print_table($table);

echo '</fieldset>';
echo '</div>';
echo '</form>';

if (!empty($report) && !empty($time)) {
if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) {
error('This type of report is only available for the site course');
}
$timesql =

$param = stats_get_parameters($time,$report,$course->id,$mode);

if ($mode == STATS_MODE_DETAILED) {
$param->table = 'user_'.$param->table;
}

if (!empty($param->sql)) {
$sql = $param->sql;
} else {
Expand Down Expand Up @@ -109,16 +109,22 @@
echo "(".get_string("gdneed").")";
} else {
if ($mode == STATS_MODE_DETAILED) {
echo '<center><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></center>';
echo '<div class="graph"><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></div';
} else {
echo '<center><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></center>';
echo '<div class="graph"><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></div>';
}
}

$table = new StdClass;
$table->align = array('left','center','center','center');
$param->table = str_replace('user_','',$param->table);
$table->head = array(get_string('periodending','moodle',$param->table));
switch ($param->table) {
case 'daily' : $period = get_string('day'); break;
case 'weekly' : $period = get_string('week'); break;
case 'monthly': $period = get_string('month', 'form'); break;
default : $period = '';
}
$table->head = array(get_string('periodending','moodle',$period));
if (empty($param->crosstab)) {
$table->head[] = $param->line1;
if (!empty($param->line2)) {
Expand All @@ -133,8 +139,8 @@
}
if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
$a[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='.
$course->id.'&chooselog=1&showusers=1&showcourses=1&user='
.$userid.'&date='.usergetmidnight($stat->timeend-(60*60*24)).'">'
$course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='
.$userid.'&amp;date='.usergetmidnight($stat->timeend-(60*60*24)).'">'
.get_string('course').' ' .get_string('logs').'</a>&nbsp;';
}
$table->data[] = $a;
Expand All @@ -153,18 +159,20 @@
if (!array_key_exists($stat->roleid,$roles)) {
$roles[$stat->roleid] = get_field('role','name','id',$stat->roleid);
}
} else {
if (!array_key_exists($stat->roleid,$roles)) {
$roles[$stat->roleid] = get_string('all');
}
}
if (!array_key_exists($stat->timeend,$times)) {
$times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
}
}

foreach ($data as $time => $rolesdata) {
if (in_array($time,$missedlines)) {
$rolesdata = array();
foreach ($roles as $roleid => $guff) {
if ($roleid == 0 ) {
continue;
}
$rolesdata[$roleid] = 0;
}
}
Expand All @@ -179,8 +187,8 @@
$row = array_merge(array($times[$time]),$rolesdata);
if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
$row[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='
.$course->id.'&chooselog=1&showusers=1&showcourses=1&user='.$userid
.'&date='.usergetmidnight($time-(60*60*24)).'">'
.$course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='.$userid
.'&amp;date='.usergetmidnight($time-(60*60*24)).'">'
.get_string('course').' ' .get_string('logs').'</a>&nbsp;';
}
$table->data[] = $row;
Expand Down

0 comments on commit 61460dd

Please sign in to comment.