Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / course / report / stats / graph.php
blob3444708b2dd15babb76fdb6efa8a98fa0d26bacc
1 <?php //$Id$
3 require_once('../../../config.php');
4 require_once($CFG->dirroot.'/lib/statslib.php');
5 require_once($CFG->dirroot.'/lib/graphlib.php');
7 $courseid = required_param('course', PARAM_INT);
8 $report = required_param('report', PARAM_INT);
9 $time = required_param('time', PARAM_INT);
10 $mode = required_param('mode', PARAM_INT);
11 $userid = optional_param('userid', 0, PARAM_INT);
12 $roleid = optional_param('roleid',0,PARAM_INT);
14 if (!$course = get_record("course","id",$courseid)) {
15 error("That's an invalid course id");
18 if (!empty($userid)) {
19 if (!$user = get_record('user','id',$userid)) {
20 error("That's an invalid user id");
24 require_login();
25 $context = get_context_instance(CONTEXT_COURSE, $course->id);
27 if (!has_capability('moodle/site:viewreports', $context)) {
28 error('You need do not have the required permission to view reports for this course');
31 stats_check_uptodate($course->id);
33 $param = stats_get_parameters($time,$report,$course->id,$mode);
35 if (!empty($userid)) {
36 $param->table = 'user_'.$param->table;
39 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
40 .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
41 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
42 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
43 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
44 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
45 .' timeend >= '.$param->timeafter
46 .' '.$param->extras
47 .' ORDER BY timeend DESC';
49 $stats = get_records_sql($sql);
51 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
53 $stats = array_reverse($stats);
55 $graph = new graph(750,400);
57 $graph->parameter['legend'] = 'outside-right';
58 $graph->parameter['legend_size'] = 10;
59 $graph->parameter['x_axis_angle'] = 90;
60 $graph->parameter['title'] = false; // moodle will do a nicer job.
61 $graph->y_tick_labels = null;
63 if (empty($param->crosstab)) {
64 foreach ($stats as $stat) {
65 $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
66 $graph->y_data['line1'][] = $stat->line1;
67 if (isset($stat->line2)) {
68 $graph->y_data['line2'][] = $stat->line2;
70 if (isset($stat->line3)) {
71 $graph->y_data['line3'][] = $stat->line3;
74 $graph->y_order = array('line1');
75 $graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
76 if (!empty($param->line2)) {
77 $graph->y_order[] = 'line2';
78 $graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
80 if (!empty($param->line3)) {
81 $graph->y_order[] = 'line3';
82 $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
84 $graph->y_tick_labels = false;
86 } else {
87 $data = array();
88 $times = array();
89 $roles = array();
90 $missedlines = array();
91 $rolenames = get_all_roles();
92 foreach ($rolenames as $r) {
93 $rolenames[$r->id] = $r->name;
95 $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id));
96 foreach ($stats as $stat) {
97 $data[$stat->roleid][$stat->timeend] = $stat->line1;
98 if (!empty($stat->zerofixed)) {
99 $missedlines[] = $stat->timeend;
101 if ($stat->roleid != 0) {
102 if (!array_key_exists($stat->roleid,$roles)) {
103 $roles[$stat->roleid] = $rolenames[$stat->roleid];
105 } else {
106 if (!array_key_exists($stat->roleid,$roles)) {
107 $roles[$stat->roleid] = get_string('all');
110 if (!array_key_exists($stat->timeend,$times)) {
111 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
114 foreach (array_keys($times) as $t) {
115 foreach ($data as $roleid => $stuff) {
116 if (!array_key_exists($t, $stuff)) {
117 $data[$roleid][$t] = 0;
122 $roleid = 0;
123 krsort($roles); // the same sorting as in table bellow graph
125 $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');
126 $colorindex = 0;
128 foreach ($roles as $roleid=>$rname) {
129 ksort($data[$roleid]);
130 $graph->y_order[] = $roleid+1;
131 if ($roleid) {
132 $color = $colors[$colorindex++];
133 $colorindex = $colorindex % count($colors);
134 } else {
135 $color = 'black';
137 $graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
139 foreach (array_keys($data[$roleid]) as $time) {
140 $graph->x_data[] = $times[$time];
142 foreach ($data as $roleid => $t) {
143 foreach ($t as $time => $data) {
144 $graph->y_data[$roleid+1][] = $data;
149 $graph->draw_stack();