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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
<?php
/**
* block_appointments.php
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* @author André Noack <noack@data-quest.de>
* @author David Siegfried <david.siegfried@uni-vechta.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
*/
class Course_BlockAppointmentsController extends AuthenticatedController
{
/**
* Common tasks for all actions
*
* @param String $action Called action
* @param Array $args Possible arguments
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
$course_id = $args[0] ?? null;
$this->course_id = Request::option('cid', $course_id);
if (!get_object_type($this->course_id, ['sem']) ||
SeminarCategories::GetBySeminarId($this->course_id)->studygroup_mode ||
!$GLOBALS['perm']->have_studip_perm("tutor", $this->course_id)
) {
throw new Trails\Exception(400);
}
PageLayout::setHelpKeyword('Basis.VeranstaltungenVerwaltenAendernVonZeitenUndTerminen');
PageLayout::setTitle(Course::findCurrent()->getFullName() . " - " . _('Blockveranstaltungstermine anlegen'));
}
/**
* Display the block appointments
*/
public function index_action()
{
if (Navigation::hasItem('/course/admin/timesrooms')) {
Navigation::activateItem('/course/admin/timesrooms');
}
PageLayout::setTitle(_('Neuen Blocktermin anlegen'));
$this->linkAttributes = ['fromDialog' => Request::int('fromDialog') ? 1 : 0];
$this->start_ts = strtotime('this monday');
$this->request = $this->flash['request'] ?? $_SESSION['block_appointments'] ?? [];
$this->lecturers = CourseMember::findByCourseAndStatus(
$this->course_id,
'dozent'
);
$this->start = null;
$this->end = null;
$this->date_types = [];
foreach ($GLOBALS['TERMIN_TYP'] as $id => $data) {
$this->date_types[] = [
'id' => $id,
'name' => $data['name']
];
}
$this->available_lecturers = [];
$course = Course::find($this->course_id);
$lecturers = $course->getMembersWithStatus('dozent');
foreach ($lecturers as $lecturer) {
$this->available_lecturers[$lecturer->user_id] = $lecturer->getUserFullname();
}
$this->selected_lecturer_ids = [];
$this->selected_date_type = 0;
$this->dow = ['all'];
$this->preparation_time = 0;
$this->subsequent_time = 0;
if ($this->request instanceof Request) {
$this->start = $this->request->getDateTime('start_date', 'd.m.Y', 'start_time', 'H:i');
$this->end = $this->request->getDateTime('end_date', 'd.m.Y', 'end_time', 'H:i');
$this->selected_lecturer_ids = $this->request->getArray('lecturers');
$this->selected_date_type = $this->request->int('date_type');
$this->dow = $this->request->getArray('dow');
$this->preparation_time = $this->request->int('preparation_time', 0);
$this->subsequent_time = $this->request->int('subsequent_time', 0);
} elseif (is_array($this->request)) {
$this->start = $this->request['start'] ?? null;
$this->end = $this->request['end'] ?? null;
$this->selected_date_type = $this->request['date_type'] ?? 0;
$this->dow = $this->request['dow'] ?? ['all'];
$this->preparation_time = $this->request['preparation_time'] ?? 0;
$this->subsequent_time = $this->request['subsequent_time'] ?? 0;
}
if (!$this->start || !$this->end) {
//Provide some default values:
$this->start = new DateTime();
$this->start = $this->start->add(new DateInterval('PT1H'));
$this->start->setTime(intval($this->start->format('H')), 0, 0);
$this->end = clone $this->start;
$this->end = $this->end->add(new DateInterval('PT30M'));
}
$this->allow_multiple_room_bookings = ResourceManager::userHasGlobalPermission(
User::findCurrent(),
Config::get()->ROOM_PERMISSIONS_FOR_MULTIPLE_BOOKINGS_PER_COURSE_DATE
);
$this->max_preparation_time = intval(Config::get()->RESOURCES_MAX_PREPARATION_TIME) ?? 999;
}
/**
* Saves the block appointments of a course
*
* @param String $course_id Id of the course
*/
public function save_action($course_id)
{
$errors = [];
$start = Request::getDateTime('start_date', 'd.m.Y', 'start_time', 'H:i');
$end = Request::getDateTime('end_date', 'd.m.Y', 'end_time', 'H:i');
if (!$start || !$end || $start >= $end) {
$errors[] = _('Bitte geben Sie korrekte Werte für Start- und Enddatum an!');
}
$room_choice = Request::get('room');
$preparation_time = 0;
$subsequent_time = 0;
$room_name = '';
if ($room_choice === 'room' && Config::get()->RESOURCES_MIN_BOOKING_TIME) {
//Calculate the duration if a minimum booking time is set
//and one or more rooms shall be booked:
$fake_start_time = clone $start;
$fake_end_time = clone $start;
$fake_end_time->setTime(intval($end->format('H')), intval($end->format('i')), 0);
$duration = $fake_end_time->getTimestamp() - $fake_start_time->getTimestamp();
if ($duration < Config::get()->RESOURCES_MIN_BOOKING_TIME * 60) {
$errors[] = sprintf(
ngettext(
'Die minimale Dauer einer Raumbuchung von einer Minute wurde unterschritten.',
'Die minimale Dauer einer Raumbuchung von %u Minuten wurde unterschritten.',
Config::get()->RESOURCES_MIN_BOOKING_TIME
),
Config::get()->RESOURCES_MIN_BOOKING_TIME
);
}
$preparation_time = Request::int('preparation_time', 0);
$subsequent_time = Request::int('subsequent_time', 0);
} elseif ($room_choice === 'freetext') {
$room_name = Request::get('room_name');
}
$date_type = Request::int('date_type', 0);
$dow = Request::getArray('dow');
if (empty($dow)) {
$errors[] = _('Bitte wählen Sie mindestens einen Tag aus!');
}
$date_count = Request::int('date_count');
if ($date_count < 1) {
$errors[] = _('Bitte setzen Sie die Menge der zu erstellenden Termine mindestens auf 1.');
}
if (count($errors)) {
$this->flash['request'] = Request::getInstance();
PageLayout::postMessage(MessageBox::error(_('Bitte korrigieren Sie Ihre Eingaben:'), $errors));
$this->redirect('course/block_appointments/index');
return;
}
$lecturer_ids = Request::getArray('assigned_lecturers');
$lecturers = [];
if ($lecturer_ids) {
$lecturers = User::findBySql(
"INNER JOIN seminar_user USING (user_id)
WHERE seminar_id = :course_id
AND seminar_user.user_id IN (:lecturer_ids)
AND seminar_user.status = 'dozent'",
[
'course_id' => $this->course_id,
'lecturer_ids' => $lecturer_ids,
]
);
}
if (in_array('all', $dow)) {
$dow = ['1', '2', '3', '4', '5', '6', '7'];
} elseif (in_array('mon_fri', $dow)) {
$dow = ['1', '2', '3', '4', '5'];
}
$dates = [];
$t = clone $start;
$i = 1;
while ($t < $end && $i <= $date_count) {
if (in_array($t->format('N'), $dow)) {
$date_end = clone $t;
$date_end->setTime(intval($end->format('H')), intval($end->format('i')), 0);
$date = new CourseDate();
$date->range_id = $course_id;
$date->date_typ = $date_type;
$date->raum = $room_name;
$date->date = $t->getTimestamp();
$date->end_time = $date_end->getTimestamp();
if ($lecturers) {
$date->dozenten = $lecturers;
}
$dates[] = $date;
$i++;
}
$t = $t->add(new DateInterval('P1D'));
}
//Store the last used values in the session as default values.
$_SESSION['block_appointments'] = [
'start' => $start,
'end' => $end,
'date_type' => $date_type,
'room_name' => $room_name,
'date_count' => $date_count,
'dow' => $dow
];
$partially_booked_dates = [];
$dates_created = array_filter(array_map(function ($d) use ($room_choice, $preparation_time, $subsequent_time, &$partially_booked_dates) {
$result = $d->store();
$room_ids = [];
if ($room_choice === 'room') {
$room_ids = Request::getArray('room_ids');
}
//Process the room-IDs: If a separable room is selected, set all its room parts as room-IDs.
//Remove the prefix in all other cases.
$processed_room_ids = [];
foreach ($room_ids as $room_id) {
$id_parts = explode('-', $room_id);
if (count($id_parts) !== 2) {
//Invalid ID.
continue;
}
if ($id_parts[0] === 'separable_room') {
//A separable room was selected.
$separable_room = SeparableRoom::find($id_parts[1]);
if ($separable_room) {
foreach ($separable_room->parts as $part) {
$processed_room_ids[] = $part->room_id;
}
}
} elseif ($id_parts[0] === 'room') {
//An ordinary room.
$processed_room_ids[] = $id_parts[1];
}
}
$room_ids = $processed_room_ids;
if ($room_ids) {
$resources = Resource::findMany($room_ids);
$rooms = [];
foreach ($resources as $resource) {
$rooms[] = $resource->getDerivedClassInstance();
}
$booking_failures = 0;
foreach ($rooms as $room) {
try {
$r = $d->bookRoom($room, $preparation_time * 60, $subsequent_time * 60);
if (!$r) {
$booking_failures++;
}
} catch (ResourceBookingException|ResourceBookingOverlapException $e) {
$booking_failures++;
}
}
if ($result && $booking_failures) {
//Not all selected rooms for the date could be booked:
$partially_booked_dates[] = htmlReady($d->getFullName());
}
}
return $result ? htmlReady($d->getFullName()) : null;
}, $dates));
if ($date_count > 1) {
$dates_created = array_count_values($dates_created);
$dates_created = array_map(function ($k, $v) {
return $k . ' (' . $v . 'x)';
}, array_keys($dates_created), array_values($dates_created));
}
PageLayout::postSuccess(_('Folgende Termine wurden erstellt:'), $dates_created);
if (!empty($partially_booked_dates)) {
PageLayout::postWarning(_('Für folgende Termine konnten nicht alle ausgewählten Räume gebucht werden:'), $partially_booked_dates);
}
if (Request::int('fromDialog')) {
$this->redirect('course/timesrooms/index');
} else {
$this->relocate('course/timesrooms/index');
}
}
}
|