Skip to content

Commit

Permalink
MDL-29030 move user stats reporting to report_stats plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Nov 6, 2011
1 parent fad8e02 commit beda8fa
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 156 deletions.
87 changes: 4 additions & 83 deletions course/user.php
Expand Up @@ -57,8 +57,12 @@
$logmode = ($mode === 'todaylogs') ? 'today' : 'all';
$url = new moodle_url('/report/log/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$logmode));
redirect($url);
} else if ($mode === 'stats') {
$url = new moodle_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
redirect($url);
}


require_login();
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
Expand Down Expand Up @@ -87,10 +91,6 @@

$modes = array();

if ($myreports or $anyreport or has_capability('report/stats:view', $coursecontext)) {
$modes[] = 'stats';
}

if (has_capability('moodle/grade:viewall', $coursecontext)) {
//ok - can view all course grades
$modes[] = 'grade';
Expand Down Expand Up @@ -164,85 +164,6 @@
}
break;

case 'stats':

if (empty($CFG->enablestats)) {
print_error('statsdisable', 'error');
}

require_once($CFG->dirroot.'/lib/statslib.php');

$statsstatus = stats_check_uptodate($course->id);
if ($statsstatus !== NULL) {
echo $OUTPUT->notification($statsstatus);
}

$earliestday = $DB->get_field_sql('SELECT timeend FROM {stats_user_daily} ORDER BY timeend');
$earliestweek = $DB->get_field_sql('SELECT timeend FROM {stats_user_weekly} ORDER BY timeend');
$earliestmonth = $DB->get_field_sql('SELECT timeend FROM {stats_user_monthly} ORDER BY timeend');

if (empty($earliestday)) $earliestday = time();
if (empty($earliestweek)) $earliestweek = time();
if (empty($earliestmonth)) $earliestmonth = time();

$now = stats_get_base_daily();
$lastweekend = stats_get_base_weekly();
$lastmonthend = stats_get_base_monthly();

$timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);

if (empty($timeoptions)) {
print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
}

// use the earliest.
$time = array_pop(array_keys($timeoptions));

$param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
$params = $param->params;

$param->table = 'user_'.$param->table;

$sql = 'SELECT timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
.' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
.' ORDER BY timeend DESC';
$stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!

if (empty($stats)) {
print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
}

// MDL-10818, do not display broken graph when user has no permission to view graph
if ($myreports or has_capability('report/stats:view', $coursecontext)) {
echo '<center><img src="'.$CFG->wwwroot.'/report/stats/graph.php?mode='.STATS_MODE_DETAILED.'&course='.$course->id.'&time='.$time.'&report='.STATS_REPORT_USER_VIEW.'&userid='.$user->id.'" alt="'.get_string('statisticsgraph').'" /></center>';
}

// What the heck is this about? -- MD
$stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));

$table = new html_table();
$table->align = array('left','center','center','center');
$param->table = str_replace('user_','',$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),$param->line1,$param->line2,$param->line3);
foreach ($stats as $stat) {
if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
continue;
}
$a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
$a[] = $stat->line2;
$a[] = $stat->line3;
$table->data[] = $a;
}
echo html_writer::table($table);
break;

case "coursecompletion":
case "coursecompletions":

Expand Down
55 changes: 6 additions & 49 deletions lib/navigationlib.php
Expand Up @@ -1973,37 +1973,15 @@ protected function load_for_user($user=null, $forceforcontext=false) {
$usernode->add(get_string('notes', 'notes'), $url);
}

// Add reports node
$reporttab = $usernode->add(get_string('activityreports'));

$reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php');
foreach ($reports as $reportfunction) {
$reportfunction($reporttab, $user, $course);
}

//TODO: hack area alert - all this must be abstracted to plugin callbacks above

// Add a reports tab and then add reports the the user has permission to see.
$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);

$statsreport = ($anyreport || has_capability('report/stats:view', $coursecontext));

$somereport = $statsreport;

$viewreports = ($anyreport || $somereport || ($course->showreports && $iscurrentuser && $forceforcontext));
if ($viewreports) {
$reportargs = array('user'=>$user->id);
if (!empty($course->id)) {
$reportargs['id'] = $course->id;
} else {
$reportargs['id'] = SITEID;
}

if (!empty($CFG->enablestats)) {
if ($viewreports || $statsreport) {
$reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats'))));
}
}

$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);
if ($anyreport || ($course->showreports && $iscurrentuser && $forceforcontext)) {
// Add grade hardcoded grade report if necessary
$gradeaccess = false;
if (has_capability('moodle/grade:viewall', $coursecontext)) {
//ok - can view all course grades
Expand All @@ -2021,18 +1999,10 @@ protected function load_for_user($user=null, $forceforcontext=false) {
}
}
if ($gradeaccess) {
$reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'grade'))));
$reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array('mode'=>'grade', 'id'=>$course->id)));
}

// Check the number of nodes in the report node... if there are none remove
// the node
$reporttab->trim_if_empty();
}

//TODO: end of hacky area

