locallib.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 contains functions used by the course overview report.
  18. *
  19. * @package report_courseoverview
  20. * @copyright 2016 Simey Lameze <simey@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die;
  24. require_once('../../config.php');
  25. require_once($CFG->dirroot . '/lib/statslib.php');
  26. /**
  27. * Gather course overview data and print the chart.
  28. *
  29. * @param int $report represents the report type field on the course overview report filter.
  30. * @param int $time represents the time period field on the course overview report filter.
  31. * @param int $numcourses represents the number of courses field on the course overview report filter.
  32. * @return void
  33. */
  34. function report_courseoverview_print_chart($report, $time, $numcourses) {
  35. global $DB, $OUTPUT, $PAGE;
  36. $param = stats_get_parameters($time, $report, SITEID, STATS_MODE_RANKED);
  37. if (!empty($param->sql)) {
  38. $sql = $param->sql;
  39. } else {
  40. $sql = "SELECT courseid, $param->fields
  41. FROM {" . 'stats_' . $param->table . "}
  42. WHERE timeend >= $param->timeafter
  43. AND stattype = 'activity'
  44. AND roleid = 0
  45. GROUP BY courseid
  46. $param->extras
  47. ORDER BY $param->orderby";
  48. }
  49. $courses = $DB->get_records_sql($sql, $param->params, 0, $numcourses);
  50. if (empty($courses)) {
  51. $PAGE->set_url('/report/courseoverview/index.php');
  52. print_error('statsnodata', 'error', $PAGE->url->out());
  53. }
  54. $data = [];
  55. $i = 0;
  56. foreach ($courses as $c) {
  57. $data['labels'][$i] = $DB->get_field('course', 'shortname', array('id' => $c->courseid));
  58. // Line3 represents the third column of the report table except for the most active users report.
  59. // It is a float number and can be participation radio or activity per user.
  60. if (isset($c->line3)) {
  61. $data['series'][$i] = round($c->line3, 2);
  62. } else {
  63. $data['series'][$i] = $c->{$param->graphline};
  64. }
  65. $i++;
  66. }
  67. $chart = new \core\chart_bar();
  68. $series = new \core\chart_series($param->{$param->graphline}, $data['series']);
  69. $chart->add_series($series);
  70. $chart->set_labels($data['labels']);
  71. echo $OUTPUT->render($chart);
  72. }