MDL-59382 core_calendar: fix behat failures
[moodle.git] / calendar / classes / local / event / mappers / create_update_form_mapper.php
CommitLineData
aa091225
RW
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 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 */
24
25namespace core_calendar\local\event\mappers;
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/calendar/lib.php');
30
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 */
45class create_update_form_mapper implements create_update_form_mapper_interface {
46
47 /**
48 * Generate the appropriate data for the form from a legacy event.
49 *
50 * @method from_legacy_event_to_data
51 * @param calendar_event $legacyevent
52 * @return stdClass
53 */
54 public function from_legacy_event_to_data(\calendar_event $legacyevent) {
55 $legacyevent->count_repeats();
56 $data = $legacyevent->properties(true);
57 $data->timedurationuntil = $legacyevent->timestart + $legacyevent->timeduration;
58 $data->duration = (empty($legacyevent->timeduration)) ? 0 : 1;
59
60 if ($legacyevent->eventtype == 'group') {
61 // Set up the correct value for the to display on the form.
62 $data->groupid = "{$legacyevent->courseid}-{$legacyevent->groupid}";
63 $data->groupcourseid = $legacyevent->courseid;
64 }
65
66 return $data;
67 }
68
69 /**
70 * Generate the appropriate calendar_event properties from the form data.
71 *
72 * @method from_data_to_event_properties
73 * @param stdClass $data
74 * @return stdClass
75 */
76 public function from_data_to_event_properties(\stdClass $data) {
77 $properties = clone($data);
78
79 // Undo the form definition work around to allow us to have two different
80 // course selectors present depending on which event type the user selects.
81 if (isset($data->groupcourseid)) {
82 $properties->courseid = $data->groupcourseid;
83 unset($properties->groupcourseid);
84 }
85
86 // Pull the group id back out of the value. The form saves the value
87 // as "<courseid>-<groupid>" to allow the javascript to work correctly.
88 if (isset($data->groupid)) {
89 list($courseid, $groupid) = explode('-', $data->groupid);
90 $properties->groupid = $groupid;
91 }
92
93 // Default course id if none is set.
94 if (!isset($data->courseid)) {
95 $properties->courseid = 0;
96 }
97
98 // Decode the form fields back into valid event property.
99 $properties->timeduration = $this->get_time_duration_from_form_data($data);
100
101 return $properties;
102 }
103
104 /**
105 * A helper function to calculate the time duration for an event based on
106 * the event_form data.
107 *
108 * @method get_time_duration_from_form_data
109 * @param \stdClass $data event_form data
110 * @return int
111 */
112 private function get_time_duration_from_form_data(\stdClass $data) {
113 if ($data->duration == 1) {
114 return $data->timedurationuntil- $data->timestart;
115 } else if ($data->duration == 2) {
116 return $data->timedurationminutes * MINSECS;
117 } else {
118 return 0;
119 }
120 }
121}