// Check the number of nodes in the report node... if there are none remove
// the node
// Check the number of nodes in the report node... if there are none remove the node
$reporttab->trim_if_empty();

// If the user is the current user add the repositories for the current user
Expand Down Expand Up @@ -2097,19 +2067,6 @@ protected function load_for_user($user=null, $forceforcontext=false) {
$reportfunction($reporttab, $user, $usercourse);
}

//TODO: hacky area - migrate to plugin callbacks above

$statsreport = ($anyreport || has_capability('report/stats:view', $usercoursecontext));
if ($statsreport) {
$reportargs = array('user'=>$user->id, 'id'=>$usercourse->id);

if (!empty($CFG->enablestats) && $statsreport) {
$reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats'))));
}
}

//TODO: end of hacky area

$reporttab->trim_if_empty();
}
}
Expand Down
40 changes: 22 additions & 18 deletions report/stats/graph.php
Expand Up @@ -24,7 +24,7 @@
*/

require('../../config.php');
require_once($CFG->dirroot.'/lib/statslib.php');
require_once($CFG->dirroot.'/report/stats/locallib.php');
require_once($CFG->dirroot.'/lib/graphlib.php');

$courseid = required_param('course', PARAM_INT);
Expand All @@ -34,32 +34,36 @@
$userid = optional_param('userid', 0, PARAM_INT);
$roleid = optional_param('roleid',0,PARAM_INT);

$url = new moodle_url('/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode));
if ($userid !== 0) {
$url->param('userid', $userid);
}
if ($roleid !== 0) {
$url->param('roleid', $roleid);
}
$url = new moodle_url('/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode, 'userid'=>$userid, 'roleid'=>$roleid));
$PAGE->set_url($url);

if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
print_error("invalidcourseid");
}
$course = $DB->get_record("course", array("id"=>$courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);
$PAGE->set_context($coursecontext);

if (!empty($userid)) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error("nousers");
$user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
$personalcontext = context_user::instance($user->id);

if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
//TODO: do not require parents to be enrolled in courses - this is a hack!
require_login();
$PAGE->set_course($course);
} else {
require_login($course);
}
}

require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (!report_stats_can_access_user_report($user, $course, true)) {
require_capability('report/stats:view', $coursecontext);
}

if (!$course->showreports or $USER->id != $userid) {
require_capability('report/stats:view', $context);
} else {
require_capability('report/stats:view', $coursecontext);
}

add_to_log($course->id, 'course', 'report stats', "report/stats/graph.php?userid=$userid&id=$course->id&mode=$mode&roleid=$roleid", $course->id);

stats_check_uptodate($course->id);

$param = stats_get_parameters($time,$report,$course->id,$mode);
Expand Down
1 change: 0 additions & 1 deletion report/stats/index.php
Expand Up @@ -24,7 +24,6 @@
*/

require_once('../../config.php');
require_once($CFG->dirroot.'/lib/statslib.php');
require_once($CFG->dirroot.'/report/stats/locallib.php');
require_once($CFG->libdir.'/adminlib.php');

Expand Down
60 changes: 56 additions & 4 deletions report/stats/lib.php
Expand Up @@ -35,13 +35,65 @@
* @param stdClass $context The context of the course
*/
function report_stats_extend_navigation_course($navigation, $course, $context) {
global $CFG, $OUTPUT;
global $CFG;
if (!empty($CFG->enablestats)) {
return;
}
if (has_capability('report/stats:view', $context)) {
if (!empty($CFG->enablestats)) {
$url = new moodle_url('/report/stats/index.php', array('course'=>$course->id));
$navigation->add(get_string('pluginname', 'report_stats'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
$url = new moodle_url('/report/stats/index.php', array('course'=>$course->id));
$navigation->add(get_string('pluginname', 'report_stats'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
}
}

/**
* This function extends the course navigation with the report items
*
* @param navigation_node $navigation The navigation node to extend
* @param stdClass $user
* @param stdClass $course The course to object for the report
*/
function report_stats_extend_navigation_user($navigation, $user, $course) {
global $CFG;
if (!empty($CFG->enablestats)) {
return;
}
if (report_stats_can_access_user_report($user, $course)) {
$url = new moodle_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
$navigation->add(get_string('stats'), $url);
}
}

/**
* Is current user allowed to access this report
*
* @private defined in lib.php for performance reasons
*
* @param stdClass $user
* @param stdClass $course
* @return bool
*/
function report_stats_can_access_user_report($user, $course) {
global $USER;

$coursecontext = context_course::instance($course->id);
$personalcontext = context_user::instance($user->id);

if (has_capability('report/stats:view', $coursecontext)) {
return true;
}

if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) {
return true;
}

} else if ($user->id == $USER->id) {
if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) {
return true;
}
}

return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion report/stats/locallib.php
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die;

require_once(dirname(__FILE__).'/lib.php');

require_once($CFG->dirroot.'/lib/statslib.php');

function report_stats_mode_menu($course, $mode, $time, $url) {
global $CFG, $OUTPUT;
Expand Down

0 comments on commit beda8fa

Please sign in to comment.