-
Notifications
You must be signed in to change notification settings - Fork 42
/
pricecategories.php
193 lines (155 loc) · 7.4 KB
/
pricecategories.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Price categories settings
*
* @package mod_booking
* @copyright 2022 Wunderbyte GmbH <[email protected]>
* @author Georg Maißer, Bernhard Fischer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_calendar\local\event\entities\event;
use mod_booking\form\pricecategories_form;
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
global $OUTPUT;
// No guest autologin.
require_login(0, false);
admin_externalpage_setup('modbookingpricecategories');
$settingsurl = new moodle_url('/admin/category.php', ['category' => 'modbookingfolder']);
$pageurl = new moodle_url('/mod/booking/pricecategories.php');
$PAGE->set_url($pageurl);
$PAGE->set_title(
format_string($SITE->shortname) . ': ' . get_string('pricecategories', 'booking')
);
$mform = new pricecategories_form($pageurl);
if ($mform->is_cancelled()) {
// If cancelled, go back to general booking settings.
redirect($settingsurl);
} else if ($data = $mform->get_data()) {
$existingpricecategories = $DB->get_records('booking_pricecategories');
if (empty($existingpricecategories)) {
// There are no price categories yet.
// Currently there can be up to nine price categories.
for ($i = 1; $i <= MOD_BOOKING_MAX_PRICE_CATEGORIES; $i++) {
$pricecategoryordernumx = 'pricecategoryordernum' . $i;
$pricecategoryidentifierx = 'pricecategoryidentifier' . $i;
$pricecategorynamex = 'pricecategoryname' . $i;
$defaultvaluex = 'defaultvalue' . $i;
$pricecatsortorderx = 'pricecatsortorder' . $i;
$disablepricecategoryx = 'disablepricecategory' . $i;
// Only add price categories if a name was entered.
if (!empty($data->{$pricecategoryidentifierx})) {
$pricecategory = new stdClass();
$pricecategory->ordernum = $data->{$pricecategoryordernumx};
$pricecategory->identifier = $data->{$pricecategoryidentifierx};
$pricecategory->name = $data->{$pricecategorynamex};
$pricecategory->defaultvalue = $data->{$defaultvaluex};
$pricecategory->pricecatsortorder = $data->{$pricecatsortorderx};
$pricecategory->disabled = $data->{$disablepricecategoryx};
$DB->insert_record('booking_pricecategories', $pricecategory);
}
}
} else {
// There are already existing price categories.
// So we need to check for changes.
$oldpricecategories = $DB->get_records('booking_pricecategories');
if ($pricecategorychanges = pricecategories_get_changes($oldpricecategories, $data)) {
foreach ($pricecategorychanges['updates'] as $record) {
$DB->update_record('booking_pricecategories', $record);
$oldidentifier = $oldpricecategories[$record->id]->identifier;
$newidentifier = $record->identifier;
// If identifier has changed, we also need to update associated prices.
if ($oldidentifier != $newidentifier) {
$event = \mod_booking\event\pricecategory_changed::create(
['objectid' => $record->id,
'context' => \context_system::instance(),
'relateduserid' => $USER->id,
'other' => [
'oldidentifier' => $oldidentifier,
'newidentifier' => $newidentifier,
],
]
);
$event->trigger();
}
}
if (count($pricecategorychanges['inserts']) > 0) {
$DB->insert_records('booking_pricecategories', $pricecategorychanges['inserts']);
}
}
}
// In any case, invalidate the cache after updating price categories.
cache_helper::purge_by_event('setbackpricecategories');
redirect($pageurl, get_string('pricecategoriessaved', 'booking'), 5);
} else {
echo $OUTPUT->header();
echo $OUTPUT->heading(new lang_string('pricecategory', 'mod_booking'));
echo get_string('pricecategoriessubtitle', 'booking');
// Show the mform.
$mform->display();
echo $OUTPUT->footer();
}
/**
* Helper function to return arrays containing all relevant pricecategories update changes.
* The returned arrays will have the prepared stdClasses for update and insert in booking_pricecategories table.
*
* @param mixed $oldpricecategories the existing price categories
* @param stdClass $data the form data
*
* @return array
*/
function pricecategories_get_changes($oldpricecategories, $data) {
$updates = [];
$inserts = [];
$existingordernumbers = [];
foreach ($oldpricecategories as $oldpricecategory) {
$existingordernumbers[] = $oldpricecategory->ordernum;
}
foreach ($data as $key => $value) {
if (preg_match('/pricecategoryid[0-9]/', $key)) {
$counter = (int)substr($key, -1);
if (in_array($counter, $existingordernumbers)) {
// Create price category object and add to updates.
$pricecategory = new stdClass();
$pricecategory->id = $value;
$pricecategory->ordernum = $data->{'pricecategoryordernum' . $counter};
$pricecategory->identifier = $data->{'pricecategoryidentifier' . $counter};
$pricecategory->name = $data->{'pricecategoryname' . $counter};
$pricecategory->defaultvalue = $data->{'defaultvalue' . $counter};
$pricecategory->pricecatsortorder = $data->{'pricecatsortorder' . $counter};
$pricecategory->disabled = $data->{'disablepricecategory' . $counter};
$updates[] = $pricecategory;
} else {
// Create new price category and add to inserts.
if (!empty($data->{'pricecategoryidentifier' . $counter})) {
$pricecategory = new stdClass();
$pricecategory->ordernum = $data->{'pricecategoryordernum' . $counter};
$pricecategory->identifier = $data->{'pricecategoryidentifier' . $counter};
$pricecategory->name = $data->{'pricecategoryname' . $counter};
$pricecategory->defaultvalue = $data->{'defaultvalue' . $counter};
$pricecategory->pricecatsortorder = $data->{'pricecatsortorder' . $counter};
$pricecategory->disabled = $data->{'disablepricecategory' . $counter};
$inserts[] = $pricecategory;
}
}
}
}
return [
'inserts' => $inserts,
'updates' => $updates,
];
}