Commit | Line | Data |
---|---|---|
5ca71c2d CB |
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 | ||
42e76c3f CB |
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; | |
5ca71c2d CB |
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 | ||
e1cd93ce | 55 | public function from_legacy_event_to_event(\calendar_event $legacyevent) { |
5ca71c2d | 56 | $coalesce = function($property) use ($legacyevent) { |
8e02018f RW |
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 | } | |
5ca71c2d CB |
64 | }; |
65 | ||
66 | return $this->factory->create_instance( | |
67 | (object)[ | |
8e02018f RW |
68 | 'id' => $coalesce('id'), |
69 | 'name' => $coalesce('name'), | |
70 | 'description' => $coalesce('description'), | |
71 | 'format' => $coalesce('format'), | |
72 | 'courseid' => $coalesce('courseid'), | |
73 | 'groupid' => $coalesce('groupid'), | |
74 | 'userid' => $coalesce('userid'), | |
75 | 'repeatid' => $coalesce('repeatid'), | |
76 | 'modulename' => $coalesce('modulename'), | |
77 | 'instance' => $coalesce('instance'), | |
78 | 'eventtype' => $coalesce('eventtype'), | |
79 | 'timestart' => $coalesce('timestart'), | |
80 | 'timeduration' => $coalesce('timeduration'), | |
81 | 'timemodified' => $coalesce('timemodified'), | |
82 | 'timesort' => $coalesce('timesort'), | |
83 | 'visible' => $coalesce('visible'), | |
84 | 'subscriptionid' => $coalesce('subscriptionid') | |
5ca71c2d CB |
85 | ] |
86 | ); | |
87 | } | |
88 | ||
89 | public function from_event_to_legacy_event(event_interface $event) { | |
42ec9f0d | 90 | $action = ($event instanceof action_event_interface) ? $event->get_action() : null; |
5ca71c2d CB |
91 | $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp(); |
92 | ||
e1cd93ce | 93 | return new \calendar_event($this->from_event_to_stdclass($event)); |
6d82ef49 CB |
94 | } |
95 | ||
96 | public function from_event_to_stdclass(event_interface $event) { | |
97 | $action = ($event instanceof action_event_interface) ? $event->get_action() : null; | |
98 | $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp(); | |
99 | ||
100 | return (object)$this->from_event_to_assoc_array($event); | |
101 | } | |
102 | ||
103 | public function from_event_to_assoc_array(event_interface $event) { | |
104 | $action = ($event instanceof action_event_interface) ? $event->get_action() : null; | |
105 | $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp(); | |
106 | ||
107 | return [ | |
aa457b7d CB |
108 | 'id' => $event->get_id(), |
109 | 'name' => $event->get_name(), | |
110 | 'description' => $event->get_description()->get_value(), | |
111 | 'format' => $event->get_description()->get_format(), | |
47a71017 MG |
112 | 'courseid' => $event->get_course() ? $event->get_course()->get('id') : null, |
113 | 'groupid' => $event->get_group() ? $event->get_group()->get('id') : null, | |
114 | 'userid' => $event->get_user() ? $event->get_user()->get('id') : null, | |
aa457b7d | 115 | 'repeatid' => $event->get_repeats()->get_id(), |
6d82ef49 CB |
116 | 'modulename' => $event->get_course_module() ? $event->get_course_module()->get('modname') : null, |
117 | 'instance' => $event->get_course_module() ? $event->get_course_module()->get('instance') : null, | |
aa457b7d CB |
118 | 'eventtype' => $event->get_type(), |
119 | 'timestart' => $event->get_times()->get_start_time()->getTimestamp(), | |
120 | 'timeduration' => $timeduration, | |
121 | 'timesort' => $event->get_times()->get_sort_time()->getTimestamp(), | |
122 | 'visible' => $event->is_visible() ? 1 : 0, | |
123 | 'timemodified' => $event->get_times()->get_modified_time()->getTimestamp(), | |
47a71017 | 124 | 'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get('id') : null, |
aa457b7d CB |
125 | 'actionname' => $action ? $action->get_name() : null, |
126 | 'actionurl' => $action ? $action->get_url() : null, | |
127 | 'actionnum' => $action ? $action->get_item_count() : null, | |
6d82ef49 CB |
128 | 'actionactionable' => $action ? $action->is_actionable() : null, |
129 | 'sequence' => 1 | |
130 | ]; | |
5ca71c2d CB |
131 | } |
132 | } |