-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroom_reservations.inc
1560 lines (1487 loc) · 49.7 KB
/
room_reservations.inc
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Classes and general Room Reservations helper functions.
*/
/**
* Reservation class represents a single reservation.
*/
class Reservation {
/**
* The key of the record in the room_reservations database table.
*
* @var int
*/
public $id;
/**
* Indicates if the record has been deleted. 'Y' = deleted. 'N' = not
* deleted.
*
* @var string
*/
public $deleted;
/**
* The date for which the reservation has been made, in format YYYY-MM-DD.
*
* @var string
*/
public $date;
/**
* The time at which the reservation begins, in format 9999 (0000-2330).
*
* @var string
*/
public $time;
/**
* The length of time of the reservation, in minutes.
*
* @var int
*/
public $length;
/**
* The name of the room being reserved.
*
* @var string
*/
public $room;
/**
* The name assigned by the user to identify the reservation on the
* reservation calendar.
*
* @var string
*/
public $name;
/**
* The number of persons who will be using the room.
*
* @var int
*/
public $groupSize;
/**
* The Drupal user name of the person who made the reservation.
*
* @var string
*/
public $userName;
/**
* Indicates if the user has selected to receive confirmation and reminder
* messages by SMS message. 'Y' indicates receive messages by SMS. 'N'
* indicates do not receive messages by SMS.
*
* @var string
*/
public $textmsg;
/**
* The id of the record in the room_reservations_variables table that
* contains information about the user's selected SMS carrier.
*
* @var int
*/
public $carrier;
/**
* The phone number to which the SMS messages are sent for this
* reservation, if this option for receiving messages has been selected by the
* user, in the format 9999999999.
*
* @var string
*/
public $phone;
/**
* One or more email addresses to receive confirmation and reminder
* messages for this reservation, if this option for receiving messages
* has been selected by the user.
*
* @var string
*/
public $emailAddresses;
/**
* The date and time the reservation was created, in the format
* YYYY-MM-DD HH:MM:SS.
*
* @var string
*/
public $createDate;
/**
* The last date and time the reservation was updated, in the format
* YYYY-MM-DD HH:MM:SS. If the reservation has not been updated, will
* contain '0000-00-00 00:00:00'.
*
* @var string
*/
public $updateDate;
/**
* The date and time a reminder was sent for this reservation, in the format
* YYYY-MM-DD HH:MM:SS. If no reminder has been sent, will
* contain '0000-00-00 00:00:00'.
*
* @var string
*/
public $reminderDate;
/**
* The number of persons who can use the room being reserved.
*
* @var int
*/
public $roomCapacity;
/**
* The month number of the date of the reservation.
*
* @var int
*/
public $monthNumber;
/**
* The day of the month of the date of the reservation.
*
* @var int
*/
public $day;
/**
* The year of the date of the reservation.
*
* @var int
*/
public $year;
/**
* A user friendly display of the date of the reservation.
*
* @var string
*/
public $displayDate;
/**
* A user friendly display of the time of the reservation.
*
* @var type
*/
public $displayTime;
/**
* The category to which the reserved room belongs.
*
* @var string
*/
public $roomCategory;
/**
* Possible lengths of time that would be allowed for a reservation
* starting on the date and time of this reservation.
*
* @var array
*/
public $validLengths;
/**
* Constructor for a reservation object.
*
* @param int $id
* (optional) The key of the record in the room_reservations database table,
* if the record already exists.
*/
public function __construct($id = NULL) {
if ($id) {
$sql = "SELECT * FROM {room_reservations} WHERE id = :id AND deleted = 'N'";
$record_count = 0;
$results = db_query($sql, array(':id' => $id));
foreach ($results as $data) {
$this->id = $id;
$this->date = $data->date;
$this->time = $data->time;
$this->length = $data->length;
$this->room = $data->room;
$this->name = $data->name;
$this->groupSize = $data->group_size;
$this->userName = $data->user_name;
$this->emailAddresses = $data->email_addresses;
$this->textmsg = $data->textmsg;
$this->carrier = $data->carrier;
$this->phone = $data->phone;
$record_count++;
}
}
}
}
/**
* Create an array of all the room categories.
*
* @return array
* An array representing all the room categories.
*/
function _room_reservations_categories() {
$categories = array();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'room_reservations_category')
// this partially makes sense - do not show Categories which are unpublished
// issue will be that Rooms maybe be published with categories that aren't.. which will be a mess
//->propertyCondition('status', NODE_PUBLISHED)
// dangerous as this does more than Order By as it does not include records which do not have this field set
// so let's make sure the Order field is REQUIRED
->fieldOrderBy('reservations_display_order', 'value', 'ASC');
$result = $query->execute();
if (isset($result['node'])) {
$cids = array_keys($result['node']);
$cat_objs = node_load_multiple($cids);
foreach ($cat_objs as $cat) {
$categories[$cat->nid] = array(
'nid' => $cat->nid,
'title' => check_plain($cat->title),
'advmin' => isset($cat->reservations_minadvbooking[LANGUAGE_NONE][0]['value']) ? $cat->reservations_minadvbooking[LANGUAGE_NONE][0]['value'] : 0,
'prebuffer' => $cat->reservations_prebuffer[LANGUAGE_NONE][0]['value'],
'postbuffer' => $cat->reservations_postbuffer[LANGUAGE_NONE][0]['value'],
);
}
}
return $categories;
}
/**
* Retrieve all the rooms from the database.
*
* @return array
* An array with each element representing a room that can be reserved.
*/
function _room_reservations_rooms() {
$rooms = array();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'room_reservations_room')
->propertyCondition('status', NODE_PUBLISHED)
// dangerous as this does more than Order By as it does not include records which do not have this field set
->fieldOrderBy('reservations_display_order', 'value', 'ASC');
$result = $query->execute();
if (isset($result['node'])) {
$rids = array_keys($result['node']);
$room_objs = node_load_multiple($rids);
foreach ($room_objs as $room) {
$rooms[$room->nid] = (array) $room;
}
}
return $rooms;
}
/**
* Determine the list ofmonths as far out as we have set Extended advanced booking.
*
* @return array
* Each element represents a month.
*/
function _room_reservations_current_months() {
// How far out do we want to set allowed hours for?
// build a list of the months we need to look at
$advance_days = variable_get('room_reservations_advance_extended', 180);
$found = false;
for ($x = 0; $x < $advance_days; $x++) {
if (date('m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y"))) != $found) {
$months[date('Y-m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")))]['m'] = date('n', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
$months[date('Y-m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")))]['mm'] = date('m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
$months[date('Y-m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")))]['y'] = date('Y', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
$found = date('m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
}
}
// Note: Month names are translated when they are displayed.
$names = array(
'Unused',
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
);
foreach ($months as $month) {
$item['display'] = $names[$month['m']] . ' ' . $month['y'];
$item['year'] = $month['y'];
$item['month'] = $month['m'];
$item['MM'] = $month['mm'];
$item['YYYY_MM'] = $month['y'] . '_' . $month['mm'];
$results[] = $item;
}
return $results;
}
/**
* Store daily open hours in the database.
*
* Initialize a record to store all of the daily open hours for a
* single month, and save the record in the room_reservations_variables
* table.
*
* @param int $year
* The year of the month being represented.
* @param int $month
* The month number of the month being represented.
* @param string $yyyy_mm
* The year and month of the month being represented in format YYYY_MM.
* @param int $array
* if TRUE return mo_hours array rather than creating db record
*
* @return object
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
function _room_reservations_create_mo_hours($year, $month, $yyyy_mm, $array = false) {
$mo_hours = array();
// Days in the month.
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
$default_hours = unserialize(_room_reservations_get_variable('default_hours'));
if (!$default_hours) {
for ($x = 0; $x < $days; $x++) {
$mo_hours[] = 'D';
$mo_hours[] = '9999';
$mo_hours[] = '9999';
$mo_hours[] = '9999';
$mo_hours[] = '9999';
}
}
else {
for ($x = 0; $x < $days; $x++) {
// Day of week.
$dow = date('w', mktime(0, 0, 0, $month, $x + 1, $year));
$mo_hours[] = 'D';
$mo_hours[] = $default_hours[$dow * 4];
$mo_hours[] = $default_hours[($dow * 4) + 1];
$mo_hours[] = $default_hours[($dow * 4) + 2];
$mo_hours[] = $default_hours[($dow * 4) + 3];
}
}
$name = 'monthly_hours_' . $yyyy_mm;
if ($array) {
return $mo_hours;
}
else {
$result = _room_reservations_set_variable($name, serialize($mo_hours));
return $result;
}
}
/**
* Create reservations dates array.
*
* Create an array containing pertinent information about all the
* possible days for which a reservation can be made.
*
* 7.x-1.3 REV:
* - start of adding features on a per Category basis
* - for now let's just key this date's array by Cat ID
*
* @param int $selected_month
* The month of the day currently selected by the user
* @param int $selected_day
* The day of the month of the day currently selected by the user.
*
* @return array
* Information about each day for which a reservation can be made, including
* display name, day of the week, month name and number, day of the month,
* date in the format YYYY-MM-DD, whether the day is currently selected by
* the user, and whether the day is today.
*/
function _room_reservations_dates($selected_month = NULL, $selected_day = NULL, $keyed = false) {
// Determine date information (month, day, year, etc.) for each of these days.
$categories = _room_reservations_categories();
$dates = array();
foreach ($categories as $cat) {
$advancedaysmax = user_access('create room reservations extended')
? variable_get('room_reservations_advance_extended', 180)
: variable_get('room_reservations_advance_standard', 14);
$advancedaysmin = user_access('bypass minimum advance booking') ? 0 : $cat['advmin'];
for ($j = $advancedaysmin; $j < $advancedaysmax; $j++) {
$day = array();
$day['display'] = date("l, n/j", strtotime("now + " . $j . " days"));
$day['day-of-week'] = date("l", strtotime("now + " . $j . " days"));
$day['month'] = date("F", strtotime("now + " . $j . " days"));
$month_number = date("n", strtotime("now + " . $j . " days"));
$day['month-number'] = $month_number;
$sday = date("j", strtotime("now + " . $j . " days"));
$day['day'] = $sday;
$year = date("Y", strtotime("now + " . $j . " days"));
$day['year'] = $year;
// Determine the date selected by the user. If none selected, default to the first day.
if (($j == 0) && (!$selected_month) && (!$selected_day)) {
$day['selected'] = TRUE;
}
elseif (($selected_month == $month_number) && ($selected_day == $sday)) {
$day['selected'] = TRUE;
}
else {
$day['selected'] = FALSE;
}
// The date in YYYY-MM-DD format.
if ($month_number < 10) {
$month_number = str_pad($month_number, 2, '0', STR_PAD_LEFT);
}
if ($sday < 10) {
$sday = str_pad($sday, 2, '0', STR_PAD_LEFT);
}
$day['yyyymmdd'] = $year . "-" . $month_number . "-" . $sday;
$day['today'] = (($day['month-number'] == date('m')) &&
($day['day'] == date('j'))) ? TRUE : FALSE;
if ($keyed) {
$dates[$cat['nid']][$day['yyyymmdd']] = $day;
}
else {
$dates[$cat['nid']][] = $day;
}
}
}
return $dates;
}
/**
* Create an array representing every half hour time slot in a single day.
*
* @param string $option
* If set to 'limited', only include time slots in the array that are
* later in the day than the current time minus the longest possible
* reservation length.
*
* @return array
* An array representing reservable time slots in a single day.
*/
function _room_reservations_hours($option = NULL) {
//watchdog('rr', 'START');
$hours = array();
$x = 0;
$y = 0;
while ($x <= 23) {
$hours_entry = array();
$hour = ($x < 10) ? '0' . $x : $x;
if ($x == 0) {
$display_hour = 12;
}
elseif ($x <= 12) {
$display_hour = $x;
}
else {
$display_hour = $x - 12;
}
$minutes = ($y % 2) ? '30' : '00';
$time = $hour . $minutes;
$ampm = ($y < 24) ? t('AM') : t('PM');
if ($y == 0) {
// these shouldn't be wrapped in t() since we are just about to do a date() with them anyway
$display = 'Midnight';
}
elseif ($y == 24) {
$display = 'Noon';
}
else {
$display = $display_hour . ':' . $minutes . ' ' . $ampm;
}
// convert display to 24:00 format if required
if (variable_get('room_reservations_hour_format', 0)) {
$display = date('H:i', strtotime($display));
}
$class = ($y % 2) ? 'even' : 'odd';
$hours_node_time = $display_hour . ':' . $minutes . $ampm;
$hours_entry['time'] = $time;
$hours_entry['display'] = $display;
$hours_entry['hours node time'] = $hours_node_time;
$hours_entry['class'] = $class;
$hours_entry['open'] = TRUE;
$hours[] = $hours_entry;
if ($y % 2) {
$x++;
}
$y++;
}
// Only return time slots that are greater than the current time minus
// the maximum reservation length.
if ($option == 'limited') {
$max_length = user_access('create room reservations extended length')
? variable_get('room_reservations_max_length_extended', 120)
: variable_get('room_reservations_max_length_standard', 120);
$margin_time = ($max_length / 60) * 100;
if ($max_length % 60) {
$margin_time += 30;
}
$str_current_time = date('H') . date('i');
$int_current_time = intval($str_current_time);
$cutoff_time = $int_current_time - $margin_time;
$cutoff_time = ($cutoff_time < 0) ? 0 : $cutoff_time;
$limited_hours = array();
foreach ($hours as $time_slot) {
$time_slot_time = intval($time_slot['time']);
if ($time_slot_time > $cutoff_time) {
$limited_hours[] = $time_slot;
}
}
return $limited_hours;
}
else {
return $hours;
}
}
/**
* Create open hours array.
*
* Create an array of open hours information for each day for which a
* reservation can be made.
*
* NOTE - D6 version included only current month and next month since we could only book 15 days in advance
* since we can now book up to almost a year in advance; we need to redo how this is done
*
* @return array
* A two dimensional array, with the first dimension representing a single
* day for which a reservation can be made, and the second dimension
* representing information about the facility's open hours for that day, such
* as whether the facility is open that day, the number of open shifts,
* open and close hours, and a string that can be used to display the hours
* in a user friendly way.
*/
function _room_reservations_facility_hours($reset = FALSE) {
static $building_hours;
if (!isset($building_hours) || $reset) {
$building_hours = array();
if (user_access('create room reservations extended')) {
$advancedays = variable_get('room_reservations_advance_extended', 180);
}
else {
$advancedays = variable_get('room_reservations_advance_standard', 14);
}
// build a list of the months we need to look at
for ($x = 0; $x < $advancedays; $x++) {
$months[] = date('Y_m', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
}
$months = array_unique($months);
//$today = date('Y-m-d');
$mo_hours = array();
foreach ($months as $month) {
$mo_hours_this = unserialize(_room_reservations_get_variable('monthly_hours_' . $month));
if ($mo_hours_this) {
$mo_hours = array_merge($mo_hours, $mo_hours_this);
}
else {
$m = intval(drupal_substr($month, 5));
$year = drupal_substr($month, 0, 4);
$mo_hours_this = _room_reservations_create_mo_hours($year, $m, $month, true);
$mo_hours = array_merge($mo_hours, $mo_hours_this);
}
}
$start = (date('j') - 1) * 5;
for ($x = 0; $x < $advancedays; $x++) {
$yyyymmdd = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d") + $x, date("Y")));
$first_shift_open = $mo_hours[($start + ($x * 5) + 1)];
$first_shift_close = $mo_hours[($start + ($x * 5) + 2)];
$second_shift_open = $mo_hours[($start + ($x * 5) + 3)];
$second_shift_close = $mo_hours[($start + ($x * 5) + 4)];
if (($first_shift_open == '9999') && ($first_shift_close == '9999') && ($second_shift_open == '9999') && ($second_shift_close == '9999')) {
$open = FALSE;
}
else {
$open = TRUE;
}
if (($open) && ($first_shift_open == '0000') && ($first_shift_close == '2400')) {
$open_24_hours = TRUE;
}
else {
$open_24_hours = FALSE;
}
if (!$open) {
$shifts = 0;
}
elseif ($open_24_hours) {
$shifts = 1;
}
elseif (($open) && ($second_shift_open == '9999') && ($second_shift_close == '9999')) {
$shifts = 1;
}
else {
$shifts = 2;
}
$day_hours = array(
$first_shift_open,
$first_shift_close,
$second_shift_open,
$second_shift_close,
);
$display = _room_reservations_hours_display($day_hours);
$hours_data = array(
'open' => $open,
'open_24_hours' => $open_24_hours,
'shifts' => $shifts,
'first_shift_open' => $first_shift_open,
'first_shift_close' => $first_shift_close,
'second_shift_open' => $second_shift_open,
'second_shift_close' => $second_shift_close,
'display' => $display,
);
$building_hours[($yyyymmdd)] = $hours_data;
}
}
return $building_hours;
}
/*
if ($this_day <= $dom) {
// This is a day in the current month.
$yyyymmdd_yyyy = $year;
$yyyymmdd_mm = $month;
if ($this_day < 10) {
$yyyymmdd_dd = '0' . $this_day;
}
else {
$yyyymmdd_dd = $this_day;
}
}
else {
// This is a day in the next month.
$yyyymmdd_yyyy = $next_month_year;
$yyyymmdd_mm = $next_month;
$next_month_day = $this_day - $dom;
if ($next_month_day < 10) {
$yyyymmdd_dd = '0' . $next_month_day;
}
else {
$yyyymmdd_dd = $next_month_day;
}
}*/
/**
* Creates a string for displaying the open hours for a single day.
*
* @param array $day_hours
* An array that represents the openning and closing hours for two
* separate shifts in a single day
*
* @return string
* A string that can be used to display the hours for a single day, such as
* 'Open 24 Hours' or 'Noon - 6:00 PM'.
*/
function _room_reservations_hours_display($day_hours) {
$hours = _room_reservations_hours();
$first_shift_open = $day_hours[0];
$first_shift_close = $day_hours[1];
$second_shift_open = $day_hours[2];
$second_shift_close = $day_hours[3];
// Closed.
if (($first_shift_open == '9999') &&
($first_shift_close == '9999') &&
($second_shift_open == '9999') &&
($second_shift_close == '9999')) {
return 'Closed';
}
// Open 24 hours.
if (($first_shift_open == '0000') &&
($first_shift_close == '2400')) {
return 'Open 24 Hours';
}
// One shift.
if (($second_shift_open == '9999') &&
($second_shift_close == '9999')) {
$first_shift_open_display
= _room_reservations_display_time($first_shift_open);
$first_shift_close_display
= _room_reservations_display_time($first_shift_close);
return $first_shift_open_display . ' - ' .
$first_shift_close_display;
}
// Two shifts.
$first_shift_open_display
= _room_reservations_display_time($first_shift_open);
$first_shift_close_display
= _room_reservations_display_time($first_shift_close);
$second_shift_open_display
= _room_reservations_display_time($second_shift_open);
$second_shift_close_display
= _room_reservations_display_time($second_shift_close);
return $first_shift_open_display . ' - ' .
$first_shift_close_display . ' and ' .
$second_shift_open_display . ' - ' .
$second_shift_close_display;
}
/**
* Create time slot array.
*
* Create an array with each element representing one of the 48 half hour time
* slots that make up a day.
*
* @return array
* An array with each element representing a half hour time slot.
*/
function _room_reservations_times() {
$times = array();
$hours = _room_reservations_hours();
foreach ($hours as $hour) {
$times[] = $hour['time'];
}
return $times;
}
/**
* Return time in display format.
*
* This function returns the time in display format (Midnight, 12:30 AM,
* 1:00 AM, etc.) for any time slot of the day given in military time
* format (0000, 0030, 0100, etc.).
*
* @param string $military_time
* Time of day represented in four digit military time.
*
* @return string
* Time of day represented as HH:MM AM.
*/
function _room_reservations_display_time($military_time) {
$hours = _room_reservations_hours();
$hours[] = array(
'time' => '2400',
'display' => 'Midnight',
);
foreach ($hours as $hour) {
$time = $hour['time'];
if ($time == $military_time) {
return $hour['display'];
}
}
return '';
}
/**
* Determine closing times.
*
* Determines which half hour time slots represent the last ones before the
* building closes.
*
* @param string $yyyymmdd
* The date for which close times are being determine, in the format
* YYYY-MM-DD.
*
* @return array
* An array representing the time slots just before closing. The array can
* contain 0, 1, or 2 items.
*/
function _room_reservations_close_times($yyyymmdd) {
$closing_times = array();
$_room_reservations_building_hours = _room_reservations_facility_hours();
$building_hours_day = $_room_reservations_building_hours[$yyyymmdd];
// 24 hours.
if ($building_hours_day['open_24_hours']) {
$next_day = date('Y-m-d', strtotime("$yyyymmdd +1 days"));
$next_day_first_time_slot_open
= _room_reservations_first_slot_open($next_day);
if (!$next_day_first_time_slot_open) {
$closing_times[] = '2330';
}
return $closing_times;
}
// First shift ends at midnight.
if ($building_hours_day['first_shift_close'] === '2400') {
$next_day = date('Y-m-d', strtotime("$yyyymmdd +1 days"));
$next_day_first_time_slot_open
= _room_reservations_first_slot_open($next_day);
if (!$next_day_first_time_slot_open) {
$closing_times[] = '2330';
}
return $closing_times;
}
// First shift does not end at midnight.
$time = $building_hours_day['first_shift_close'];
$hours = _room_reservations_hours();
$time_found = FALSE;
while (!$time_found) {
$time_slot = array_pop($hours);
if ($time_slot['time'] == $time) {
$time_found = TRUE;
}
}
$time_slot = array_pop($hours);
$int_second_shift_close = intval($building_hours_day['second_shift_close']);
$closing_times[] = $time_slot['time'];
// Second shift ends at midnight.
if (($building_hours_day['shifts'] == 2) &&
($int_second_shift_close == 2400)) {
$next_day = date('Y-m-d', strtotime("$yyyymmdd +1 days"));
$next_day_first_time_slot_open
= _room_reservations_first_slot_open($next_day);
if (!$next_day_first_time_slot_open) {
$closing_times[] = '2330';
}
}
// Second shift does not end at midnight.
if (($building_hours_day['shifts'] == 2) &&
($int_second_shift_close < 2400)) {
$time = $building_hours_day['second_shift_close'];
$hours = _room_reservations_hours();
$time_found = FALSE;
while (!$time_found) {
$time_slot = array_pop($hours);
if ($time_slot['time'] == $time) {
$time_found = TRUE;
}
}
$time_slot = array_pop($hours);
$closing_times[] = $time_slot['time'];
}
return $closing_times;
}
/**
* Determine if facility is open from midnight to 12:30 AM.
*
* Determines if the facility is open during the first half hour of the day,
* from midnight to 12:30 AM. This information is needed when determining if
* any particular half hour time slot is the last one before the building
* closes.
*
* @param string $yyyymmdd
* The date being examined, in the format YYYY-MM-DD.
*
* @return bool
* TRUE - The facility is open during the first half hour of the day.
* FALSE - The facility is not open during the first half hour of the day.
*/
function _room_reservations_first_slot_open($yyyymmdd) {
$_room_reservations_building_hours = _room_reservations_facility_hours();
$building_hours_day = $_room_reservations_building_hours[$yyyymmdd];
if (!$building_hours_day['open']) {
return FALSE;
}
if ($building_hours_day['open_24_hours']) {
return TRUE;
}
if ($building_hours_day['first_shift_open'] == '0000') {
return TRUE;
}
return FALSE;
}
/**
* Determine reservation start conflicts.
*
* Determines if a new reservation room, date and start time conflicts with a
* previously existing reservation.
*
* @param string $room
* The room that is being reserved.
* @param string $yyyymmdd
* The date of the start time for the reservation, in the format 'yyyy-mm-dd'.
* @param string $time
* The start time for the reservation, in military time. 9:00 AM is
* represented as '0900', and 9:00 PM is represented as '2100'.
*
* @return bool
* TRUE - A scheduling conflict was found.
* FALSE - A scheduling conflict was not found.
*/
function _room_reservations_start_conflicts($room, $yyyymmdd, $time) {
// Previous and next days.
$previous_day = date('Y-m-d', strtotime("$yyyymmdd -1 days"));
// Start times of other reservations that could conflict with this one.
// Since reservations are limited to 2 hours, we are interested in times that
// are less than or equal to the reservation time and greater than or equal
// to 90 minutes before the reservation time.
$max_length = user_access('create room reservations extended length') ? variable_get('room_reservations_max_length_extended', 120) : variable_get('room_reservations_max_length_standard', 120);
$max_slots = $max_length / 30;
$search_items = array();
for ($x = 0; $x < 8; $x++) {
$search_items = array(
'date' => '1999-01-01',
'start_time' => '9999',
'length' => 999,
);
}
$day = $yyyymmdd;
$hours = _room_reservations_hours();
$time_found = FALSE;
$time_slot = NULL;
while (!$time_found) {
$time_slot = array_pop($hours);
if ($time_slot['time'] == $time) {
$time_found = TRUE;
}
}
for ($x = 0; $x < $max_slots; $x++) {
if ($time_slot == NULL) {
$day = $previous_day;
$hours = _room_reservations_hours();
$time_slot = array_pop($hours);
}
$search_item['date'] = $day;
$search_item['start_time'] = $time_slot['time'];
$search_item['length'] = 30 * $x;
$search_items[] = $search_item;
$time_slot = array_pop($hours);
}
$sql = "
SELECT id FROM {room_reservations}
WHERE (
(deleted = 'N' AND room = :room AND date = :date0 AND time = :time0 AND length > :length0)
OR
(deleted = 'N' AND room = :room AND date = :date1 AND time = :time1 AND length > :length1)
OR
(deleted = 'N' AND room = :room AND date = :date2 AND time = :time2 AND length > :length2)
OR
(deleted = 'N' AND room = :room AND date = :date3 AND time = :time3 AND length > :length3)
OR
(deleted = 'N' AND room = :room AND date = :date4 AND time = :time4 AND length > :length4)
OR
(deleted = 'N' AND room = :room AND date = :date5 AND time = :time5 AND length > :length5)
OR
(deleted = 'N' AND room = :room AND date = :date6 AND time = :time6 AND length > :length6)
OR
(deleted = 'N' AND room = :room AND date = :date7 AND time = :time7 AND length > :length7)
)
";
$conflicts_found = FALSE;
$conflicts_found = db_query($sql, array(
':room' => $room,
':date0' => $search_items[0]['date'], ':time0' => $search_items[0]['start_time'], ':length0' => $search_items[0]['length'],
':date1' => $search_items[1]['date'], ':time1' => $search_items[1]['start_time'], ':length1' => $search_items[1]['length'],
':date2' => $search_items[2]['date'], ':time2' => $search_items[2]['start_time'], ':length2' => $search_items[2]['length'],
':date3' => $search_items[3]['date'], ':time3' => $search_items[3]['start_time'], ':length3' => $search_items[3]['length'],
':date4' => $search_items[4]['date'], ':time4' => $search_items[4]['start_time'], ':length4' => $search_items[4]['length'],
':date5' => $search_items[5]['date'], ':time5' => $search_items[5]['start_time'], ':length5' => $search_items[5]['length'],
':date6' => $search_items[6]['date'], ':time6' => $search_items[6]['start_time'], ':length6' => $search_items[6]['length'],
':date7' => $search_items[7]['date'], ':time7' => $search_items[7]['start_time'], ':length7' => $search_items[7]['length'])
)->rowCount();
return $conflicts_found;
}
/**
* Determine valid reservation lengths of time.
*
* Determines which lengths of time are valid for a reservation for a particular
* room starting at particular time. Valid lengths are limited by the
* following:
* (1) Previously scheduled reservations.
* (2) Building close times.
* (3) Last time slot of the day. Reservations possibly end 15 minutes before
* the building closes.
*
* @param string $room
* The room that is being reserved.
* @param string $yyyymmdd
* The date of the start time for the reservation, in the format 'yyyy-mm-dd'.
* @param string $time
* The start time for the reservation, in military time.
* 9:00 AM is represented as '0900', and 9:00 PM is represented as '2100'.
* @param int $id
* The id of the reservation being made.
* @param int $all