MDL-36206 - Unit Test - Create unit test for pagination bar
[moodle.git] / report / stats / user.php
blob28b8ac39a32fb6f3fa296ddefabe1a8efeba4e5f
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 add_to_log($course->id, 'course', 'report stats', "report/stats/user.php?id=$user->id&course=$course->id", $course->id);
54 $stractivityreport = get_string('activityreport');
56 $PAGE->set_pagelayout('admin');
57 $PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
58 $PAGE->navigation->extend_for_user($user);
59 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
60 $PAGE->set_title("$course->shortname: $stractivityreport");
61 $PAGE->set_heading($course->fullname);
62 echo $OUTPUT->header();
65 if (empty($CFG->enablestats)) {
66 print_error('statsdisable', 'error');
69 $statsstatus = stats_check_uptodate($course->id);
70 if ($statsstatus !== NULL) {
71 echo $OUTPUT->notification($statsstatus);
74 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}');
75 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}');
76 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}');
78 if (empty($earliestday)) {
79 $earliestday = time();
81 if (empty($earliestweek)) {
82 $earliestweek = time();
84 if (empty($earliestmonth)) {
85 $earliestmonth = time();
88 $now = stats_get_base_daily();
89 $lastweekend = stats_get_base_weekly();
90 $lastmonthend = stats_get_base_monthly();
92 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
94 if (empty($timeoptions)) {
95 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
98 // use the earliest.
99 $time = array_pop(array_keys($timeoptions));
101 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
102 $params = $param->params;
104 $param->table = 'user_'.$param->table;
106 $sql = 'SELECT timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
107 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
108 .' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
109 .' ORDER BY timeend DESC';
110 $stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
112 if (empty($stats)) {
113 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
116 if (!empty($CFG->gdversion)) {
117 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>';
120 // What the heck is this about? -- MD
121 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
123 $table = new html_table();
124 $table->align = array('left','center','center','center');
125 $param->table = str_replace('user_','',$param->table);
126 switch ($param->table) {
127 case 'daily' : $period = get_string('day'); break;
128 case 'weekly' : $period = get_string('week'); break;
129 case 'monthly': $period = get_string('month', 'form'); break;
130 default : $period = '';
132 $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
133 foreach ($stats as $stat) {
134 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
135 continue;
137 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
138 $a[] = $stat->line2;
139 $a[] = $stat->line3;
140 $table->data[] = $a;
142 echo html_writer::table($table);
145 echo $OUTPUT->footer();