MDL-42670 mod_assign: Fix recent activity block when blind marking is on
[moodle.git] / report / stats / graph.php
blob8505e33ac96d31651a9642a9897396a23add9746
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 * This file is part of the User section Moodle
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');
28 require_once($CFG->dirroot.'/lib/graphlib.php');
30 $courseid = required_param('course', PARAM_INT);
31 $report = required_param('report', PARAM_INT);
32 $time = required_param('time', PARAM_INT);
33 $mode = required_param('mode', PARAM_INT);
34 $userid = optional_param('userid', 0, PARAM_INT);
35 $roleid = optional_param('roleid',0,PARAM_INT);
37 $url = new moodle_url('/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode, 'userid'=>$userid, 'roleid'=>$roleid));
38 $PAGE->set_url($url);
40 $course = $DB->get_record("course", array("id"=>$courseid), '*', MUST_EXIST);
41 $coursecontext = context_course::instance($course->id);
42 $PAGE->set_context($coursecontext);
44 if (!empty($userid)) {
45 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
46 $personalcontext = context_user::instance($user->id);
48 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
49 and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
50 //TODO: do not require parents to be enrolled in courses - this is a hack!
51 require_login();
52 $PAGE->set_course($course);
53 } else {
54 require_login($course);
57 if (!report_stats_can_access_user_report($user, $course, true)) {
58 require_capability('report/stats:view', $coursecontext);
60 } else if ($mode === STATS_MODE_DETAILED) {
61 print_error('invaliduser');
62 } else {
63 require_capability('report/stats:view', $coursecontext);
66 stats_check_uptodate($course->id);
68 $param = stats_get_parameters($time,$report,$course->id,$mode);
70 if (!empty($userid)) {
71 $param->table = 'user_'.$param->table;
74 // TODO: cleanup this ugly mess!
75 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
76 .' FROM {stats_'.$param->table.'} WHERE '
77 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
78 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
79 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
80 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
81 .' timeend >= '.$param->timeafter
82 .' '.$param->extras
83 .' ORDER BY timeend DESC';
85 $stats = $DB->get_records_sql($sql, $param->params);
87 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
89 $stats = array_reverse($stats);
91 $graph = new graph(750,400);
93 $graph->parameter['legend'] = 'outside-right';
94 $graph->parameter['legend_size'] = 10;
95 $graph->parameter['x_axis_angle'] = 90;
96 $graph->parameter['title'] = false; // moodle will do a nicer job.
97 $graph->y_tick_labels = null;
99 if (empty($param->crosstab)) {
100 foreach ($stats as $stat) {
101 $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
102 $graph->y_data['line1'][] = $stat->line1;
103 if (isset($stat->line2)) {
104 $graph->y_data['line2'][] = $stat->line2;
106 if (isset($stat->line3)) {
107 $graph->y_data['line3'][] = $stat->line3;
110 $graph->y_order = array('line1');
111 $graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
112 if (!empty($param->line2)) {
113 $graph->y_order[] = 'line2';
114 $graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
116 if (!empty($param->line3)) {
117 $graph->y_order[] = 'line3';
118 $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
120 $graph->y_tick_labels = false;
122 } else {
123 $data = array();
124 $times = array();
125 $roles = array();
126 $missedlines = array();
127 $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true);
128 foreach ($stats as $stat) {
129 $data[$stat->roleid][$stat->timeend] = $stat->line1;
130 if (!empty($stat->zerofixed)) {
131 $missedlines[] = $stat->timeend;
133 if ($stat->roleid != 0) {
134 if (!array_key_exists($stat->roleid,$roles)) {
135 $roles[$stat->roleid] = $rolenames[$stat->roleid];
137 } else {
138 if (!array_key_exists($stat->roleid,$roles)) {
139 $roles[$stat->roleid] = get_string('all');
142 if (!array_key_exists($stat->timeend,$times)) {
143 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
146 foreach (array_keys($times) as $t) {
147 foreach ($data as $roleid => $stuff) {
148 if (!array_key_exists($t, $stuff)) {
149 $data[$roleid][$t] = 0;
154 $roleid = 0;
155 krsort($roles); // the same sorting as in table below graph
157 $colors = array('green', 'blue', 'red', 'purple', 'yellow', 'olive', 'navy', 'maroon', 'gray', 'ltred', 'ltltred', 'ltgreen', 'ltltgreen', 'orange', 'ltorange', 'ltltorange', 'lime', 'ltblue', 'ltltblue', 'fuchsia', 'aqua', 'grayF0', 'grayEE', 'grayDD', 'grayCC', 'gray33', 'gray66', 'gray99');
158 $colorindex = 0;
160 foreach ($roles as $roleid=>$rname) {
161 ksort($data[$roleid]);
162 $graph->y_order[] = $roleid+1;
163 if ($roleid) {
164 $color = $colors[$colorindex++];
165 $colorindex = $colorindex % count($colors);
166 } else {
167 $color = 'black';
169 $graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
171 foreach (array_keys($data[$roleid]) as $time) {
172 $graph->x_data[] = $times[$time];
174 foreach ($data as $roleid => $t) {
175 foreach ($t as $time => $data) {
176 $graph->y_data[$roleid+1][] = $data;
181 $graph->draw_stack();