MDL-59057 analytics: Unit test fixes
[moodle.git] / analytics / classes / local / analyser / courses.php
blob5db36b1987bf039aac76ec28c1b9d1e705b7e094
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 * Courses analyser working at course level (insights for the course teachers).
20 * @package core_analytics
21 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_analytics\local\analyser;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Courses analyser working at course level (insights for the course teachers).
32 * @package core_analytics
33 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class courses extends by_course {
38 public function get_samples_origin() {
39 return 'course';
42 /**
43 * get_sample_analysable
45 * @param int $sampleid
46 * @return \core_analytics\analysable
48 public function get_sample_analysable($sampleid) {
49 return new \core_analytics\course($sampleid);
52 protected function provided_sample_data() {
53 return array('course', 'context');
56 public function sample_access_context($sampleid) {
57 return \context_course::instance($sampleid);
60 /**
61 * get_all_samples
63 * @param \core_analytics\analysable $course
64 * @return array
66 protected function get_all_samples(\core_analytics\analysable $course) {
67 global $DB;
69 $context = \context_course::instance($course->get_id());
71 // Just 1 sample per analysable.
72 return array(
73 array($course->get_id() => $course->get_id()),
74 array($course->get_id() => array('course' => $course->get_course_data(), 'context' => $context))
78 /**
79 * get_samples
81 * @param int[] $sampleids
82 * @return array
84 public function get_samples($sampleids) {
85 global $DB;
87 list($sql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
88 $courses = $DB->get_records_select('course', "id $sql", $params);
90 $courseids = array_keys($courses);
91 $sampleids = array_combine($courseids, $courseids);
93 $courses = array_map(function($course) {
94 return array('course' => $course, 'context' => \context_course::instance($course->id));
95 }, $courses);
97 // No related data attached.
98 return array($sampleids, $courses);
102 * sample_description
104 * @param int $sampleid
105 * @param int $contextid
106 * @param array $sampledata
107 * @return array
109 public function sample_description($sampleid, $contextid, $sampledata) {
110 $description = format_string($sampledata['course']->fullname, true, array('context' => $sampledata['context']));
111 $courseimage = new \pix_icon('i/course', get_string('course'));
112 return array($description, $courseimage);