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) {
58 return $legacyevent->$property;
59 } catch (\coding_exception $e) {
60 // The magic setter throews an exception if the
61 // property doesn't exist.
66 return $this->factory->create_instance(
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')
89 public function from_event_to_legacy_event(event_interface $event) {
90 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
91 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
93 return new \calendar_event($this->from_event_to_stdclass($event));
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();
100 return (object)$this->from_event_to_assoc_array($event);
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();
108 'id' => $event->get_id(),
109 'name' => $event->get_name(),
110 'description' => $event->get_description()->get_value(),
111 'format' => $event->get_description()->get_format(),
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,
115 'repeatid' => $event->get_repeats()->get_id(),
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,
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(),
124 'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get('id') : null,
125 'actionname' => $action ? $action->get_name() : null,
126 'actionurl' => $action ? $action->get_url() : null,
127 'actionnum' => $action ? $action->get_item_count() : null,
128 'actionactionable' => $action ? $action->is_actionable() : null,