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