This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
1143 lines (917 loc) · 54.1 KB
/
database.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package mod_videoannotation
* @copyright 2015 UC Regents
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
error_reporting(E_ALL);
ini_set('display_errors', true);
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/lib.php');
$inputs = array_merge($_GET, $_POST);
if (is_array($inputs)) {
$outputs = array();
for ($commandnum = 1; isset($inputs["c{$commandnum}_command"]); $commandnum++) {
$input = array();
foreach ($inputs as $key => $value) {
if (stripos($key, "c{$commandnum}_") === 0) {
$input[substr($key, strlen("c{$commandnum}_"))] = $value;
}
}
$outputs[] = handle_command($input, $commandnum);
}
echo json_encode($outputs);
} else {
echo json_encode(array("success" => false, "message" => "Data not given"));
}
function get_context_coursemodule($coursemoduleid) {
global $CFG, $DB;
$sql = "SELECT ctx.*
FROM {context} ctx
WHERE ctx.contextlevel = " . CONTEXT_MODULE . "AND ctx.instanceid = " . (int) $coursemoduleid;
return $DB->get_record_sql($sql);
}
function get_context_videoannotation($videoannotationid) {
global $CFG, $DB;
$sql = "SELECT ctx.*
FROM {context} ctx
JOIN {course_modules} cm ON ctx.contextlevel = " . CONTEXT_MODULE . " AND ctx.instanceid = cm.id
JOIN {modules} m ON cm.module = m.id
JOIN {videoannotation} va ON cm.instance = va.id
WHERE m.name = 'videoannotation'
AND va.id = " . (int) $videoannotationid;
return $DB->get_record_sql($sql);
}
function get_coursemodule_clip($clipid) {
global $CFG, $DB;
$sql = "SELECT cm.*
FROM mdl_course_modules cm
JOIN mdl_modules m ON cm.module = m.id and m.name = 'videoannotation'
JOIN mdl_videoannotation v ON cm.instance = v.id
JOIN mdl_videoannotation_clips vc ON v.id = vc.videoannotationid
AND vc.id = " . (int) $clipid;
return $DB->get_record_sql($sql);
}
function get_videoannotation_clip($clipid) {
global $CFG, $DB;
$sql = "SELECT v.*
FROM {videoannotation} v
JOIN {videoannotation_clips} vc ON v.id = vc.videoannotationid
WHERE vc.id = " . (int) $clipid;
return $DB->get_record_sql($sql);
}
function get_videoannotation_event($eventid) {
global $CFG, $DB;
$sql = "SELECT v.*
FROM {videoannotation} v
JOIN {videoannotation_clips} vc ON v.id = vc.videoannotationid
JOIN {videoannotation_tags} vt ON vc.id = vt.clipid
JOIN {videoannotation_events} ve ON vt.id = ve.tagid
WHERE ve.id = " . (int) $eventid;
return $DB->get_record_sql($sql);
}
function get_videoannotation_tag($tagid) {
global $CFG, $DB;
$sql = "SELECT v.*
FROM {videoannotation} v
JOIN {videoannotation_clips} vc ON v.id = vc.videoannotationid
JOIN {videoannotation_tags} vt ON vc.id = vt.clipid
WHERE vt.id = " . (int) $tagid;
return $DB->get_record_sql($sql);
}
function handle_command($input, $commandnum) {
global $CFG, $USER, $db, $DB;
switch ($input['command']) {
case 'test':
return array("success" => true, "test" => "testing");
case 'gettagsevents':
if (!isset($input['clipid'])) {
return array("success" => false, "message" => "clipid must be given.");
}
$videoannotation = get_videoannotation_clip($input['clipid']);
$coursemodule = get_coursemodule_clip($input['clipid']);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$canview = has_capability('mod/videoannotation:view', $modulecontext);
$isadmin = is_siteadmin($USER->id);
// Require mod/videoannotation:manage or mod/videoannotation:view capability.
if (!$canmanage and ! $canview) {
return array(
"success" => false,
"message" => "mod/videoannotation:manage or mod/videoannotation:view capability required."
);
}
// Grant access only in one of these cases:
// 1. Requestor has "manage" capability (instructor, etc.)
// 2. Requestor has "view" capability, is in the given group, and the course has a group mode of SEPARATEGROUPS
// 3. Requestor has "view" capability, is in any of the groups in the course, and the course has a group mode of VISIBLEGROUPS
// Now that we have the course module and the context, let's determine if the requester can access the data he asks for
$onlineusers = array();
switch ($videoannotation->groupmode) {
case NOGROUPS:
// "userid" parameter is required; we don't care about "groupid" parameter
if (!isset($input['userid'])) {
return array("success" => false, "message" => "userid is required.");
}
// If only "view" capability present, the requestor's user ID must be the same as the given user ID
if (!$isadmin and ! $canmanage and $USER->id != $input['userid']) {
return array("success" => false, "message" => "Access denied (cannot access other users' tags).");
}
// Get and return data: all tags and events "owned" by
// the user (given userid and null groupid)
$tagusergroupclause = '(t.userid = ' . (int) $input['userid'] . ' AND t.groupid IS NULL )';
$eventusergroupclause = '(e.userid = ' . (int) $input['userid'] . ' AND e.groupid IS NULL )';
$groupid = null;
$onlineusers = null;
break;
case VIDEOANNOTATION_GROUPMODE_USER_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_USER:
case VIDEOANNOTATION_GROUPMODE_ALL_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_ALL:
// "groupid" parameter is required; we don't care about "userid" parameter
if (!isset($input['groupid'])) {
return array("success" => false, "message" => "groupid is required.");
}
// If only "view" capability present, the requestor's must be in
// * the given group (for separate group), or
// * one of the groups in the course (for visible group)
if (!$isadmin and ! $canmanage) {
if ($videoannotation->groupmode == VIDEOANNOTATION_GROUPMODE_GROUP_USER) {
$groups = groups_get_all_groups($coursemodule->course, null, $coursemodule->groupingid);
} else {
$groups = groups_get_all_groups($coursemodule->course, $USER->id, $coursemodule->groupingid);
}
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_USER_USER, VIDEOANNOTATION_GROUPMODE_GROUP_USER, VIDEOANNOTATION_GROUPMODE_GROUP_GROUP)) and ! isset($groups[$input['groupid']])) {
return array("success" => false, "message" => "Access denied (not in specified group).");
}
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_ALL_USER, VIDEOANNOTATION_GROUPMODE_ALL_GROUP, VIDEOANNOTATION_GROUPMODE_ALL_ALL)) and ! $groups) {
return array("success" => false, "message" => "Access denied (not in any group).");
}
if (isset($groups[$input['groupid']])) {
$lock = $DB->get_record('videoannotation_locks', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => $input['groupid']));
if ($lock) {
$DB->update_record('videoannotation_locks', (object) array('id' => $lock->id, 'timemodified' => time()));
} else {
$DB->insert_record('videoannotation_locks', (object) array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => $input['groupid'], 'timecreated' => time(), 'timemodified' => time()));
}
}
} else {
// SSC-1231: Add user info to videoannotation_locks for admin users too
$lock = $DB->get_record('videoannotation_locks', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => $input['groupid']));
if ($lock) {
$DB->update_record('videoannotation_locks', (object) array('id' => $lock->id, 'timemodified' => time()));
} else {
$DB->insert_record('videoannotation_locks', (object) array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => $input['groupid'], 'timecreated' => time(), 'timemodified' => time()));
}
}
$DB->delete_records('videoannotation_locks', array('timemodified' => (time() - (.25 * 60))));
$sql = "SELECT u.id, u.lastname FROM {user} u
JOIN {videoannotation_locks} vl ON u.id = vl.userid
WHERE vl.videoannotationid = " . $videoannotation->id . "
AND vl.groupid = " . (int) $input['groupid'] . "
AND vl.userid != " . $USER->id;
$onlineuserrecords = $DB->get_records_sql($sql);
$onlineusers = array();
if ($onlineuserrecords) {
foreach ($onlineuserrecords as $onlineuserrecord) {
$onlineusers[] = $onlineuserrecord->lastname;
}
}
// Get and return data
// For "individual" group mode: all tags and events "owned" by the user and the group (the given userid and groupid)
// For other group modes: all tags and events "owned" by the group (null/non-null userid and the given groupid)
if ($videoannotation->groupmode == VIDEOANNOTATION_GROUPMODE_USER_USER) {
$tagusergroupclause = '(t.userid = ' . (int) $USER->id . ' AND t.groupid = ' . (int) $input['groupid'] . ')';
$eventusergroupclause = '(e.userid = ' . (int) $USER->id . ' AND e.groupid = ' . (int) $input['groupid'] . ')';
} else {
$tagusergroupclause = '(t.groupid = ' . (int) $input['groupid'] . ')';
$eventusergroupclause = '(e.groupid = ' . (int) $input['groupid'] . ')';
}
$groupid = (int) $input['groupid'];
break;
default:
return array("success" => false, "message" => "Access denied (invalid group mode).");
}
if (ini_get('max_execution_time') == 0 or $input['timeout'] + 5 >= ini_get('max_execution_time')) {
$timeout = 0;
} else {
$timeout = $input['timeout'];
}
// Start the timer
$starttime = time();
// Keep fetching new tags and events (see break condition near the end of the loop)
do {
// Get tags
$sql = "SELECT t.id, t.name, t.color, t.timecreated, t.timemodified, t.level
FROM {videoannotation_tags} t
WHERE t.clipid = " . (int) $input['clipid'] . '
AND ' . $tagusergroupclause;
if (isset($input['timestamp'])) {
$sql .= ' AND (t.timecreated > ' . (int) $input['timestamp'] . ' OR t.timemodified > ' . (int) $input['timestamp'] . ')';
}
$sql .= ' ORDER BY t.sortorder';
$rs = $DB->get_recordset_sql($sql);
// Fill tags into array, even if there are none (new video annotation has no tags). Only error on database error, not empty rs
try {
$data = array();
foreach ($rs as $record) {
array_push($data, $record);
}
$rs->close();
$tags = $data;
} catch (dml_exception $e) {
return array("success" => false, "message" => "Database error ");
}
// Get events
$sql = "SELECT e.id, e.tagid, e.starttime, e.endtime, e.content, e.timecreated, e.timemodified, e.latitude, e.longitude, e.scope, e.level
FROM {videoannotation_events} e
JOIN {videoannotation_tags} t ON e.tagid = t.id
WHERE t.clipid = " . (int) $input['clipid'] . '
AND ' . $tagusergroupclause . '
AND ' . $eventusergroupclause;
if (isset($input['timestamp'])) {
$sql .= ' AND (e.timecreated > ' . (int) $input['timestamp'] . ' OR e.timemodified > ' . (int) $input['timestamp'] . ')';
}
$rs = $DB->get_recordset_sql($sql);
try {
$rsarray = array();
foreach ($rs as $record) {
array_push($rsarray, $record);
}
$rs->close();
$events = $rsarray;
} catch (dml_exception $e) {
return array("success" => false, "message" => "Database error ");
}
// Get deleted tags.
if (isset($input['tags']) and preg_match('/^\d+(\,\d+)*$/', $input['tags'])) {
$oldexistingtags = explode(',', $input['tags']);
$newexistingtags = $DB->get_records_sql("SELECT id FROM {videoannotation_tags} t WHERE id IN (" . $input['tags'] . ") AND " . $tagusergroupclause);
if ($newexistingtags) {
$newexistingtags = array_keys($newexistingtags);
} else {
$newexistingtags = array();
}
$deletedtags = array_values(array_diff($oldexistingtags, $newexistingtags));
} else {
$deletedtags = array();
}
// Get deleted events.
if (isset($input['events']) and preg_match('/^\d+(\,\d+)*$/', $input['events'])) {
$oldexistingevents = explode(',', $input['events']);
$newexistingevents = $DB->get_records_sql("SELECT id FROM {videoannotation_events} e WHERE id IN (" . $input['events'] . ")");
if ($newexistingevents) {
$newexistingevents = array_keys($newexistingevents);
} else {
$newexistingevents = array();
}
$deletedevents = array_values(array_diff($oldexistingevents, $newexistingevents));
} else {
$deletedevents = array();
}
// Determine new timestmp
$newtimestamp = isset($input['timestamp']) ? $input['timestamp'] : 0;
foreach ($tags as &$tag) {
$newtimestamp = max($newtimestamp, (int) $tag->timecreated, (int) $tag->timemodified);
unset($tag->timecreated);
unset($tag->timemodified);
}
foreach ($events as &$event) {
$newtimestamp = max($newtimestamp, (int) $event->timecreated, (int) $event->timemodified);
unset($event->timecreated);
unset($event->timemodified);
}
if ((time() - $starttime >= $timeout) or $tags or $events or $deletedtags or $deletedevents) {
break;
}
sleep(1);
} while (true);
// Return tags and events
$result = array(
"success" => true,
"tags" => $tags,
'events' => $events,
'deletedtags' => $deletedtags,
'deletedevents' => $deletedevents,
'timestamp' => $newtimestamp
);
if ($onlineusers !== null) {
$result['onlineusers'] = $onlineusers;
}
return $result;
case 'getclipinfo':
if (isset($input['clipurl'])) {
$info = videoannotation_get_clip_info($input['clipurl']);
$info['success'] = true;
return $info;
} else {
return array("success" => false, "message" => "clipurl is not given.");
}
// SSC-1191: Detect changes to the clip during editing
case 'getclipdata':
if (!isset($input['clipid'])) {
return array("success" => false, "message" => "clipid is not given.");
}
$sql = "SELECT c.videoannotationid, c.groupid, c.url, c.playabletimestart, c.playabletimeend,
c.videowidth, c.videoheight, c.timecreated, c.timemodified
FROM {videoannotation_clips} c
WHERE c.id =" . (int) $input['clipid'];
$rs = $DB->get_recordset_sql($sql);
if (!$rs->valid()) {
return array("success" => false, "message" => "Database error. ");
}
$data = array();
foreach ($rs as $record) {
array_push($data, $record);
}
$rs->close();
$result = array(
"success" => true,
"data" => $data
);
return $result;
// END SSC-1191
case 'addtag':
if (!isset($input['clipid']) or ! isset($input['name'])) {
return array("success" => false, "message" => "clipid or name is not given.");
}
$videoannotation = get_videoannotation_clip($input['clipid']);
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$canview = has_capability('mod/videoannotation:view', $modulecontext);
$isadmin = is_siteadmin($USER->id);
// Security check
if (!$isadmin) {
// Case 1: "userid" not given, "groupid" not given (tag will be owned by the activity)
if (!isset($input['userid']) and ! isset($input['groupid'])) {
// The user needs to have "manage" capability.
if (!$canmanage) {
return array("success" => false, "message" => "mod/videoannotation:manage capability required.");
}
// Case 2: "userid" given, "groupid" given (tag will be owned by the group)
} else if (isset($input['userid']) and isset($input['groupid'])) {
// The user needs to have "manage" or "submit" capability.
if (!$canmanage and ! $cansubmit) {
return array("success" => false, "message" => "mod/videoannotation:manage or mod/videoannotation:submit capability required.");
}
// The group has not submitted this activity yet.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'groupid' => $input['groupid']))) {
return array("success" => false, "Cannot add event in a timeline that has already been submitted.");
}
// The activity needs to have group mode on.
if ($videoannotation->groupmode == NOGROUPS) {
return array("success" => false, "message" => "Group mode must be on.");
}
// The user ID given (required) needs to equal the requestor's user ID.
if ($USER->id != $input['userid']) {
return array("success" => false, "message" => "userid, if given, must be the requestor's user ID.");
}
// The user have to be in the given group.
if (!groups_is_member($input['groupid'], $input['userid'])) {
return array("success" => false, "message" => "The given user must be in the given group.");
}
// Case 3: "userid" given, "groupid" not given (tag will be owned by the user)
} else if (isset($input['userid'])) {
// The user needs to have "manage" or "submit" capability
if (!$canmanage and ! $cansubmit) {
return array("success" => false, "message" => "mod/videoannotation:manage or mod/videoannotation:submit capability required.");
}
// The user must not have submitted this activity yet.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $input['userid'], 'groupid' => null))) {
return array("success" => false, "Cannot add event in a timeline that has already been submitted.");
}
// The activity must have group mode off.
if ($videoannotation->groupmode != NOGROUPS) {
return array("success" => false, "message" => "Group mode must be off.");
}
// The user ID given needs to equal the requestor's user ID.
if ($USER->id != $input['userid']) {
return array("success" => false, "message" => "userid, if given, must be the requestor's user ID.");
}
// Case 4: "userid" not given, "groupid" given
} else {
// Not acceptable; complain and abort.
return array("success" => false, "message" => "userid must be given if groupid is given.");
}
}
// Insert record.
$data = (object) array('clipid' => $input['clipid'], 'name' => $input['name'], 'timecreated' => time());
if (isset($input['userid'])) {
$data->userid = $input['userid'];
}
if (isset($input['groupid'])) {
$data->groupid = $input['groupid'];
}
if (isset($input['color'])) {
$data->color = $input['color'];
}
if (isset($input['level'])) {
$data->level = $input['level'];
}
$lastid = $DB->insert_record('videoannotation_tags', $data);
if ($lastid !== false) {
return array("success" => true, "id" => $lastid);
} else {
return array("success" => false, "message" => "Database error ");
}
case 'edittag':
// id must be given
if (!isset($input['id'])) {
return array("success" => false, "message" => "id is not given.");
}
if (!isset($input['name']) and ! isset($input['color'])) {
return array("success" => false, "message" => "name or color must be given.");
}
$videoannotation = get_videoannotation_tag($input['id']);
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$isadmin = is_siteadmin($USER->id);
if (!$isadmin) {
// Security check
switch ($videoannotation->groupmode) {
// Case 1: group mode is off
case NOGROUPS:
// The tag must belong to the requestor and not a group.
if (!$DB->record_exists('videoannotation_tags', array('id' => $input['id'], 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "message" => "Access denied (not owner of the tag).");
}
// The requestor must not have submitted the activity.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "Cannot edit tag in a timeline that has already been submitted.");
}
break;
// Case 2: group mode is "separate" or "visible"
case VIDEOANNOTATION_GROUPMODE_USER_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_USER:
case VIDEOANNOTATION_GROUPMODE_ALL_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_ALL:
// The tag must belong to a group that the requestor belongs.
// Also, if group mode is "individual", "read all", the tag must be belong to the requestor.
$sql = "SELECT g.*
FROM {videoannotation_tags} vt
JOIN {groups_members} gm ON vt.groupid = gm.groupid
JOIN {groups} g ON gm.groupid = g.id
WHERE vt.id = " . (int) $input['id'] . " AND gm.userid = " . $USER->id;
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_USER_USER, VIDEOANNOTATION_GROUPMODE_ALL_USER))) {
$sql .= " AND vt.userid = " . $USER->id;
}
$group = $DB->get_record_sql($sql);
if (!$group) {
return array("success" => false, "message" => "Access denied (not member the of the owner group of the tag).");
}
// The requestor must not have submitted the activity.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'groupid' => $group->id))) {
return array("success" => false, "Cannot edit tag in a timeline that has already been submitted.");
}
break;
// Case 3: group mode is something else
default:
// Complain and abort.
return array("success" => false, "message" => "Access denied (invalid group mode).");
}
}
// Update record.
$dataobject = new stdClass();
$dataobject->id = $input['id'];
if (isset($input['name'])) {
$dataobject->name = $input['name'];
}
if (isset($input['color'])) {
$dataobject->color = $input['color'];
}
if (isset($input['level'])) {
$dataobject->level = $input['level'];
}
try {
$rs = $DB->update_record('videoannotation_tags', $dataobject);
if (!$rs) {
return array("success" => false, "errortype" => "writeconflict", "message" => "Another user might have edited or deleted the tag you are editing.");
}
} catch (dml_exeception $e) {
return array("success" => false, "errortype" => "database", "message" => "Database error ");
}
return array("success" => true);
case 'deletetag':
// id must be given
if (!isset($input['id'])) {
return array("success" => false, "message" => "id is not given.");
}
$videoannotation = get_videoannotation_tag($input['id']);
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$isadmin = is_siteadmin($USER->id);
// Security check.
if (!$isadmin) {
switch ($videoannotation->groupmode) {
// Case 1: group mode is off
case NOGROUPS:
// The tag must belong to the requestor and not a group.
if (!$DB->record_exists('videoannotation_tags', array('id' => $input['id'], 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "message" => "Access denied (not owner of the tag).");
}
// The requestor must not have submitted the activity.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "Cannot edit tag in a timeline that has already been submitted.");
}
break;
// Case 2: group mode is "separate" or "visible"
case VIDEOANNOTATION_GROUPMODE_USER_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_USER:
case VIDEOANNOTATION_GROUPMODE_ALL_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_ALL:
// The tag must belong to a group that the requestor belongs.
// Also, if group mode is "individua" or "read all", the tag must be belong to the requestor.
$sql = "SELECT g.*
FROM {videoannotation_tags} vt
JOIN {groups_members} gm ON vt.groupid = gm.groupid
JOIN {groups} g ON gm.groupid = g.id
WHERE vt.id = " . (int) $input['id'] . " AND gm.userid = " . $USER->id;
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_USER_USER, VIDEOANNOTATION_GROUPMODE_ALL_USER))) {
$sql .= " AND vt.userid = " . $USER->id;
}
$group = $DB->get_record_sql($sql);
if (!$group) {
return array("success" => false, "message" => "Access denied (not member of the owner of the tag).");
}
// The requestor must not have submitted the activity.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'groupid' => $group->id))) {
return array("success" => false, "Cannot edit tag in a timeline that has already been submitted.");
}
break;
// Case 3: group mode is something else
default:
// Complain and abort.
return array("success" => false, "message" => "Access denied (invalid group mode).");
}
}
// Delete record.
$rs = $DB->delete_records('videoannotation_tags', array('id' => $input['id']));
if (!$rs) {
return array("success" => false, "errortype" => "database", "message" => "Database error ");
}
$rs = $DB->delete_records('videoannotation_events', array('tagid' => $input['id']));
if (!$rs) {
return array("success" => false, "errortype" => "database", "message" => "Database error ");
}
return array("success" => true);
case 'reordertags':
// clipid and orders must be given
if (!isset($input['clipid']) or ! isset($input['orders'])) {
return array("success" => false, "message" => "clipid or orders is not given.");
}
// Require mod/videoannotation:manage or mod/videoannotation:submit capability.
// If only "submit" capability exists, there cannot have a submission for this activity and user.
$videoannotation = get_videoannotation_clip($input['clipid']);
$coursemodule = get_coursemodule_clip($input['clipid']);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$isadmin = is_siteadmin($USER->id);
$dataobject = new stdClass();
if ($isadmin || $canmanage) {
// OK
} else if ($cansubmit) {
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id))) {
return array("success" => false, "Cannot reorder tags in a timeline that has already been submitted.");
}
} else {
return array("success" => false, "message" => "mod/videoannotation:manage or mod/videoannotation:submit capability required.");
}
// Access is granted in two cases
// 1. If "userid" is given and the group mode is off,
// then the requestor can reorder her own tags
// 2. If "groupid" is given and the group mode is "separate" or "visible",
// and the user is in the given group,
// then the requestor can reorder her group's tags
// Either "userid" or "groupid", but not both, should be given
switch ($videoannotation->groupmode) {
case NOGROUPS:
$dataobject->userid = (int) $USER->id;
break;
case VIDEOANNOTATION_GROUPMODE_USER_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_USER:
case VIDEOANNOTATION_GROUPMODE_ALL_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_ALL:
if (!isset($input['groupid'])) {
return array("success" => false, "message" => "groupid must be given.");
}
if (!$isadmin and ! $canmanage and ! groups_is_member($input['groupid'], $USER->id)) {
return array("success" => false, "message" => "Access denied (user not in group).");
}
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_USER_USER, VIDEOANNOTATION_GROUPMODE_GROUP_USER, VIDEOANNOTATION_GROUPMODE_ALL_USER))) {
$dataobject->userid = (int) $USER->id;
$dataobject->groupid = (int) $input['groupid'];
} else {
$dataobject->groupid = (int) $input['groupid'];
}
break;
default:
return array("success" => false, "message" => "Access denied (invalid group mode).");
}
// Update records.
$i = 0;
try {
$arr = explode(",", $input['orders']);
foreach ($arr as $id) {
$dataobject->id = $id;
$dataobject->sortorder = $i;
$result = $DB->update_record('videoannotation_tags', $dataobject);
$i++;
}
} catch (dml_exeception $e) {
return array("success" => false, "message" => "Database error");
}
return array("success" => true);
case 'addevent':
if (!isset($input['tagid']) or ! isset($input['starttime']) or ! isset($input['endtime'])) {
return array("success" => false, "message" => "tagid or starttime or endtime is not given.");
}
if (!$DB->record_exists('videoannotation_tags', array('id' => $input['tagid']))) {
return array("success" => false, "message" => "Tag does not exist.");
}
$videoannotation = get_videoannotation_tag($input['tagid']);
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$isadmin = is_siteadmin($USER->id);
// Security check.
if (!$isadmin) {
// Case 1: "userid" not given, "groupid" not given (event will be owned by the activity)
if (!isset($input['userid']) and !isset($input['groupid'])) {
// The user needs to have "manage" capability.
if (!$isadmin and !$canmanage) {
return array("success" => false, "message" => "mod/videoannotation:manage capability required.");
}
// Case 2: "userid" given, "groupid" given (event will be owned by the group)
} else if (isset($input['userid']) and isset($input['groupid'])) {
// The user needs to have "manage" or "submit" capability.
if (!$isadmin and !$canmanage and !$cansubmit) {
return array("success" => false, "message" => "mod/videoannotation:manage or mod/videoannotation:submit capability required.");
}
// The group has not submitted this activity yet.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'groupid' => $input['groupid']))) {
return array("success" => false, "Cannot add event in a timeline that has already been submitted.");
}
// The activity needs to have group mode on.
if ($videoannotation->groupmode == NOGROUPS) {
return array("success" => false, "message" => "Group mode must be on.");
}
// The user ID given (required) needs to equal the requestor's user ID.
if ($USER->id != $input['userid']) {
return array("success" => false, "message" => "userid, if given, must be the requestor's user ID.");
}
// The user have to be in the given group.
if (!$DB->record_exists('groups_members', array('groupid' => $input['groupid'], 'userid' => $input['userid']))) {
return array("success" => false, "message" => "The given user must be in the given group.");
}
// Case 3: "userid" given, "groupid" not given (tag will be owned by the user)
} else if (isset($input['userid'])) {
// The user needs to have "manage" or "submit" capability.
if (!$isadmin and !$canmanage and !$cansubmit) {
return array("success" => false, "message" => "mod/videoannotation:manage or mod/videoannotation:submit capability required.");
}
// The user must not have submitted this activity yet.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $input['userid'], 'groupid' => null))) {
return array("success" => false, "Cannot add event in a timeline that has already been submitted.");
}
// The activity must have group mode off.
if ($videoannotation->groupmode != NOGROUPS) {
return array("success" => false, "message" => "Group mode must be off.");
}
// The user ID given needs to equal the requestor's user ID.
if ($USER->id != $input['userid']) {
return array("success" => false, "message" => "userid, if given, must be the requestor's user ID.");
}
// Case 4: "userid" not given, "groupid" given
} else {
// Not acceptable; complain and abort.
return array("success" => false, "message" => "userid must be given if groupid is given.");
}
}
// Make sure that the tag record is still there.
if (!$DB->record_exists('videoannotation_tags', array('id' => $input['tagid']))) {
return array("success" => false, "errortype" => "writeconflict", "message" => "Another user might have deleted the tag you are editing.");
}
// Insert event record.
$data = (object) array('tagid' => $input['tagid'], 'starttime' => $input['starttime'], 'endtime' => $input['endtime'], 'timecreated' => time());
if (isset($input['content'])) {
$data->content = $input['content'];
} else {
$data->content = "";
}
if (isset($input['userid'])) {
$data->userid = $input['userid'];
}
if (isset($input['groupid'])) {
$data->groupid = $input['groupid'];
}
if (isset($input['latitude'])) {
$data->latitude = $input['latitude'];
}
if (isset($input['longitude'])) {
$data->longitude = $input['longitude'];
}
if (isset($input['scope'])) {
$data->scope = $input['scope'];
}
if (isset($input['level'])) {
$data->level = $input['level'];
}
$lastid = $DB->insert_record('videoannotation_events', $data);
if ($lastid !== false) {
return array("success" => true, "id" => $lastid);
} else {
return array("success" => false, "message" => "Database error w");
}
case 'editevent':
// id must be given
if (!isset($input['id'])) {
return array("success" => false, "message" => "id is not given.");
}
if (!$DB->record_exists('videoannotation_events', array('id' => $input['id']))) {
return array("success" => false, "message" => "Event does not exist.");
}
$videoannotation = get_videoannotation_event($input['id']);
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
$modulecontext = context_module::instance($coursemodule->id);
$canmanage = has_capability('mod/videoannotation:manage', $modulecontext);
$cansubmit = has_capability('mod/videoannotation:submit', $modulecontext);
$isadmin = is_siteadmin($USER->id);
if (!$isadmin) {
// Security check.
switch ($videoannotation->groupmode) {
// Case 1: group mode is off
case NOGROUPS:
// The event must belong to the requestor and not a group.
if (!$DB->record_exists('videoannotation_events', array('id' => $input['id'], 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "message" => "Access denied (not owner of the event).");
}
// The requestor must not have submitted the activity.
if ($DB->record_exists('videoannotation_submissions', array('videoannotationid' => $videoannotation->id, 'userid' => $USER->id, 'groupid' => null))) {
return array("success" => false, "Cannot edit event in a timeline that has already been submitted.");
}
break;
// Case 2: group mode is "separate" or "visible"
case VIDEOANNOTATION_GROUPMODE_USER_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_USER:
case VIDEOANNOTATION_GROUPMODE_GROUP_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_USER:
case VIDEOANNOTATION_GROUPMODE_ALL_GROUP:
case VIDEOANNOTATION_GROUPMODE_ALL_ALL:
// The event must belong to a group that the requestor belongs.
// Also, if group mode is "individual" or "read all", the tag must be belong to the requestor.
$sql = "SELECT g.*
FROM {videoannotation_events} ve
JOIN {groups_members} gm ON ve.groupid = gm.groupid
JOIN {groups} g ON gm.groupid = g.id
WHERE ve.id = " . (int) $input['id'] . " AND gm.userid = " . $USER->id;
$group = $DB->get_record_sql($sql);
if (!$group) {
return array("success" => false, "message" => "Access denied (not member of the owner of the event).");
}
if (in_array($videoannotation->groupmode, array(VIDEOANNOTATION_GROUPMODE_USER_USER, VIDEOANNOTATION_GROUPMODE_GROUP_USER, VIDEOANNOTATION_GROUPMODE_ALL_USER))) {
$sql .= " AND ve.userid = " . $USER->id;
}
// The requestor must not have submitted the activity.