Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Test analyser
  19   *
  20   * @package   core
  21   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Test analyser
  29   *
  30   * @package   core
  31   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class test_site_users_analyser extends \core_analytics\local\analyser\sitewide {
  35  
  36      /**
  37       * Samples origin is course table.
  38       *
  39       * @return string
  40       */
  41      public function get_samples_origin() {
  42          return 'user';
  43      }
  44  
  45      /**
  46       * Returns the sample analysable
  47       *
  48       * @param int $sampleid
  49       * @return \core_analytics\analysable
  50       */
  51      public function get_sample_analysable($sampleid) {
  52          return new \core_analytics\site();
  53      }
  54  
  55      /**
  56       * Data this analyer samples provide.
  57       *
  58       * @return string[]
  59       */
  60      protected function provided_sample_data() {
  61          return array('user');
  62      }
  63  
  64      /**
  65       * Returns the sample context.
  66       *
  67       * @param int $sampleid
  68       * @return \context
  69       */
  70      public function sample_access_context($sampleid) {
  71          return \context_system::instance();
  72      }
  73  
  74      /**
  75       * Returns all site courses.
  76       *
  77       * @param \core_analytics\analysable $site
  78       * @return array
  79       */
  80      public function get_all_samples(\core_analytics\analysable $site) {
  81          global $DB;
  82  
  83          $users = $DB->get_records('user');
  84          $userids = array_keys($users);
  85          $sampleids = array_combine($userids, $userids);
  86  
  87          $users = array_map(function($user) {
  88              return array('user' => $user);
  89          }, $users);
  90  
  91          return array($sampleids, $users);
  92      }
  93  
  94      /**
  95       * Return all complete samples data from sample ids.
  96       *
  97       * @param int[] $sampleids
  98       * @return array
  99       */
 100      public function get_samples($sampleids) {
 101          global $DB;
 102  
 103          list($userssql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
 104          $users = $DB->get_records_select('user', "id {$userssql}", $params);
 105          $userids = array_keys($users);
 106          $sampleids = array_combine($userids, $userids);
 107  
 108          $users = array_map(function($user) {
 109              return array('user' => $user);
 110          }, $users);
 111  
 112          return array($sampleids, $users);
 113      }
 114  
 115      /**
 116       * Returns the description of a sample.
 117       *
 118       * @param int $sampleid
 119       * @param int $contextid
 120       * @param array $sampledata
 121       * @return array array(string, \renderable)
 122       */
 123      public function sample_description($sampleid, $contextid, $sampledata) {
 124          $description = fullname($sampledata['user']);
 125          $userimage = new \pix_icon('i/user', get_string('user'));
 126          return array($description, $userimage);
 127      }
 128  
 129      /**
 130       * We need to delete associated data if a user requests his data to be deleted.
 131       *
 132       * @return bool
 133       */
 134      public function processes_user_data() {
 135          return true;
 136      }
 137  
 138      /**
 139       * Join the samples origin table with the user id table.
 140       *
 141       * @param string $sampletablealias
 142       * @return string
 143       */
 144      public function join_sample_user($sampletablealias) {
 145          return "JOIN {user} u ON u.id = {$sampletablealias}.sampleid";
 146      }
 147  }