2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 namespace core_calendar\local\event\mappers;
27 defined('MOODLE_INTERNAL') || die();
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;
37 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class event_mapper implements event_mapper_interface {
42 * @var event_factory_interface $factory Event factory.
49 * @param event_factory_interface $factory Event factory.
51 public function __construct(event_factory_interface $factory) {
52 $this->factory = $factory;
55 public function from_legacy_event_to_event(\calendar_event $legacyevent) {
56 $coalesce = function($property) use ($legacyevent) {
57 return property_exists($legacyevent, $property) ? $legacyevent->{$property} : null;
60 return $this->factory->create_instance(
64 $coalesce('description'),
66 $coalesce('courseid'),
69 $coalesce('repeatid'),
70 $coalesce('modulename'),
71 $coalesce('instance'),
73 $coalesce('timestart'),
74 $coalesce('timeduration'),
75 $coalesce('timemodified'),
76 $coalesce('timesort'),
78 $coalesce('subscription')
83 public function from_event_to_legacy_event(event_interface $event) {
84 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
85 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
87 return new \calendar_event($this->from_event_to_stdclass($event));
90 public function from_event_to_stdclass(event_interface $event) {
91 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
92 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
94 return (object)$this->from_event_to_assoc_array($event);
97 public function from_event_to_assoc_array(event_interface $event) {
98 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
99 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
102 'id' => $event->get_id(),
103 'name' => $event->get_name(),
104 'description' => $event->get_description()->get_value(),
105 'format' => $event->get_description()->get_format(),
106 'courseid' => $event->get_course() ? $event->get_course()->get('id') : null,
107 'groupid' => $event->get_group() ? $event->get_group()->get('id') : null,
108 'userid' => $event->get_user() ? $event->get_user()->get('id') : null,
109 'repeatid' => $event->get_repeats()->get_id(),
110 'modulename' => $event->get_course_module() ? $event->get_course_module()->get('modname') : null,
111 'instance' => $event->get_course_module() ? $event->get_course_module()->get('instance') : null,
112 'eventtype' => $event->get_type(),
113 'timestart' => $event->get_times()->get_start_time()->getTimestamp(),
114 'timeduration' => $timeduration,
115 'timesort' => $event->get_times()->get_sort_time()->getTimestamp(),
116 'visible' => $event->is_visible() ? 1 : 0,
117 'timemodified' => $event->get_times()->get_modified_time()->getTimestamp(),
118 'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get('id') : null,
119 'actionname' => $action ? $action->get_name() : null,
120 'actionurl' => $action ? $action->get_url() : null,
121 'actionnum' => $action ? $action->get_item_count() : null,
122 'actionactionable' => $action ? $action->is_actionable() : null,