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   * Abstract analyser in course basis.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2016 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\local\analyser;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Abstract analyser in course basis.
  31   *
  32   * @package   core_analytics
  33   * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  abstract class by_course extends base {
  37  
  38      /**
  39       * Return the list of courses to analyse.
  40       *
  41       * @param string|null $action 'prediction', 'training' or null if no specific action needed.
  42       * @param \context[] $contexts Only analysables that depend on the provided contexts. All analysables in the system if empty.
  43       * @return \Iterator
  44       */
  45      public function get_analysables_iterator(?string $action = null, array $contexts = []) {
  46          global $DB;
  47  
  48          list($sql, $params) = $this->get_iterator_sql('course', CONTEXT_COURSE, $action, 'c', $contexts);
  49  
  50          $ordersql = $this->order_sql('sortorder', 'ASC', 'c');
  51  
  52          $recordset = $DB->get_recordset_sql($sql . $ordersql, $params);
  53  
  54          if (!$recordset->valid()) {
  55              $this->add_log(get_string('nocourses', 'analytics'));
  56              return new \ArrayIterator([]);
  57          }
  58  
  59          return new \core\dml\recordset_walk($recordset, function($record) {
  60  
  61              if ($record->id == SITEID) {
  62                  return false;
  63              }
  64              $context = \context_helper::preload_from_record($record);
  65              return \core_analytics\course::instance($record, $context);
  66          });
  67      }
  68  
  69      /**
  70       * Can be limited to course categories or specific courses.
  71       *
  72       * @return array
  73       */
  74      public static function context_restriction_support(): array {
  75          return [CONTEXT_COURSE, CONTEXT_COURSECAT];
  76      }
  77  }