PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/course/report/stats/graph.php

https://github.com/kpike/moodle
PHP | 183 lines | 136 code | 25 blank | 22 comment | 25 complexity | 44b531d53795b9af547607edbdae559f MD5 | raw file
  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.
  13. //
  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/>.
  16. /**
  17. * This file is part of the User section Moodle
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package course
  22. */
  23. require_once('../../../config.php');
  24. require_once($CFG->dirroot.'/lib/statslib.php');
  25. require_once($CFG->dirroot.'/lib/graphlib.php');
  26. $courseid = required_param('course', PARAM_INT);
  27. $report = required_param('report', PARAM_INT);
  28. $time = required_param('time', PARAM_INT);
  29. $mode = required_param('mode', PARAM_INT);
  30. $userid = optional_param('userid', 0, PARAM_INT);
  31. $roleid = optional_param('roleid',0,PARAM_INT);
  32. $url = new moodle_url('/course/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode));
  33. if ($userid !== 0) {
  34. $url->param('userid', $userid);
  35. }
  36. if ($roleid !== 0) {
  37. $url->param('roleid', $roleid);
  38. }
  39. $PAGE->set_url($url);
  40. if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
  41. print_error("invalidcourseid");
  42. }
  43. if (!empty($userid)) {
  44. if (!$user = $DB->get_record('user', array('id'=>$userid))) {
  45. print_error("nousers");
  46. }
  47. }
  48. require_login($course);
  49. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  50. if (!$course->showreports or $USER->id != $userid) {
  51. require_capability('coursereport/stats:view', $context);
  52. }
  53. stats_check_uptodate($course->id);
  54. $param = stats_get_parameters($time,$report,$course->id,$mode);
  55. if (!empty($userid)) {
  56. $param->table = 'user_'.$param->table;
  57. }
  58. // TODO: cleanup this ugly mess!
  59. $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
  60. .' FROM {stats_'.$param->table.'} WHERE '
  61. .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
  62. .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
  63. .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
  64. . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
  65. .' timeend >= '.$param->timeafter
  66. .' '.$param->extras
  67. .' ORDER BY timeend DESC';
  68. $stats = $DB->get_records_sql($sql, $param->params);
  69. $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
  70. $stats = array_reverse($stats);
  71. $graph = new graph(750,400);
  72. $graph->parameter['legend'] = 'outside-right';
  73. $graph->parameter['legend_size'] = 10;
  74. $graph->parameter['x_axis_angle'] = 90;
  75. $graph->parameter['title'] = false; // moodle will do a nicer job.
  76. $graph->y_tick_labels = null;
  77. if (empty($param->crosstab)) {
  78. foreach ($stats as $stat) {
  79. $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
  80. $graph->y_data['line1'][] = $stat->line1;
  81. if (isset($stat->line2)) {
  82. $graph->y_data['line2'][] = $stat->line2;
  83. }
  84. if (isset($stat->line3)) {
  85. $graph->y_data['line3'][] = $stat->line3;
  86. }
  87. }
  88. $graph->y_order = array('line1');
  89. $graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
  90. if (!empty($param->line2)) {
  91. $graph->y_order[] = 'line2';
  92. $graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
  93. }
  94. if (!empty($param->line3)) {
  95. $graph->y_order[] = 'line3';
  96. $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
  97. }
  98. $graph->y_tick_labels = false;
  99. } else {
  100. $data = array();
  101. $times = array();
  102. $roles = array();
  103. $missedlines = array();
  104. $rolenames = get_all_roles();
  105. foreach ($rolenames as $r) {
  106. $rolenames[$r->id] = $r->name;
  107. }
  108. $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id));
  109. foreach ($stats as $stat) {
  110. $data[$stat->roleid][$stat->timeend] = $stat->line1;
  111. if (!empty($stat->zerofixed)) {
  112. $missedlines[] = $stat->timeend;
  113. }
  114. if ($stat->roleid != 0) {
  115. if (!array_key_exists($stat->roleid,$roles)) {
  116. $roles[$stat->roleid] = $rolenames[$stat->roleid];
  117. }
  118. } else {
  119. if (!array_key_exists($stat->roleid,$roles)) {
  120. $roles[$stat->roleid] = get_string('all');
  121. }
  122. }
  123. if (!array_key_exists($stat->timeend,$times)) {
  124. $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
  125. }
  126. }
  127. foreach (array_keys($times) as $t) {
  128. foreach ($data as $roleid => $stuff) {
  129. if (!array_key_exists($t, $stuff)) {
  130. $data[$roleid][$t] = 0;
  131. }
  132. }
  133. }
  134. $roleid = 0;
  135. krsort($roles); // the same sorting as in table bellow graph
  136. $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');
  137. $colorindex = 0;
  138. foreach ($roles as $roleid=>$rname) {
  139. ksort($data[$roleid]);
  140. $graph->y_order[] = $roleid+1;
  141. if ($roleid) {
  142. $color = $colors[$colorindex++];
  143. $colorindex = $colorindex % count($colors);
  144. } else {
  145. $color = 'black';
  146. }
  147. $graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
  148. }
  149. foreach (array_keys($data[$roleid]) as $time) {
  150. $graph->x_data[] = $times[$time];
  151. }
  152. foreach ($data as $roleid => $t) {
  153. foreach ($t as $time => $data) {
  154. $graph->y_data[$roleid+1][] = $data;
  155. }
  156. }
  157. }
  158. $graph->draw_stack();