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
/
Copy pathlib.php
448 lines (401 loc) · 15.4 KB
/
lib.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
<?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/>.
/**
* Library of functions and constants for module videoannotation
* This file should have two well differenced parts:
* - All the core Moodle functions, neeeded to allow
* the module to work integrated in Moodle.
* - All the videoannotation specific functions, needed
* to implement all the module logic. Please, note
* that, if the module become complex and this lib
* grows a lot, it's HIGHLY recommended to move all
* these module specific functions to a new php file,
* called "locallib.php" (see forum, quiz...). This will
* help to save some memory when Moodle is performing
* actions across all modules.
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/mod/videoannotation/locallib.php');
define('VIDEOANNOTATION_GROUPMODE_USER_USER', 3);
define('VIDEOANNOTATION_GROUPMODE_GROUP_USER', 2);
define('VIDEOANNOTATION_GROUPMODE_GROUP_GROUP', 1);
define('VIDEOANNOTATION_GROUPMODE_ALL_USER', 6);
define('VIDEOANNOTATION_GROUPMODE_ALL_GROUP', 7);
define('VIDEOANNOTATION_GROUPMODE_ALL_ALL', 8);
/**
* Returns the information on whether the module supports a feature
*
* See {@link plugin_supports()} for more info.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function videoannotation_supports($feature) {
switch($feature) {
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_CONTROLS_GRADE_VISIBILITY:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_IDNUMBER:
return true;
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMMENT:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $videoannotation An object from the form in mod_form.php
* @return int The id of the newly inserted videoannotation record
*/
function videoannotation_add_instance($videoannotation) {
global $DB;
$videoannotation->timecreated = time();
$videoannotation->timemodified = time();
$videoannotation->id = $DB->insert_record('videoannotation', $videoannotation);
if (!$videoannotation->id) {
return false;
}
if ($videoannotation->clipselect == 1) {
$clip = new stdClass();
$clip->videoannotationid = $videoannotation->id;
$clip->userid = null;
$clip->url = $videoannotation->clipurl;
$clip->playabletimestart = $videoannotation->playabletimestart;
$clip->playabletimeend = $videoannotation->playabletimeend;
$clip->videowidth = $videoannotation->videowidth;
$clip->videoheight = $videoannotation->videoheight;
$clip->timecreated = time();
$clip->timemodified = time();
$clipid = $DB->insert_record('videoannotation_clips', $clip);
if (!$clipid) {
return false;
}
}
// Set groupmode field of the record in mdl_course_modules table.
// Valid values are: NOGROUPS (group mode off) or SEPARATEGROUPS (group mode on) or VISIBLEGROUPS (group mode on visible).
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
if ($videoannotation->groupmode == 0) {
$gm = NOGROUPS;
$videoannotation->groupmode = NOGROUPS;
} else if ($videoannotation->groupmode == 1) {
$gm = SEPARATEGROUPS;
$videoannotation->groupmode = SEPARATEGROUPS;
} else {
$gm = VISIBLEGROUPS;
$videoannotation->groupmode = VISIBLEGROUPS;
}
$DB->set_field('course_modules', 'groupmode', $gm, array('instance' => $videoannotation->id));
return $videoannotation->id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $videoannotation An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function videoannotation_update_instance($videoannotation) {
global $DB;
$videoannotation->timemodified = time();
$videoannotation->id = $videoannotation->instance;
if (!$DB->update_record('videoannotation', $videoannotation)) {
return false;
}
if ($videoannotation->clipselect == 1) {
$clip = new stdClass();
$clip->videoannotationid = $videoannotation->instance;
$clip->userid = null;
$clip->url = $videoannotation->clipurl;
$clip->playabletimestart = $videoannotation->playabletimestart;
$clip->playabletimeend = $videoannotation->playabletimeend;
$clip->videowidth = $videoannotation->videowidth;
$clip->videoheight = $videoannotation->videoheight;
if ($clipid = $DB->get_field('videoannotation_clips', 'id', array(
'videoannotationid' => $videoannotation->instance, 'userid' => null))) {
$clip->id = $clipid;
$clip->timemodified = time();
if (!$DB->update_record('videoannotation_clips', $clip)) {
return false;
}
} else {
$clip->timecreated = time();
$clip->timemodified = time();
if (!($clipid = $DB->insert_record('videoannotation_clips', $clip))) {
return false;
}
}
}
// Set groupmode field of the record in mdl_course_modules table.
// Valid values are: NOGROUPS (group mode off) or SEPARATEGROUPS (group mode on) or VISIBLEGROUPS (group mode on visible).
$coursemodule = videoannotation_get_course_module_by_video_annotation($videoannotation->id);
if ($videoannotation->groupmode == 0) {
$gm = NOGROUPS;
$videoannotation->groupmode = NOGROUPS;
} else if ($videoannotation->groupmode == 1) {
$gm = SEPARATEGROUPS;
$videoannotation->groupmode = SEPARATEGROUPS;
} else {
$gm = VISIBLEGROUPS;
$videoannotation->groupmode = VISIBLEGROUPS;
}
$DB->set_field('course_modules', 'groupmode', $gm, array('instance' => $videoannotation->id));
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function videoannotation_delete_instance($id) {
global $CFG, $DB;
if (!$videoannotation = $DB->get_record('videoannotation', array('id' => $id))) {
return false;
}
$result = true;
// Delete any dependent records here.
// Delete events.
$sql = "DELETE ve
FROM {videoannotation_clips} vc
JOIN {videoannotation_tags} vt ON vc.id = vt.clipid
JOIN {videoannotation_events} ve ON vt.id = ve.tagid
WHERE vc.videoannotationid =" . $videoannotation->id;
if (!$DB->execute($sql)) {
$result = false;
}
// Delete tags.
$sql = "DELETE vt
FROM {videoannotation_clips} vc
JOIN {videoannotation_tags} vt ON vc.id = vt.clipid
WHERE vc.videoannotationid =" . $videoannotation->id;
if (!$DB->execute($sql)) {
$result = false;
}
// Delete module, clips, locks, submissions.
// Each operation can be done independently.
if (!$DB->delete_records('videoannotation_clips', array('videoannotationid' => $videoannotation->id))) {
$result = false;
}
if (!$DB->delete_records('videoannotation_locks', array('videoannotationid' => $videoannotation->id))) {
$result = false;
}
if (!$DB->delete_records('videoannotation_submissions', array('videoannotationid' => $videoannotation->id))) {
$result = false;
}
// Delete grades.
videoannotation_grade_item_delete($videoannotation);
if (!$DB->delete_records('videoannotation', array('id' => $videoannotation->id))) {
$result = false;
}
return $result;
}
/**
* Delete grade item for given videoannotation
*
* @param object $videoannotation object
* @return object videoannotation
*/
function videoannotation_grade_item_delete($va) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
return grade_update('mod/videoannotation', $va->course, 'mod', 'videoannotation', $va->id, 0, null, array('deleted' => 1));
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @return null
* @todo Finish documenting this function
*/
function videoannotation_user_outline($course, $user, $mod, $videoannotation) {
return $return;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @return boolean
* @todo Finish documenting this function
*/
function videoannotation_user_complete($course, $user, $mod, $videoannotation) {
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in videoannotation activities and print it out.
* Return true if there was output, or false is there was none.
*
* @return boolean
* @todo Finish documenting this function
*/
function videoannotation_print_recent_activity($course, $isteacher, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* @return boolean
* @todo Finish documenting this function
**/
function videoannotation_cron () {
return true;
}
/**
* Must return an array of user records (all data) who are participants
* for a given instance of videoannotation. Must include every user involved
* in the instance, independient of his role (student, teacher, admin...)
* See other modules as example.
*
* @param int $videoannotationid ID of an instance of this module
* @return mixed boolean/array of students
*/
function videoannotation_get_participants($videoannotationid) {
return false;
}
/**
* Checks if scale is being used by any instance of videoannotation.
* This function was added in 1.9
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean True if the scale is used by any videoannotation
*/
function videoannotation_scale_used_anywhere($scaleid) {
return false;
}
/**
* Execute post-install custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function videoannotation_install() {
return true;
}
/**
* Execute post-uninstall custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function videoannotation_uninstall() {
return true;
}
// Any other videoannotation functions go here. Each of them must have a name that
// starts with videoannotation_
// Remember (see note in first lines) that, if this section grows, it's HIGHLY
// recommended to move all funcions below to a new "localib.php" file.
function videoannotation_get_submission_count($videoannotationid, $groupid=null) {
global $CFG, $DB;
$basesql = "SELECT COUNT(*)
FROM {$CFG->prefix}videoannotation_submissions
WHERE videoannotationid = " . (int) $videoannotationid . '
AND timesubmitted IS NOT NULL';
if ($groupid == 'all') {
$basesql .= ' AND groupid IS NOT NULL';
} else if ($groupid) {
$basesql .= ' AND groupid = ' . (int) $groupid;
} else {
$basesql .= ' AND groupid IS NULL';
}
$totalsubmissioncount = $DB->count_records_sql($basesql);
$ungradedsubmissioncount = $DB->count_records_sql($basesql . ' AND timegraded IS NULL');
return array($totalsubmissioncount, $ungradedsubmissioncount);
}
function videoannotation_get_clip_info($clipurl) {
global $CFG;
$result = array();
// If the URL is a TNA permalink, use the web service to translate it into a RTMP link.
foreach ($CFG->tnapermalinkurl as $idx => $tnapermalinkurl) {
if ($clipurl and stripos($clipurl, $tnapermalinkurl) === 0) {
@list($uuid, $offset) = explode(',', substr($clipurl, strlen($tnapermalinkurl)));
if ($uuid) {
$content = file_get_contents($CFG->tnawebserviceurl[$idx] .
'?action=uuidToFileName&uuid=' . urlencode(trim($uuid)));
$contentobj = json_decode($content);
if (preg_match('/^(\d{4})\-(\d{2})\-(\d{2})_(\d{2})(\d{2})/', $contentobj->filename, $matches)) {
return array(
'streamer' => $CFG->tnastreamerurl,
'file' => "{$matches[1]}/{$matches[1]}-{$matches[2]}/{$matches[1]}-{$matches[2]}-{$matches[3]}/" .
basename($contentobj->filename, '.txt') . '.mp4',
'start' => $offset > 0 ? $offset : 0
);
}
}
}
}
if (preg_match('/^(rtmp:\/\/.*?\/.*?)\/(.*)/', $clipurl, $matches)) {
return array('streamer' => $matches[1], 'file' => $matches[2]);
}
if (stripos($clipurl, 'http://www.youtube.com/watch?') === 0) {
return array('file' => $clipurl, 'width' => 640, 'height' => 385);
}
return array('file' => $clipurl);
}
function videoannotation_get_course_module_by_video_annotation($videoannotationid) {
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 AND v.id = " . (int) $videoannotationid;
return $DB->get_record_sql($sql);
}
// Define json_encode and json_decode if they are not defined (i.e. PHP < 5.2).
if (!function_exists('json_decode')) {
require_once(dirname(__FILE__) . '/JSON.php');
function json_encode($data) {
$value = new Services_JSON();
return $value->encode($data);
}
function json_decode($data, $assoc = false) {
if ($assoc) {
$value = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$value = new Services_JSON();
}
return $value->decode($data);
}
}