-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib_model.php
1489 lines (1236 loc) · 53.2 KB
/
lib_model.php
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
require_once 'global_config.inc';
require_once 'lib_various.php';
require_once 'lib_template.php';
require_once 'lib_error.php';
require_once $auth_lib;
require_once __DIR__.'/lib_recording_session.php';
if ($cam_enabled)
require_once $cam_lib;
if ($slide_enabled)
require_once $slide_lib;
if ($sound_backup_enabled)
require_once $sound_backup_lib;
if ($cam_management_enabled)
require_once $cam_management_lib;
/**
* We are called by a browser with no action, but sessions is alive and login has succeeded, so go to the according screen
* @global <type> $status
* @global <type> $already_recording
*/
function reconnect_active_session() {
global $status;
global $already_recording;
global $redraw;
global $logger;
log_append("Reconnect active session");
$status = status_get();
//lets check what the 'real' state we're in
$already_recording = ($status == 'recording' || $status == 'paused');
try {
$user_id = RecordingSession::instance()->get_current_user();
} catch (Exception $e) {
$user_id = "<no_connected_user>";
}
$logger->log(EventType::RECORDER_LOGIN, LogLevel::DEBUG, 'User '.$user_id." reconnected active session. Current recorder status is $status", array(__FUNCTION__));
if ($already_recording || $status == 'open') {
//state is one of the recording mode
$redraw = true;
view_record_screen();
} else if ($status == 'stopped') {
//stopped means we have already clicked on stop
$ok = controller_view_record_submit();
if(!$ok) //something was wrong with the submit form
recording_force_quit();
} else
controller_view_record_form(); //none of the above cases to this is a first form screen
}
/**
* Handles recording_form values and open recorder_view
* @global type $input
* @global <type> $classroom
*/
function controller_recording_submit_infos() {
global $input;
global $classroom;
global $auth_module;
global $dir_date_format;
global $logger;
global $already_recording;
$user_id = RecordingSession::instance()->get_current_user();
if($already_recording == false)
{
// Sanity checks
if (!isset($input['title']) || empty($input['title'])) {
error_print_message(template_get_message('title_not_defined', get_lang()), false);
return false;
}
if (!isset($input['record_type']) || empty($input['record_type'])) {
error_print_message(template_get_message('type_not_defined', get_lang()), false);
return false;
}
$valid_record_type = validate_allowed_record_type($input['record_type'], get_allowed_record_type());
if($valid_record_type == false) {
$logger->log(EventType::RECORDER_USER_SUBMIT_INFO, LogLevel::CRITICAL, "Invalid record type given (".$input['record_type']."), cannot continue", array(__FUNCTION__));
error_print_message(template_get_message('type_not_valid', get_lang()), false);
return false;
}
$streaming = (isset($input['streaming']) && $input['streaming'] == 'enabled') ? 'true' : 'false';
$user_id = RecordingSession::instance()->get_current_user();
// authorization check
$fct_user_has_course = "auth_" . $auth_module . "_user_has_course";
if (!$fct_user_has_course($user_id, $input['course'])) {
error_print_message('You do not have permission to access course ' . $input['course'], false);
$msg = 'submit_record_infos: ' .$user_id . ' tried to access course ' . $input['course'] . ' without permission';
log_append('warning', $msg);
$logger->log(EventType::RECORDER_USER_SUBMIT_INFO, LogLevel::WARNING, $msg, array(__FUNCTION__));
return false;
}
$datetime = date($dir_date_format);
$course_id = $input['course'];
$title = trim($input['title']);
$description = $input['description'];
$user_extended_info = UserExtendedInfo::get($user_id);
$full_name = $user_extended_info !== null ? $user_extended_info->full_name : '';
// Now we create and store the metadata
$record_meta_assoc = array(
'course_name' => $course_id,
'origin' => $classroom,
'title' => $title,
'description' => $description,
'record_type' => $valid_record_type,
'moderation' => 'true',
'author' => $full_name,
'netid' => RecordingSession::instance()->get_current_user(),
'record_date' => $datetime,
'streaming' => $streaming,
'super_highres' => false
);
$res = RecordingSession::instance()->metadata_save($record_meta_assoc);
if ($res == false) {
error_print_message('submit_record_infos: something went wrong while saving metadata');
return false;
}
log_append("submit info from recording form");
$asset_name = get_asset_name($record_meta_assoc['course_name'], $record_meta_assoc['record_date']);
RecordingSession::instance()->init_record($asset_name, $course_id, $valid_record_type);
insert_form_data($user_id, $course_id, $title, $description, $valid_record_type);
} else {
$logger->log(EventType::RECORDER_USER_SUBMIT_INFO, LogLevel::WARNING, "User $user_id tried to re submit infos while we were already recording, ignoring submitted infos...", array(__FUNCTION__));
}
// And finally we can display the main screen! This will init the recorders (blocking call)
view_init_record_screen();
return true;
}
function insert_form_data($author, $course, $title, $decription, $record_type)
{
global $database;
$database->form_data_insert($author, $course, $title, $decription, $record_type);
}
/** Update record_type in metadata file according to allowed cam/slide
* You can for example use this to disable camera in metadata if camera failed
* Return false if type was changed
*/
function update_metadata_with_allowed_types($meta_assoc, $allow_cam, $allow_slide, &$new_record_type = null) {
$record_type = $meta_assoc['record_type'];
$allowed = RecordType::to_int_for_options($allow_cam, $allow_slide);
$new_record_type = validate_allowed_record_type($record_type, $allowed);
if($record_type != $new_record_type) {
//write new type
$meta_assoc['record_type'] = $new_record_type;
return false;
}
//else nothing to do, type is ok
return true;
}
/**
* Same as update_metadata_with_allowed_types but will open file and write to it if needed
* Return true on success
* @param $new_record_type new record type after filtering
* @param $meta_assoc metadata array that was read from file
*/
function update_metadata_file_with_allowed_types($metadata_file, $allow_cam, $allow_slide, &$new_record_type = null, &$meta_assoc = null) {
global $logger;
$meta_assoc = xml_file2assoc_array($metadata_file);
if($meta_assoc == false) {
$logger->log(EventType::TEST, LogLevel::ERROR, "Could not get session metadata file: $metadata_file", array(__FUNCTION__));
return false;
}
update_metadata_with_allowed_types($meta_assoc, $allow_cam, $allow_slide, $new_record_type);
//write back to file if needed
if($new_record_type != $meta_assoc['record_type']) {
$ok = xml_assoc_array2file($meta_assoc, $metadata_file);
if(!$ok) {
$logger->log(EventType::TEST, LogLevel::ERROR, "Could not write new record type to file ($metadata_file).", array(__FUNCTION__));
return false;
}
}
return true;
}
/**
* Recording is ready, start the actual recording
*/
function controller_recording_start() {
global $logger;
global $dir_date_format;
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
global $session_module;
global $classroom;
RecordingSession::instance()->start_record();
$asset = RecordingSession::instance()->get_current_asset();
$user = RecordingSession::instance()->get_current_user();
//get current status and check if its compatible with current action
$status = status_get();
if ($status != 'open') {
error_print_message("capture_start: error status ($status): status not 'open'");
$logger->log(EventType::RECORDER_START, LogLevel::ERROR, "(action recording_start) Could not start recording because of status '$status'", array(__FUNCTION__), $asset);
return false;
}
$asset_dir = get_asset_dir($asset, "local_processing");
if (!file_exists($asset_dir)) {
$logger->log(EventType::RECORDER_START, LogLevel::ERROR, "Asset dir $asset_dir not found. Invalid asset? Trying to restore asset from session module.", array(__FUNCTION__), $asset);
$asset = RecordingSession::instance()->get_current_asset();
$asset_dir = get_asset_dir($asset, "local_processing");
if (!file_exists($asset_dir)) {
$logger->log(EventType::RECORDER_START, LogLevel::ERROR, "Could not start recording because asset dir does not exists: $asset_dir", array(__FUNCTION__), $asset);
return false;
}
}
$metadata_file = $asset_dir . '/_metadata.xml';
$meta_assoc = xml_file2assoc_array($metadata_file);
if($meta_assoc == false) {
$logger->log(EventType::TEST, LogLevel::ERROR, "Could not get session metadata file: $metadata_file", array(__FUNCTION__));
return false;
}
//we always start every module enabled, so that we have some backup files in case of problems (doesn't depend on the
// recording format chose by user - cam, slide, camslide)
if ($cam_enabled) {
$fct_capture_start = 'capture_' . $cam_module . '_start';
// ideally, capture_start should return the pid
// $res_cam = $fct_capture_start($cam_pid);
$res_cam = $fct_capture_start($asset);
}
if ($slide_enabled) {
$fct_capture_start = 'capture_' . $slide_module . '_start';
// ideally, capture_start should return the pid
// $res_slide = $fct_capture_start($slide_pid);
$res_slide = $fct_capture_start($asset);
}
$requested_record_type = $meta_assoc['record_type'];
// something went wrong while starting the recording
if (($cam_enabled && !$res_cam) || ($slide_enabled && !$res_slide)) {
$logger->log(EventType::RECORDER_START, LogLevel::ERROR, "Error in record start in capture module (cam_enabled:$cam_enabled,res_cam:$res_cam/slide_enabled:$slide_enabled,res_slide:$res_slide)", array(__FUNCTION__), $asset);
//At this point, if user requested both cam & slide, we continue anyway if at least one started
$new_type = '';
update_metadata_file_with_allowed_types($metadata_file, $cam_enabled && $res_cam, $slide_enabled && $res_slide, $new_type, $meta_assoc);
if($new_type == '') {
$logger->log(EventType::RECORDER_START, LogLevel::CRITICAL, "All requested modules failed to start (Requested type: $requested_record_type). Cannot continue.", array(__FUNCTION__), $asset);
return false; // nothing we can do
} else if($new_type != $meta_assoc['record_type']) {
$logger->log(EventType::RECORDER_START, LogLevel::CRITICAL, "One requested module failed to start. Continuing with what's left. (Requested type: $requested_record_type, effective type: $new_type)", array(__FUNCTION__), $asset);
}
}
log_append("recording_start", "started recording by user request");
$logger->log(EventType::ASSET_CREATED, LogLevel::NOTICE,
"Started recording at user $user request. Requested type: $requested_record_type", array(__FUNCTION__), $asset,
$user, $requested_record_type, RecordingSession::instance()->get_course_id(), $classroom);
echo "OK";
return true;
}
function destroy_session() {
// releases the recording session
//re open session to destroy it if needed
if(session_status() == PHP_SESSION_NONE)
session_start();
// And finally, closing the user's session
session_destroy();
}
function try_stopping()
{
global $logger;
global $input;
global $recorder_monitoring_pid;
$asset = RecordingSession::instance()->get_current_asset();
if(!RecordingSession::instance()->get_current_asset() || $asset == "") {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::ERROR, "controller_stop_and_publish called without asset in session", array(__FUNCTION__));
return false;
}
// stops the timeout monitoring
if (file_exists($recorder_monitoring_pid))
unlink($recorder_monitoring_pid);
$moderation = 'false';
if (isset($input['moderation']) && $input['moderation'] == 'true')
$moderation = 'true';
// Logging the operation
$rec_start_time = RecordingSession::instance()->get_rec_start_time();
$course = RecordingSession::instance()->get_course_id();
log_append('recording_stop', 'Stopped recording by user request (course ' . $course . ', started on ' . $rec_start_time . ', moderation: ' . $moderation . ')');
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::NOTICE, 'Publishing recording at user request (course ' . $course . ', started on ' . $rec_start_time . ', moderation: ' . $moderation . ').', array(__FUNCTION__), $asset);
//get the start time and course from metadata
$meta_assoc = RecordingSession::instance()->metadata_get();
if($meta_assoc == false) {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::CRITICAL, "Could not get metadata from session for publishing, stopping now", array(__FUNCTION__), $asset);
destroy_session();
return false;
}
$asset_dir = get_asset_dir($asset, 'local_processing');
if(!file_exists($asset_dir)) {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::CRITICAL, "Trying to publish unknown asset $asset from dir $asset_dir", array(__FUNCTION__), $asset);
destroy_session();
return false;
}
if($moderation != 'true' && $moderation != 'false') {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::ERROR, "Invalid moderation type given: '$moderation'. Defaulting to true.", array(__FUNCTION__), $asset);
$moderation = true;
}
//update session metadata with moderation
$meta_assoc['moderation'] = $moderation;
$meta_xml_string = RecordingSession::instance()->metadata_save($meta_assoc);
if($meta_xml_string == false) {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::CRITICAL, "Could not write metadata to session.", array(__FUNCTION__), $asset);
destroy_session();
return false;
}
//also update metadata file in asset dir
file_put_contents("$asset_dir/metadata.xml", $meta_xml_string);
// launches the video processing in background
start_post_process($asset);
destroy_session();
return true;
}
/**
* Stops the recording and processes it
*/
function controller_stop_and_publish() {
global $logger;
$ok = try_stopping();
// releases the recording session. Someone else can now record
RecordingSession::unlock();
destroy_session();
if(!$ok) {
$logger->log(EventType::RECORDER_PUBLISH, LogLevel::ERROR, "Something went wrong while publishing record, reseting recorder state to avoid locking users in publish menu", array(__FUNCTION__));
status_set('');
error_print_message("Failure");
return;
}
// Displaying a confirmation message
require_once template_getpath('div_record_submitted.php');
}
/**
* Cancel the recording after the record form has been submitted or at the
* end of the recording
* @global type $recstarttime_file
* @global type $cam_enabled
* @global type $cam_module
* @global type $slide_enabled
* @global type $slide_module
* @global type $visca_enabled
* @return boolean
*/
function controller_recording_cancel() {
global $logger;
global $recorder_monitoring_pid;
$asset = null;
$metadata = RecordingSession::instance()->metadata_get();
if($metadata) {
$asset = get_asset_name($metadata['course_name'], $metadata['record_date']);
$logger->log(EventType::ASSET_CANCELED, LogLevel::NOTICE, "Record cancelled at user request", array(__FUNCTION__), $asset);
} else {
$logger->log(EventType::ASSET_CANCELED, LogLevel::NOTICE, "Cancelling current record, but we could not get asset name. It may be that there is no current recording.", array(__FUNCTION__));
}
// stops the timeout monitoring
if (file_exists($recorder_monitoring_pid))
unlink($recorder_monitoring_pid);
// Logging the operation
$rec_start_time = RecordingSession::instance()->get_rec_start_time();
$course = RecordingSession::instance()->get_course_id();
log_append('recording_cancel', 'Deleted recording by user request (course ' . $course . ', started on ' . $rec_start_time . ')');
// Stopping and releasing the recorder
$result = cancel_current_record($asset, true);
// something wrong happened while cancelling the recording
if (!$result) {
$logger->log(EventType::ASSET_CANCELED, LogLevel::ERROR, "Something wrong happened while cancelling record. Destroying session and status anway." . error_last_message(), array(__FUNCTION__));
error_print_message(error_last_message());
}
// releases the recording session. Someone else can now record
RecordingSession::unlock();
status_set('');
// Displaying a confirmation message
require_once template_getpath('div_record_cancelled.php');
}
function cancel_current_record($asset, $reset_cam_position = true) {
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
global $session_module;
$res_cam = true;
$res_slide = true;
// if cam module is enabled
if ($cam_enabled) {
$fct_capture_cancel = 'capture_' . $cam_module . '_cancel';
$res_cam = $fct_capture_cancel($asset);
}
// if slide module is enabled
if ($slide_enabled) {
$fct_capture_cancel = 'capture_' . $slide_module . '_cancel';
$res_slide = $fct_capture_cancel($asset);
}
if($reset_cam_position) {
reset_cam_position();
}
//moving asset to trash. Don't forget to clean-up trash folder regularly!
move_asset($asset, 'trash',true);
return $res_cam && $res_slide;
}
/* Start post process for asset.
* Asset must be in local processing directory
* */
function start_post_process($asset) {
global $php_cli_cmd;
global $cli_post_process;
global $logger;
if(!$asset) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "No asset given " . PHP_EOL . print_r(debug_backtrace(), true), array(__FUNCTION__), $asset);
return false;
}
$asset_dir = get_local_processing_dir($asset);
if(!file_exists($asset_dir)) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "Asset directory does not exists: $asset_dir", array(__FUNCTION__), $asset);
return false;
}
$return_val = 0;
system("$php_cli_cmd $cli_post_process $asset > $asset_dir/post_process.log 2>&1 &", $return_val);
if($return_val != 0) {
$logger->log(EventType::RECORDER_STOP, LogLevel::CRITICAL, "$cli_post_process returned error $return_val", array(__FUNCTION__), $asset);
return false;
}
return true;
}
function stop_current_record($start_post_process = true) {
global $logger;
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
$logger->log(EventType::RECORDER_STOP, LogLevel::DEBUG, "stop_current_record called with post process: $start_post_process.", array(__FUNCTION__));
if(!RecordingSession::is_locked()) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "stop_current_record was called recorder was not currently locked", array(__FUNCTION__));
return false;
}
$asset = RecordingSession::instance()->get_current_asset();
if(!$asset || $asset == "") {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "No asset found in session", array(__FUNCTION__));
return false;
}
$asset_dir = get_asset_dir($asset, 'local_processing');
if(!$asset_dir) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "Could not find asset dir for asset $asset", array(__FUNCTION__), $asset);
return false;
}
//write almost final metadata here a first time in asset folder. Note that it may still be overwritten when choosing a moderation type later
{
$ok = copy($asset_dir . "/_metadata.xml", $asset_dir . "/metadata.xml");
if(!$ok) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, "Could not create pre-final metadata file. Processing will probably fail later on", array(__FUNCTION__), $asset);
}
}
// Stopping the recording
$slide_pid = 0;
$cam_pid = 0;
// if slide module is enabled
if ($slide_enabled) {
$fct_capture_stop = 'capture_' . $slide_module . '_stop';
$success = $fct_capture_stop($slide_pid, $asset);
if (!$success) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, 'Cam module stopping failed. Trying to continue anyway.', array(__FUNCTION__), $asset);
}
}
// if cam module is enabled
if ($cam_enabled) {
$fct_capture_stop = 'capture_' . $cam_module . '_stop';
$success = $fct_capture_stop($cam_pid, $asset);
if (!$success) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, 'Cam module stopping failed. Trying to continue anyway.', array(__FUNCTION__), $asset);
}
}
// waits until both processes are finished to continue.
while (is_process_running($cam_pid) || is_process_running($slide_pid))
sleep(0.5);
//video stopping is done
if($start_post_process) {
$ok = start_post_process($asset);
if(!$ok) {
$logger->log(EventType::RECORDER_STOP, LogLevel::ERROR, 'Start post processing failed', array(__FUNCTION__), $asset);
return false;
}
}
return true;
}
function recording_force_quit()
{
global $logger;
global $recorder_monitoring_pid;
// stops the timeout monitoring
if (file_exists($recorder_monitoring_pid))
unlink($recorder_monitoring_pid);
$asset = RecordingSession::instance()->get_current_asset();
$logger->log(EventType::ASSET_CANCELED, LogLevel::NOTICE, "Record was forcefully cancelled", array('controller_recording_force_quit'), $asset);
$logger->log(EventType::RECORDER_FORCE_QUIT, LogLevel::NOTICE, "Record was forcefully cancelled", array('controller_recording_force_quit'), $asset);
$status = status_get();
if ($status == '' || $status == 'open') {
$result = cancel_current_record($asset, false);
if(!$result) {
$logger->log(EventType::RECORDER_FORCE_QUIT, LogLevel::ERROR, "Previous record cancelling returned an error. Trying to continue anyway.", array('controller_recording_force_quit'), $asset);
}
} else { // a recording is pending (or stopped)
$result = stop_current_record(true);
if(!$result) {
$logger->log(EventType::RECORDER_FORCE_QUIT, LogLevel::ERROR, "Previous record stopping returned an error. Trying to continue anyway.", array('controller_recording_force_quit'), $asset);
}
}
// reinits the recording status
status_set('');
// releases the recording session. Someone else can now record
RecordingSession::unlock();
}
/**
* Interrupts current recording
* (example: this is called when someone tries to log in, but someone else was already recording)
*/
function controller_recording_force_quit()
{
global $notice;
global $logger;
if(RecordingSession::is_locked() === false)
return false; //nothing to do
$old_asset = RecordingSession::instance()->get_current_asset();
//$old_user_id = RecordingSession::instance()->get_current_user();
recording_force_quit();
template_load_dictionnary('translations.xml');
$notice = template_get_message('ongoing_record_interrupted_message', get_lang()); // Message to display on top of the page, warning the user that they just stopped someone else's record*/
if(!isset($_SESSION['user_login']) || $_SESSION['user_login'] == "") {
echo "No user given";
return false;
}
$new_user = $_SESSION['user_login'];
try {
RecordingSession::lock($new_user);
} catch (Exception $e) {
error_print_message('lib_model: recording_force_quit: Could not lock recorder: ' . $e->getMessage());
$logger->log(EventType::RECORDER_FORCE_QUIT, LogLevel::ERROR, "Could not lock recorder: " . $e->getMessage(), array('controller_recording_force_quit'), $old_asset);
return false;
}
//make sure the user is considered as logged
$_SESSION['recorder_logged'] = true;
// 4) And finally, we can display the record form
controller_view_record_form();
return true;
}
function recording_pause() {
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
$asset = RecordingSession::instance()->get_current_asset();
$res_cam = true;
$res_slide = true;
// if cam module is enabled
if ($cam_enabled) {
$fct_capture_pause = 'capture_' . $cam_module . '_pause';
$res_cam = $fct_capture_pause($asset);
}
// if slide module is enabled
if ($slide_enabled) {
$fct_capture_pause = 'capture_' . $slide_module . '_pause';
$res_slide = $fct_capture_pause($asset);
}
return $res_cam && $res_slide;
}
/*
* Pauses the current recording
*/
function controller_recording_pause() {
global $logger;
$result = recording_pause();
if(!$result) {
$logger->log(EventType::RECORDER_PAUSE_RESUME, LogLevel::ERROR, "Pause failed: " . error_last_message(), array('controller_recording_pause'));
error_print_message(error_last_message());
die;
}
$logger->log(EventType::RECORDER_PAUSE_RESUME, LogLevel::INFO, "Pause recording at user request", array('controller_recording_pause'));
log_append("paused recording by request");
echo '';
}
function recording_resume() {
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
$asset = RecordingSession::instance()->get_current_asset();
$res_cam = true;
$res_slide = true;
// if cam module is enabled
if ($cam_enabled) {
$fct_capture_resume = 'capture_' . $cam_module . '_resume';
$res_cam = $fct_capture_resume($asset);
}
// if slide module is enabled
if ($slide_enabled) {
$fct_capture_resume = 'capture_' . $slide_module . '_resume';
$res_slide = $fct_capture_resume($asset);
}
return $res_cam && $res_slide;
}
/*
* Resumes the current recording
*/
function controller_recording_resume() {
global $logger;
$result = recording_resume();
if(!$result) {
$logger->log(EventType::RECORDER_PAUSE_RESUME, LogLevel::ERROR, "Resuming failed. " . error_last_message(), array('controller_view_record_form'), $asset);
error_print_message(error_last_message());
die;
}
log_append("resumed recording by request");
$logger->log(EventType::RECORDER_PAUSE_RESUME, LogLevel::INFO, "Resumed recording at user request", array('controller_recording_pause'));
echo '';
}
/**
* Moves the camera to the position given as a POST parameter (position name)
* @global type $input
*/
function controller_camera_move() {
global $input;
global $cam_management_module;
if (!isset($input['position'])) {
error_print_message('Asked to move camera but no position given');
die;
}
$scene = $input["position"];
$fct_cam_move = "cam_" . $cam_management_module . "_move";
$fct_cam_move($scene);
log_append("camera moved to position : $scene");
}
//
// Functions calling the view
//
/**
* Displays the login form
*/
function controller_view_login_form() {
global $url;
session_destroy();
require_once template_getpath('login.php');
die;
}
function get_last_session_data($author)
{
global $database;
return $database->form_data_get_data_for_day_of_week($author, time());
}
/**
* Displays the form people get when they log in (i.e. asking for a title, description, ...)
*/
function controller_view_record_form()
{
global $input;
global $cam_enabled;
global $cam_module;
global $slide_enabled;
global $slide_module;
global $auth_module;
global $streaming_available;
global $recorder_monitoring_pid;
global $logger;
//$logger->log(EventType::TEST, LogLevel::DEBUG, "controller_view_record_form called. Backtrace:\n" . print_r(debug_backtrace(), true), array('controller_view_record_form'));
// stops the timeout monitoring
if (file_exists($recorder_monitoring_pid))
unlink($recorder_monitoring_pid);
$asset = RecordingSession::instance()->get_current_asset();
if ($asset !== false && isset($input['reset_player']) && $input['reset_player'] == 'true') {
$logger->log(EventType::RECORDER_CANCEL, LogLevel::NOTICE, "Input has 'reset_player' argument, cancelling record", array(__FUNCTION__), $asset);
$result = cancel_current_record($asset, true);
if(!$result) {
$logger->log(EventType::RECORDER_CANCEL, LogLevel::ERROR, "Something wrong happened while cancelling current record. Trying to continue anyway.", array(__FUNCTION__), $asset);
}
status_set('');
}
// if cam module is enabled
if ($cam_enabled) {
$fct_capture_features_get = 'capture_' . $cam_module . '_features_get';
$cam_features = $fct_capture_features_get();
}
// if slide module is enabled
if ($slide_enabled) {
$fct_capture_features_get = 'capture_' . $slide_module . '_features_get';
$slide_features = $fct_capture_features_get();
}
$cam_stream_ok = !$cam_enabled || in_array('streaming', $cam_features);
$slide_stream_ok = !$slide_enabled || in_array('streaming', $slide_features);
if ($cam_stream_ok && $slide_stream_ok) {
$streaming_available = true;
}
$current_user = RecordingSession::instance()->get_current_user();
$last_session_data = get_last_session_data($current_user);
//form data
$prefill_course = "";
$prefill_title = "";
$prefill_description = "";
$prefill_type = "slide";
if($last_session_data) {
//Pre fill with previous data
$prefill_course = $last_session_data->course;
$prefill_title = $last_session_data->title;
$prefill_description = $last_session_data->description;
$prefill_type = $last_session_data->record_type; //Improvement: what if the type is not supported anymore on this recorder?
} else {
//set a default record type
if ($cam_enabled && $slide_enabled)
$prefill_type = 'camslide';
elseif ($cam_enabled)
$prefill_type = 'cam';
else if($slide_enabled)
$prefill_type = 'slide';
}
// Retrieving the course list (to display in the web interface)
$fct_user_courselist_get = "auth_" . $auth_module . "_user_courselist_get";
$courselist = $fct_user_courselist_get($current_user);
global $notice; // Possible errors that occurred at previous steps.
require_once template_getpath('record_form.php');
}
//
// Helper functions
//
/**
* Helper function
* @return bool true if the user is already logged in; false otherwise
*/
function user_logged_in() {
global $logger;
if(!isset($_SESSION['recorder_logged'])) {
return false;
}
//Session should always be locked when a user is logged in
if(!RecordingSession::is_locked()) {
$logger->log(EventType::RECORDER_LOGIN, LogLevel::DEBUG, "User is logged in but session is not locked", array(__FUNCTION__));
return false;
}
if (!RecordingSession::can_access_lock($_SESSION['user_login'])) {
$logger->log(EventType::RECORDER_LOGIN, LogLevel::WARNING, "User is logged in but session is not locked by this user", array(__FUNCTION__));
return false;
}
return true;
}
/**
* Logs a user in and send him a new form depending on the result
* On success, record form is showed.
* On failure, send login form again.
*
* Return wheter the user successfully logged
*/
function user_login($login, $passwd) {
global $logger;
global $input;
global $template_folder;
global $notice;
global $redraw;
global $already_recording;
global $status;
global $session_module;
global $auth_module;
global $session;
global $session_class;
// 0) Sanity checks
if (empty($login) || empty($passwd)) {
$error = template_get_message('Empty_username_password', get_lang());
//show login form again
require_once template_getpath('login.php');
$logger->log(EventType::RECORDER_LOGIN, LogLevel::INFO, 'Login failed, no login/password provided', array(__FUNCTION__));
return false;
}
// 1) We check the user's identity and retrieve their personal information
$fct_auth_check = "auth_" . $auth_module . "_check";
$res = $fct_auth_check($login, $passwd);
if (!$res) {
$fct_auth_last_error = "auth_" . $auth_module . "_last_error";
$error = $fct_auth_last_error();
require_once template_getpath('login.php');
$logger->log(EventType::RECORDER_LOGIN, LogLevel::INFO, "Login failed, wrong credentials for login: $login", array(__FUNCTION__));
return false;
}
$user = $res['user_login'];
$_SESSION['user_login'] = $user;
// 3) Now we have to check whether or not there is still a recording ongoing.
// If the user who started the recording is the one trying to log in again,
// then we display the recording screen again. If not, then we stop the current recording
// and display the record_form.
if (RecordingSession::is_locked()) {
//change language if specified an init rempository path
if(isset($input['lang']))
set_lang($input['lang']);
template_repository_path($template_folder . get_lang());
if (RecordingSession::instance()->can_access_lock($user)) {
// We retrieve the recorder page
log_append('reconnecting', $user . ' trying to log in but was already using recorder. Retrieving lost session.');
$logger->log(EventType::RECORDER_LOGIN, LogLevel::INFO, $user . ' trying to log in but was already using recorder. Retrieving lost session.', array(__FUNCTION__));
$_SESSION['recorder_logged'] = true; // "Boolean" telling that we're logged in
$redraw = true;
$status = status_get();
$already_recording = ($status == 'recording' || $status == 'paused');
if ($status == 'recording' || $status == 'paused' || $status == 'open')
view_record_screen(); //go directly to record screen
else if ($status == 'stopped')
controller_stop_and_view_record_submit();
else
controller_view_record_form(); //ask metadata again
return true;
} else {
$logger->log(EventType::RECORDER_LOGIN, LogLevel::WARNING, "User " . $user . " tried to login but session was locked, asking him if he wants to interrupt the current record", array(__FUNCTION__));
// We ask the user if they want to stop the current recording and save it.
//
// Various information we want to display
$current_user = RecordingSession::instance()->get_current_user();
$start_time_ts = RecordingSession::instance()->get_init_time();
$start_time = $start_time_ts != 0 ? date('Y-m-d H:i:s', $start_time_ts) : "NA";
$course_id = RecordingSession::instance()->get_course_id();
$course = $course_id != "" ? $course_id : "NA";
require_once template_getpath('div_error_recorder_in_use.php');
return false;
}
}
try {
$admin_id = $res['real_login'];
RecordingSession::lock($user, $admin_id);
$_SESSION['recorder_logged'] = true; // "Boolean" telling that we're logged in
$_SESSION['user_login'] = $user;
UserExtendedInfo::write($res['user_login'], $res['full_name'], $res['email']);
} catch (Exception $e) {
$logger->log(EventType::RECORDER_LOGIN, LogLevel::ERROR, "Could not lock recorder for user $user. Exception message: " . $e->getMessage(), array(__FUNCTION__));
error_print_message('Could not lock recorder: ' . $e->getMessage());
return false;
}
if(isset($input['lang']))
set_lang($input['lang']);
template_repository_path($template_folder . get_lang());
$logger->log(EventType::RECORDER_LOGIN, LogLevel::INFO, "User $login logged in", array(__FUNCTION__));
log_append('login');
// 4) And finally, we can display the record form
controller_view_record_form();
}
function reset_cam_position() {