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/**
18 * Deprecated analyser for testing purposes.
19 *
20 * @package   core_analytics
21 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Deprecated analyser for testing purposes.
29 *
30 * @package   core_analytics
31 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
32 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class deprecated_analyser extends \core_analytics\local\analyser\base {
35
36    /**
37     * Implementation of a deprecated method.
38     *
39     * It should be called by get_analysables_iterator, which triggers a debugging message.
40     * @return \core_analytics\analysable[]
41     */
42    public function get_analysables() {
43        $analysable = new \core_analytics\site();
44        return [SYSCONTEXTID => $analysable];
45    }
46
47    /**
48     * Samples origin is course table.
49     *
50     * @return string
51     */
52    public function get_samples_origin() {
53        return 'user';
54    }
55
56    /**
57     * Returns the sample analysable
58     *
59     * @param int $sampleid
60     * @return \core_analytics\analysable
61     */
62    public function get_sample_analysable($sampleid) {
63        return new \core_analytics\site();
64    }
65
66    /**
67     * Data this analyer samples provide.
68     *
69     * @return string[]
70     */
71    protected function provided_sample_data() {
72        return array('user');
73    }
74
75    /**
76     * Returns the sample context.
77     *
78     * @param int $sampleid
79     * @return \context
80     */
81    public function sample_access_context($sampleid) {
82        return \context_system::instance();
83    }
84
85    /**
86     * Returns all site courses.
87     *
88     * @param \core_analytics\analysable $site
89     * @return array
90     */
91    public function get_all_samples(\core_analytics\analysable $site) {
92        global $DB;
93
94        $users = $DB->get_records('user');
95        $userids = array_keys($users);
96        $sampleids = array_combine($userids, $userids);
97
98        $users = array_map(function($user) {
99            return array('user' => $user);
100        }, $users);
101
102        return array($sampleids, $users);
103    }
104
105    /**
106     * Return all complete samples data from sample ids.
107     *
108     * @param int[] $sampleids
109     * @return array
110     */
111    public function get_samples($sampleids) {
112        global $DB;
113
114        list($userssql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
115        $users = $DB->get_records_select('user', "id {$userssql}", $params);
116        $userids = array_keys($users);
117        $sampleids = array_combine($userids, $userids);
118
119        $users = array_map(function($user) {
120            return array('user' => $user);
121        }, $users);
122
123        return array($sampleids, $users);
124    }
125
126    /**
127     * Returns the description of a sample.
128     *
129     * @param int $sampleid
130     * @param int $contextid
131     * @param array $sampledata
132     * @return array array(string, \renderable)
133     */
134    public function sample_description($sampleid, $contextid, $sampledata) {
135        $description = fullname($sampledata['user']);
136        $userimage = new \pix_icon('i/user', get_string('user'));
137        return array($description, $userimage);
138    }
139
140    /**
141     * We need to delete associated data if a user requests his data to be deleted.
142     *
143     * @return bool
144     */
145    public function processes_user_data() {
146        return true;
147    }
148
149    /**
150     * Join the samples origin table with the user id table.
151     *
152     * @param string $sampletablealias
153     * @return string
154     */
155    public function join_sample_user($sampletablealias) {
156        return "JOIN {user} u ON u.id = {$sampletablealias}.sampleid";
157    }
158}