adding some strings
[moodle-linuxchix.git] / course / report / stats / graph.php
blob1c1c18c09d21d859bee09df68e4ddae4551feeae
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 $c = array_keys($graph->colour);
65 if (empty($param->crosstab)) {
66 foreach ($stats as $stat) {
67 $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
68 $graph->y_data['line1'][] = $stat->line1;
69 if (isset($stat->line2)) {
70 $graph->y_data['line2'][] = $stat->line2;
72 if (isset($stat->line3)) {
73 $graph->y_data['line3'][] = $stat->line3;
76 $graph->y_order = array('line1');
77 $graph->y_format['line1'] = array('colour' => $c[1],'line' => 'line','legend' => $param->line1);
78 if (!empty($param->line2)) {
79 $graph->y_order[] = 'line2';
80 $graph->y_format['line2'] = array('colour' => $c[2],'line' => 'line','legend' => $param->line2);
82 if (!empty($param->line3)) {
83 $graph->y_order[] = 'line3';
84 $graph->y_format['line3'] = array('colour' => $c[3],'line' => 'line','legend' => $param->line3);
86 $graph->y_tick_labels = false;
87 } else {
88 $data = array();
89 $times = array();
90 $roles = array();
91 $missedlines = array();
92 foreach ($stats as $stat) {
93 $data[$stat->roleid][$stat->timeend] = $stat->line1;
94 if (!empty($stat->zerofixed)) {
95 $missedlines[] = $stat->timeend;
97 if ($stat->roleid != 0) {
98 if (!array_key_exists($stat->roleid,$roles)) {
99 $roles[$stat->roleid] = get_field('role','name','id',$stat->roleid);
102 if (!array_key_exists($stat->timeend,$times)) {
103 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
106 foreach (array_keys($times) as $t) {
107 foreach ($data as $roleid => $stuff) {
108 if (!array_key_exists($t, $stuff)) {
109 $data[$roleid][$t] = 0;
113 foreach ($data as $role => $stuff) {
114 ksort($data[$role]);
116 $nonzeroroleid = 0;
117 foreach (array_keys($data) as $roleid) {
118 if ($roleid == 0) {
119 continue;
121 $graph->y_order[] = $roleid;
122 $graph->y_format[$roleid] = array('colour' => $c[$roleid], 'line' => 'line','legend' => $roles[$roleid]);
123 $nonzeroroleid = $roleid;
125 foreach (array_keys($data[$nonzeroroleid]) as $time) {
126 $graph->x_data[] = $times[$time];
128 foreach ($data as $roleid => $t) {
129 if ($roleid == 0) {
130 continue;
132 foreach ($t as $time => $data) {
133 $graph->y_data[$roleid][] = $data;
138 $graph->draw_stack();