Merge branch 'wip-MDL-41921-master' of git://github.com/marinaglancy/moodle
[moodle.git] / report / stats / user.php
blob42364af6344a7e9f4b5f08881759a4752081afcc
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Display user activity reports for a course (totals)
20 * @package report
21 * @subpackage stats
22 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../../config.php');
27 require_once($CFG->dirroot.'/report/stats/locallib.php');
29 $userid = required_param('id', PARAM_INT);
30 $courseid = required_param('course', PARAM_INT);
32 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
33 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
35 $coursecontext = context_course::instance($course->id);
36 $personalcontext = context_user::instance($user->id);
38 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
39 and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
40 //TODO: do not require parents to be enrolled in courses - this is a hack!
41 require_login();
42 $PAGE->set_course($course);
43 } else {
44 require_login($course);
47 if (!report_stats_can_access_user_report($user, $course, true)) {
48 // this should never happen
49 error('Can not access user statistics report');
52 $stractivityreport = get_string('activityreport');
54 $PAGE->set_pagelayout('admin');
55 $PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
56 $PAGE->navigation->extend_for_user($user);
57 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
58 $PAGE->set_title("$course->shortname: $stractivityreport");
59 $PAGE->set_heading($course->fullname);
60 echo $OUTPUT->header();
62 // Trigger a content view event.
63 $event = \report_stats\event\content_viewed::create(array('courseid' => $course->id,
64 'other' => array('content' => 'user stats')));
65 $event->set_page_detail();
66 $event->set_legacy_logdata(array($course->id, 'course', 'report stats',
67 "report/stats/user.php?id=$user->id&course=$course->id", $course->id));
68 $event->trigger();
70 if (empty($CFG->enablestats)) {
71 print_error('statsdisable', 'error');
74 $statsstatus = stats_check_uptodate($course->id);
75 if ($statsstatus !== NULL) {
76 echo $OUTPUT->notification($statsstatus);
79 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}');
80 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}');
81 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}');
83 if (empty($earliestday)) {
84 $earliestday = time();
86 if (empty($earliestweek)) {
87 $earliestweek = time();
89 if (empty($earliestmonth)) {
90 $earliestmonth = time();
93 $now = stats_get_base_daily();
94 $lastweekend = stats_get_base_weekly();
95 $lastmonthend = stats_get_base_monthly();
97 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
99 if (empty($timeoptions)) {
100 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
103 // use the earliest.
104 $time = array_pop(array_keys($timeoptions));
106 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
107 $params = $param->params;
109 $param->table = 'user_'.$param->table;
111 $sql = 'SELECT timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
112 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
113 .' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
114 .' ORDER BY timeend DESC';
115 $stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
117 if (empty($stats)) {
118 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
121 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>';
123 // What the heck is this about? -- MD
124 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
126 $table = new html_table();
127 $table->align = array('left','center','center','center');
128 $param->table = str_replace('user_','',$param->table);
129 switch ($param->table) {
130 case 'daily' : $period = get_string('day'); break;
131 case 'weekly' : $period = get_string('week'); break;
132 case 'monthly': $period = get_string('month', 'form'); break;
133 default : $period = '';
135 $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
136 foreach ($stats as $stat) {
137 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
138 continue;
140 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
141 $a[] = $stat->line2;
142 $a[] = $stat->line3;
143 $table->data[] = $a;
145 echo html_writer::table($table);
148 echo $OUTPUT->footer();