-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
1582 lines (1318 loc) · 54.3 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
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/>.
/**
* Library of interface functions and constants for module coursework
*
* @package mod_coursework
* @copyright 2011 University of London Computer Centre {@link ulcc.ac.uk}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_coursework\ability;
use mod_coursework\models\coursework;
use mod_coursework\exceptions\access_denied;
use mod_coursework\models\feedback;
use mod_coursework\models\submission;
use mod_coursework\models\user;
use mod_coursework\models\group;
use mod_coursework\models\outstanding_marking;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/calendar/lib.php');
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once($CFG->dirroot.'/mod/coursework/renderable.php');
/**
* Lists all file areas current user may browse
*
* @param object $course
* @param object $cm
* @param context $context
* @return array
*/
function coursework_get_file_areas($course, $cm, $context) {
$areas = [];
if (has_capability('mod/coursework:submit', $context)) {
$areas['submission'] = get_string('submissionfiles', 'coursework');
}
return $areas;
}
/**
* Serves files for pluginfile.php
* @param $course
* @param $cm
* @param $context
* @param $filearea
* @param $args
* @param $forcedownload
* @return bool
*/
function mod_coursework_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
// Lifted form the assignment version.
global $CFG, $DB, $USER;
$user = \mod_coursework\models\user::find($USER);
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login($course, false, $cm);
if (!$coursework = $DB->get_record('coursework', ['id' => $cm->instance])) {
return false;
}
$ability = new ability($user, coursework::find($coursework));
// From assessment send_file().
require_once($CFG->dirroot.'/lib/filelib.php');
if ($filearea === 'submission') {
$submissionid = (int)array_shift($args);
$submission = submission::find($submissionid);
if (!$submission) {
return false;
}
if ($ability->cannot('show', $submission)) {
return false;
}
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/mod_coursework/submission/{$submission->id}/{$relativepath}";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
return true;
} else {
if ($filearea === 'feedback') {
$feedbackid = (int)array_shift($args);
/**
* @var feedback $feedback
*/
$feedback = feedback::find($feedbackid);
if (!$feedback) {
return false;
}
if (!$ability->can('show', $feedback)) {
throw new access_denied(coursework::find($coursework));
}
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/mod_coursework/feedback/".
"{$feedback->id}/{$relativepath}";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, true);
return true;
}
}
return false;
}
/**
* 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 $formdata An object from the form in mod_form.php
* @return int The id of the newly inserted coursework record
*/
function coursework_add_instance($formdata) {
global $DB;
$formdata->timecreated = time();
// You may have to add extra stuff in here.
// We have to check to see if this coursework has a deadline.
// If it doesn't we need to set the deadline to zero.
$formdata->deadline = empty($formdata->deadline) ? 0 : $formdata->deadline;
$subnotify = '';
$comma = '';
if (!empty($formdata->submissionnotification)) {
foreach ($formdata->submissionnotification as $uid) {
$subnotify .= $comma . $uid;
$comma = ',';
}
}
$formdata->submissionnotification = $subnotify;
// If blind marking is set we will rename files.
if ($formdata->blindmarking == 1) {
$formdata->renamefiles = 1;
}
$returnid = $DB->insert_record('coursework', $formdata);
$formdata->id = $returnid;
// IMPORTANT: at this point, the coursemodule will be in existence, but will
// Not have the coursework id saved, because we only just made it.
$coursemodule = $DB->get_record('course_modules', ['id' => $formdata->coursemodule]);
$coursemodule->instance = $returnid;
// This is doing what will be done later by the core routines. Makes it simpler to use existing
// Code without special cases.
$DB->update_record('course_modules', $coursemodule);
// Get all the other data e.g. coursemodule.
$coursework = coursework::find($returnid);
// Create event for coursework deadline [due]
if ($coursework && $coursework->deadline) {
$event = \mod_coursework\calendar::coursework_event($coursework, 'due', $coursework->deadline);
calendar_event::create($event);
}
// Create event for coursework initialmarking deadline [initialgradingdue]
if ($coursework && $coursework->marking_deadline_enabled() && $coursework->initialmarkingdeadline) {
$event = \mod_coursework\calendar::coursework_event($coursework, 'initialgradingdue', $coursework->initialmarkingdeadline);
calendar_event::create($event);
}
// Create event for coursework agreedgrademarking deadline [agreedgradingdue]
if ($coursework && $coursework->marking_deadline_enabled() && $coursework->agreedgrademarkingdeadline && $coursework->has_multiple_markers()) {
$event = \mod_coursework\calendar::coursework_event($coursework, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline);
calendar_event::create($event);
}
coursework_grade_item_update($coursework);
return $returnid;
}
/**
* Is the event visible?
*
* This is used to determine global visibility of an event in all places throughout Moodle. For example,
* the ASSIGN_EVENT_TYPE_GRADINGDUE event will not be shown to students on their calendar, and
* ASSIGN_EVENT_TYPE_DUE events will not be shown to teachers.
*
* @param calendar_event $event
* @return bool Returns true if the event is visible to the current user, false otherwise.
*/
function mod_coursework_core_calendar_is_event_visible(calendar_event $event) {
global $DB, $USER;
$cm = get_fast_modinfo($event->courseid)->instances['coursework'][$event->instance];
$dbcoursework = $DB->get_record('coursework', ['id' => $cm->instance]);
$coursework = coursework::find($dbcoursework);
$user = user::find($USER->id);
$student = $coursework->can_submit();
$marker = $coursework->is_assessor($user);
if (($event->eventtype == 'due' && $student) || (($event->eventtype == 'initialgradingdue' || $event->eventtype == 'agreedgradingdue') && $marker)) {
return true;
}
return false;
}
/**
* This function receives a calendar event and returns the action associated with it, or null if there is none.
*
* This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
* is not displayed on the block.
*
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
* @return \core_calendar\local\event\entities\action_interface|\core_calendar\local\event\value_objects\action|null
*/
function mod_coursework_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $DB, $USER;
$cm = get_fast_modinfo($event->courseid)->instances['coursework'][$event->instance];
$submissionurl = new \moodle_url('/mod/coursework/view.php', ['id' => $cm->id]);
$name = '';
$itemcount = 0;
$dbcoursework = $DB->get_record('coursework', ['id' => $cm->instance]);
$coursework = coursework::find($dbcoursework);
$user = user::find($USER->id);
$student = $coursework->can_submit();
$marker = $coursework->is_assessor($user);
if ($marker) { // For markers
// Check how many submissions to mark
$outstandingmarking = new outstanding_marking();
if ($event->eventtype == 'initialgradingdue') {
// Initial grades
$togradeinitialcount = $outstandingmarking->get_to_grade_initial_count($dbcoursework, $user->id());
$name = ($coursework->has_multiple_markers()) ? get_string('initialgrade', 'coursework') : get_string('grade', 'mod_coursework');
$itemcount = $togradeinitialcount;
} else if ($event->eventtype == 'agreedgradingdue') {
// Agreed grades
$togradeagreedcount = $outstandingmarking->get_to_grade_agreed_count($dbcoursework, $user->id());
$name = get_string('agreedgrade', 'coursework');
$itemcount = $togradeagreedcount;
}
$submissionurl = new \moodle_url('/mod/coursework/view.php', ['id' => $cm->id]);
} else if ($student) { // for students
// if group cw check if student is in group, if not then don't display 'Add submission' link
if ($coursework->is_configured_to_have_group_submissions() && !$coursework->get_student_group($user)) {
// return null;
$submissionurl = new \moodle_url('/mod/coursework/view.php', ['id' => $cm->id]);
$itemcount = 1;
} else {
$submission = $coursework->get_user_submission($user);
$newsubmission = $coursework->build_own_submission($user);
if (!$submission) {
$submission = $newsubmission;
}
// Check if user can still submit
$ability = new ability($user, $coursework);
if (!$submission || $ability->can('new', $submission)) {
$name = get_string('addsubmission', 'coursework');
$itemcount = 1;
$allocatableid = $submission->get_allocatable()->id();
$allocatabletype = $submission->get_allocatable()->type();
$submissionurl = new \moodle_url('/mod/coursework/actions/submissions/new.php', ['allocatableid' => $allocatableid,
'allocatabletype' => $allocatabletype,
'courseworkid' => $coursework->id]);
} else {
return null;
}
}
}
return $factory->create_instance($name,
$submissionurl,
$itemcount,
true);
}
/**
* Callback function that determines whether an action event should be showing its item count
* based on the event type and the item count.
*
* @param calendar_event $event The calendar event.
* @param int $itemcount The item count associated with the action event.
* @return bool
*/
function mod_coursework_core_calendar_event_action_shows_item_count(calendar_event $event, $itemcount = 0) {
global $DB;
// List of event types where the action event's item count should be shown.
$initialgradingdueeventtype = ['initialgradingdue'];
$agreedgradingdueeventtype = ['agreedgradingdue'];
$cm = get_fast_modinfo($event->courseid)->instances['coursework'][$event->instance];
$dbcoursework = $DB->get_record('coursework', ['id' => $cm->instance]);
$coursework = coursework::find($dbcoursework);
$student = $coursework->can_submit();
// For mod_coursework we use 'initialgrading' and 'agreedgrading' event type; item count should be shown if there is one or more item count and user is not a student.
return (in_array($event->eventtype, $initialgradingdueeventtype) || in_array($event->eventtype, $agreedgradingdueeventtype)) && $itemcount > 0 && !$student;
}
/**
* Create grade item for given coursework
* @param \mod_coursework\models\coursework|stdClass $coursework object with extra cmid number
* @param null|array $grades array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function coursework_grade_item_update($coursework, $grades = null) {
global $CFG;
require_once($CFG->dirroot.'/lib/gradelib.php');
$paramtype = gettype($coursework);
if ($paramtype != 'object') {
throw new invalid_parameter_exception("Invalid type '$paramtype' for coursework");
}
if (get_class($coursework) == 'stdClass') {
// On activity rename, core will pass in stdClass object here.
// Otherwise expect coursework or coursework_groups_decorator to be passed.
$coursework = \mod_coursework\models\coursework::find($coursework);
}
$courseid = $coursework->get_course_id();
$params = [
'itemname' => $coursework->name,
'idnumber' => $coursework->get_coursemodule_idnumber(),
];
if ($coursework->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $coursework->grade;
$params['grademin'] = 0;
} else {
if ($coursework->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$coursework->grade;
} else {
$params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
}
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update(
'mod/coursework', $courseid, 'mod', 'coursework',
$coursework->id, 0, $grades, $params
);
}
/**
* Update coursework grades in the gradebook.
* This will be called to rename the grade item when {@link core_courseformat\local\cmactions::rename()} is used.
* Needed by {@link grade_update_mod_grades()} (Force full update of module grades in central gradebook).
*
* @param stdClass $moduleinstance Instance object with extra cmidnumber and modname property.
* @param int $userid Update grade of specific user only, 0 means all participants.
* @param bool $nullifnone If true and the user has no grade then a grade item with rawgrade == null
*/
function coursework_update_grades(stdClass $moduleinstance, int $userid = 0, $nullifnone = true) {
// Code adapted from mod_assign.
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
if ($moduleinstance->grade == 0) {
coursework_grade_item_update($moduleinstance);
} else if ($grades = coursework_get_user_grades($moduleinstance, $userid)) {
foreach ($grades as $k => $v) {
if ($v->rawgrade == -1) {
$grades[$k]->rawgrade = null;
}
}
coursework_grade_item_update($moduleinstance, $grades);
} else {
coursework_grade_item_update($moduleinstance);
}
}
/**
* Return gradebook grade for given user or all users.
*
* @param object $moduleinstance
* @param int $userid user ID.
* @return array array of grades
*/
function coursework_get_user_grades(object $moduleinstance, int $userid): array {
// If no user ID supplied, this returns information about grade_item only.
$grades = grade_get_grades(
$moduleinstance->course,
'mod',
'coursework',
$moduleinstance->id,
$userid != 0 ? $userid : null
);
return reset($grades->items)->grades ?? [];
}
/**
* Delete grade item for given coursework
*
* @param coursework $coursework object
* @return int
*/
function coursework_grade_item_delete(coursework $coursework) {
global $CFG;
require_once($CFG->dirroot.'/lib/gradelib.php');
return grade_update('mod/coursework', $coursework->get_course_id(), 'mod', 'coursework',
$coursework->id, 0, null, ['deleted' => 1]);
}
/**
* 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 $coursework An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function coursework_update_instance($coursework) {
global $DB, $USER;
$coursework->timemodified = time();
$coursework->id = $coursework->instance;
if ($coursework->finalstagegrading == 1) {
$coursework->automaticagreementstrategy = 'none';
$coursework->automaticagreementrange = 10;
}
$subnotify = '';
$comma = '';
if (!empty($coursework->submissionnotification)) {
foreach ($coursework->submissionnotification as $uid) {
$subnotify .= $comma . $uid;
$comma = ',';
}
}
$coursework->submissionnotification = $subnotify;
$courseworkhassubmissions = $DB->record_exists('coursework_submissions', ['courseworkid' => $coursework->id]);
// If the coursework has submissions then we the renamefiles setting can't be changes
if ($courseworkhassubmissions) {
$currentcoursework = $DB->get_record('coursework', ['id' => $coursework->id]);
$coursework->renamefiles = $currentcoursework->renamefiles;
} else if ($coursework->blindmarking == 1) {
$coursework->renamefiles = 1;
}
$oldsubmissiondeadline = $DB->get_field('coursework', 'deadline', ['id' => $coursework->id]);
$oldgeneraldeadline = $DB->get_field('coursework', 'generalfeedback', ['id' => $coursework->id]);
$oldindividualdeadline = $DB->get_field('coursework', 'individualfeedback', ['id' => $coursework->id]);
if ($oldsubmissiondeadline != $coursework->deadline ||
$oldgeneraldeadline != $coursework->generalfeedback ||
$oldindividualdeadline != $coursework->individualfeedback) {
// Fire an event to send emails to students affected by any deadline change.
$courseworkobj = coursework::find($coursework->id);
$params = [
'context' => context_module::instance($courseworkobj->get_course_module()->id),
'courseid' => $courseworkobj->get_course()->id,
'objectid' => $coursework->id,
'other' => [
'courseworkid' => $coursework->id,
'oldsubmissiondeadline' => $oldsubmissiondeadline,
'newsubmissionsdeadline' => $coursework->deadline,
'oldgeneraldeadline' => $oldgeneraldeadline,
'newgeneraldeadline' => $coursework->generalfeedback,
'oldindividualdeadline' => $oldindividualdeadline,
'newindividualdeadline' => $coursework->individualfeedback,
'userfrom' => $USER->id,
],
];
$event = \mod_coursework\event\coursework_deadline_changed::create($params);
$event->trigger();
}
// Update event for calendar(cw name/deadline) if a coursework has a deadline
if ($coursework->deadline) {
coursework_update_events($coursework, 'due'); // Cw deadline
if ($coursework->initialmarkingdeadline) {
// Update
coursework_update_events($coursework, 'initialgradingdue'); // Cw initial grading deadine
} else {
// Remove it
\mod_coursework\calendar::remove_event($coursework, 'initialgradingdue');
}
if ($coursework->agreedgrademarkingdeadline && $coursework->numberofmarkers > 1) {
// Update
coursework_update_events($coursework, 'agreedgradingdue'); // Cw agreed grade deadine
} else {
// Remove it
\mod_coursework\calendar::remove_event($coursework, 'agreedgradingdue' );
}
} else {
// Remove all deadline events for this coursework regardless the type
\mod_coursework\calendar::remove_event($coursework);
}
return $DB->update_record('coursework', $coursework);
}
/**
* Update coursework deadline and name in the event table
*
* @param $coursework
* @param $eventtype
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function coursework_update_events($coursework, $eventtype) {
global $DB;
$event = "";
$eventid = $DB->get_record('event', ['modulename' => 'coursework', 'instance' => $coursework->id, 'eventtype' => $eventtype]);
if ($eventid) {
$event = calendar_event::load($eventid->id);
}
// Update/create event for coursework deadline [due]
if ($eventtype == 'due') {
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->deadline);
if ($event) {
$event->update($data); // Update if event exists
} else {
calendar_event::create($data); // Create new event as it doesn't exist
}
}
// Update/create event for coursework initialmarking deadline [initialgradingdue]
if ($eventtype == 'initialgradingdue') {
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->initialmarkingdeadline);
if ($event) {
$event->update($data); // Update if event exists
} else {
calendar_event::create($data); // Create new event as it doesn't exist
}
}
// Update/create event for coursework agreedgrademarking deadline [agreedgradingdue]
if ($eventtype == 'agreedgradingdue') {
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->agreedgrademarkingdeadline);
if ($event) {
$event->update($data); // Update if event exists
} else {
calendar_event::create($data); // Create new event as it doesn't exist
}
}
}
/**
* 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 coursework_delete_instance($id) {
global $DB;
if (!$coursework = $DB->get_record('coursework', ['id' => $id])) {
return false;
}
// Delete any dependent records here.
// TODO delete feedbacks.
// TODO delete allocations.
// TODO delete submissions.
$DB->delete_records('coursework', ['id' => $coursework->id]);
return true;
}
/**
* @return array
*/
function coursework_get_view_actions() {
return ['view'];
}
/**
* @return array
*/
function coursework_get_post_actions() {
return ['upload'];
}
/**
* 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
*
* @param $course
* @param $user
* @param $mod
* @param $coursework
* @return null
* @todo Finish documenting this function
*/
function coursework_user_outline($course, $user, $mod, $coursework) {
$return = new stdClass;
$return->time = 0;
$return->info = '';
return $return;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param $course
* @param $user
* @param $mod
* @param $coursework
* @return boolean
* @todo Finish documenting this function
*/
function coursework_user_complete($course, $user, $mod, $coursework) {
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in coursework activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param $course
* @param $viewfullnames
* @param $timestart
* @return boolean
* @todo Finish documenting this function
*/
function coursework_print_recent_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Must return an array of users who are participants for a given instance
* of coursework. Must include every user involved in the instance,
* independent of his role (student, teacher, admin...). The returned
* objects must contain at least id property.
* See other modules as example.
*
* @todo make this work.
*
* @param int $courseworkid ID of an instance of this module
* @return boolean|array false if no participants, array of objects otherwise
*/
function coursework_get_participants($courseworkid) {
return false;
}
/**
* This function returns if a scale is being used by one coursework
* if it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* as reference.
*
* @param int $courseworkid ID of an instance of this module
* @param $scaleid
* @return bool
*/
function coursework_scale_used($courseworkid, $scaleid) {
global $DB;
$params = ['grade' => $scaleid,
'id' => $courseworkid];
if ($scaleid && $DB->record_exists('coursework', $params)) {
return true;
} else {
return false;
}
}
/**
* Checks if scale is being used by any instance of coursework.
* 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 coursework
*/
function coursework_scale_used_anywhere($scaleid) {
global $DB;
if ($scaleid && $DB->record_exists('coursework', ['grade' => $scaleid])) {
return true;
} else {
return false;
}
}
/**
* Returns all other caps used in module
* @return array
*/
function coursework_get_extra_capabilities() {
return ['moodle/site:accessallgroups',
'moodle/site:viewfullnames'];
}
/**
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function coursework_supports($feature) {
switch ($feature) {
case FEATURE_ADVANCED_GRADING:
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_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
default:
return null;
}
}
/**
* Returns submission details for a plagiarism file submission.
*
* @param int $cmid
* @return array
*/
function coursework_plagiarism_dates($cmid) {
$cm = get_coursemodule_from_id('coursework', $cmid);
$coursework = coursework::find($cm->instance);
$datesarray = ['timeavailable' => $coursework->timecreated];
$datesarray['timedue'] = $coursework->deadline;
$datesarray['feedback'] = (string)$coursework->get_individual_feedback_deadline();
return $datesarray;
}
/**
* Extend the navigation settings for each individual coursework to allow markers to be allocated, etc.
*
* @param settings_navigation $settings
* @param navigation_node $navref
* @return void
*/
function coursework_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
global $PAGE;
$cm = $PAGE->cm;
if (!$cm) {
return;
}
$context = $PAGE->context;
$course = $PAGE->course;
$coursework = coursework::find($cm->instance);
if (!$course) {
return;
}
// Link to marker allocation screen. No point showing it if we are not using allocation or moderation.
if (has_capability('mod/coursework:allocate', $context) &&
($coursework->allocation_enabled() || $coursework->sampling_enabled())) {
$link = new moodle_url('/mod/coursework/actions/allocate.php', ['id' => $cm->id]);
$langstr = ($coursework->moderation_agreement_enabled()) ? 'allocateassessorsandmoderators' : 'allocateassessors';
$navref->add(get_string($langstr, 'mod_coursework'), $link, navigation_node::TYPE_SETTING);
}
// Link to personal deadlines screen
if (has_capability('mod/coursework:editpersonaldeadline', $context) && ($coursework->personal_deadlines_enabled())) {
$link = new moodle_url('/mod/coursework/actions/set_personal_deadlines.php', ['id' => $cm->id]);
$navref->add(get_string('setpersonaldeadlines', 'mod_coursework'), $link, navigation_node::TYPE_SETTING);
}
}
/**
* Auto-allocates after a new student or teacher is added to a coursework.
*
* @param $roleassignment - record from role_assignments table
* @return bool
*/
function coursework_role_assigned_event_handler($roleassignment) {
global $DB;
// return true; // Until we fix the auto allocator. The stuff below causes an infinite loop.
$courseworkids = coursework_get_coursework_ids_from_context_id($roleassignment->contextid);
foreach ($courseworkids as $courseworkid) {
$DB->set_field('coursework', 'processenrol', 1, ['id' => $courseworkid]);
}
return true;
}
/**
* Auto allocates when a student or teacher leaves.
*
* @param $roleassignment
* @throws coding_exception
* @return bool
*/
function coursework_role_unassigned_event_handler($roleassignment) {
global $DB;
$courseworkids = coursework_get_coursework_ids_from_context_id($roleassignment->contextid);
foreach ($courseworkids as $courseworkid) {
$DB->set_field('coursework', 'processunenrol', 1, ['id' => $courseworkid]);
}
return true;
}
/**
* Role may be assigned at course or coursemodule level. This gives us an array of relevant coursework
* ids to loop through so we can re-allocate.
*
* @param $contextid
* @return array
*/
function coursework_get_coursework_ids_from_context_id($contextid) {
global $DB;
$courseworkids = [];
// Is this a coursework?
$context = context::instance_by_id($contextid);
switch ($context->contextlevel) {
case CONTEXT_MODULE:
$coursemodule = get_coursemodule_from_id('coursework', $context->instanceid);
$courseworkmoduleid = $DB->get_field('modules', 'id', ['name' => 'coursework']);
if ($coursemodule && $coursemodule->module == $courseworkmoduleid) {
$courseworkids[] = $coursemodule->instance;
}
break;
case CONTEXT_COURSE:
$coursemodules = $DB->get_records('coursework', ['course' => $context->instanceid]);
if ($coursemodules) {
$courseworkids = array_keys($coursemodules);
}
break;
}
return $courseworkids;
}
/**
* Makes a number of seconds into a human readable string, like '3 days'.
*
* @param int $seconds
* @return string
*/
function coursework_seconds_to_string($seconds) {
$units = [
604800 => [get_string('week', 'mod_coursework'),
get_string('weeks', 'mod_coursework')],
86400 => [get_string('day', 'mod_coursework'),
get_string('days', 'mod_coursework')],
3600 => [get_string('hour', 'mod_coursework'),
get_string('hours', 'mod_coursework')],
60 => [get_string('minute', 'mod_coursework'),
get_string('minutes', 'mod_coursework')],
1 => [get_string('second', 'mod_coursework'),
get_string('seconds', 'mod_coursework')],
];
$result = [];
foreach ($units as $divisor => $unitame) {
$units = intval($seconds / $divisor);
if ($units) {
$seconds %= $divisor;
$name = $units == 1 ? $unitame[0] : $unitame[1];
$result[] = "$units $name";
}
}
return implode(', ', $result);
}
/**
* Checks the DB to see how many feedbacks we already have. This is so we can stop people from setting the
* number of markers lower than that in the mod form.
*
* @param int $courseworkid
* @return int
*/
function coursework_get_current_max_feedbacks($courseworkid) {