Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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   * Event mapper.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Cameron Ball <cameron@cameron1729.xyz>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_calendar\local\event\mappers;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_calendar\event;
  30  use core_calendar\local\event\entities\action_event_interface;
  31  use core_calendar\local\event\entities\event_interface;
  32  use core_calendar\local\event\factories\event_factory_interface;
  33  
  34  /**
  35   * Event mapper class.
  36   *
  37   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  38   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class event_mapper implements event_mapper_interface {
  41      /**
  42       * @var event_factory_interface $factory Event factory.
  43       */
  44      protected $factory;
  45  
  46      /**
  47       * Constructor.
  48       *
  49       * @param event_factory_interface $factory Event factory.
  50       */
  51      public function __construct(event_factory_interface $factory) {
  52          $this->factory = $factory;
  53      }
  54  
  55      public function from_legacy_event_to_event(\calendar_event $legacyevent) {
  56          $coalesce = function($property) use ($legacyevent) {
  57              try {
  58                  return $legacyevent->$property;
  59              } catch (\coding_exception $e) {
  60                  // The magic setter throews an exception if the
  61                  // property doesn't exist.
  62                  return null;
  63              }
  64          };
  65  
  66          return $this->factory->create_instance(
  67              (object)[
  68                  'id' => $coalesce('id'),
  69                  'name' => $coalesce('name'),
  70                  'description' => $coalesce('description'),
  71                  'location' => $coalesce('location'),
  72                  'format' => $coalesce('format'),
  73                  'categoryid' => $coalesce('categoryid'),
  74                  'courseid' => $coalesce('courseid'),
  75                  'groupid' => $coalesce('groupid'),
  76                  'userid' => $coalesce('userid'),
  77                  'repeatid' => $coalesce('repeatid'),
  78                  'component' => $coalesce('component'),
  79                  'modulename' => $coalesce('modulename'),
  80                  'instance' => $coalesce('instance'),
  81                  'eventtype' => $coalesce('eventtype'),
  82                  'timestart' => $coalesce('timestart'),
  83                  'timeduration' => $coalesce('timeduration'),
  84                  'timemodified' => $coalesce('timemodified'),
  85                  'timesort' => $coalesce('timesort'),
  86                  'visible' => $coalesce('visible'),
  87                  'subscriptionid' => $coalesce('subscriptionid')
  88              ]
  89          );
  90      }
  91  
  92      public function from_event_to_legacy_event(event_interface $event) {
  93          $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
  94          $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
  95          $properties = $this->from_event_to_stdclass($event);
  96  
  97          // Normalise for the legacy event because it wants zero rather than null.
  98          $properties->courseid = empty($properties->courseid) ? 0 : $properties->courseid;
  99          $properties->categoryid = empty($properties->categoryid) ? 0 : $properties->categoryid;
 100          $properties->groupid = empty($properties->groupid) ? 0 : $properties->groupid;
 101          $properties->userid = empty($properties->userid) ? 0 : $properties->userid;
 102          $properties->component = empty($properties->component) ? 0 : $properties->component;
 103          $properties->modulename = empty($properties->modulename) ? 0 : $properties->modulename;
 104          $properties->instance = empty($properties->instance) ? 0 : $properties->instance;
 105          $properties->repeatid = empty($properties->repeatid) ? 0 : $properties->repeatid;
 106  
 107          return new \calendar_event($properties);
 108      }
 109  
 110      public function from_event_to_stdclass(event_interface $event) {
 111          $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
 112          $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
 113  
 114          return (object)$this->from_event_to_assoc_array($event);
 115      }
 116  
 117      public function from_event_to_assoc_array(event_interface $event) {
 118          $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
 119          $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
 120  
 121          return [
 122              'id'               => $event->get_id(),
 123              'name'             => $event->get_name(),
 124              'description'      => $event->get_description()->get_value(),
 125              'format'           => $event->get_description()->get_format(),
 126              'location'         => $event->get_location(),
 127              'courseid'         => $event->get_course() ? $event->get_course()->get('id') : null,
 128              'categoryid'       => $event->get_category() ? $event->get_category()->get('id') : null,
 129              'groupid'          => $event->get_group() ? $event->get_group()->get('id') : null,
 130              'userid'           => $event->get_user() ? $event->get_user()->get('id') : null,
 131              'repeatid'         => $event->get_repeats() ? $event->get_repeats()->get_id() : null,
 132              'component'        => $event->get_component(),
 133              'modulename'       => $event->get_course_module() ? $event->get_course_module()->get('modname') : null,
 134              'instance'         => $event->get_course_module() ? $event->get_course_module()->get('instance') : null,
 135              'eventtype'        => $event->get_type(),
 136              'timestart'        => $event->get_times()->get_start_time()->getTimestamp(),
 137              'timeduration'     => $timeduration,
 138              'timesort'         => $event->get_times()->get_sort_time()->getTimestamp(),
 139              'timeusermidnight' => $event->get_times()->get_usermidnight_time()->getTimestamp(),
 140              'visible'          => $event->is_visible() ? 1 : 0,
 141              'timemodified'     => $event->get_times()->get_modified_time()->getTimestamp(),
 142              'subscriptionid'   => $event->get_subscription() ? $event->get_subscription()->get('id') : null,
 143              'actionname'       => $action ? $action->get_name() : null,
 144              'actionurl'        => $action ? $action->get_url() : null,
 145              'actionnum'        => $action ? $action->get_item_count() : null,
 146              'actionactionable' => $action ? $action->is_actionable() : null,
 147              'sequence' => 1
 148          ];
 149      }
 150  }