MDL-37624 calendar: Added location support
[moodle.git] / calendar / classes / local / event / mappers / event_mapper.php
CommitLineData
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
25namespace core_calendar\local\event\mappers;
26
27defined('MOODLE_INTERNAL') || die();
28
42e76c3f
CB
29use core_calendar\event;
30use core_calendar\local\event\entities\action_event_interface;
31use core_calendar\local\event\entities\event_interface;
32use 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 */
40class 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'),
ef4e04ee 71 'location' => $coalesce('location'),
8e02018f 72 'format' => $coalesce('format'),
0085b0ea 73 'categoryid' => $coalesce('categoryid'),
8e02018f
RW
74 'courseid' => $coalesce('courseid'),
75 'groupid' => $coalesce('groupid'),
76 'userid' => $coalesce('userid'),
77 'repeatid' => $coalesce('repeatid'),
78 'modulename' => $coalesce('modulename'),
79 'instance' => $coalesce('instance'),
80 'eventtype' => $coalesce('eventtype'),
81 'timestart' => $coalesce('timestart'),
82 'timeduration' => $coalesce('timeduration'),
83 'timemodified' => $coalesce('timemodified'),
84 'timesort' => $coalesce('timesort'),
85 'visible' => $coalesce('visible'),
86 'subscriptionid' => $coalesce('subscriptionid')
5ca71c2d
CB
87 ]
88 );
89 }
90
91 public function from_event_to_legacy_event(event_interface $event) {
42ec9f0d 92 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
5ca71c2d 93 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
0dc3aa12 94 $properties = $this->from_event_to_stdclass($event);
5ca71c2d 95
0dc3aa12
RW
96 // Normalise for the legacy event because it wants zero rather than null.
97 $properties->courseid = empty($properties->courseid) ? 0 : $properties->courseid;
98 $properties->groupid = empty($properties->groupid) ? 0 : $properties->groupid;
99 $properties->userid = empty($properties->userid) ? 0 : $properties->userid;
100 $properties->modulename = empty($properties->modulename) ? 0 : $properties->modulename;
101 $properties->instance = empty($properties->instance) ? 0 : $properties->instance;
28852998 102 $properties->repeatid = empty($properties->repeatid) ? 0 : $properties->repeatid;
0dc3aa12
RW
103
104 return new \calendar_event($properties);
6d82ef49
CB
105 }
106
107 public function from_event_to_stdclass(event_interface $event) {
108 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
109 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
110
111 return (object)$this->from_event_to_assoc_array($event);
112 }
113
114 public function from_event_to_assoc_array(event_interface $event) {
115 $action = ($event instanceof action_event_interface) ? $event->get_action() : null;
116 $timeduration = $event->get_times()->get_end_time()->getTimestamp() - $event->get_times()->get_start_time()->getTimestamp();
117
118 return [
aa457b7d
CB
119 'id' => $event->get_id(),
120 'name' => $event->get_name(),
121 'description' => $event->get_description()->get_value(),
122 'format' => $event->get_description()->get_format(),
ef4e04ee 123 'location' => $event->get_location(),
47a71017 124 'courseid' => $event->get_course() ? $event->get_course()->get('id') : null,
44ae0838 125 'categoryid' => $event->get_category() ? $event->get_category()->get('id') : null,
47a71017
MG
126 'groupid' => $event->get_group() ? $event->get_group()->get('id') : null,
127 'userid' => $event->get_user() ? $event->get_user()->get('id') : null,
28852998 128 'repeatid' => $event->get_repeats() ? $event->get_repeats()->get_id() : null,
6d82ef49
CB
129 'modulename' => $event->get_course_module() ? $event->get_course_module()->get('modname') : null,
130 'instance' => $event->get_course_module() ? $event->get_course_module()->get('instance') : null,
aa457b7d
CB
131 'eventtype' => $event->get_type(),
132 'timestart' => $event->get_times()->get_start_time()->getTimestamp(),
133 'timeduration' => $timeduration,
134 'timesort' => $event->get_times()->get_sort_time()->getTimestamp(),
135 'visible' => $event->is_visible() ? 1 : 0,
136 'timemodified' => $event->get_times()->get_modified_time()->getTimestamp(),
47a71017 137 'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get('id') : null,
aa457b7d
CB
138 'actionname' => $action ? $action->get_name() : null,
139 'actionurl' => $action ? $action->get_url() : null,
140 'actionnum' => $action ? $action->get_item_count() : null,
6d82ef49
CB
141 'actionactionable' => $action ? $action->is_actionable() : null,
142 'sequence' => 1
143 ];
5ca71c2d
CB
144 }
145}