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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
<?php
/**
* Room.php - model class for a resource which is a room
*
* The Room class is a derived class of the Resource class.
* It containts specialisations for room resources.
*
* 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 Moritz Strohm <strohm@data-quest.de>
* @copyright 2017-2019
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package resources
* @since 4.5
*
* @property string $id database column
* @property string $parent_id database column
* @property string $category_id database column
* @property int|null $level database column
* @property string $name database column
* @property I18NString|null $description database column
* @property int $requestable database column
* @property int $lockable database column
* @property int $mkdate database column
* @property int $chdate database column
* @property int $sort_position database column
* @property SimpleORMapCollection|ResourceProperty[] $properties has_many ResourceProperty
* @property SimpleORMapCollection|ResourcePermission[] $permissions has_many ResourcePermission
* @property SimpleORMapCollection|ResourceRequest[] $requests has_many ResourceRequest
* @property SimpleORMapCollection|ResourceBooking[] $bookings has_many ResourceBooking
* @property SimpleORMapCollection|Resource[] $children has_many Resource
* @property ResourceCategory $category belongs_to ResourceCategory
* @property Resource $parent belongs_to Resource
* @property mixed $room_type additional field
* @property mixed $seats additional field
* @property mixed $booking_plan_is_public additional field
* @property-read mixed $building additional field
* @property mixed $class_name additional field
*/
class Room extends Resource
{
protected static $required_properties = [
'room_type',
'seats',
'booking_plan_is_public'
];
protected static function configure($config = [])
{
if (!isset($config['additional_fields'])) {
$config['additional_fields'] = [];
}
foreach (self::$required_properties as $property) {
$config['additional_fields'][$property] = [
'get' => 'getProperty',
'set' => 'setProperty'
];
}
$config['additional_fields']['building']['get'] = 'findBuilding';
$config['registered_callbacks']['before_store'][] = 'cbValidate';
$config['registered_callbacks']['after_delete'][] = 'cbDeleteClipboardItems';
parent::configure($config);
}
public static function getTranslatedClassName($item_count = 1)
{
return ngettext(
'Raum',
'Räume',
$item_count
);
}
public static function countAll()
{
return self::countBySql(
"INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE rc.class_name IN ( :room_class_names )",
[
'room_class_names' => RoomManager::getAllRoomClassNames()
]
);
}
public static function findAll()
{
return self::findBySql(
"INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE rc.class_name IN ( :room_class_names )
ORDER BY sort_position DESC, name ASC, mkdate ASC",
[
'room_class_names' => RoomManager::getAllRoomClassNames()
]
);
}
public static function findByNameOrBuilding($room, $building)
{
$params = [];
$sql = "INNER JOIN resource_categories rc
ON resources.category_id = rc.id";
if ($building) {
$sql .= " INNER JOIN resources pr
ON resources.parent_id = pr.id";
}
$sql .= " WHERE rc.class_name IN ( :room_class_names )";
$params['room_class_names'] = RoomManager::getAllRoomClassNames();
if ($room) {
$sql .= " AND resources.name LIKE CONCAT('%', :room_name, '%')";
$params['room_name'] = $room;
}
if ($building) {
$sql .= " AND pr.name LIKE CONCAT('%', :building_name, '%')";
$params['building_name'] = $building;
}
$sql .= " ORDER BY sort_position DESC, name ASC, mkdate ASC";
return self::findBySql($sql, $params);
}
public static function getRequiredProperties()
{
return self::$required_properties;
}
/**
* Finds rooms by a building specified by its ID.
*
* @param string $building_id The ID of the building.
*
* @return array An array with rooms or an empty array.
*/
public static function findByBuilding(string $building_id)
{
$building = Building::find($building_id);
if (!$building) {
return [];
}
//Return all found Room objects below the building:
return $building->findChildrenByClassName('Room', 0, true);
}
/**
* Returns rooms that match the criteria of the room request.
*
* @param RoomRequest $request A RoomRequest object.
* @param User $user The user who wishes to search for rooms matching
* the room request.
* @param int $offset An offset for the result set.
* @param int $limit A limit for the result set.
* @param Room[] $searchable_rooms An (optional) array of rooms
* which will limit the search to the rooms in the array.
* @param array $properties An array providing request properties
* and their values in case the request doesn't have (the desired)
* properties set.
*
* @return Room[] An array of room resources.
*/
public static function findByRoomRequestAndProperties(
RoomRequest $request,
User $user,
$offset = 0,
$limit = 0,
$searchable_rooms = [],
$properties = []
)
{
//We have to check first if the user is permitted to search:
//The user must have at least 'tutor' status in the
//room and resource management:
if (!ResourceManager::userHasGlobalPermission($user, 'tutor')) {
throw new AccessDeniedException(
_('Ihre Berechtigungen sind unzureichend, um Räume anhand einer Raumanfrage zu suchen!')
);
}
if (!$properties) {
$properties = [];
foreach ($request->properties as $property) {
$properties[$property->name] = $property->state;
}
}
//Build the SQL query, based on the room request's properties
//and the specified parameters:
$sql = "INNER JOIN resource_properties rp
ON resources.id = rp.resource_id
INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE
resources.requestable = '1'
AND
rc.class_name IN ( :room_class_names ) ";
$sql_array = [
'room_class_names' => RoomManager::getAllRoomClassNames()
];
if (count($properties)) {
$sql .= "AND ( ";
$property_c = 1;
foreach ($properties as $name => $state) {
if (!$state) {
continue;
}
$definition = ResourcePropertyDefinition::findOneBySql(
"INNER JOIN resource_category_properties rcp
WHERE rcp.category_id = :category_id
AND resource_property_definitions.name = :name",
[
'category_id' => $request->category_id,
'name' => $name
]
);
if (!$definition) {
//Such a property doesn't exist for the specified category.
continue;
}
if ($property_c > 1) {
$sql .= ' OR ';
}
$sql .= "(rp.property_id = :property_id$property_c ";
//By looking at the definition we can determine if we have to do
//a range search or a search for an exact match.
if ($definition->range_search || $name == 'seats') {
$sql .= "AND rp.state >= :property_state$property_c";
} else {
$sql .= "AND rp.state = :property_state$property_c";
}
$sql .= ")";
$sql_array["property_id$property_c"] = $definition->id;
$sql_array["property_state$property_c"] = $state;
$property_c++;
}
$sql .= ") ";
}
if (!empty($searchable_rooms)) {
$room_ids = [];
foreach ($searchable_rooms as $room) {
if ($room instanceof Room) {
$room_ids[] = $room->id;
}
}
if ($room_ids) {
$sql .= "AND resources.id IN ( :room_ids ) ";
$sql_array['room_ids'] = $room_ids;
} else {
//We can't look for non-existing rooms:
return [];
}
}
$sql .= "GROUP BY resource_id ORDER BY resources.name ASC ";
$offset = intval($offset);
if ($offset > 0) {
$sql .= "OFFSET :offset ";
$sql_array['offset'] = $offset;
}
$limit = intval($limit);
if ($limit > 0) {
$sql .= "LIMIT :limit ";
$sql_array['limit'] = $limit;
}
return Room::findBySql($sql, $sql_array);
}
/**
* Determins if the specified room is a room part
* and then returns all other room parts.
*
* @param Room $room The room part whose other room parts shall be found.
*
* @return Room[] An array of room objects or an empty array
* if no other room parts can be found.
*/
public static function findOtherRoomParts(Room $room)
{
$other_room_parts = [];
$separable_room = SeparableRoom::findByRoomPart($room);
if ($separable_room instanceof SeparableRoom) {
$other_room_parts = $separable_room->findOtherRoomParts(
[$room]
);
}
return $other_room_parts;
}
/**
* Helper method to return the SQL code for publicBookingPlansExists,
* countByPublicBookingPlans and getByPublicBookingPlans.
*/
protected static function getPublicBookingPlansSql()
{
return "INNER JOIN resource_categories AS rc
ON resources.category_id = rc.id
INNER JOIN resource_properties AS rp
ON resources.id = rp.resource_id
INNER JOIN resource_property_definitions AS rpd
USING (property_id)
WHERE rc.class_name IN ( :room_class_names )
AND rpd.name = 'booking_plan_is_public'
AND rp.state = '1'
ORDER BY resources.name ASC, resources.mkdate ASC";
}
/**
* Checks whether rooms with public booking plans exist.
*
* @return bool True, if at least one room has a public booking plan,
* false otherwise.
*/
public static function publicBookingPlansExists()
{
return Room::countBySql(
self::getPublicBookingPlansSql(),
['room_class_names' => RoomManager::getAllRoomClassNames()]
) > 0;
}
/**
* Retrieves all rooms that have a public booking plan,
* ordered by name and creation date.
*
* @return array A list of rooms with public booking plans.
*/
public static function findByPublicBookingPlans()
{
return self::findBySql(
self::getPublicBookingPlansSql(),
['room_class_names' => RoomManager::getAllRoomClassNames()]
);
}
/**
* Retrieves all existing room types from the database.
* Only room types which have at least one room object with that
* type in the database are considered here.
*
* @return array An array consisting of all room types which
* exist in the database.
*/
public static function getAllRoomTypes()
{
$db = DBManager::get();
$stmt = $db->prepare(
"SELECT DISTINCT state FROM resource_properties
INNER JOIN resource_property_definitions rpd
USING (property_id)
INNER JOIN resource_category_properties rcp
USING (property_id)
INNER JOIN resource_categories rc
ON rcp.category_id = rc.id
WHERE
state != ''
AND
rc.class_name IN ( :room_class_names )
AND
rpd.name = 'room_type'
ORDER BY state ASC"
);
$stmt->execute(
['room_class_names' => RoomManager::getAllRoomClassNames()]
);
return $stmt->fetchAll(
PDO::FETCH_COLUMN,
0
);
}
/**
* Returns the part of the URL for getLink and getURL which will be
* placed inside the calls to URLHelper::getLink and URLHelper::getURL
* in these methods.
*
* @param string $action The action for the room.
* @param string $id The ID of the room.
*
* @return string The URL path for the specified action.
* @throws InvalidArgumentException If $room_id is empty.
*
*/
protected static function buildPathForAction($action = 'show', $id = null)
{
if (!$id) {
throw new InvalidArgumentException(
_('Zur Erstellung der URL fehlt eine Raum-ID!')
);
}
switch ($action) {
case 'show':
return 'dispatch.php/resources/room/index/' . $id;
case 'add':
return 'dispatch.php/resources/room/add';
case 'edit':
return 'dispatch.php/resources/room/edit/' . $id;
case 'delete':
return 'dispatch.php/resources/room/delete/' . $id;
case 'request':
return 'dispatch.php/resources/room_request/add/' . $id;
case 'request_list':
return 'dispatch.php/resources/room_request/overview?room_id=' . $id;
case 'booking_plan':
return 'dispatch.php/resources/room_planning/booking_plan/' . $id;
default:
//There are some actions which can be handled by the general
//resource controller:
return parent::buildPathForAction($action, $id);
}
}
/**
* Returns the appropriate link for the room action that shall be
* executed on a room.
*
* @param string $action The action which shall be executed.
* For rooms the actions 'show', 'booking_plan', 'add', 'edit' and 'delete'
* are defined.
* @param string $id The ID of the room on which the specified
* action shall be executed.
* @param array $link_parameters Optional parameters for the link.
*
* @return string The Link for the room action.
* @throws InvalidArgumentException If $room_id is empty.
*
*/
public static function getLinkForAction($action = 'show', $id = null, $link_parameters = [])
{
return URLHelper::getLink(
self::buildPathForAction($action, $id),
$link_parameters
);
}
/**
* Returns the appropriate URL for the room action that shall be
* executed on a room.
*
* @param string $action The action which shall be executed.
* For rooms the actions 'show', 'booking_plan', 'add', 'edit' and 'delete'
* are defined.
* @param string $id The ID of the room on which the specified
* action shall be executed.
* @param array $url_parameters Optional parameters for the URL.
*
* @return string The URL for the room action.
* @throws InvalidArgumentException If $room_id is empty.
*
*/
public static function getURLForAction($action = 'show', $id = null, $url_parameters = [])
{
return URLHelper::getURL(
self::buildPathForAction($action, $id),
$url_parameters
);
}
public function cbValidate()
{
$building = $this->findParentByClassName('Building');
if (!$building) {
//Rooms must have parents! They have to be placed below buildings!
throw new InvalidResourceException(
sprintf(
_('Der Raum %1$s ist keinem Gebäude zugeordnet!'),
$this->name
)
);
}
if (!is_a($this->category->class_name, get_class($this), true)) {
//Only resources with the Building category can be handled
//with this class!
throw new InvalidResourceException(
sprintf(
_('Der Raum %1$s ist der falschen Ressourcen-Klasse zugeordnet!'),
$this->name
)
);
}
return true;
}
public function cbDeleteClipboardItems()
{
ClipboardItem::deleteBySQL('range_id = ?', [$this->id]);
}
public function getRequiredPropertyNames()
{
return self::$required_properties;
}
/**
* @see StudipItem::__toString
*/
public function __toString()
{
return $this->getFullName();
}
/**
* This method calls Resource::createRequest and transforms the
* resulting ResourceRequest object into a RoomRequest object.
*
* @return RoomRequest A room request object.
* @see Resource::createRequest for paramter descriptions
* and thrown exceptions.
* @inheritDoc
*/
public function createRequest(
User $user,
$date_range_id = null,
$comment = '',
$properties = [],
$preparation_time = 0
)
{
$request = parent::createRequest(
$user,
$date_range_id,
$comment,
$properties,
$preparation_time
);
return RoomRequest::build($request, false);
}
/**
* Adds a child resource to this room. The child resource
* must not be a resource of the class Room, Building or Location.
*
* @param Resource $resource The resource which shall be added as child.
*
* @return bool True, if the resource could be added as child, false otherwise.
* @throws InvalidResourceException If the specified resource belongs to
* the resource classes Room, Building or Location.
*
*/
public function addChild(Resource $resource)
{
if ($resource->class_name == $this->class_name) {
throw new InvalidResourceException(
_('Ein Raum darf keinem anderen Raum in der Hierarchie untergeordnet werden!')
);
} elseif ($resource->class_name == 'Location') {
throw new InvalidResourceException(
_('Ein Standort darf keinem Raum in der Hierarchie untergeordnet werden!')
);
} elseif ($resource->class_name == 'Building') {
throw new InvalidResourceException(
_('Ein Gebäude darf keinem Raum in der Hierarchie untergeordnet werden!')
);
}
return parent::addChild($resource);
}
/**
* Returns the full name of this room.
*
* @return string The full name of this room.
*/
public function getFullName()
{
return sprintf(
_('Raum %s'),
$this->name
);
}
public function getDefaultPictureUrl()
{
return $this->getIcon()->asImagePath();
}
public function getIcon($role = Icon::ROLE_INFO)
{
return Icon::create('room', $role);
}
public function checkHierarchy()
{
//We must check if this room has rooms as children
//or as parents. In any of those cases the hierarchy
//is invalid!
$children = $this->findChildrenByClassName('Room');
if (count($children) > 0) {
//At least one child anywhere below this room
//resource is a room, too.
return false;
}
$parents = ResourceManager::getHierarchy($this);
//We do not need to check this element:
array_shift($parents);
foreach ($parents as $parent) {
$parent = $parent->getDerivedClassInstance();
if ($parent instanceof Room) {
//Hierarchy error
return false;
}
}
//If code execution reaches this point then
//the hierarchy around this room is valid.
return true;
}
/**
* @see Resource::bookingPlanVisibleForUser
* @inheritDoc
*/
public function bookingPlanVisibleForUser(?User $user, $time_range = [])
{
return $this->booking_plan_is_public
|| parent::bookingPlanVisibleForUser($user, $time_range);
}
/**
* Returns the link for an action for this room.
* This is the non-static variant of Room::getLinkForAction.
*
* @param string $action The action which shall be executed.
* For rooms the actions 'show', 'booking_plan', 'add', 'edit' and 'delete'
* are defined.
* @param array $link_parameters Optional parameters for the link.
* @return string @TODO
*/
public function getActionLink($action = 'show', $link_parameters = [])
{
return self::getLinkForAction(
$action,
$this->id,
$link_parameters
);
}
/**
* Returns the URL for an action for this room.
* This is the non-static variant of Room::getURLForAction.
*
* @param string $action The action which shall be executed.
* For rooms the actions 'show', 'booking_plan', 'add', 'edit' and 'delete'
* are defined.
* @param array $url_parameters Optional parameters for the URL.
* @return string @TODO
*/
public function getActionURL($action = 'show', $url_parameters = [])
{
return self::getURLForAction(
$action,
$this->id,
$url_parameters
);
}
/**
* Retrieves the building where this room resides in by looking up
* the parent resources of this Room.
*
* @return Building|null A Building object if it can be found,
* null otherwise.
*/
public function findBuilding()
{
$building = self::findParentByClassName('Building');
if ($building instanceof Resource) {
return $building->getDerivedClassInstance();
}
return null;
}
}
|