-
Notifications
You must be signed in to change notification settings - Fork 0
/
om_timeslot_scheduler.module
executable file
·1663 lines (1318 loc) · 73.5 KB
/
om_timeslot_scheduler.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
// $Id$
/**
* Implementation of hook_views_api().
*/
function om_timeslot_scheduler_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'om_timeslot_scheduler'),
);
}
/**
* Implementation of hook_perm().
*/
function om_timeslot_scheduler_perm() {
return array('access scheduling options of own shows', 'access scheduling options of aired shows', 'access scheduling options of all shows', 'manage scheduling', 'administer scheduling', 'administer Open Media System');
} // function om_timeslot_scheduler_perm
/**
* Implementation of hook_help().
*/
function om_timeslot_scheduler_help($section) {
switch ($section) {
case 'admin/help#timeslot_scheduler':
return t('Core scheduling functions.');
break;
}
}
/**
* Implementation of hook_node_info().
*/
function om_timeslot_scheduler_node_info() {
return array(
'om_timeslot_event' => array(
'name' => t('Timeslot Event'),
'module' => 'om_timeslot_scheduler',
),
'om_timeslot_theme' => array(
'name' => t('Timeslot Theme'),
'module' => 'om_timeslot_scheduler',
),
);
}
/**
* Implementation of hook_form().
*
*/
function om_timeslot_scheduler_form(&$node, $form_state) {
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
}
if ($type->has_body) {
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
return $form;
}
/**
* Implementation of hook_menu().
*/
function om_timeslot_scheduler_menu() {
$items = array();
// Adds Open Media Section to Admin Interface
// Duplicated in om_show, om_project
$items['admin/openmedia'] = array(
'title' => 'Open Media System',
'description' => 'Administration and configuration options for Open Media System.',
'position' => 'right',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer Open Media System'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
'weight' => -20,
);
$items['admin/openmedia/om-timeslot-scheduler'] = array(
'title' => 'Scheduling Configuration',
'description' => 'Configuration options for the Open Media Timeslot Scheduler.',
'page callback' => 'drupal_get_form',
'page arguments' => array('om_timeslot_admin_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/openmedia/om-timeslot-scheduler/remount/%node'] = array(
'title' => 'Remount Playback Server',
'description' => 'Attempts to remount playback server mount point using Apache user.',
'page callback' => 'om_timeslot_playback_server_remount',
'page arguments' => array(4),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
$items['om-timeslot-scheduler/styles'] = array(
'title' => 'Returns CSS for timeslots',
'description' => 'Returns CSS for timeslots',
'page callback' => 'om_timeslot_theme_styles',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['om-timeslot-scheduler/add/show/%node'] = array(
'title' => 'Moving Video and Metadata to Playback Server',
'description' => 'Takes node ID and channel tid. Returns confirmation video and metadata have been transfered',
'page arguments' => array(3, 4),
'page callback' => 'om_timeslot_scheduler_add_show',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['om-timeslot-scheduler/self_schedule_airing'] = array(
'title' => 'Self Schedule Airing',
'description' => 'Use OPS to allow users to self schedule an airing.',
'page arguments' => array(2, 3, 4, 5),
'page callback' => '_om_timeslot_scheduler_find_openings',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['om-timeslot-scheduler/find/openings/%node'] = array(
'title' => 'Openings in Timeslot longer than the Duration of this Show',
'description' => 'Takes node ID, timeslot ID. Returns openings',
'page arguments' => array(3, 4),
'page callback' => 'om_timeslot_scheduler_find_openings',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['timeslots/bychannel'] = array(
'title' => 'Scheduling Options by Channel',
'page callback' => 'om_timeslot_channel_selector_result',
'page arguments' => array(2, 3),
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/airing-options'] = array(
'title' => 'Scheduling Options',
'description' => 'Takes node ID and returns options scheduling based on project and themes',
'page callback' => 'om_timeslot_airing_options',
'page arguments' => array(1),
'access callback' => 'om_timeslot_airing_options_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['node/%node/playback-status'] = array(
'title' => 'Status',
'description' => 'Takes node ID and returns the server status variables based on sub_module functionality',
'page callback' => 'om_timeslot_playback_server_status',
'page arguments' => array(1),
'access callback' => 'om_timeslot_playback_server_status_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
// Adds Manage Scheduling to Admin Interface
$items['admin/om-timeslot-scheduler/manage'] = array(
'title' => 'Manage Scheduling',
'description' => 'Manage Scheduling of Open Media Shows onto Playback Servers',
'position' => 'right',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('manage scheduling'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
'weight' => -17,
);
$items['admin/om-timeslot-scheduler/manage/default'] = array(
'title' => 'Overview',
'description' => 'Dashboard of options for Scheduling',
'page callback' => 'om_timeslot_scheduler_manage',
'access arguments' => array('manage scheduling'),
'type' => MENU_NORMAL_ITEM,
'weight' => 20,
);
$items['admin/om-timeslot-scheduler/manage/themes'] = array(
'title' => 'Manage Timeslot Themes',
'description' => 'Configure Timeslot Themes',
'page callback' => 'om_timeslot_scheduler_manage_themes',
'access arguments' => array('manage scheduling'),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items['admin/om-timeslot-scheduler/manage/events'] = array(
'title' => 'Manage Timeslot Events',
'description' => 'f of Timeslot Events',
'page callback' => 'om_timeslot_scheduler_manage_events',
'access arguments' => array('manage scheduling'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
/**
* Builds the Timeslot Scheduler admininstration settings form.
*/
function om_timeslot_admin_settings() {
$form = array();
$channels = om_timeslot_scheduler_get_channels();
//Check to see if om_show and om_broadcast_sync are installed and configured. While Timeslot Scheduler does not require these modules, it will not be bale to do much until they are installed and configured
if (!module_exists('om_show')) {
$status_message = $status_message . '<p><a href="http://drupal.org/project/om_show">Open Media Show</a> is not installed. While you can configure Timeslot Themes and Events without Show, Timeslow Scheduler is designed to schedule Shows.</p>';
}
/* I'm guessing we can remove this now that we are on airings and om_broadcast_sync is deprecated (?) */
if (!module_exists('om_broadcast_sync')) {
$status_message = $status_message . '<p><a href="http://drupal.org/project/om_show">Open Media Broadcast Synchronization</a> is not installed. While you can configure Timeslot Themes and Eventsn, you will not be able to accurately determine what is already scheduled on your playback servers without installing and configuring Broadcast Synchronization.</p>';
}
if (module_exists('om_timeslot_princeton')||module_exists('om_timeslot_cablecast')||module_exists('om_timeslot_synergy')||module_exists('om_timeslot_leightronix')||module_exists('om_timeslot_maestrovision')) {
$status_message = $status_message . '<p>At least one playback server specific sub module is installed.</p>';
}
else {
$status_message = $status_message . '<p>There are no playback server specific sub modules installed.</p>';
}
$form['om_timeslot_scheduler_user_scheduled_first_airing'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to schedule first airings'),
'#default_value' => variable_get('om_timeslot_scheduler_user_scheduled_first_airing', ''),
'#description' => t('Producers will be given scheduling options when new Shows are added.'),
);
$form['om_timeslot_scheduler_admin_scheduled_first_airing'] = array(
'#type' => 'checkbox',
'#title' => t('Allow project administrators to schedule first airings for all shows in project'),
'#default_value' => variable_get('om_timeslot_scheduler_admin_scheduled_first_airing', ''),
'#description' => t('Project administrators will be given scheduling options when new Shows are added.'),
);
$form['om_timeslot_scheduler_first_airing_channel'] = array(
'#type' => 'select',
'#title' => t('Default Airing on Channel'),
'#options' => $channels,
'#default_value' => variable_get('om_timeslot_scheduler_first_airing_channel', ''),
'#description' => t('New Shows will be have their first airing scheduled to Timeslot Events marked as User Schedulable on this channel.'),
);
$form['om_timeslot_scheduler_number_of_airing_options'] = array(
'#type' => 'textfield',
'#title' => t('Number of Airing options producers are shown'),
'#default_value' => variable_get('om_timeslot_scheduler_number_of_airing_options', '3'),
'#description' => t('Producers are given a few options for their initial airing.'),
);
$form['om_timeslot_scheduler_hours_between_airing_options'] = array(
'#type' => 'textfield',
'#title' => t('Minimum hours between Airing options'),
'#default_value' => variable_get('om_timeslot_scheduler_hours_between_airing_options', '7'),
'#description' => t('After finding the first available airing time, the system will move forward this number of hours before looking for another opening. This is designed to give some variation in the airing options between primetime, late night, and early morning hours. If the first available airing is at 3AM and the system is set for a minimum of 7 hours, it will look for the next available time to air this program after 10AM.'),
);
$form['om_timeslot_scheduler_minutes_since_cron'] = array(
'#type' => 'textfield',
'#title' => t('Maximum Minutes Since Cron'),
'#default_value' => variable_get('om_timeslot_scheduler_minutes_since_cron', '15'),
'#description' => t('If cron has not run within this many minutes, scheduling options are disabled. This is to ensure a current version of the schedule exists preventing the user from scheduling over an existing show. Set to 0 to ignore cron when scheduling.'),
);
$form['om_timeslot_scheduler_cron_error'] = array(
'#type' => 'textarea',
'#title' => t('Cron Error'),
'#default_value' => variable_get('om_timeslot_scheduler_cron_error', 'Scheduling options are disabled because the Playback server\'s schedule has not been synchronized since ***lastcron***. Without a current version of the schedule, your show could be scheduling over an existing show. If you continue to get this error for several hours, contact the site Administrator.'),
'#description' => t('Cron plays an important role in scheduling, but most producers have no ideas what cron is. This allows you to customize the error they see when they are prevented from scheduling due to cron issues.'),
);
$form['om_timeslot_scheduler_self_max_days'] = array(
'#type' => 'textfield',
'#title' => t('Maximum Number of Days in Advance a User can Self Schedule'),
'#default_value' => variable_get('om_timeslot_scheduler_self_max_days', '7'),
'#description' => t('This will limit the display of available Timeslots to this number of day in advance. Set to 0 to ignore when scheduling.'),
);
$form['om_timeslot_scheduler_self_max_days_for_project'] = array(
'#type' => 'textfield',
'#title' => t('Maximum Number of Days in Advance a User can Self Schedule a Series'),
'#default_value' => variable_get('om_timeslot_scheduler_self_max_days_for_series', '30'),
'#description' => t('This will extend the display of available Timeslots to this number of day in advance for Timeslots associated to a Project. Set to 0 to ignore when scheduling.'),
);
$form['om_timeslot_scheduler_limit_to_project_slots'] = array(
'#type' => 'checkbox',
'#title' => t('Project Timeslots Only'),
'#default_value' => variable_get('om_timeslot_scheduler_limit_to_project_slots', '0'),
'#description' => t('If this setting is checked, non-admin users will only see options for scheduling a show into a Project Timeslot if any Project Timeslots exist.'),
);
$hours_description = t('<div>Enter military time for both start and end time of when adult content should be blocked, separated by a dash, in the format <em>hh:mm-hh:mm</em></div>ex. <em>05:00-22:00</em> would block adult content from 5AM to 10PM. Leave blank to allow adult content to be scheduled at any time.');
$form['om_timeslot_scheduler_adult_block_hours'] = array(
'#type' => 'textfield',
'#title' => t('Block Adult Content During These Hours'),
'#default_value' => variable_get('om_timeslot_scheduler_adult_block_hours', '05:00-22:00'),
'#description' => $hours_description,
);
return system_settings_form($form);
}
/**
* Implementation of hook_form_alter().
*/
function om_timeslot_scheduler_form_alter(&$form, $form_state, $form_id) {
// Node add/edit forms.
if (isset($form['type']) && isset($form['#node'])) {
$type = $form['type']['#value'];
if ($form_id == 'om_timeslot_event_node_form' || $form_id == 'om_timeslot_theme_node_form' || $form_id == 'om_timeslot_server_node_form') {
$node = (object) $form['#node'];
$timeslot_rules = db_query("SELECT n.title FROM {node} n WHERE n.type = '%s' AND n.status = %d ORDER BY n.title", 'om_timeslot_rule', 1);
while ($timeslot_rule = db_fetch_object($timeslot_rules)) {
$rules = $rules . $timeslot_rule->title . ', ';
} // while
$form['om_timeslot_rule_list'] = array(
'#type' => 'fieldset',
'#title' => t('Available Timeslot Rules'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 1,
'#description' => $rules,
);
}
}
}
function om_timeslot_scheduler_themes() {
$result = db_query("SELECT title FROM {node} n JOIN {content_type_om_timeslot_theme} ct ON n.vid = ct.vid WHERE type = 'om_timeslot_theme'");
return db_fetch_object($result);
}
function om_timeslot_scheduler_theme_stats($theme_id, $date) {
if ($date) {
$result = db_query("SELECT count(c.vid) as total FROM {content_field_om_theme} c JOIN {node} n ON n.vid = c.vid WHERE field_om_theme_nid = %d AND created > '%s'", $theme_id, $date);
}
else {
$result = db_query("SELECT count(c.vid) as total FROM {content_field_om_theme} c WHERE field_om_theme_nid = %d", $theme_id);
}
$theme_total = db_fetch_object($result);
return $theme_total->total;
}
function om_timeslot_scheduler_theme_duration($theme_id, $date) {
if ($date) {
$result = db_query("SELECT field_om_show_duration_value FROM {content_type_om_show} c JOIN {node} n ON n.vid = c.vid JOIN {content_field_om_theme} cf ON c.nid = cf.nid WHERE field_om_theme_nid = %d AND created > '%s'", $theme_id, $date);
}
else {
$result = db_query("SELECT field_om_show_duration_value FROM {content_type_om_show} c JOIN {content_field_om_theme} cf ON c.nid = cf.nid WHERE field_om_theme_nid = %d", $theme_id);
}
while ($theme_total = db_fetch_object($result)) {
$duration = $duration + $theme_total->field_om_show_duration_value;
}
return $duration;
}
function om_timeslot_scheduler_event_duration($theme_id, $startdate, $enddate) {
if ($startdate) {
//2010-02-16 14:44:24
$formatdate = date("y-m-d", $startdate) . ' 00:00:00';
$result = db_query("SELECT field_om_timeslot_date_value, field_om_timeslot_date_value2 FROM {content_type_om_timeslot_event} JOIN {content_field_om_timeslot_date} ON content_type_om_timeslot_event.vid = content_field_om_timeslot_date.vid JOIN {content_field_om_theme} cf ON content_type_om_timeslot_event.nid = cf.nid WHERE field_om_theme_nid = %d AND field_om_timeslot_date_value > '%s'", $theme_id, $date);
}
else {
$result = db_query("SELECT field_om_timeslot_date_value, field_om_timeslot_date_value2 FROM {content_type_om_timeslot_event} JOIN {content_field_om_timeslot_date} ON content_type_om_timeslot_event.vid = content_field_om_timeslot_date.vid JOIN {content_field_om_theme} cf ON content_type_om_timeslot_event.nid = cf.nid WHERE field_om_theme_nid = %d", $theme_id);
}
while ($theme_total = db_fetch_object($result)) {
$duration = $duration + (strtotime($theme_total->field_om_timeslot_date_value2.' UTC') - strtotime($theme_total->field_om_timeslot_date_value.' UTC'));
}
return $duration;
}
/**
* Used to add the CSS to the timeslot calendar.
*/
function om_timeslot_theme_styles() {
if (user_access('administer scheduling') || user_access('administer Open Media System')) {
$result = db_query("SELECT node.nid as nnid,field_om_timeslot_theme_color_value, title FROM {content_type_om_timeslot_theme} JOIN {node} ON node.vid = content_type_om_timeslot_theme.vid");
}
else {
$result = db_query("SELECT node.nid as nnid,field_om_timeslot_theme_color_value, title FROM {content_type_om_timeslot_theme} JOIN {node} ON node.vid = content_type_om_timeslot_theme.vid WHERE status > 0");
}
$result = db_query("SELECT node.nid as nnid,field_om_timeslot_theme_color_value, title FROM {content_type_om_timeslot_theme} JOIN {node} ON node.vid = content_type_om_timeslot_theme.vid");
print '<style>';
while ($om_theme = db_fetch_object($result)) {
$timeslot_theme = str_replace(array('&', ',', ' ', '/'), array('', '', '-', ''), strtolower($om_theme->title));
print '.theme-' . $timeslot_theme . ' div , .theme-' . $timeslot_theme . ' div a { ' . "\n" . ' background-color: ' . $om_theme->field_om_timeslot_theme_color_value . ' !important;' . "\n" . '}' . "\n";
}
print '</style>';
}
/**
* Adds the HTML key to the Timeslot Calendar
*/
function om_timeslot_theme_style_key() {
global $base_path;
if (user_access('administer scheduling') || user_access('administer Open Media System')) {
$result = db_query("SELECT node.nid as nnid,field_om_timeslot_theme_color_value, title FROM {content_type_om_timeslot_theme} JOIN {node} ON node.vid = content_type_om_timeslot_theme.vid ORDER BY title");
}
else {
$result = db_query("SELECT node.nid as nnid,field_om_timeslot_theme_color_value, title FROM {content_type_om_timeslot_theme} JOIN {node} ON node.vid = content_type_om_timeslot_theme.vid WHERE status > 0 ORDER BY title");
}
$output_array = array();
$count = 0;
while ($om_theme = db_fetch_object($result)) {
$timeslot_theme = str_replace(array('&', ',', ' ', '/'), array('', '', '-', ''), strtolower($om_theme->title));
$output_array[$count] = '<div class="theme-' . $timeslot_theme . '"><div style="float:left; width: 20px;"> </div> ' . l($om_theme->title, 'node/' . $om_theme->nnid) . '</div>';
$count++;
}
$loopcount = 0;
if (count($output_array)) {
$halfoutput = round(count($output_array)/3);
$output .= '<table width="100%"><tr><td valign="top">';
foreach ($output_array as $i => $value) {
$output .= $output_array[$i];
if ($halfoutput==$loopcount) {
$output .= '</td><td valign="top">';
$loopcount = 0;
}
$loopcount++;
}
$output .= '</td></tr></table>';
}
return $output;
}
/**
* Builds the Timeslot Scheduler admininstration settings form.
*/
function om_timeslot_scheduler_add_show($node, $channel_tid) {
$server = om_timeslot_scheduler_get_servers($channel_tid);
if ($server->field_om_timeslot_server_type_value == 'princeton' && module_exists('om_timeslot_princeton')) {
if ( user_access('access scheduling options of own shows') || user_access('access scheduling options of all shows') || user_access('manage scheduling') || user_access('administer scheduling') || user_access('administer Open Media System') ) {
$output .= om_timeslot_princeton_transfer($node, $channel_tid);
}
else {
$output .= t('You do not have permission schedule this show.');
}
}
else {
$output .= t('om_timeslot_princeton is not enabled');
}
print theme('page', $output);
}
/**
* Adds the Airing to the playback server and creates a temp om_airing record
*/
function om_timeslot_scheduler_check_existing($nid, $channel) {
//NEEDS TO LOOKUP WHICH SERVER FUNCTION TO CALL BASED ON CHANNEL
om_timeslot_princeton_check_existing($nid, $channel);
}
/**
* om_timeslot_channel_selector_form_handler()
* AHAH form handler for om_timeslot_airing_options_channel_selector
*/
function om_timeslot_channel_selector_form_handler($form) {
$channel = $form['channel-selector']['#value'];
$nid = $form['channel-selector']['#storage'];
drupal_goto('timeslots/bychannel/' . $nid . '/' . $channel);
}
function om_timeslot_channel_selector_result($nid, $channel) {
global $user;
$show_title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
$channel_number = om_playback_servers_get_channel_number_from_tid($channel);
drupal_set_title('Timeslots for ' . $show_title . ' on ' .$channel_number);
$timeslots = om_timeslot_generate_available_self_scheduling_timeslots($nid, $channel);
$output = '<p>';
if($timeslots) {
foreach ($timeslots AS $key => $value) {
// Watch for series and plan accordingly.
if(preg_match('/s-/', $key)) {
$series_links .= $value . '- ' . l('Schedule It!', 'om-timeslot-scheduler/find/openings/' . $nid . '/' . trim(str_replace('s-', '', $key))) . '<br />';
} else {
$theme_links .= $value . '- ' . l('Schedule It!', 'om-timeslot-scheduler/find/openings/' . $nid . '/' . $key) . '<br />';
}
}
// If you wanted everyone to be able to schedule all available timeslots you could change/remove this check
if ($user->uid == 1 || in_array('staff', $user->roles) || in_array('interns', $user->roles)) {
$series_links = $series_links ? $series_links : 'There are no series timeslots available.';
$output .= '<h3>Series Timeslots For This Show</h3>' . $series_links . '<h3>Theme Timeslots For This Show</h3>' . $theme_links;
}
else {
if ($series_links) {
$output .= '<h3>Series Timeslots For This Show</h3>' . $series_links;
} else {
$output .= 'Your show seems to have no series timeslots available. Here are some options based on your show\'s theme: <h3>Theme Based Timeslots For This Show</h3>' . $theme_links;
}
}
}
else {
$output .= t('There has been an a system error, please contact station staff. There are no timeslots available for your show.');
}
$output .= '</p>';
print theme('page', $output);
}
/**
* om_timeslot_airing_options_channel_selector(&$form_state)
* Generate airing options form if you are privy to see it.
* @returns $form drupal standard form array.
*/
function om_timeslot_airing_options_channel_selector() {
global $user;
// If you are of the correct role choose a channel.
if (user_access('manage scheduling') || user_access('administer scheduling')) {
$channels = om_playback_servers_get_channel_taxonomy();
foreach ($channels AS $value) {
$channels_options[$value->tid] = $value->name;
}
$form['#cache'] = TRUE;
$form['channel-selector'] = array(
'#title' => 'Select a channel to self-schedule',
'#description' => 'Select a channel to schedule.',
'#type' => 'select',
'#options' => $channels_options,
'#default_value' => variable_get('om_timeslot_scheduler_first_airing_channel'),
'#storage' => arg(1),
);
$form['channel-submit'] = array(
'#type' => 'submit',
'#value' => 'submit',
);
$form['#submit'][] = 'om_timeslot_channel_selector_form_handler';
}
// Otherwise use default.
else {
$channel = variable_get('om_timeslot_scheduler_first_airing_channel', NULL);
$timeslots = om_timeslot_generate_available_self_scheduling_timeslots(arg(1), $channel);
if($timeslots) {
foreach ($timeslots AS $key => $value) {
$options .= $value . '- ' . l('Schedule It!', 'om-timeslot-scheduler/find/openings/' . arg(1) . '/' . $key) . '<br />';
}
}
$form['channel-selector'] = array(
//@TODO: GENERALIZE 56
//'#prefix' => '<b>You are currently scheduling on DOM 56</b><br/>',
'#value' => 'Please select a Timeslot below. <br/>' . $options,
);
}
return $form;
}
/**
* om_timeslot_generate_available_self_scheduling_timeslots($nid, $channel_tid)
* Given the show nid and channel tid generate a list of matching theme based timeslots.
* @param $nid, show nid
* @param $channel_tid, tid of desired channel
* @returns $timeslots array of available timelots in the format: $timeslots[tid] = name.
*/
function om_timeslot_generate_available_self_scheduling_timeslots($nid, $channel_tid) {
$node = node_load($nid);
// @TODO: Why were these seperate queries?
//$is_adult_audience = db_result(db_query('SELECT field_om_rating_value FROM {content_field_om_rating} WHERE nid = %d', $nid)) == 'TV-MA' ? TRUE : FALSE;
//$group_nid = db_result(db_query('SELECT group_nid FROM {og_ancestry} WHERE nid = %d', $nid));
$is_adult_audience = $node->field_om_rating[0]['value'] == 'TV-MA' ? TRUE : FALSE;
$group_nid = reset($node->og_groups);
$series_timeslot_query = db_query("SELECT node.nid, title FROM {content_type_om_timeslot_event} JOIN {node} ON node.vid = content_type_om_timeslot_event.vid JOIN {term_node} ON node.nid = term_node.nid WHERE field_om_project_nid = %d AND tid = %d", $group_nid, $channel_tid);
while ($series_timeslot = db_fetch_array($series_timeslot_query)) {
$timeslots['s-' . $series_timeslot['nid']] = $series_timeslot['title'];
}
if (module_exists('om_timeslot_pairings')) {
$show_theme = db_result(db_query('SELECT field_om_theme_nid FROM {content_field_om_theme} WHERE nid = %d', $nid));
$show_pair = db_result(db_query('SELECT pairing_nid FROM {om_timeslot_pairings} WHERE theme_nid = %d', $show_theme));
$result = db_query("SELECT node.nid, title, field_om_timeslot_date_value, field_om_timeslot_date_value2 FROM {content_type_om_timeslot_event} JOIN {node} ON node.vid = content_type_om_timeslot_event.vid JOIN {content_field_om_timeslot_date} ON content_field_om_timeslot_date.vid = node.vid JOIN {content_field_om_theme} cf ON node.nid = cf.nid LEFT JOIN {term_node} ON content_type_om_timeslot_event.nid = term_node.nid WHERE (
cf.field_om_theme_nid = %d OR cf.field_om_theme_nid = %d ) AND node.status = 1 AND term_node.tid = %d GROUP BY title", $show_theme, $show_pair, $channel_tid);
while ($timeslot_event = db_fetch_array($result)) {
if(!$is_adult_audience) {
$timeslots[$timeslot_event['nid']] = $timeslot_event['title'];
}
else {
$start_time_of_event = date('G', strtotime($timeslot_event['field_om_timeslot_date_value'] . 'UTC'));
$end_time_of_event = date('G', strtotime($timeslot_event['field_om_timeslot_date_value2'] . 'UTC'));
if($start_time_of_event >= 22 && ($end_time_of_event == 23 || $end_time_of_event <= 6)) {
$timeslots[$timeslot_event['nid']] = $timeslot_event['title'];
}
}
}
} else {
//dsm('not using pairings');
//dsm($nid);
$show_theme = $node->field_om_theme[0]['nid'];
//$show_theme = db_result(db_query('SELECT field_om_theme_nid FROM {content_field_om_theme} WHERE nid = %d'), $nid);
//dsm('show theme:');
//dsm($show_theme);
//dsm('channel:');
//dsm($channel_tid);
//SELECT node.nid, title, field_om_theme_nid FROM `term_node` JOIN node ON node.nid = term_node.nid JOIN content_field_om_theme cf ON node.nid = cf.nid WHERE `tid` = 344 AND type = 'om_timeslot_event' AND field_om_theme_nid = 2123
$result = db_query("SELECT node.nid, title, field_om_timeslot_date_value, field_om_timeslot_date_value2 FROM {content_type_om_timeslot_event} JOIN {node} ON node.vid = content_type_om_timeslot_event.vid JOIN {content_field_om_timeslot_date} ON content_field_om_timeslot_date.vid = node.vid JOIN {content_field_om_theme} cf ON node.nid = cf.nid LEFT JOIN {term_node} ON content_type_om_timeslot_event.nid = term_node.nid WHERE
cf.field_om_theme_nid = %d AND node.status = 1 AND term_node.tid = %d GROUP BY title", $show_theme , $channel_tid);
while ($timeslot_event = db_fetch_array($result)) {
if(!$is_adult_audience) {
$timeslots[$timeslot_event['nid']] = $timeslot_event['title'];
}
else {
$start_time_of_event = date('G', strtotime($timeslot_event['field_om_timeslot_date_value'] . 'UTC'));
$end_time_of_event = date('G', strtotime($timeslot_event['field_om_timeslot_date_value2'] . 'UTC'));
if($start_time_of_event >= 22 && ($end_time_of_event == 23 || $end_time_of_event <= 6)) {
$timeslots[$timeslot_event['nid']] = $timeslot_event['title'];
}
}
}
}
return $timeslots ? $timeslots : array();
}
/**
* Shows user the Timeslot Events that match their Theme or Project
*/
function om_timeslot_airing_options($node) {
global $user;
global $base_path;
if ($node->field_om_show_aired[0]['value'] && !user_access('access scheduling options of aired shows')) {
$output .= '<div class="messages error">' . t('Sorry. This show has either already aired or is scheduled to air soon. If you want this show to air again, get everyone you know to vote for it!') . '</div>';
}
else {
$lastcron = variable_get('cron_last', '');
$humandate = date("F j, Y, g:i a", $lastcron);
$sincecron = round((time() - $lastcron)/60);
$output .= '<div class="message">Playback server schedule last synchronized: ' . date("F j, Y, g:i a", $lastcron) . ' (' . $sincecron . ' minutes ago)</div>';
$cron_tolerance = variable_get('om_timeslot_scheduler_minutes_since_cron', 0);
//If cron tolerance is set to 0, don't bother checking
if ($cron_tolerance) {
if ($cron_tolerance < $sincecron) {
$cron_error = TRUE;
}
else {
$cron_error = FALSE;
}
}
else {
$cron_error = FALSE;
}
if ($cron_error) {
// @TODO: Cahnge to use token
$error = variable_get('om_timeslot_scheduler_cron_error', 'error');
$output = $output . '<div class="messages error">' . str_replace('***lastcron***', $humandate, $error) . '</div>';
}
else {
if ($node->field_om_show_aired[0]['value'] && !user_access('access scheduling options of aired shows')) {
$output = $output . '<div class="messages error">' . t('This show has already aired. It can only be rescheduled by users with administrative scheduling permissions.') . '</div>';
}
else {
$output .= drupal_get_form('om_timeslot_airing_options_channel_selector');
}
}
} // end if aired
print theme('page', $output);
}
/**
* Checks om_airings for existing airings. Displays possible start times.
*/
function om_timeslot_scheduler_find_openings($node, $timeslot_nid) {
global $user;
date_default_timezone_set('UTC');
//$interval_between_scheduling = 1800; //seconds
//$interval_between_airings = 86400; // seconds
// First check to see if the show in question has been scheduled within 30 minutes.
//$last_airing = db_result(db_query('SELECT MAX(node.changed)FROM `node` join content_type_om_airing AS airing ON airing.nid = node.nid WHERE field_om_airing_mpeg2_value = "%S" AND node.type = "om_airing"', $node->field_om_show_mpeg2[0]['filename']));
if(user_access('manage scheduling') || user_access('administer scheduling') || $user->uid == 1){
//bypass
}
else{
//@TODO: This should be changed to check nid against node_reference in om_airing
//$last_airing = db_result(db_query('SELECT MAX(nid) FROM {content_type_om_airing} WHERE field_om_airing_mpeg2_value = "%s"', trim($node->field_om_show_mpeg2[0]['filename'])));
//$last_airing = db_result(db_query('SELECT MAX(nid) FROM {content_type_om_airing} WHERE field_om_airing_filename_value = "%s"', trim($node->field_om_show_mpeg2_path[0]['value'])));
$last_airing = db_result(db_query('SELECT MAX(nid) FROM {content_type_om_airing} WHERE field_om_airing_mpeg2_value = "%s"', trim($node->field_om_show_mpeg2_path[0]['value'])));
if($last_airing){
$output .= '<p>This show has already been scheduled once. If you would like to schedule it again, please contact station personnel.</p>';
print theme('page', $output);
return;
}
}
if (module_exists('om_timeslot_pairings')) {
/* temp */
$output .= '<div><h3>Schedule Your Show</h3>';
$output .= 'Trying to schedule ' . $node->title . '<br />';
$output .= 'Duration: ' . $node->field_om_show_duration[0]['value'] . ' seconds<br />';
// For Error Tracking -- Need to detect if file is too big or if there are no gaps.
$limit_by = 4;
$current_timeslot = 0;
$openings_status = NULL; // tristate 'Success', 'Too Big', 'No Gaps'
$result = db_query("SELECT * FROM {content_field_om_timeslot_date} otdv JOIN {term_node} ON otdv.nid = term_node.nid JOIN {content_type_om_timeslot_event} ote ON otdv.nid = ote.nid WHERE otdv.nid = %d AND field_om_timeslot_date_value > NOW() AND ote.field_om_timeslot_userscheduled_value = 1 ORDER BY field_om_timeslot_date_value LIMIT 0, %d", $timeslot_nid, $limit_by);
// Wish there was a cleaner way to do all this over endless looping.
while ($timeslottheme = db_fetch_object($result)) {
$channel_tid = $timeslottheme->tid;
$channel_number = om_playback_servers_get_channel_number_from_tid($channel_tid);
//Update airings for our time range.
om_playback_servers_update_schedule($channel_number, strtotime($timeslottheme -> field_om_timeslot_date_value), strtotime($timeslottheme -> field_om_timeslot_date_value2));
// call purge
om_timeslot_purge_temp_airings($start, $end, $channel_tid);
$current_timeslot ++;
$channel = om_timeslot_scheduler_get_channel_name($timeslottheme->tid);
// Anticipate Series: Essentialy operate as if pairings is turend off for this timeslot.
// $is_series_nid just prevents repetion since series timeslot nids seem to all be the same;
if ($timeslottheme->field_om_timeslot_theme_nid == NULL) {
// get the gaps
$gaps = om_timeslot_scheduler_find_timeslot_event_openings($timeslot_nid, strtotime($timeslottheme -> field_om_timeslot_date_value), strtotime($timeslottheme -> field_om_timeslot_date_value2), $channel_tid);
//loop through gaps
for ($i = 0; $i < count($gaps); $i++) {
if ($gaps[$i]['start'] >= strtotime($timeslottheme -> field_om_timeslot_date_value + 'UTC')) {
if ($gaps[$i]['start']+$node->field_om_show_duration[0]['value'] <= $gaps[$i]['end']) {
$openings_status = 'Success';
if ($gaps[$i]['first_airing'] == 1) {
$scheduling_links = $scheduling_links . l('Schedule series to start ' . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']) . '/' . $channel_tid) . '<br />';
}
else {
$scheduling_links = $scheduling_links . l('Schedule series to start ' . $lead['title'] . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']) . '/' . $channel_tid) . ' * <br />';
}
}
else {
// Too Long
$openings_status = 'Too Long';
}
}
else{
// no gaps
$openings_status = 'No Gaps';
}
} // end for
$is_series_nid = $timeslot_nid;
}
$lead_nid = $timeslottheme->field_om_timeslot_theme_nid;
$pairing_nid = db_result(db_query("SELECT pairing_nid FROM {om_timeslot_pairings} WHERE theme_nid = %d", $lead_nid));
// build theme pair node object
$theme_pair -> field_om_timeslot_lead_last_value = $timeslottheme -> field_om_timeslot_lead_last_value;
$theme_pair -> pairing_nid = $pairing_nid;
$theme_pair -> theme_nid = $lead_nid;
// build timeslot event object from above query.
$te_node -> field_om_timeslot_date[0]['value'] = $timeslottheme -> field_om_timeslot_date_value;
$te_node -> field_om_timeslot_date[0]['value2'] = $timeslottheme -> field_om_timeslot_date_value2;
// pass both nodes to om_timeslot_pairings_calculate_pair_times and get pairing times
$pairing_times = om_timeslot_pairings_calculate_pair_times($te_node, $theme_pair);
// get pairing ends from returned times
$lead['start'] = $pairing_times['leadstart'];
$lead['end'] = $pairing_times['pairstart']; // leaves no gap between lead and pairing is this correct?
$lead['nid'] = $pairing_times['leadnid'];
$lead['title'] = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $lead['nid']));
// paring end time = end of time slot event. I think this should work. Don't forget UTC conversion!
$timeslot_end = strtotime($timeslottheme -> field_om_timeslot_date_value2.' UTC');
$pairing['start'] = $pairing_times['pairstart'];
$pairing['end'] = $timeslot_end; //presumes pairing goes all the way to the end of the timeslot.
$pairing['nid'] = $pairing_times['pairnid'];
$pairing['title'] = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $pairing['nid']));
// determine if show has theme lead or pairing
$show_theme = $node->field_om_theme[0]['nid'];
$gaps = om_timeslot_scheduler_find_timeslot_event_openings($timeslot_nid, strtotime($timeslottheme -> field_om_timeslot_date_value), strtotime($timeslottheme -> field_om_timeslot_date_value2), $channel_tid);
if ($show_theme == $lead['nid'] && $lead['nid'] !== NULL) {
// show has the lead theme in this timeslot
//loop through gaps
for ($i = 0; $i<count($gaps); $i++) {
if ($gaps[$i]['start'] >= $lead['start']) {
if ($gaps[$i]['start']+$node->field_om_show_duration[0]['value'] <= $gaps[$i]['end'] && $gaps[$i]['start']+$node->field_om_show_duration[0]['value'] <= $lead['end']) {
$openings_status = 'Success';
if ($gaps[$i]['first_airing'] == 1) {
$scheduling_links = $scheduling_links . l('Schedule to start in lead theme ' . $lead['title'] . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']) . '/' . $channel_tid) . '<br />';
}
else {
$scheduling_links = $scheduling_links . l('Schedule to start in lead theme ' . $lead['title'] . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']). '/' . $channel_tid) . ' * <br />';
}
}
else{
// too long
$openings_status = 'Too Long';
}
}
else{
// no gaps
$openings_status = 'No Gaps';
}
} // end for
}// end if lead
if ($show_theme == $pairing['nid'] && $pairing['nid'] != NULL) {
// show has the pairing theme in this timeslot
//loop through gaps
for ($i = 0; $i<count($gaps); $i++) {
if ($gaps[$i]['start'] <= $pairing['start'] && $gaps[$i]['end'] > $pairing['start']) {
// Create a revised start value based on paired theme start (lead duration + lead start)
$revised_start = ($lead['end']-$lead['start']) + $lead['start'];
$gaps[$i]['start'] = $revised_start;
}
if ($gaps[$i]['start'] >= $pairing['start']) {
if ($gaps[$i]['start']+$node->field_om_show_duration[0]['value'] <= $gaps[$i]['end'] && $gaps[$i]['start']+$node->field_om_show_duration[0]['value'] <= $pairing['end']) {
$openings_status = 'Success';
if ($gaps[$i]['first_airing'] == 1) {
$scheduling_links = $scheduling_links . l('Schedule to start in paired theme ' . $pairing['title'] . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']). '/' . $channel_tid) . '<br />';
}
else {
$scheduling_links = $scheduling_links . l('Schedule to start in paired theme ' . $pairing['title'] . date(" M j \a\\t g:i a", $gaps[$i]['start']) . ' on ' . $channel, 'om-timeslot-scheduler/self_schedule_airing/' . $node->nid . '/' . $gaps[$i]['start'] . '/' . ( $gaps[$i]['start']+$node->field_om_show_duration[0]['value']). '/' . $channel_tid) . ' * <br />';
}
}
else {
// Too Long
$openings_status = 'Too Long';
}
}
else {
// No Gaps
$openings_status = 'No Gaps';
}
} // end for
}// end if pairing
// Write error if any, if we are on the last iteration.
if($current_timeslot == $limit_by){
switch($openings_status){
case 'Success':
$output .= $scheduling_links;
$output .= '*first airing in timeslot';
break;
case "Too Long":
if($scheduling_links){
$output .= $scheduling_links;
$output .= '*first airing in timeslot';
}
else {
$output .= '<p>The show you are trying to schedule is larger than any available gaps in the schedule Try another timeslot.</p>';
}
break;
case "No Gaps":
$output .= '<p>There are currently no gaps in this times lot. Try again later or pick another times lot.</p>';
break;
}
}
} // end while timeslottheme
// if there are fewer timeslots than limit send error
if($current_timeslot < $limit_by) {
$output .= '<p>Your show seems to have very few timeslots left. Please contact station personel to get more added.</p>';