2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Display user activity reports for a course (totals)
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 $pageheading = $course->fullname
;
39 $userfullname = fullname($user);
40 if ($courseid == SITEID
) {
41 $PAGE->set_context($personalcontext);
42 $pageheading = $userfullname;
45 if ($USER->id
!= $user->id
and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
46 and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
47 //TODO: do not require parents to be enrolled in courses - this is a hack!
49 $PAGE->set_course($course);
51 require_login($course);
54 if (!report_stats_can_access_user_report($user, $course, true)) {
55 // this should never happen
56 print_error('nocapability', 'report_stats');
59 $stractivityreport = get_string('activityreport');
61 $PAGE->set_pagelayout('report');
62 $PAGE->set_url('/report/stats/user.php', array('id'=>$user->id
, 'course'=>$course->id
));
63 $PAGE->navigation
->extend_for_user($user);
64 $PAGE->navigation
->set_userid_for_parent_checks($user->id
); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
66 $navigationnode = array(
67 'name' => get_string('stats'),
68 'url' => new moodle_url('/report/stats/user.php', array('id' => $user->id
, 'course' => $course->id
))
70 $PAGE->add_report_nodes($user->id
, $navigationnode);
72 $PAGE->set_title("$course->shortname: $stractivityreport");
73 $PAGE->set_heading($pageheading);
74 echo $OUTPUT->header();
75 if ($courseid != SITEID
) {
76 echo $OUTPUT->context_header(
78 'heading' => $userfullname,
80 'usercontext' => $personalcontext
84 // Trigger a user report viewed event.
85 $event = \report_stats\event\user_report_viewed
::create(array('context' => $coursecontext, 'relateduserid' => $user->id
));
88 if (empty($CFG->enablestats
)) {
89 print_error('statsdisable', 'error');
92 $statsstatus = stats_check_uptodate($course->id
);
93 if ($statsstatus !== NULL) {
94 echo $OUTPUT->notification($statsstatus);
97 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}');
98 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}');
99 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}');
101 if (empty($earliestday)) {
102 $earliestday = time();
104 if (empty($earliestweek)) {
105 $earliestweek = time();
107 if (empty($earliestmonth)) {
108 $earliestmonth = time();
111 $now = stats_get_base_daily();
112 $lastweekend = stats_get_base_weekly();
113 $lastmonthend = stats_get_base_monthly();
115 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
117 if (empty($timeoptions)) {
118 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
122 $timekeys = array_keys($timeoptions);
123 $time = array_pop($timekeys);
125 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW
,$course->id
,STATS_MODE_DETAILED
);
126 $params = $param->params
;
128 $param->table
= 'user_'.$param->table
;
130 $sql = 'SELECT id, timeend,'.$param->fields
.' FROM {stats_'.$param->table
.'} WHERE '
131 .(($course->id
== SITEID
) ?
'' : ' courseid = '.$course->id
.' AND ')
132 .' userid = '.$user->id
.' AND timeend >= '.$param->timeafter
.$param->extras
133 .' ORDER BY timeend DESC';
134 $stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
137 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
140 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>';
142 // What the heck is this about? -- MD
143 $stats = stats_fix_zeros($stats,$param->timeafter
,$param->table
,(!empty($param->line2
)),(!empty($param->line3
)));
145 $table = new html_table();
146 $table->align
= array('left','center','center','center');
147 $param->table
= str_replace('user_','',$param->table
);
148 switch ($param->table
) {
149 case 'daily' : $period = get_string('day'); break;
150 case 'weekly' : $period = get_string('week'); break;
151 case 'monthly': $period = get_string('month', 'form'); break;
152 default : $period = '';
154 $table->head
= array(get_string('periodending','moodle',$period),$param->line1
,$param->line2
,$param->line3
);
155 foreach ($stats as $stat) {
156 if (!empty($stat->zerofixed
)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
159 $a = array(userdate($stat->timeend
,get_string('strftimedate'),$CFG->timezone
),$stat->line1
);
164 echo html_writer
::table($table);
167 echo $OUTPUT->footer();