Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * Site courses analyser working at system level (insights for the site admin).
  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  namespace core\analytics\analyser;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Site courses analyser working at system level (insights for the site admin).
  31   *
  32   * @package   core
  33   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class site_courses extends \core_analytics\local\analyser\sitewide {
  37  
  38      /**
  39       * Samples origin is course table.
  40       *
  41       * @return string
  42       */
  43      public function get_samples_origin() {
  44          return 'course';
  45      }
  46  
  47      /**
  48       * Returns the sample analysable
  49       *
  50       * @param int $sampleid
  51       * @return \core_analytics\analysable
  52       */
  53      public function get_sample_analysable($sampleid) {
  54          return new \core_analytics\site();
  55      }
  56  
  57      /**
  58       * Data this analyer samples provide.
  59       *
  60       * @return string[]
  61       */
  62      protected function provided_sample_data() {
  63          return array('course', 'context');
  64      }
  65  
  66      /**
  67       * Returns the sample context.
  68       *
  69       * @param int $sampleid
  70       * @return \context
  71       */
  72      public function sample_access_context($sampleid) {
  73          return \context_system::instance();
  74      }
  75  
  76      /**
  77       * Returns all site courses.
  78       *
  79       * @param \core_analytics\analysable $site
  80       * @return array
  81       */
  82      public function get_all_samples(\core_analytics\analysable $site) {
  83          global $DB;
  84  
  85          // Getting courses from DB instead of from the site as these samples
  86          // will be stored in memory and we just want the id.
  87          $select = 'id != 1';
  88          $courses = get_courses('all', 'c.sortorder ASC');
  89          unset($courses[SITEID]);
  90  
  91          $courseids = array_keys($courses);
  92          $sampleids = array_combine($courseids, $courseids);
  93  
  94          $courses = array_map(function($course) {
  95              return array('course' => $course, 'context' => \context_course::instance($course->id));
  96          }, $courses);
  97  
  98          // No related data attached.
  99          return array($sampleids, $courses);
 100      }
 101  
 102      /**
 103       * Return all complete samples data from sample ids.
 104       *
 105       * @param int[] $sampleids
 106       * @return array
 107       */
 108      public function get_samples($sampleids) {
 109          global $DB;
 110  
 111          list($sql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
 112          $courses = $DB->get_records_select('course', "id $sql", $params);
 113  
 114          $courseids = array_keys($courses);
 115          $sampleids = array_combine($courseids, $courseids);
 116  
 117          $courses = array_map(function($course) {
 118              return array('course' => $course, 'context' => \context_course::instance($course->id));
 119          }, $courses);
 120  
 121          // No related data attached.
 122          return array($sampleids, $courses);
 123      }
 124  
 125      /**
 126       * Returns the description of a sample.
 127       *
 128       * @param int $sampleid
 129       * @param int $contextid
 130       * @param array $sampledata
 131       * @return array array(string, \renderable)
 132       */
 133      public function sample_description($sampleid, $contextid, $sampledata) {
 134          $description = format_string(
 135              get_course_display_name_for_list($sampledata['course']), true, array('context' => $sampledata['context']));
 136          $courseimage = new \pix_icon('i/course', get_string('course'));
 137          return array($description, $courseimage);
 138      }
 139  }