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   * Courses analyser working at course level (insights for the course teachers).
  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   * Courses analyser working at course level (insights for the course teachers).
  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 courses extends \core_analytics\local\analyser\by_course {
  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       * Just one sample per analysable.
  49       *
  50       * @return bool
  51       */
  52      public static function one_sample_per_analysable() {
  53          return true;
  54      }
  55  
  56      /**
  57       * Returns the analysable of a sample
  58       *
  59       * @param int $sampleid
  60       * @return \core_analytics\analysable
  61       */
  62      public function get_sample_analysable($sampleid) {
  63          return \core_analytics\course::instance($sampleid);
  64      }
  65  
  66      /**
  67       * This provides samples' course and context.
  68       *
  69       * @return string[]
  70       */
  71      protected function provided_sample_data() {
  72          return array('course', 'context');
  73      }
  74  
  75      /**
  76       * Returns the context of a sample.
  77       *
  78       * @param int $sampleid
  79       * @return \context
  80       */
  81      public function sample_access_context($sampleid) {
  82          return \context_course::instance($sampleid);
  83      }
  84  
  85      /**
  86       * This will return just one course as we analyse 'by_course'.
  87       *
  88       * @param \core_analytics\analysable $course
  89       * @return array
  90       */
  91      public function get_all_samples(\core_analytics\analysable $course) {
  92  
  93          $context = \context_course::instance($course->get_id());
  94  
  95          // Just 1 sample per analysable.
  96          return array(
  97              array($course->get_id() => $course->get_id()),
  98              array($course->get_id() => array('course' => $course->get_course_data(), 'context' => $context))
  99          );
 100      }
 101  
 102      /**
 103       * Returns 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 sample description
 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  }