on-demand release 3.8dev+
[moodle.git] / calendar / classes / local / event / mappers / create_update_form_mapper.php
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/>.
17 /**
18  * Event create form and update form mapper.
19  *
20  * @package    core_calendar
21  * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 namespace core_calendar\local\event\mappers;
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/calendar/lib.php');
31 /**
32  * Event create form and update form mapper class.
33  *
34  * This class will perform the necessary data transformations to take
35  * a legacy event and build the appropriate data structure for both the
36  * create and update event forms.
37  *
38  * It will also do the reverse transformation
39  * and take the returned form data and provide a data structure that can
40  * be used to set legacy event properties.
41  *
42  * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
43  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class create_update_form_mapper implements create_update_form_mapper_interface {
47     /**
48      * Generate the appropriate data for the form from a legacy event.
49      *
50      * @param \calendar_event $legacyevent
51      * @return stdClass
52      */
53     public function from_legacy_event_to_data(\calendar_event $legacyevent) {
54         $legacyevent->count_repeats();
55         $data = $legacyevent->properties();
56         $data->timedurationuntil = $legacyevent->timestart + $legacyevent->timeduration;
57         $data->duration = (empty($legacyevent->timeduration)) ? 0 : 1;
59         if ($legacyevent->eventtype == 'group') {
60             // Set up the correct value for the to display on the form.
61             $data->groupid = $legacyevent->groupid;
62             $data->groupcourseid = $legacyevent->courseid;
63         }
64         if ($legacyevent->eventtype == 'course') {
65             // Set up the correct value for the to display on the form.
66             $data->courseid = $legacyevent->courseid;
67         }
68         $data->description = [
69             'text' => $data->description,
70             'format' => $data->format
71         ];
73         // We don't want to return the context because it's not a
74         // form value and breaks the validation.
75         if (isset($data->context)) {
76             unset($data->context);
77         }
79         return $data;
80     }
82     /**
83      * Generate the appropriate calendar_event properties from the form data.
84      *
85      * @param \stdClass $data
86      * @return stdClass
87      */
88     public function from_data_to_event_properties(\stdClass $data) {
89         $properties = clone($data);
91         if ($data->eventtype == 'group') {
92             if (isset($data->groupcourseid)) {
93                 $properties->courseid = $data->groupcourseid;
94                 unset($properties->groupcourseid);
95             }
96             if (isset($data->groupid)) {
97                 $properties->groupid = $data->groupid;
98             }
99         } else {
100             // Default course id if none is set.
101             if (empty($properties->courseid)) {
102                 if ($properties->eventtype == 'site') {
103                     $properties->courseid = SITEID;
104                 } else {
105                     $properties->courseid = 0;
106                 }
107             } else {
108                 $properties->courseid = $data->courseid;
109             }
110         }
112         // Decode the form fields back into valid event property.
113         $properties->timeduration = $this->get_time_duration_from_form_data($data);
115         return $properties;
116     }
118     /**
119      * A helper function to calculate the time duration for an event based on
120      * the event_form data.
121      *
122      * @param \stdClass $data event_form data
123      * @return int
124      */
125     private function get_time_duration_from_form_data(\stdClass $data) {
126         if ($data->duration == 1) {
127             return $data->timedurationuntil - $data->timestart;
128         } else if ($data->duration == 2) {
129             return $data->timedurationminutes * MINSECS;
130         } else {
131             return 0;
132         }
133     }