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.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Contains event class for providing the related objects when exporting a list of calendar events.
  19   *
  20   * @package   core_calendar
  21   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_calendar\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use context;
  30  use \core_calendar\local\event\entities\event_interface;
  31  use stdClass;
  32  
  33  /**
  34   * Class to providing the related objects when exporting a list of calendar events.
  35   *
  36   * This class is only meant for use with exporters. It attempts to bulk load
  37   * the related objects for a list of events and cache them to avoid having
  38   * to query the database when exporting each individual event.
  39   *
  40   * @package   core_calendar
  41   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class events_related_objects_cache {
  45  
  46      /**
  47       * @var array $events The events for which we need related objects.
  48       */
  49      protected $events;
  50  
  51      /**
  52       * @var array $courses The related courses.
  53       */
  54      protected $courses = null;
  55  
  56      /**
  57       * @var array $groups The related groups.
  58       */
  59      protected $groups = null;
  60  
  61      /**
  62       * @var array $coursemodules The related course modules.
  63       */
  64      protected $coursemodules = [];
  65  
  66      /**
  67       * @var array $moduleinstances The related module instances.
  68       */
  69      protected $moduleinstances = null;
  70  
  71      /**
  72       * Constructor.
  73       *
  74       * @param array $events Array of event_interface events
  75       * @param array $courses Array of courses to populate the cache with
  76       */
  77      public function __construct(array $events, array $courses = null) {
  78          $this->events = $events;
  79  
  80          if (!is_null($courses)) {
  81              $this->courses = [];
  82  
  83              foreach ($courses as $course) {
  84                  $this->courses[$course->id] = $course;
  85              }
  86          }
  87      }
  88  
  89      /**
  90       * Get the related course object for a given event.
  91       *
  92       * @param event_interface $event The event object.
  93       * @return stdClass|null
  94       */
  95      public function get_course(event_interface $event) {
  96          if (is_null($this->courses)) {
  97              $this->load_courses();
  98          }
  99  
 100          if ($course = $event->get_course()) {
 101              $courseid = $course->get('id');
 102              return isset($this->courses[$courseid]) ? $this->courses[$courseid] : null;
 103          } else {
 104              return null;
 105          }
 106      }
 107  
 108      /**
 109       * Get the related context for a given event.
 110       *
 111       * @param event_interface $event The event object.
 112       * @return context|null
 113       */
 114      public function get_context(event_interface $event) {
 115          global $USER;
 116  
 117          $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
 118          $courseid = $event->get_course() ? $event->get_course()->get('id') : null;
 119          $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
 120          $userid = $event->get_user() ? $event->get_user()->get('id') : null;
 121          $moduleid = $event->get_course_module() ? $event->get_course_module()->get('id') : null;
 122  
 123          if (!empty($categoryid)) {
 124              return \context_coursecat::instance($categoryid);
 125          } else if (!empty($courseid)) {
 126              return \context_course::instance($event->get_course()->get('id'));
 127          } else if (!empty($groupid)) {
 128              $group = $this->get_group($event);
 129              return \context_course::instance($group->courseid);
 130          } else if (!empty($userid) && $userid == $USER->id) {
 131              return \context_user::instance($userid);
 132          } else if (!empty($userid) && $userid != $USER->id && $moduleid && $moduleid > 0) {
 133              $cm = $this->get_course_module($event);
 134              return \context_course::instance($cm->course);
 135          } else {
 136              return \context_user::instance($userid);
 137          }
 138      }
 139  
 140      /**
 141       * Get the related group object for a given event.
 142       *
 143       * @param event_interface $event The event object.
 144       * @return stdClass|null
 145       */
 146      public function get_group(event_interface $event) {
 147          if (is_null($this->groups)) {
 148              $this->load_groups();
 149          }
 150  
 151          if ($group = $event->get_group()) {
 152              $groupid = $group->get('id');
 153              return isset($this->groups[$groupid]) ? $this->groups[$groupid] : null;
 154          } else {
 155              return null;
 156          }
 157      }
 158  
 159      /**
 160       * Get the related course module for a given event.
 161       *
 162       * @param event_interface $event The event object.
 163       * @return stdClass|null
 164       */
 165      public function get_course_module(event_interface $event) {
 166          if (!$event->get_course_module()) {
 167              return null;
 168          }
 169  
 170          $id = $event->get_course_module()->get('id');
 171          $name = $event->get_course_module()->get('modname');
 172          $key = $name . '_' . $id;
 173  
 174          if (!isset($this->coursemodules[$key])) {
 175              $this->coursemodules[$key] = get_coursemodule_from_instance($name, $id, 0, false, MUST_EXIST);
 176          }
 177  
 178          return $this->coursemodules[$key];
 179      }
 180  
 181      /**
 182       * Get the related module instance for a given event.
 183       *
 184       * @param event_interface $event The event object.
 185       * @return stdClass|null
 186       */
 187      public function get_module_instance(event_interface $event) {
 188          if (!$event->get_course_module()) {
 189              return null;
 190          }
 191  
 192          if (is_null($this->moduleinstances)) {
 193              $this->load_module_instances();
 194          }
 195  
 196          $id = $event->get_course_module()->get('instance');
 197          $name = $event->get_course_module()->get('modname');
 198  
 199          if (isset($this->moduleinstances[$name])) {
 200              if (isset($this->moduleinstances[$name][$id])) {
 201                  return $this->moduleinstances[$name][$id];
 202              }
 203          }
 204  
 205          return null;
 206      }
 207  
 208      /**
 209       * Load the list of all of the distinct courses required for the
 210       * list of provided events and save the result in memory.
 211       */
 212      protected function load_courses() {
 213          global $DB;
 214  
 215          $courseids = [];
 216          foreach ($this->events as $event) {
 217              if ($course = $event->get_course()) {
 218                  $id = $course->get('id');
 219                  $courseids[$id] = true;
 220              }
 221          }
 222  
 223          if (empty($courseids)) {
 224              $this->courses = [];
 225              return;
 226          }
 227  
 228          list($idsql, $params) = $DB->get_in_or_equal(array_keys($courseids));
 229          $sql = "SELECT * FROM {course} WHERE id {$idsql}";
 230  
 231          $this->courses = $DB->get_records_sql($sql, $params);
 232      }
 233  
 234      /**
 235       * Load the list of all of the distinct groups required for the
 236       * list of provided events and save the result in memory.
 237       */
 238      protected function load_groups() {
 239          global $DB;
 240  
 241          $groupids = [];
 242          foreach ($this->events as $event) {
 243              if ($group = $event->get_group()) {
 244                  $id = $group->get('id');
 245                  $groupids[$id] = true;
 246              }
 247          }
 248  
 249          if (empty($groupids)) {
 250              $this->groups = [];
 251              return;
 252          }
 253  
 254          list($idsql, $params) = $DB->get_in_or_equal(array_keys($groupids));
 255          $sql = "SELECT * FROM {groups} WHERE id {$idsql}";
 256  
 257          $this->groups = $DB->get_records_sql($sql, $params);
 258      }
 259  
 260      /**
 261       * Load the list of all of the distinct module instances required for the
 262       * list of provided events and save the result in memory.
 263       */
 264      protected function load_module_instances() {
 265          global $DB;
 266  
 267          $this->moduleinstances = [];
 268          $modulestoload = [];
 269          foreach ($this->events as $event) {
 270              if ($module = $event->get_course_module()) {
 271                  $id = $module->get('instance');
 272                  $name = $module->get('modname');
 273  
 274                  $ids = isset($modulestoload[$name]) ? $modulestoload[$name] : [];
 275                  $ids[$id] = true;
 276                  $modulestoload[$name] = $ids;
 277              }
 278          }
 279  
 280          if (empty($modulestoload)) {
 281              return;
 282          }
 283  
 284          foreach ($modulestoload as $modulename => $ids) {
 285              list($idsql, $params) = $DB->get_in_or_equal(array_keys($ids));
 286              $sql = "SELECT * FROM {" . $modulename . "} WHERE id {$idsql}";
 287              $this->moduleinstances[$modulename] = $DB->get_records_sql($sql, $params);
 288          }
 289      }
 290  }