-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocallib.php
657 lines (572 loc) · 24.3 KB
/
locallib.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
<?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/>.
/**
* Main locallib.php file for the Panopto Student Submission mod
*
* @package mod_panoptosubmission
* @copyright Panopto 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('PANOPTOSUBMISSION_ALL', 0);
define('PANOPTOSUBMISSION_REQ_GRADING', 1);
define('PANOPTOSUBMISSION_SUBMITTED', 2);
define('PANOPTOSUBMISSION_NOT_SUBMITTED', 3);
// File areas for file submission assignment.
define('STUDENTSUBMISSION_FILE_COMPONENT', 'mod_panoptosubmission');
define('STUDENTSUBMISSION_FILE_FILEAREA', 'studentsubmission_files');
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->libdir . '/moodlelib.php');
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/grade/grading/lib.php');
require_once($CFG->dirroot . '/blocks/panopto/lib/panopto_data.php');
require_once($CFG->dirroot . '/blocks/panopto/lib/block_panopto_lib.php');
/**
* Check if the assignment submission end date has passed or if late submissions
* are prohibited
*
* @param object $targetactivity - Instance of a Panopto Student Submission activity
* @return bool - true if past due, otherwise false
*/
function panoptosubmission_submission_past_due($targetactivity) {
$pastdue = false;
if ($targetactivity->preventlate) {
$pastdue = (0 != $targetactivity->timedue) && (time() > $targetactivity->timedue);
}
return $pastdue;
}
/**
* Check if the assignment submission cut off has passed
*
* @param object $targetactivity - Instance of a Panopto Student Submission activity
* @return bool - true if past cut off, otherwise false
*/
function panoptosubmission_submission_past_cutoff($targetactivity) {
return (0 != $targetactivity->cutofftime) && (time() > $targetactivity->cutofftime);
}
/**
* Check if the assignment submission start date is set and if it has arrived yet.
*
* @param object $targetactivity - Instance of a Panopto Student Submission activity
* @return bool - true if available, otherwise false
*/
function panoptosubmission_submission_available_yet($targetactivity) {
$availableyet = true;
if (isset($targetactivity->timeavailable)) {
$availableyet = time() >= $targetactivity->timeavailable;
}
return $availableyet;
}
/**
* Retrieve a list of users who have submitted assignments
*
* @param int $targetinstance The assignment id.
* @param string $filter Filter results by assignments that have been submitted or
* assignment that need to be graded or no filter at all.
* @return mixed collection of users or false.
*/
function panoptosubmission_get_submissions($targetinstance, $filter = '') {
global $DB;
$where = '';
switch ($filter) {
case PANOPTOSUBMISSION_SUBMITTED:
$where = ' timemodified > 0 AND ';
break;
case PANOPTOSUBMISSION_REQ_GRADING:
$where = ' timemarked < timemodified AND ';
break;
}
$param = ['instanceid' => $targetinstance];
$where .= ' panactivityid = :instanceid';
// Reordering the fields returned to make it easier to use in the grade_get_grades function.
$columns = 'userid,panactivityid,grade,submissioncomment,format,teacher,mailed,' .
'timemarked,timecreated,timemodified,source,width,height';
$records = $DB->get_records_select('panoptosubmission_submission', $where, $param, 'timemodified DESC', $columns);
if (empty($records)) {
return false;
}
return $records;
}
/**
* This function retrives the user's submission record.
*
* @param int $targetinstanceid The panopto submission instance id.
* @param int $userid The user id.
* @return object A data object consisting of the user's submission.
*/
function panoptosubmission_get_submission($targetinstanceid, $userid) {
global $DB;
$param = ['instanceid' => $targetinstanceid, 'userid' => $userid];
$where = '';
$where .= ' panactivityid = :instanceid AND userid = :userid';
// Reordering the fields returned to make it easier to use in the grade_get_grades function.
$columns = 'userid,id,panactivityid,grade,submissioncomment,format,teacher,mailed,' .
'timemarked,timecreated,timemodified,source,width,height';
$record = $DB->get_record_select('panoptosubmission_submission', $where, $param, '*');
if (empty($record)) {
return null;
}
return $record;
}
/**
* This function retrieves the submission grade object.
*
* @param int $targetinstanceid The activity instance id.
* @param int $userid The user id.
* @return object A data object consisting of the user's submission.
*/
function panoptosubmission_get_submission_grade_object($targetinstanceid, $userid) {
global $DB;
$param = ['panactivityid' => $targetinstanceid, 'userid' => $userid];
$sql = "SELECT u.id, u.id AS userid, s.grade AS rawgrade, s.submissioncomment AS feedback, s.format AS feedbackformat, " .
"s.teacher AS usermodified, s.timemarked AS dategraded, s.timemodified AS datesubmitted " .
"FROM {user} u, {panoptosubmission_submission} s " .
"WHERE u.id = s.userid AND s.panactivityid = :panactivityid " .
"AND u.id = :userid";
$data = $DB->get_record_sql($sql, $param);
if (-1 == $data->rawgrade) {
$data->rawgrade = null;
}
return $data;
}
/**
* This function validates the course module id and returns the course module object, course object and activity instance object.
*
* @param int $cmid the id of the context for the module instance
* @return array an array with the following values [course module object, $course object, activity instance object].
* @throws moodle_exception
*/
function panoptosubmission_validate_cmid($cmid) {
global $DB;
if (!$cm = get_coursemodule_from_id('panoptosubmission', $cmid)) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$course = $DB->get_record('course', ['id' => $cm->course])) {
throw new moodle_exception('coursemisconf');
}
if (!$targetpanoptosubmission = $DB->get_record('panoptosubmission', ['id' => $cm->instance])) {
throw new moodle_exception('invalidid', 'panoptosubmission');
}
return [$cm, $course, $targetpanoptosubmission];
}
/**
* This function returns HTML markup to signify a submission was late.
*
* @param string $timesubmitted the time the assignment was submitted
* @param string $timedue the time the assignment was due
* @return string HTML markup
*/
function panoptosubmission_display_lateness($timesubmitted, $timedue) {
if (!$timedue) {
return '';
}
$time = $timedue - $timesubmitted;
if ($time < 0) {
$timetext = get_string('late', 'panoptosubmission', format_time($time));
return ' (<span class="late">'.$timetext.'</span>)';
} else {
$timetext = get_string('early', 'panoptosubmission', format_time($time));
return ' (<span class="early">'.$timetext.'</span>)';
}
}
/**
* Alerts users by email of new or changed assignments.
*
*
* @param object $cm Panopto Student Submission activity course module object.
* @param object $course The course object.
* @param string $name Name of the Panopto activity instance.
* @param object $submission The submission that has changed.
* @param stdClass $userfrom User that is sending notification.
* @param stdClass $userto User that is receiving notification.
* @param string $messagetype Message type.
*/
function panoptosubmission_send_notification($cm,
$course,
$name,
$submission,
$userfrom,
$userto,
$messagetype) {
global $CFG;
$modulename = get_string('modulename', 'panoptosubmission');
$strsubmitted = get_string('submitted', 'panoptosubmission');
$courseid = $cm->course ?? $course->id;
$info = new stdClass();
$info->username = fullname($userfrom, true);
$info->assignment = format_string($name, true);
$info->url = $CFG->wwwroot . '/mod/panoptosubmission/grade_submissions.php?cmid=' . $cm->id;
$info->timeupdated = date('c', $submission->timemodified);
$info->courseid = $courseid;
$info->cmid = $cm->id;
$postsubject = $strsubmitted . ': ' . $userfrom->username . ' -> ' . $name;
$posttext = panoptosubmission_format_notification_message_text($messagetype, $course, $info, $modulename);
$posthtml = ($userto->mailformat == 1)
? panoptosubmission_format_notification_message_html($messagetype, $course, $info)
: '';
$eventdata = new \core\message\message();
$eventdata->courseid = $courseid;
$eventdata->modulename = 'panoptosubmission';
$eventdata->userfrom = $userfrom;
$eventdata->userto = $userto;
$eventdata->subject = $postsubject;
$eventdata->fullmessage = $posttext;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = $posthtml;
$eventdata->smallmessage = $postsubject;
$eventdata->name = 'panoptosubmission_updates';
$eventdata->component = 'mod_panoptosubmission';
$eventdata->notification = 1;
$eventdata->contexturl = $info->url;
$eventdata->contexturlname = $info->assignment;
message_send($eventdata);
}
/**
* Returns a list of teachers that should be grading given submission.
*
* @param object $cm Panopto video assignment course module object.
* @param object $user The Moodle user object.
* @param object $context A context object.
* @return array An array of grading userids
*/
function panoptosubmission_get_graders($cm, $user, $context) {
// Potential graders.
$potgraders = get_enrolled_users($context, 'mod/panoptosubmission:gradesubmission',
0, 'u.*', null, 0, 0, true);
$graders = [];
// Separate groups are being used.
if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
// Try to find all groups.
if ($groups = groups_get_all_groups($cm->course, $user->id)) {
foreach ($groups as $group) {
foreach ($potgraders as $potgrader) {
if ($potgrader->id == $user->id) {
continue; // Do not send self.
}
if (groups_is_member($group->id, $potgrader->id)) {
$graders[$potgrader->id] = $potgrader;
}
}
}
} else {
// User not in group, try to find graders without group.
foreach ($potgraders as $potgrader) {
if ($potgrader->id == $user->id) {
// Do not send self.
continue;
}
// Ugly hack.
if (!groups_get_all_groups($cm->course, $potgrader->id)) {
$graders[$potgrader->id] = $potgrader;
}
}
}
} else {
foreach ($potgraders as $potgrader) {
if ($potgrader->id == $user->id) {
// Do not send self.
continue;
}
$graders[$potgrader->id] = $potgrader;
}
}
return $graders;
}
/**
* Send notifications to graders upon student submissions.
*
* @param object $pansubmissionactivity The submission object or NULL in which case it will be loaded
* @param object $submission The submission that has changed.
* @param object $cm Panopto Student Submission activity course module object.
* @param object $context The context object.
* @param object $course The course object.
* @return void
*/
function panoptosubmission_notify_graders($pansubmissionactivity,
$submission,
$cm,
$context,
$course) {
global $DB, $USER;
$late = $pansubmissionactivity->timedue && ($pansubmissionactivity->timedue < time());
if (!$pansubmissionactivity->sendnotifications && !($late && $pansubmissionactivity->sendlatenotifications)) {
// No need to do anything.
return;
}
if ($submission->userid) {
$user = $DB->get_record('user', ['id' => $submission->userid]);
} else {
$user = $USER;
}
if ($teachers = panoptosubmission_get_graders($cm, $user, $context)) {
foreach ($teachers as $teacher) {
panoptosubmission_send_notification($cm,
$course,
$pansubmissionactivity->name,
$submission,
$user,
$teacher,
'gradersubmissionupdated'
);
}
}
}
/**
* Creates the text content for emails.
*
* @param string $messagetype Message type.
* @param object $course Course object.
* @param object $info The info used by the messagetype language strings.
* @param string $modulename Module name.
* @return string
*/
function panoptosubmission_format_notification_message_text($messagetype, $course, $info, $modulename) {
global $DB;
if (empty($course)) {
$param = ['id' => $info->courseid];
$course = $DB->get_record('course', $param);
}
$posttext = '';
if (!empty($course)) {
$posttext = format_string($course->shortname, true, $course->id) .
' -> ' .
$modulename .
' -> ' .
format_string($info->assignment, true, $course->id) . "\n";
$posttext .= '---------------------------------------------------------------------' . "\n";
$posttext .= get_string($messagetype . 'text', 'panoptosubmission', $info) . "\n";
$posttext .= "\n---------------------------------------------------------------------\n";
}
return $posttext;
}
/**
* Creates the html content for emails.
*
* @param string $messagetype Message type.
* @param object $course Course object.
* @param object $info The info used by the messagetype language strings.
* @return string
*/
function panoptosubmission_format_notification_message_html($messagetype, $course, $info) {
global $DB;
if (empty($course)) {
$param = ['id' => $info->courseid];
$course = $DB->get_record('course', $param);
}
$posthtml = '';
if (!empty($course)) {
$posthtml .= html_writer::start_tag('p');
$attr = ['href' => new moodle_url('/course/view.php', ['id' => $course->id])];
$posthtml .= html_writer::tag('a', format_string($course->shortname, true, $course->id), $attr);
$posthtml .= '->';
$attr = ['href' => new moodle_url('/mod/panoptosubmission/view.php', ['id' => $info->cmid])];
$posthtml .= html_writer::tag('a', format_string($info->assignment, true, $course->id), $attr);
$posthtml .= html_writer::end_tag('p');
$posthtml .= html_writer::start_tag('hr');
$posthtml .= html_writer::tag('p', get_string($messagetype . 'html', 'panoptosubmission', $info));
$posthtml .= html_writer::end_tag('hr');
}
return $posthtml;
}
/**
* This function retrieves a list of enrolled users with the capability to submit to the activity.
*
* @param object $cm the context object for the module instance
* @return array An array of user objects.
*/
function panoptosubmission_get_assignment_students($cm) {
$context = context_module::instance($cm->id);
$users = get_enrolled_users($context, 'mod/panoptosubmission:submit', 0, 'u.id');
return $users;
}
/**
* Returns the grading instance for the assignment
*
* @param object $cminstance Panopto video assignment course module object.
* @param object $context A context object.
* @param object $submission The current submission object
* @param bool $gradingdisabled whether or not advanced grading is disabled
* @return array An array of grading userids
*/
function panoptosubmission_get_grading_instance($cminstance, $context, $submission, $gradingdisabled) {
global $USER;
$grademenu = make_grades_menu($cminstance->grade);
$allowgradedecimals = $cminstance->grade > 0;
$advancedgradingwarning = false;
$gradingmanager = get_grading_manager($context, 'mod_panoptosubmission', 'submissions');
$gradinginstance = null;
if ($gradingmethod = $gradingmanager->get_active_method()) {
$controller = $gradingmanager->get_controller($gradingmethod);
if ($controller->is_form_available()) {
$itemid = null;
if ($submission) {
$itemid = $submission->id;
}
if ($gradingdisabled && $itemid) {
$gradinginstance = $controller->get_current_instance($USER->id, $itemid);
} else if (!$gradingdisabled) {
$instanceid = optional_param('advancedgradinginstanceid', 0, PARAM_INT);
$gradinginstance = $controller->get_or_create_instance($instanceid,
$USER->id,
$itemid);
}
} else {
$advancedgradingwarning = $controller->form_unavailable_notification();
}
}
if ($gradinginstance) {
$gradinginstance->get_controller()->set_grade_range($grademenu, $allowgradedecimals);
}
return $gradinginstance;
}
/**
* Creates an panoptosubmission_submissions_feedback_status renderable.
*
* @param object $cm Panopto video assignment course module object.
* @param object $pansubmissionactivity The submission object or NULL in which case it will be loaded
* @param object $submission current submission with grade information
* @param object $context A context object.
* @param string $userid of the user to get the report for
* @param object $grade user grade
* @param object $teacher user that graded the submission
* @return panoptosubmission_submissions_feedback_status renderable object
*/
function panoptosubmission_get_feedback_status_renderable($cm,
$pansubmissionactivity,
$submission,
$context,
$userid,
$grade,
$teacher) {
global $DB, $PAGE;
$gradinginfo = grade_get_grades($pansubmissionactivity->course,
'mod',
'panoptosubmission',
$cm->instance,
$userid);
$gradingitem = null;
$gradebookgrade = null;
if (isset($gradinginfo->items[0])) {
$gradingitem = $gradinginfo->items[0];
$gradebookgrade = $gradingitem->grades[$userid];
}
$cangrade = has_capability('mod/panoptosubmission:gradesubmission', $context);
$hasgrade = !is_null($gradebookgrade) && !is_null($gradebookgrade->grade);
$gradevisible = $cangrade || (!is_null($gradebookgrade) && !$gradebookgrade->hidden);
// If there is a visible grade, show the summary.
if ($hasgrade && $gradevisible) {
$gradefordisplay = null;
$gradeddate = null;
$grader = null;
$gradingmanager = get_grading_manager($context, 'mod_panoptosubmission', 'submissions');
// Criteria feedback.
if ($controller = $gradingmanager->get_active_controller()) {
$menu = make_grades_menu($submission->grade);
$controller->set_grade_range($menu, $submission->grade > 0);
$gradefordisplay = $controller->render_grade($PAGE,
$submission->id,
$gradingitem,
$gradebookgrade->str_long_grade,
$cangrade);
} else {
// Normal feedback, which is just a grade.
$gradefordisplay = $grade->str_long_grade;
}
$gradeddate = $gradebookgrade->dategraded;
if (isset($teacher)) {
$grader = $DB->get_record('user', ['id' => $teacher->id]);
} else if (isset($gradebookgrade->usermodified)
&& $gradebookgrade->usermodified > 0
&& has_capability('mod/panoptosubmission:gradesubmission', $context, $gradebookgrade->usermodified)) {
// Grader not provided. Check that usermodified is a user who can grade.
// Case 1: When an assignment is reopened an empty grade is created so the feedback
// plugin can know which attempt it's referring to. In this case, usermodifed is a student.
// Case 2: When an assignment's grade is overrided via the gradebook, usermodified is a grader.
$grader = $DB->get_record('user', ['id' => $gradebookgrade->usermodified]);
}
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
$feedbackstatus = new panoptosubmission_submissions_feedback_status($gradefordisplay,
$gradeddate,
$grader,
$grade,
$cm->id,
$viewfullnames);
return $feedbackstatus;
}
return;
}
/**
* Creates an panoptosubmission_grading_summary renderable.
*
* @param object $cm Panopto video assignment course module object.
* @param object $course Course object.
* @return panoptosubmission_grading_summary renderable object
*/
function panoptosubmission_get_grading_summary_renderable($cm, $course) {
global $DB;
$instance = $DB->get_record('panoptosubmission', ['id' => $cm->instance], '*', MUST_EXIST);
$isvisible = $cm->visible;
$countparticipants = count(array_keys(panoptosubmission_get_assignment_students($cm)));
$submissionssubmitted = panoptosubmission_get_submissions($cm->instance, PANOPTOSUBMISSION_SUBMITTED);
$submissionssubmittedcount = $submissionssubmitted ? count($submissionssubmitted) : 0;
$submissionrequiregrading = panoptosubmission_get_submissions($cm->instance, PANOPTOSUBMISSION_REQ_GRADING);
$submissionrequiregradingcount = $submissionrequiregrading ? count($submissionrequiregrading) : 0;
$summary = new panoptosubmission_grading_summary(
$countparticipants,
true,
$submissionssubmittedcount,
$instance->cutofftime,
$instance->timedue,
$instance->timeavailable,
$course->id,
$submissionrequiregradingcount,
$course->relativedatesmode,
$course->startdate,
$isvisible
);
return $summary;
}
/**
* Provision the course if not provisioned already.
*
* @param int $courseid - the id of the course we are targetting in moodle.
* @return bool if success or failure
*/
function panoptosubmission_verify_panopto($courseid) {
$targetautoservername = get_config('block_panopto', 'automatic_operation_target_server');
if (empty($targetautoservername)) {
throw new moodle_exception('no_automatic_operation_target_server', 'panoptosubmission');
return false;
}
try {
$targetserver = panopto_get_target_panopto_server();
$panopto = new \panopto_data($courseid);
if (!$panopto->has_valid_panopto()) {
$panopto->servername = $targetserver->name;
$panopto->applicationkey = $targetserver->appkey;
$provisioninginfo = $panopto->get_provisioning_info();
if ( (isset($provisioninginfo->unknownerror) && $provisioninginfo->unknownerror === true)
|| (isset($provisioninginfo->accesserror) && $provisioninginfo->accesserror === true)) {
return false;
}
$panopto->provision_course($provisioninginfo, false);
}
return true;
} catch (Exception $e) {
\panopto_data::print_log($e->getMessage());
return false;
}
}