MDL-36983 fix incorrect test method name
[moodle.git] / report / log / graph.php
blob842f84706c1bc1b7fe1ba8d216a2bee4fccbbf50
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 * Produces a graph of log accesses for a user
20 * Generates an image representing the log data in a graphical manner for a user.
22 * @package report_log
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require("../../config.php");
28 require_once("$CFG->libdir/graphlib.php");
29 require_once($CFG->dirroot.'/report/log/locallib.php');
31 $id = required_param('id', PARAM_INT); // Course ID
32 $type = required_param('type', PARAM_FILE); // Graph Type
33 $user = required_param('user', PARAM_INT); // Student ID
34 $date = optional_param('date', 0, PARAM_INT); // A time of a day (in GMT)
36 $url = new moodle_url('/report/log/graph.php', array('id'=>$id,'type'=>$type,'user'=>$user,'date'=>$date));
37 $PAGE->set_url($url);
39 if ($type !== "usercourse.png" and $type !== "userday.png") {
40 $type = 'userday.png';
43 $course = $DB->get_record("course", array("id"=>$id), '*', MUST_EXIST);
44 $user = $DB->get_record("user", array("id"=>$user, 'deleted'=>0), '*', MUST_EXIST);
46 $coursecontext = context_course::instance($course->id);
47 $personalcontext = context_user::instance($user->id);
49 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
50 and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
51 //TODO: do not require parents to be enrolled in courses - this is a hack!
52 require_login();
53 $PAGE->set_course($course);
54 } else {
55 require_login($course);
58 list($all, $today) = report_log_can_access_user_report($user, $course);
60 if ($type === "userday.png") {
61 if (!$today) {
62 require_capability('report/log:viewtoday', $coursecontext);
64 } else {
65 if (!$all) {
66 require_capability('report/log:view', $coursecontext);
70 add_to_log($course->id, 'course', 'report log', "report/log/graph.php?user=$user->id&id=$course->id&type=$type&date=$date", $course->id);
72 $logs = array();
74 $timenow = time();
76 if ($type === "usercourse.png") {
78 $site = get_site();
80 if ($course->id == $site->id) {
81 $courseselect = 0;
82 } else {
83 $courseselect = $course->id;
86 $maxseconds = REPORT_LOG_MAX_DISPLAY * 3600 * 24; // seconds
87 //$maxseconds = 60 * 3600 * 24; // seconds
88 if ($timenow - $course->startdate > $maxseconds) {
89 $course->startdate = $timenow - $maxseconds;
92 if (!empty($CFG->loglifetime)) {
93 $maxseconds = $CFG->loglifetime * 3600 * 24; // seconds
94 if ($timenow - $course->startdate > $maxseconds) {
95 $course->startdate = $timenow - $maxseconds;
99 $timestart = $coursestart = usergetmidnight($course->startdate);
101 if ((($timenow - $timestart)/86400.0) > 40) {
102 $reducedays = 7;
103 } else {
104 $reducedays = 0;
107 $days = array();
108 $i = 0;
109 while ($timestart < $timenow) {
110 $timefinish = $timestart + 86400;
111 if ($reducedays) {
112 if ($i % $reducedays) {
113 $days[$i] = "";
114 } else {
115 $days[$i] = userdate($timestart, "%a %d %b");
117 } else {
118 $days[$i] = userdate($timestart, "%a %d %b");
120 $logs[$i] = 0;
121 $i++;
122 $timestart = $timefinish;
125 if ($rawlogs = get_logs_usercourse($user->id, $courseselect, $coursestart)) {
126 foreach ($rawlogs as $rawlog) {
127 $logs[$rawlog->day] = $rawlog->num;
131 $graph = new graph(750, 400);
133 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
134 $a->username = fullname($user, true);
135 $graph->parameter['title'] = get_string("hitsoncourse", "", $a);
137 $graph->x_data = $days;
139 $graph->y_data['logs'] = $logs;
140 $graph->y_order = array('logs');
142 if (!empty($CFG->preferlinegraphs)) {
143 $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
144 } else {
145 $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.6);
146 $graph->parameter['bar_spacing'] = 0;
150 $graph->parameter['y_label_left'] = get_string("hits");
151 $graph->parameter['label_size'] = "12";
152 $graph->parameter['x_axis_angle'] = 90;
153 $graph->parameter['x_label_angle'] = 0;
154 $graph->parameter['tick_length'] = 0;
157 $graph->parameter['shadow'] = 'none';
159 error_reporting(5); // ignore most warnings such as font problems etc
160 $graph->draw_stack();
162 } else {
164 $site = get_site();
166 if ($course->id == $site->id) {
167 $courseselect = 0;
168 } else {
169 $courseselect = $course->id;
172 if ($date) {
173 $daystart = usergetmidnight($date);
174 } else {
175 $daystart = usergetmidnight(time());
177 $dayfinish = $daystart + 86400;
179 $hours = array();
180 for ($i=0; $i<=23; $i++) {
181 $logs[$i] = 0;
182 $hour = $daystart + $i * 3600;
183 $hours[$i] = $i;
186 if ($rawlogs = get_logs_userday($user->id, $courseselect, $daystart)) {
187 foreach ($rawlogs as $rawlog) {
188 $logs[$rawlog->hour] = $rawlog->num;
192 $graph = new graph(750, 400);
194 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
195 $a->username = fullname($user, true);
196 $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);
198 $graph->x_data = $hours;
200 $graph->y_data['logs'] = $logs;
201 $graph->y_order = array('logs');
203 if (!empty($CFG->preferlinegraphs)) {
204 $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
205 } else {
206 $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.9);
209 $graph->parameter['y_label_left'] = get_string("hits");
210 $graph->parameter['label_size'] = "12";
211 $graph->parameter['x_axis_angle'] = 0;
212 $graph->parameter['x_label_angle'] = 0;
214 $graph->parameter['shadow'] = 'none';
216 error_reporting(5); // ignore most warnings such as font problems etc
217 $graph->draw_stack();