mirror of
https://github.com/moodle/moodle.git
synced 2025-04-22 08:55:15 +02:00
more work on stats reports ...
This commit is contained in:
parent
be91f51dab
commit
96edb010da
@ -66,13 +66,18 @@
|
||||
|
||||
if (!empty($report) && !empty($time)) {
|
||||
$param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);
|
||||
|
||||
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
|
||||
." WHERE timeend >= ".$param->timeafter
|
||||
." GROUP BY courseid "
|
||||
.$param->extras
|
||||
." ORDER BY ".$param->orderby
|
||||
." LIMIT ".$numcourses;
|
||||
|
||||
if (!empty($param->sql)) {
|
||||
$sql = $param->sql ." LIMIT ".$numcourses;
|
||||
} else {
|
||||
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
|
||||
." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
|
||||
." GROUP BY courseid "
|
||||
.$param->extras
|
||||
." ORDER BY ".$param->orderby
|
||||
." LIMIT ".$numcourses;
|
||||
}
|
||||
error_log($sql);
|
||||
|
||||
$courses = get_records_sql($sql);
|
||||
|
||||
@ -82,7 +87,7 @@
|
||||
} else {
|
||||
echo '<div align="center"><img src="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/courseoverview/reportsgraph.php?time='.$time.'&report='.$report.'&numcourses='.$numcourses.'" /></div>';
|
||||
|
||||
$table = new object();
|
||||
$table = new StdClass;
|
||||
$table->align = array('left','center','center','center');
|
||||
$table->head = array(get_string('course'),$param->line1);
|
||||
if (!empty($param->line2)) {
|
||||
@ -101,7 +106,7 @@
|
||||
$a[] = $c->line2;
|
||||
}
|
||||
if (isset($c->line3)) {
|
||||
$a[] = $c->line3;
|
||||
$a[] = round($c->line3,2);
|
||||
}
|
||||
$table->data[] = $a;
|
||||
}
|
||||
|
@ -16,12 +16,16 @@
|
||||
|
||||
$param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);
|
||||
|
||||
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
|
||||
." WHERE timeend >= ".$param->timeafter
|
||||
." GROUP BY courseid "
|
||||
.$param->extras
|
||||
." ORDER BY ".$param->orderby
|
||||
." LIMIT ".$numcourses;
|
||||
if (!empty($param->sql)) {
|
||||
$sql = $param->sql ." LIMIT ".$numcourses;
|
||||
} else {
|
||||
$sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
|
||||
." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
|
||||
." GROUP BY courseid "
|
||||
.$param->extras
|
||||
." ORDER BY ".$param->orderby
|
||||
." LIMIT ".$numcourses;
|
||||
}
|
||||
|
||||
$courses = get_records_sql($sql);
|
||||
|
||||
|
@ -18,13 +18,19 @@
|
||||
$time = optional_param('time', 0, PARAM_INT);
|
||||
$mode = optional_param('mode', STATS_MODE_GENERAL, PARAM_INT);
|
||||
$userid = optional_param('userid', 0, PARAM_INT);
|
||||
$roleid = 0;
|
||||
|
||||
if ($report > 50) {
|
||||
$roleid = substr($report,1);
|
||||
$report = 5;
|
||||
}
|
||||
|
||||
if ($report == STATS_REPORT_USER_LOGINS) {
|
||||
$courseid = SITEID; //override
|
||||
}
|
||||
|
||||
if ($mode == STATS_MODE_RANKED) {
|
||||
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report.php?time='.$time);
|
||||
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
|
||||
}
|
||||
|
||||
if (!$course = get_record("course","id",$courseid)) {
|
||||
|
@ -37,7 +37,7 @@
|
||||
}
|
||||
|
||||
$sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
|
||||
.' FROM '.$CFG->prefix.'stats_'.$param->table.'_tmp WHERE '
|
||||
.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
|
||||
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
|
||||
.((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
|
||||
.((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
|
||||
|
@ -25,7 +25,7 @@
|
||||
}
|
||||
|
||||
if ($mode == STATS_MODE_RANKED) {
|
||||
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report.php?time='.$time);
|
||||
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
|
||||
}
|
||||
|
||||
if (!$course = get_record("course","id",$courseid)) {
|
||||
|
@ -24,10 +24,19 @@
|
||||
$table->width = '*';
|
||||
|
||||
if ($mode == STATS_MODE_DETAILED) {
|
||||
// if we have time,
|
||||
// get all users from the stats table, joining on user, where they are smaller than us... or ourselves (or all, if admins) where timestuff.
|
||||
// else
|
||||
// get all users in this course (role_assignments)
|
||||
|
||||
|
||||
|
||||
if (!empty($time)) {
|
||||
$param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string.
|
||||
$sql = 'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber 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.'\'' : '');
|
||||
$sql = 'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber
|
||||
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 (!has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
|
||||
$sql .= ' AND (s.roleid = 1 OR s.userid = '.$USER->id .")";
|
||||
}
|
||||
@ -57,7 +66,9 @@
|
||||
}
|
||||
if (empty($time)) {
|
||||
if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
|
||||
$sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_teachers t JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id;
|
||||
$sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid
|
||||
FROM '.$CFG->prefix.'user_teachers t
|
||||
JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id;
|
||||
$moreusers = get_records_sql($sql);
|
||||
foreach ($moreusers as $u) {
|
||||
$users[$u->userid] = $course->teacher .' - '.fullname($u,true);
|
||||
@ -80,7 +91,12 @@
|
||||
get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
|
||||
get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
|
||||
'<input type="submit" value="'.get_string('view').'" />') ;
|
||||
} else {
|
||||
} else if ($mode == STATS_MODE_RANKED) {
|
||||
$table->align = array('left','left','left','left','left','left');
|
||||
$table->data[] = array(get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
|
||||
get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
|
||||
'<input type="submit" value="'.get_string('view').'" />') ;
|
||||
} else if ($mode == STATS_MODE_GENERAL) {
|
||||
$table->align = array('left','left','left','left','left','left','left');
|
||||
$table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
|
||||
get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
|
||||
@ -96,20 +112,25 @@
|
||||
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;
|
||||
}
|
||||
$sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
|
||||
.' FROM '.$CFG->prefix.'stats_'.$param->table.'_tmp WHERE '
|
||||
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
|
||||
.((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
|
||||
.((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
|
||||
. ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
|
||||
.' timeend >= '.$param->timeafter
|
||||
.' '.$param->extras
|
||||
.' ORDER BY timeend DESC';
|
||||
if (!empty($param->sql)) {
|
||||
$sql = $param->sql;
|
||||
} else {
|
||||
$sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
|
||||
.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
|
||||
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
|
||||
.((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
|
||||
.((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
|
||||
. ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
|
||||
.' timeend >= '.$param->timeafter
|
||||
.' '.$param->extras
|
||||
.' ORDER BY timeend DESC';
|
||||
}
|
||||
|
||||
$stats = get_records_sql($sql);
|
||||
|
||||
|
@ -579,7 +579,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
}
|
||||
|
||||
switch ($report) {
|
||||
case STATS_REPORT_LOGINS: // done
|
||||
// ******************** STATS_MODE_GENERAL ******************** //
|
||||
case STATS_REPORT_LOGINS:
|
||||
$param->fields = 'timeend,sum(stat1) as line1,sum(stat2) as line2';
|
||||
$param->fieldscomplete = true;
|
||||
$param->stattype = 'logins';
|
||||
@ -589,7 +590,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
$param->extras = 'GROUP BY timeend';
|
||||
}
|
||||
break;
|
||||
case STATS_REPORT_READS: // done
|
||||
|
||||
case STATS_REPORT_READS:
|
||||
$param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, timeend, roleid, stat1 as line1';
|
||||
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
|
||||
$param->aggregategroupby = 'roleid';
|
||||
@ -601,7 +603,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
$param->extras = 'GROUP BY timeend,roleid';
|
||||
}
|
||||
break;
|
||||
case STATS_REPORT_WRITES: //done
|
||||
|
||||
case STATS_REPORT_WRITES:
|
||||
$param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, timeend, roleid, stat2 as line1';
|
||||
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
|
||||
$param->aggregategroupby = 'roleid';
|
||||
@ -613,7 +616,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
$param->extras = 'GROUP BY timeend,roleid';
|
||||
}
|
||||
break;
|
||||
case STATS_REPORT_ACTIVITY: //done
|
||||
|
||||
case STATS_REPORT_ACTIVITY:
|
||||
$param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, timeend, roleid, sum(stat1+stat2) as line1';
|
||||
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
|
||||
$param->aggregategroupby = 'roleid';
|
||||
@ -624,29 +628,38 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
$param->extras = 'GROUP BY timeend,roleid';
|
||||
}
|
||||
break;
|
||||
|
||||
case STATS_REPORT_ACTIVITYBYROLE;
|
||||
$param->fields = 'stat1 AS line1, stat2 AS line2';
|
||||
$param->stattype = 'activity';
|
||||
$rolename = get_field('role','name','id',$roleid);
|
||||
$param->line1 = $rolename . get_string('statsreads');
|
||||
$param->line2 = $rolename . get_string('statswrites');
|
||||
if ($courseid == SITEID) {
|
||||
$param->extras = 'GROUP BY timeend';
|
||||
}
|
||||
break;
|
||||
|
||||
// ******************** STATS_MODE_DETAILED ******************** //
|
||||
case STATS_REPORT_USER_ACTIVITY:
|
||||
$param->fields = 'statsreads as line1, statswrites as line2';
|
||||
$param->line1 = get_string('statsuserreads');
|
||||
$param->line2 = get_string('statsuserwrites');
|
||||
$param->stattype = 'activity';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_USER_ALLACTIVITY:
|
||||
$param->fields = 'statsreads+statswrites as line1';
|
||||
$param->line1 = get_string('statsuseractivity');
|
||||
$param->stattype = 'activity';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_USER_LOGINS:
|
||||
$param->fields = 'statsreads as line1';
|
||||
$param->line1 = get_string('statsuserlogins');
|
||||
$param->stattype = 'logins';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_USER_VIEW:
|
||||
$param->fields = 'statsreads as line1, statswrites as line2, statsreads+statswrites as line3';
|
||||
$param->line1 = get_string('statsuserreads');
|
||||
@ -654,50 +667,92 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
|
||||
$param->line3 = get_string('statsuseractivity');
|
||||
$param->stattype = 'activity';
|
||||
break;
|
||||
|
||||
// ******************** STATS_MODE_RANKED ******************** //
|
||||
case STATS_REPORT_ACTIVE_COURSES:
|
||||
$param->fields = 'sum(studentreads+studentwrites+teacherreads+teacherwrites) AS line1';
|
||||
$param->fields = 'sum(stat1+stat2) AS line1';
|
||||
$param->stattype = 'activity';
|
||||
$param->orderby = 'line1 DESC';
|
||||
$param->line1 = get_string('activity');
|
||||
$param->graphline = 'line1';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_ACTIVE_COURSES_WEIGHTED:
|
||||
$param->fields = 'sum(studentreads+studentwrites+teacherreads+teacherwrites) AS line1,'
|
||||
.'max(students+teachers) AS line2,'
|
||||
.'sum(studentreads+studentwrites+teacherreads+teacherwrites)'.$real.'/max(students+teachers)'.$real.' AS line3';
|
||||
$param->extras = 'HAVING max(students+teachers) != 0';
|
||||
$threshold = 0;
|
||||
if (!empty($CFG->statsuserthreshold) && is_numeric($CFG->statsuserthreshold)) {
|
||||
$param->extras .= ' AND max(students+teachers) > '.$CFG->statsuserthreshold;
|
||||
$threshold = $CFG->statsuserthreshold;
|
||||
}
|
||||
$param->orderby = 'line3 DESC';
|
||||
$param->fields = '';
|
||||
$param->sql = 'SELECT activity.courseid, activity.all_activity AS line1, enrolments.highest_enrolments AS line2,
|
||||
activity.all_activity / enrolments.highest_enrolments as line3
|
||||
FROM (
|
||||
SELECT courseid, sum(stat1+stat2) AS all_activity
|
||||
FROM '.$CFG->prefix.'stats_'.$param->table.'
|
||||
WHERE stattype=\'activity\' AND timeend >= '.$param->timeafter.'
|
||||
GROUP BY courseid
|
||||
) activity
|
||||
INNER JOIN
|
||||
(
|
||||
SELECT courseid, max(stat1) AS highest_enrolments
|
||||
FROM '.$CFG->prefix.'stats_'.$param->table.'
|
||||
WHERE stattype=\'enrolments\' AND timeend >= '.$param->timeafter.'
|
||||
GROUP BY courseid
|
||||
) enrolments
|
||||
ON (activity.courseid = enrolments.courseid)
|
||||
WhERE enrolments.highest_enrolments > '.$threshold.'
|
||||
ORDER BY line3 DESC';
|
||||
$param->line1 = get_string('activity');
|
||||
$param->line2 = get_string('users');
|
||||
$param->line3 = get_string('activityweighted');
|
||||
$param->graphline = 'line3';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_PARTICIPATORY_COURSES:
|
||||
$param->fields = 'max(students+teachers) as line1,max(activestudents+activeteachers) AS line2,'
|
||||
.'max(activestudents+activeteachers)'.$real.'/max(students+teachers)'.$real.' AS line3';
|
||||
$param->extras = 'HAVING max(students+teachers) != 0';
|
||||
$threshold = 0;
|
||||
if (!empty($CFG->statsuserthreshold) && is_numeric($CFG->statsuserthreshold)) {
|
||||
$param->extras .= ' AND max(students+teachers) > '.$CFG->statsuserthreshold;
|
||||
$threshold = $CFG->statsuserthreshold;
|
||||
}
|
||||
$param->orderby = 'line3 DESC';
|
||||
$param->fields = '';
|
||||
$param->sql = 'SELECT courseid, ceil(avg(all_enrolments)) as line1,
|
||||
ceil(avg(active_enrolments)) as line2, avg(proportion_active) AS line3
|
||||
FROM (
|
||||
SELECT courseid, timeend, sum(stat2) as active_enrolments,
|
||||
sum(stat1) as all_enrolments, sum(stat2)'.$real.'/sum(stat1)'.$real.' as proportion_active
|
||||
FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE stattype=\'enrolments\'
|
||||
GROUP BY courseid, timeend
|
||||
HAVING sum(stat1) > '.$threshold.'
|
||||
) aq
|
||||
WHERE timeend >= '.$param->timeafter.'
|
||||
GROUP BY courseid
|
||||
ORDER BY line3 DESC';
|
||||
|
||||
$param->line1 = get_string('users');
|
||||
$param->line2 = get_string('activeusers');
|
||||
$param->line3 = get_string('participationratio');
|
||||
$param->graphline = 'line3';
|
||||
break;
|
||||
|
||||
case STATS_REPORT_PARTICIPATORY_COURSES_RW:
|
||||
$param->fields = 'sum(studentreads+teacherreads) as line1,sum(studentwrites+teacherwrites) AS line2,'
|
||||
.'sum(studentwrites+teacherwrites)'.$real.'/sum(studentreads+teacherreads)'.$real.' AS line3';
|
||||
$param->extras = 'HAVING sum(studentreads+teacherreads) != 0';
|
||||
$param->orderby = 'line3 DESC';
|
||||
$param->fields = '';
|
||||
$param->sql = 'SELECT courseid, sum(views) AS line1, sum(posts) AS line2,
|
||||
avg(proportion_active) AS line3
|
||||
FROM (
|
||||
SELECT courseid, timeend,sum(stat1) as views, sum(stat2) AS posts,
|
||||
sum(stat2)'.$real.'/sum(stat1)'.$real.' as proportion_active
|
||||
FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE stattype=\'activity\'
|
||||
GROUP BY courseid, timeend
|
||||
HAVING sum(stat1) > 0
|
||||
) aq
|
||||
WHERE timeend >= '.$param->timeafter.'
|
||||
GROUP BY courseid
|
||||
ORDER BY line3 DESC';
|
||||
$param->line1 = get_string('views');
|
||||
$param->line2 = get_string('posts');
|
||||
$param->line3 = get_string('participationratio');
|
||||
$param->graphline = 'line3';
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
if ($courseid == SITEID && $mode != STATS_MODE_RANKED) { // just aggregate all courses.
|
||||
$param->fields = preg_replace('/(?:sum)([a-zA-Z0-9+_]*)\W+as\W+([a-zA-Z0-9_]*)/i','sum($1) as $2',$param->fields);
|
||||
@ -910,7 +965,7 @@ function stats_get_report_options($courseid,$mode) {
|
||||
switch ($mode) {
|
||||
case STATS_MODE_GENERAL:
|
||||
$reportoptions[STATS_REPORT_ACTIVITY] = get_string('statsreport'.STATS_REPORT_ACTIVITY);
|
||||
if ($context = get_record('context','instanceid',$courseid,'contextlevel',CONTEXT_COURSE)) {
|
||||
if ($courseid != SITEID && $context = get_record('context','instanceid',$courseid,'contextlevel',CONTEXT_COURSE)) {
|
||||
$sql = 'SELECT r.id,r.name FROM '.$CFG->prefix.'role r JOIN '.$CFG->prefix.'stats_daily s ON s.roleid = r.id WHERE s.courseid = '.$courseid;
|
||||
if ($roles = get_records_sql($sql)) {
|
||||
foreach ($roles as $role) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user