-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsked.module
executable file
·1539 lines (1179 loc) · 50.2 KB
/
sked.module
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
* Administration of schedule upload and display for X-Ray Associates radiologist group.
* Märt Matsoo. [email protected].
*/
/**
* Implementation of hook_init
*/
function sked_init() {
// Load includes.
module_load_include('inc', 'sked', 'includes/sked_csv_upload');
// Load css.
drupal_add_css(drupal_get_path('module', 'sked') . '/css/sked.css');
// Assigning these here to avoid php error when doing isset(arg(0)) check.
$arg0 = arg(0);
$arg1 = arg(1);
$arg2 = arg(2);
$sked_settings = array();
$sked_settings['arg0'] = (isset($arg0)) ? $arg0 : 0;
$sked_settings['arg1'] = (isset($arg1)) ? $arg1 : 0;
$sked_settings['arg2'] = (isset($arg2)) ? $arg2 : 0;
// Add settings to Drupal.settings object.
drupal_add_js(array('sked' => $sked_settings), 'setting');
// Load js.
drupal_add_js(drupal_get_path('module', 'sked') . '/js/sked.js');
//drupal_add_js('jQuery(document).ready(function () { var S5 = "do this and do that";//alert(S5); });', array('type' => 'inline', 'scope' => 'header'));
}
/**
* Implementation of hook_permission().
*/
function sked_permission() {
$perms = array(
'admin xray schedule' => array(
'title' => t('Admin X-Ray Schedule'),
'description' => t('Full administrator access for schedule-related options.'),
'restrict access' => TRUE,
),
);
return $perms;
}
/**
* Implements hook_user_login.
* Taken from: http://api.drupal.org/api/drupal/modules%21user%21user.api.php/function/hook_user_login/7
*/
function sked_user_login(&$edit, $account) {
// Just in case, clear out $_GET['destination'].
if (isset($_GET['destination'])):
unset($_GET['destination']);
endif;
// Redirect South Lake, Mackenzie and Clinic roles directly to schedule page for that location.
// south lake calendar-work-date-ts-sl/month
// mackenzie calendar-work-date-ts-mh/month
// clinic calendar-work-date-ts-clinic/month
if (in_array('south lake', array_values($account->roles))):
$redirection = 'calendar-work-date-ts-sl/month';
endif;
if (in_array('mackenzie health', array_values($account->roles))):
$redirection = 'calendar-work-date-ts-mh/month';
endif;
if (in_array('clinic', array_values($account->roles))):
$redirection = 'calendar-work-date-ts-clinic/month';
endif;
if (in_array('rad', array_values($account->roles))):
$redirection = variable_get('xray_login_redirect', 'node/2');
endif;
// Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.
if (!is_null($redirection)) {
$edit['redirect'] = $redirection;
}
}
/**
* Implementation of hook_menu().
*/
function sked_menu() {
$items = array();
$items['sked/location-shifts/ych'] = array(
'title' => 'YCH Shifts',
'description' => t('Redirect to schedule page for YCH (C).'),
'menu_name' => 'main-menu',
'access callback' => TRUE,
// 'access arguments' => array('admin xray schedule'),
'page callback' => 'sked_goto_location_shifts',
'page arguments' => array(2),
'type' => MENU_NORMAL_ITEM,
);
$items['sked/location-shifts/southlake'] = array(
'title' => 'Southlake Shifts',
'description' => t('Redirect to schedule page for Southlake (S) shifts.'),
'menu_name' => 'main-menu',
'access callback' => TRUE,
// 'access arguments' => array('admin xray schedule'),
'page callback' => 'sked_goto_location_shifts',
'page arguments' => array(2),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked'] = array(
'title' => 'X-Ray Schedule Management',
'access arguments' => array('admin xray schedule'),
'position' => 'right',
'weight' => 69,
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/sked/settings'] = array(
'title' => 'X-Ray Schedule Settings',
'description' => t('General settings form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('sked_settings'),
'access arguments' => array('admin xray schedule'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked/upload'] = array(
'title' => 'Upload Schedule CSV',
'description' => t('Upload schedule CSV file.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('sked_upload_csv'),
'access arguments' => array('admin xray schedule'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked/summary'] = array(
'title' => 'Shift Summary',
'description' => t('Adding up shift hours per month. Shift reconciliation.'),
'page callback' => 'sked_shift_summary',
'access arguments' => array('admin xray schedule'),
'file' => 'includes/sked_shift_summary.inc',
'file path' => drupal_get_path('module', 'sked'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked/duplicate'] = array(
'title' => 'Check for duplicate shifts',
'description' => t('YYYY-MM in URL (ex. admin/config/sked/duplicate/@date/month) determines which month is being checked.', array('@date' => date('Y-m', time()))),
'page callback' => 'sked_duplicate_shift_check',
'access arguments' => array('admin xray schedule'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked/missing'] = array(
'title' => 'Check for missing shifts',
'description' => t('Uses the assigned shift days to make sure no shifts are missing for the month. YYYY-MM in URL (ex. admin/config/sked/missing/@date/month) determines which month is being checked.', array('@date' => date('Y-m', time()))),
'page callback' => 'sked_missing_shift_check',
'access arguments' => array('admin xray schedule'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sked/shiftdays'] = array(
'title' => 'Assign Shifts to Days of Week',
'description' => t('Certain days are expected to have certain shifts. System can use this to find if there are days with missing shifts.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('sked_shift_assign_day'),
'access arguments' => array('admin xray schedule'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Redirect user to schedule page for either YCH or Southlake.
*/
function sked_goto_location_shifts($ych_sl = 'ych') {
if ($ych_sl == 'ych') {
drupal_goto('shift-calendar-work-date-ts/month/' . date('Y-m') . '/C');
}
else {
drupal_goto('shift-calendar-work-date-ts/month/' . date('Y-m') . '/S');
}
}
/**
* Form to administer which shifts are expected on which days.
*/
function sked_shift_assign_day($form, &$form_state) {
$form = array();
$shifts = _sked_build_shift_dropdown_options();
$day_names = array(0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat'));
for ($counter = 0;$counter < 7;$counter++) {
switch ($counter) {
case 0:
$first_or_last = 'first';
break;
case 6:
$first_or_last = 'last';
break;
default:
$first_or_last = 'interior';
break;
}
$form['shift_day_open_' . $counter] = array(
'#markup' => '<div class="shift-day-wrapper ' . $first_or_last . '"><h3>' . $day_names[$counter] . '</h3>',
);
// Reset shifts array.
reset($shifts);
foreach ($shifts as $key => $value) {
if (strstr($key, '*') === FALSE): // strlen($key) <= 2 &&
$form['shift_day_' . $key . '_' . $counter] = array(
'#type' => 'checkbox',
'#title' => t($value),
'#default_value' => variable_get('shift_day_' . $key . '_' . $counter, 0),
'#description' => t($key . ' on ' . $day_names[$counter]),
);
endif;
}
$form['shift_day_close' . $counter] = array(
'#markup' => '</div>',
);
}
return system_settings_form($form);
}
/**
* Implements hook_entity_info.
*/
// Started going by http://www.istos.it/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity
function sked_entity_info() {
// @TODO. This probably needs to be beefier and I am not sure it is all correct. (ie. bundles)
$return = array(
'xray_sked' => array(
'label' => t('X-Ray Schedule'),
'base table' => 'xray_sked',
'entity keys' => array(
'id' => 'sid',
),
'uri callback' => 'sked_uri',
'bundles' => array(
'xray'=> array(
'label' => 'X-Ray',
'admin' => array(
'path' => 'admin/config/sked/settings',
'access arguments' => array('admin xray schedule'),
),
),
),
),
);
return $return;
}
// See http://www.istos.it/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity
function sked_uri(){
return array(
'path' => 'sked/0',
);
}
/**
* Let Views know to look for include file.
*/
function sked_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'sked') . '/includes/views',
);
}
/**
* Form builder for general settings form.
*/
function sked_settings($form, &$form_state) {
$form = array();
$form['xray_test_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Test mode'),
'#default_value' => variable_get('xray_test_mode', 0),
'#description' => t('Just here as stub for settings form.'),
);
$form['xray_test_rad_id_to_use'] = array(
'#type' => 'textfield',
'#title' => t('Show Info for Rad with Drupal User ID...'),
'#default_value' => variable_get('xray_test_rad_id_to_use', 2), // 2 is Drupal uid for Silmberg.
'#size' => 1,
'#description' => t('While testing mode is turned on, info will be displayed as if this Drupal user is the logged in user. Default value of 2 is the user id that corresponds with Silmberg.'),
);
$form['xray_login_redirect'] = array(
'#type' => 'textfield',
'#title' => t('Login Redirect'),
'#default_value' => variable_get('xray_login_redirect', 'node/2'), // node/2 is stub for My Schedule
'#size' => 40,
'#description' => t('Page we want user to visit once they log in. Default is node/2.'),
);
$form['xray_dayoff_id'] = array(
'#type' => 'textfield',
'#title' => t('Node ID for X rotation (day off)'),
'#default_value' => variable_get('xray_dayoff_id', 32),
'#size' => 1,
'#description' => t('X rotation is treated as a day off. At time of development, 32 was node id for day off.'),
);
$form['xray_unknown_rotation_id'] = array(
'#type' => 'textfield',
'#title' => t('Node ID for unknown rotation'),
'#default_value' => variable_get('xray_unknown_rotation_id', 36),
'#size' => 1,
'#description' => t('Catch-all in case we have codes the system does not recognise. At time of development, 36 was node id for unknown rotation.'),
);
return system_settings_form($form);
}
/**
UPLOAD NOTES
csv file might be messy with lots of extra fields so program defensively.
1. make sure doc id exists and is valid
2. Match month with number of days to loop through, don't go beyond 31 no matter what (Erik puts stuff into columns beyond the scheduling days)
3. Make sure shift code is valid. How do I do this? The current shiftcode has almost 200 distinct codes! Erik combines them as he sees fit.
*/
/**
* Form builder for csv upload.
*/
function sked_upload_csv($form, &$form_state) {
// Going to upload so need this as a form attribute.
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['upload'] = array(
'#type' => 'file',
'#title' => t('Select CSV file'),
'#description' => t('CSV file is expected to hold shift information for a single month.'),
'#name' => 'upload',
'#size' => 22,
'#theme_wrappers' => array(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload')
);
return $form;
}
// What about PE*, VD, PC1V*, QQBB ???
function sked_upload_csv_validate($form, &$form_state) {
$bad_file = _sked_csv_upload_reject_csv($form, $form_state);
if ($bad_file):
form_set_error('upload', t('CSV file does not match expected format.'));
endif;
}
function sked_upload_csv_submit($form, &$form_state) {
_sked_csv_upload_save_sked_info($form, $form_state);
}
/**
* Implements hook_block_info().
*/
function sked_block_info() {
$blocks = array();
$blocks['my_shift'] = array(
'info' => t('Current rad\'s shift info.'),
);
$blocks['rounds'] = array(
'info' => t('Rounds info'),
);
$blocks['shift_schedule_dd'] = array(
'info' => t('Shift code schedule by month'),
);
$blocks['rad_schedule_dd'] = array(
'info' => t('Rad schedule by month'),
);
$blocks['duplicates'] = array(
'info' => t('Duplicate shift info'),
);
$blocks['missing_shifts'] = array(
'info' => t('Missing shift info'),
);
$blocks['jump_to_month_dd'] = array(
'info' => t('Jump forward or back several months'),
);
$blocks['important_mtg'] = array(
'info' => t('Important XRA Event'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function sked_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'my_shift':
$block['subject'] = t('My Shift');
$block['content'] = _sked_build_shift();
break;
case 'rounds':
$block['subject'] = t('Rounds');
$block['content'] = _sked_build_round();
break;
case 'shift_schedule_dd':
global $user;
if ($user->uid !== 0):
$block['subject'] = t('Check Shift');
$block['content'] = drupal_get_form('_sked_build_shift_dropdown');
endif;
break;
case 'rad_schedule_dd':
global $user;
if ($user->uid !== 0):
$block['subject'] = t('Check Other Rad');
$block['content'] = drupal_get_form('_sked_build_rad_dropdown');
endif;
break;
case 'duplicates':
global $user;
// Check to see if $user has the admin role.
if (in_array('admin', array_values($user->roles))):
$block['subject'] = t('Duplicate Shifts');
// Duplicate shift check returns an object.
$objDuplicate = sked_duplicate_shift_check(arg(2), 'month'); // arg(2) has format of 2013-07.
$block['content'] = $objDuplicate->strOutput;
endif;
break;
case 'missing_shifts':
global $user;
// Check to see if $user has the admin role.
if (in_array('admin', array_values($user->roles))):
$block['subject'] = t('Missing Shifts');
// Missing shift check returns an object.
$objMissing = sked_missing_shift_check(arg(2), 'month'); // arg(2) has format of 2013-07.
$block['content'] = $objMissing->strOutput;
endif;
break;
case 'jump_to_month_dd':
$block['subject'] = t('Jump to');
$block['content'] = drupal_get_form('_sked_build_jump_to_dd');
break;
case 'important_mtg':
$block['subject'] = ''; // Will place title in content.
$block['content'] = _sked_display_important_mtg();
break;
}
return $block;
}
/**
* Recognise important meeting and display
* message for user.
*/
function _sked_display_important_mtg() {
// Get default value for dropdown.
$arg2 = arg(2);
$default_value = (isset($arg2)) ? $arg2 : date('Y-m');
$query = db_select('xray_sked', 'x');
$query
->condition('code_type', 'event', '=')
->condition('call_flag', '1', '=')
->condition('work_date', date('Y-m-d'), '>=')
->condition('work_date', $default_value . '-01', '>=')
->condition('work_date', $default_value . '-' . cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($default_value . '-01')), date('Y', strtotime($default_value . '-01'))), '<=')
->fields('x', array('nid', 'code', 'work_date'))
->orderBy('work_date');
$result = $query->execute();
$strOutput = '<!-- rowCount:' . $result->rowCount() . ' ' . $default_value . '-01' . ' to ' . $default_value . '-' . cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($default_value . '-01')), date('Y', strtotime($default_value . '-01'))) . ' -->';
// Only return if we have something important to return.
if ($result->rowCount() > 0) {
$strOutput .= '<div id="imp-mtg"><h3>' . t('Important Meeting Coming Up') . '</h3>';
$strOutput .= '<ul>';
foreach($result as $row) {
$strOutput .= '<li>Meeting on: ' . date('D, d M Y', strtotime($row->work_date)) . '. <em>' . $row->code . '</em></li>';
}
$strOutput .= '</ul><p class="small extra-info extrainfo">' . t('See calendar for more details') . '</p></div>';
}
return $strOutput;
}
/**
* Build dropdown for rads to skip to specific months.
*/
function _sked_build_jump_to_dd() {
// Get default value for dropdown.
$arg2 = arg(2);
$default_value = (isset($arg2)) ? $arg2 : date('Y-m');
$form['jump-month'] = array(
'#type' => 'select',
'#options' => _sked_build_jump_to_dropdown_options(),
'#description' => '',
'#default_value' => $default_value, //date('Y-m'),
'#required' => TRUE,
);
$form['jump-submit'] = array(
'#type' => 'submit',
'#weight' => 100,
'#value' => t('Go')
);
return $form;
}
/**
* Helper function to jump back or forward 6 months.
*/
function _sked_build_jump_to_dropdown_options() {
$current_year = date('Y');
$next_year = $current_year + 1;
$last_year = $current_year - 1;
$current_month = date('n');
$day_to_use = '15';
// Figure out month to start with.
$months = array(
'1' => array($last_year . '-08', $last_year . '-09', $last_year . '-10', $last_year . '-11', $last_year . '-12', $current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06'),
'2' => array($last_year . '-09', $last_year . '-10', $last_year . '-11', $last_year . '-12', $current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07'),
'3' => array($last_year . '-10', $last_year . '-11', $last_year . '-12', $current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08'),
'4' => array($last_year . '-11', $last_year . '-12', $current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09'),
'5' => array($last_year . '-12', $current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10'),
'6' => array($current_year . '-01', $current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11'),
'7' => array($current_year . '-02', $current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12'),
'8' => array($current_year . '-03', $current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12', $next_year . '-01'),
'9' => array($current_year . '-04', $current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12', $next_year . '-01', $next_year . '-02'),
'10' => array($current_year . '-05', $current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12', $next_year . '-01', $next_year . '-02', $next_year . '-03'),
'11' => array($current_year . '-06', $current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12', $next_year . '-01', $next_year . '-02', $next_year . '-03', $next_year . '-04'),
'12' => array($current_year . '-07', $current_year . '-08', $current_year . '-09', $current_year . '-10', $current_year . '-11', $current_year . '-12', $next_year . '-01', $next_year . '-02', $next_year . '-03', $next_year . '-04', $next_year . '-05'),
);
$options = array();
foreach ($months[$current_month] as $value) {
$options[$value] = date('M Y', strtotime($value . '-' . $day_to_use));
}
return $options;
}
/**
* Build shift dropdown which takes user to schedule page for that shift.
*/
function _sked_build_rad_dropdown() {
$form['rad'] = array(
'#type' => 'select',
'#options' => _sked_build_rad_dropdown_options(),
'#description' => t('Choose Rad then "Go"'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 100,
'#value' => t('Go')
);
return $form;
}
/**
* Build shift dropdown which takes user to schedule page for that shift.
*/
function _sked_build_shift_dropdown() {
$form['shift'] = array(
'#type' => 'select',
'#options' => _sked_build_shift_dropdown_options(),
'#description' => t('Choose Shift then "Go"'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 100,
'#value' => t('Go')
);
return $form;
}
/**
* Submit function.
*/
function _sked_build_shift_dropdown_submit($form_id, &$form_state) {
$url = 'shift-calendar-work-date-ts/month/' . date('Y-m') . '/' . $form_state['values']['shift'];
drupal_goto($url);
}
/**
* Submit function.
*/
function _sked_build_rad_dropdown_submit($form_id, &$form_state) {
$url = 'rad-shift-calendar-work-date-ts/' . date('Y-m') . '/' . $form_state['values']['rad'];
drupal_goto($url);
}
/**
* Build rad dropdown options.
*/
function _sked_build_rad_dropdown_options() {
$query = db_select('users', 'u');
$query->join('users_roles', 'ur', 'u.uid = ur.uid');
$query
->condition('status', '1', '=')
->condition('rid', '4', '=')
->fields('u', array('uid', 'name'))
->orderBy('name');
$result = $query->execute();
$options = array();
foreach($result as $row) {
$options[$row->uid] = $row->name;
}
return $options;
}
/**
* Build shift dropdown options.
* Takes all shift codes from xray_sked table.
*/
function _sked_build_shift_dropdown_options() {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node', '=')
->propertyCondition('type', 'rotation', '=')
->propertyCondition('status', 1, '=')
->propertyOrderBy('title ', 'ASC');
$result = $query->execute();
$nids = array_keys($result['node']);
$shifts = entity_load('node', $nids);
foreach ($shifts as $key => $value) {
$options[$value->field_shift_code['und'][0]['safe_value']] = $value->field_shift_code['und'][0]['safe_value'];
}
ksort($options);
return $options;
}
/**
* Build html output for custom block that has current rad's shift info.
*/
function _sked_build_shift() {
global $user;
$current_rad_id = $user->uid;
$shift_nid = db_query("SELECT nid FROM {xray_sked} WHERE uid = :uid AND work_date = :today AND code_type LIKE :rotation", array(':uid' => $current_rad_id, ':today' => date('Y-m-d'), ':rotation' => 'rotation'))->fetchField();
if ($shift_nid) {
$node = node_load($shift_nid);
$strOutput = '<h3>' . $node->field_shift_code['und'][0]['safe_value'] . '</h3>';
$strOutput .= $node->body['und'][0]['safe_value'];
}
else {
$strOutput = '<h3 class="error missing">' . t('No Shift Today') . '</h3><p>' . t('According to the system, you are not working today!') . '</p>';
}
return $strOutput;
}
/**
* Build html output for custom block that has current rad's shift info.
*/
function _sked_build_round() {
global $user;
$current_rad_id = $user->uid;
$dayoff_id = variable_get('xray_dayoff_id', 32);
$unknown_rotation_id = variable_get('xray_unknown_rotation_id', 36);
$round_name = db_query("SELECT code FROM {xray_sked} WHERE uid = :uid AND work_date = :today AND code_type LIKE :rounds", array(':uid' => $current_rad_id, ':today' => date('Y-m-d'), ':rounds' => 'rounds'))->fetchField();
// If we get a round name that is not X, then build display.
if ($round_name && strtolower($round_name) !== 'x') {
$strOutput = '<h3>' . $round_name. '</h3>';
//$strOutput .= $node->body['und'][0]['safe_value'];
}
else {
$strOutput = '<h3 class="error missing">' . t('No Rounds Today') . '</h3><p>' . t('According to the system, no rounds for you today!') . '</p>';
}
return $strOutput;
}
/**
* Implements hook_views_query_alter
*/
function sked_views_query_alter(&$view, &$query) {
switch ($view->name) {
case 'schedule':
_sked_shift_code_operator_to_like($view, $query);
break;
}
}
/**
* Implementation of hook_node_insert().
* Adding this here because getting the
* event added to the schedule view as event
* content type would be too ugly.
* This way each event is also thrown into
* xray_sked table and it's associated with
* the actual event node here.
*/
function sked_node_insert($node) {
// watchdog('xray', 'in hook_node_insert <pre>' . print_r($node,true) . '</pre>');
// Event nodes get added to xray_sked table for ease of display.
if ($node->type === 'event') {
$importance_flag = ($node->field_importance['und'][0]['value'] == 'important') ? 1 : 0;
$work_date = date('Y-m-d', strtotime($node->field_event_start['und'][0]['value']));
$work_date_ts = strtotime($node->field_event_start['und'][0]['value']);
$sid = db_insert('xray_sked')
->fields(array(
'uid' => $node->uid,
'nid' => $node->nid,
'rad_id' => 0,
'code' => $node->title, // May as well pass node title here.
'code_type' => 'event',
'call_flag' => $importance_flag,
'work_date' => $work_date,
'work_date_ts' => $work_date_ts,
))
->execute();
// watchdog('xray', $node->title . ' nid:' . $node->nid . ' added into xray_sked table.');
}
}
/**
* Implementation of hook_node_update().
*/
function sked_node_update($node) {
// Should be in xray_sked table too so update here.
if ($node->type === 'event') {
// Find the sid from xray_sked.
$sked_id = db_query("SELECT sid FROM {xray_sked} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
$importance_flag = ($node->field_importance['und'][0]['value'] == 'important') ? 1 : 0;
$work_date = date('Y-m-d', strtotime($node->field_event_start['und'][0]['value']));
$work_date_ts = strtotime($node->field_event_start['und'][0]['value']);
$sid = db_update('xray_sked')
->fields(array(
'uid' => $node->uid,
'nid' => $node->nid,
'rad_id' => 0,
'code' => $node->title, // May as well pass node title here.
'code_type' => 'event',
'call_flag' => $importance_flag,
'work_date' => $work_date,
'work_date_ts' => $work_date_ts,
))
->condition('sid', $sked_id, '=')
->execute();
}
}
/**
* Implementation of hook_node_delete().
*/
function sked_node_delete($node) {
// Clean-up xray_sked table if an event node is deleted.
if ($node->type === 'event') {
db_delete('xray_sked')->condition('nid', $node->nid)->execute();
}
}
/**
* Event info for calendar view.
*/
function _sked_get_event_info_for_template($nid) {
$node = node_load($nid);
// @todo. Use l function.
// return l($node->title, 'node/' . $node->nid);
$strOutput = '<a href="/node/' . $node->nid . '" title="' . $node->body['und'][0]['value'] . '" class="beautytips">' . $node->title . '</a>';
return $strOutput;
}
/**
* Changing operator and value to do LIKE '%C7%'
* instead of = 'C7'.
*/
function _sked_shift_code_operator_to_like($view, $query) {
// @TODO. Remove watchdog.
// watchdog('sked', 'view <pre>' . print_r($view, true) . '</pre>');
if ($view->current_display == 'by_shift'):
if ($query->where[0]['conditions'][0]['field'] == 'xray_sked.code'
&& $query->where[0]['conditions'][0]['operator'] == '=') {
$query->where[0]['conditions'][0]['operator'] = 'LIKE';
$query->where[0]['conditions'][0]['value'] = '%' . $query->where[0]['conditions'][0]['value'] . '%';
}
endif;
}
/**
* Check for duplicate shifts on day or month.
* @param
* String. YYYY-MM format.
* @param
* String. "day" or "month".
*/
function sked_duplicate_shift_check($day = '', $day_or_month = 'month') {
// Default to this month.
if ($day == ''):
$year = date('Y', time());
$month = date('m', time());
$day = $year . '-' . $month . '-01';
// Get number of days in the month.
$max_days = date('t', strtotime($year . '-' . $month . '-01'));
// Properly formatted date for db query.
$end = $year . '-' . $month . '-' . $max_days;
endif;
// Are we checking duplicates for just one day or a whole month?
if ($day_or_month == 'day') {
$result = db_query("SELECT code, COUNT(*) as nums FROM {xray_sked} WHERE code NOT LIKE :x AND work_date = :day GROUP BY code ORDER BY code", array(':x' => 'X', ':day' => $day));
}
else {
// Separate year and month from $day variable. (ex. 2013-07-01)
$arrDate = explode('-', $day);
$year = $arrDate[0];
$month = $arrDate[1];
// Get number of days in the month.
$max_days = date('t', strtotime($year . '-' . $month . '-01'));
// Properly formatted date for db query.
$end = $year . '-' . $month . '-' . $max_days;
$result = db_query("SELECT code, work_date, COUNT(*) as nums FROM {xray_sked} WHERE code NOT LIKE :x AND work_date >= :start and work_date <= :end GROUP BY code, work_date ORDER BY code", array(':x' => 'X', ':start' => $day, ':end' => $end));
}
$shifts_bad = array();
$counter = 0;
foreach ($result as $row) {
// Greater than 1 means a duplicate.
if ($row->nums > 1) {
$shifts_bad[$counter]['code'] = $row->code;
$shifts_bad[$counter]['nums'] = $row->nums;
if ($day_or_month == 'day') {
$shifts_bad[$counter]['day'] = $day;
}
else {
$shifts_bad[$counter]['day'] = $row->work_date;