-
Notifications
You must be signed in to change notification settings - Fork 65
/
turnitintooltwo_assignment.class.php
executable file
·2033 lines (1754 loc) · 89.4 KB
/
turnitintooltwo_assignment.class.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 turnitintooltwo
* @copyright 2012 iParadigms LLC *
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__."/lib.php");
require_once(__DIR__.'/turnitintooltwo_comms.class.php');
require_once(__DIR__.'/turnitintooltwo_user.class.php');
require_once(__DIR__.'/turnitintooltwo_submission.class.php');
class turnitintooltwo_assignment {
private $timecreated;
private $id;
private $type;
public $turnitintooltwo;
public function __construct($id = 0, $turnitintooltwo = '', $type = 'TT') {
global $DB;
$this->id = $id;
$this->type = $type;
if (($type == 'TT') || ($type == 'V1')) {
if (!empty($turnitintooltwo)) {
$this->turnitintooltwo = $turnitintooltwo;
} else {
$this->turnitintooltwo = $DB->get_record("turnitintooltwo", array("id" => $id));
// Get part variables if data not passed in (used in restore).
$parts = $this->get_parts();
$i = 0;
foreach ($parts as $part) {
$i++;
$attribute = "maxmarks".$i;
$this->turnitintooltwo->$attribute = $part->maxmarks;
$attribute = "dtstart".$i;
$this->turnitintooltwo->$attribute = $part->dtstart;
$attribute = "dtdue".$i;
$this->turnitintooltwo->$attribute = $part->dtdue;
$attribute = "dtpost".$i;
$this->turnitintooltwo->$attribute = $part->dtpost;
$attribute = "partname".$i;
$this->turnitintooltwo->$attribute = $part->partname;
}
}
}
}
/**
* Get all the assignments in the course
*
* @param object $course
* @return array instances of each assignment
*/
public static function get_all_assignments_in_course($course) {
if (!$turnitintooltwos = get_all_instances_in_course("turnitintooltwo", $course)) {
turnitintooltwo_print_error('noturnitinassignemnts', 'turnitintooltwo', null, null, __FILE__, __LINE__);
exit;
}
return $turnitintooltwos;
}
/**
* Add a Moodle Tutor to the class in Turnitin
*
* @param int $tutorid
* @return string $notice to display to user whether mehtod has been successful or failed
*/
public function add_tii_tutor($tutorid) {
// Get Course data.
$coursetype = turnitintooltwo_get_course_type($this->turnitintooltwo->legacy);
$course = $this->get_course_data($this->turnitintooltwo->course, $coursetype);
$user = new turnitintooltwo_user($tutorid, 'Instructor');
$notice = array();
if ($user->join_user_to_class($course->turnitin_cid)) {
$notice = get_string('tutoradded', 'turnitintooltwo');
} else {
$notice = get_string('tutoraddingerror', 'turnitintooltwo');
}
return $notice;
}
/**
* Remove a user from the specified role in a class in Turnitin
*
* @param int $membershipid id that links user to the class
* @param string $role the user has in the class
* @return string to display to user whether method has been successful or failed
*/
public function remove_tii_user_by_role($membershipid, $role = "Learner") {
if (turnitintooltwo_user::remove_user_from_class($membershipid)) {
$notice = ($role == "Learner") ? 'studentremoved' : 'tutorremoved';
} else {
$notice = ($role == "Learner") ? 'studentremovingerror' : 'tutorremovingerror';
}
return get_string($notice, 'turnitintooltwo');
}
/**
* Returns the Turnitin owner of the current class
*
* @global object
* @param int $courseid The ID of the course to check the owner of
* @param string $coursetype whether the course is TT (Turnitintool) or PP (Plagiarism Plugin)
* @return object The user object for the Turnitin Class Owner or null if there is no owner stored
*/
private function get_tii_owner($courseid, $coursetype = "TT") {
global $DB;
if ($course = $DB->get_record('turnitintooltwo_courses', array("courseid" => $courseid, "course_type" => $coursetype))) {
return $course->ownerid;
} else {
return null;
}
}
/**
* Find the course data, including Turnitin id
*
* @param int $courseid The ID of the course to get the data for
* @param string $coursetype whether the course is TT (Turnitintool) or PP (Plagiarism Plugin)
* @param string $workflowcontext whether we are in a cron context (from PP) or using the site as normal.
* @return object The course object with the Turnitin Class data if it's been created
*/
public static function get_course_data($courseid, $coursetype = "TT", $workflowcontext = "site") {
global $DB;
if (!$course = $DB->get_record("course", array("id" => $courseid))) {
if ($workflowcontext != "cron") {
turnitintooltwo_print_error('coursegeterror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
exit;
}
}
$course->turnitin_cid = 0;
$course->turnitin_ctl = "";
$course->course_type = $coursetype;
$course->tii_rel_id = '';
if ($turnitincourse = $DB->get_record('turnitintooltwo_courses',
array("courseid" => $courseid, "course_type" => $coursetype))) {
$course->turnitin_cid = $turnitincourse->turnitin_cid;
$course->turnitin_ctl = $turnitincourse->turnitin_ctl;
$course->course_type = $turnitincourse->course_type;
$course->ownerid = $turnitincourse->ownerid;
$course->tii_rel_id = $turnitincourse->id;
}
return $course;
}
/**
* Create a migrated turnitin assignment in Moodle
*
* @global type $DB
* @global type $CFG
* @param array $partids the ids of turnitin assignment to create as parts of new assignment
* @param int $courseid
* @param string $assignmentname
* @return boolean false if failed
*/
public static function create_migration_assignment($partids, $courseid, $assignmentname) {
global $DB, $CFG, $OUTPUT;
$config = turnitintooltwo_admin_config();
$partids = (array)$partids;
$tempassignment = new turnitintooltwo_assignment(0, '', 'M');
$newassignment = $tempassignment->update_assignment_from_tii($partids);
$newassignment["turnitintooltwo"]->course = $courseid;
$newassignment["turnitintooltwo"]->name = $assignmentname;
$newassignment["turnitintooltwo"]->numparts = count($partids);
$newassignment["turnitintooltwo"]->gradedisplay = $config->default_gradedisplay;
$newassignment["turnitintooltwo"]->shownonsubmission = 1;
$newassignment["turnitintooltwo"]->usegrademark = $config->usegrademark;
// Get maximum grade.
$newassignment["turnitintooltwo"]->grade = 0;
foreach ($newassignment["parts"] as $part) {
if ($newassignment["turnitintooltwo"]->grade < $part->maxmarks) {
$newassignment["turnitintooltwo"]->grade = $part->maxmarks;
}
}
$turnitintooltwoassignment = new turnitintooltwo_assignment(0, $newassignment["turnitintooltwo"]);
if (!$toolid = $DB->insert_record("turnitintooltwo", $turnitintooltwoassignment->turnitintooltwo)) {
turnitintooltwo_activitylog(get_string('migrationassignmentcreationerror', 'turnitintooltwo', $courseid), "REQUEST");
return false;
} else {
turnitintooltwo_activitylog(get_string('migrationassignmentcreated', 'turnitintooltwo', $toolid), "REQUEST");
}
$module = $DB->get_record("modules", array("name" => "turnitintooltwo"));
$coursemodule = new stdClass();
$coursemodule->course = $courseid;
$coursemodule->module = $module->id;
$coursemodule->added = time();
$coursemodule->instance = $toolid;
$coursemodule->section = 0;
include_once($CFG->dirroot."/course/lib.php");
// Add Course module and get course section.
if (! $coursemodule->coursemodule = add_course_module($coursemodule) ) {
echo $OUTPUT->notification(get_string('migrationassignmenterror1', 'turnitintooltwo', $courseid));
turnitintooltwo_activitylog(get_string('migrationassignmenterror1', 'turnitintooltwo', $courseid), "REQUEST");
return false;
}
if (is_callable('course_add_cm_to_section')) {
if (!$sectionid = course_add_cm_to_section($coursemodule->course,
$coursemodule->coursemodule, $coursemodule->section)) {
echo $OUTPUT->notification(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid));
turnitintooltwo_activitylog(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid), "REQUEST");
return false;
}
} else {
if (!$sectionid = add_mod_to_section($coursemodule)) {
echo $OUTPUT->notification(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid));
turnitintooltwo_activitylog(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid), "REQUEST");
return false;
}
}
$DB->set_field("course_modules", "section", $sectionid, array("id" => $coursemodule->coursemodule));
rebuild_course_cache($courseid);
foreach ($newassignment["parts"] as $part) {
$part->turnitintooltwoid = $toolid;
$part->deleted = 0;
$part->migrated = -1;
if ($part->id = $DB->insert_record("turnitintooltwo_parts", $part)) {
turnitintooltwo_activitylog(get_string('migrationassignmentpartcreated', 'turnitintooltwo', $part->id), "REQUEST");
}
if ($turnitintooltwoassignment->create_event($toolid, $part->partname, $part->dtdue)) {
$part->migrated = 1;
$DB->update_record("turnitintooltwo_parts", $part);
} else {
echo $OUTPUT->notification(get_string('migrationassignmenterror3', 'turnitintooltwo', $courseid));
turnitintooltwo_activitylog(get_string('migrationassignmenterror3', 'turnitintooltwo', $courseid), "REQUEST");
}
}
}
/**
* Create a course in Moodle (Migration)
*
* @global type $DB
* @global type $CFG
* @global type $USER
* @param int $tiicourseid The course id on Turnitin
* @param string $tiicoursetitle The course title on Turnitin
* @param string $coursename The new course name on Moodle
* @param int $coursecategory The category that the course is to be created in
* @return mixed course object if created or 0 if failed
*/
public static function create_moodle_course($tiicourseid, $tiicoursetitle, $coursename, $coursecategory) {
global $DB, $CFG, $USER;
require_once($CFG->dirroot."/course/lib.php");
$data = new stdClass();
$data->category = $coursecategory;
$data->fullname = $coursename;
$data->shortname = "Turnitin (".$tiicourseid.")";
$data->maxbytes = 2097152;
if ($course = create_course($data)) {
$turnitincourse = new stdClass();
$turnitincourse->courseid = $course->id;
$turnitincourse->turnitin_cid = $tiicourseid;
$turnitincourse->turnitin_ctl = $tiicoursetitle;
$turnitincourse->ownerid = $USER->id;
$turnitincourse->course_type = 'TT';
// Enrol user as instructor on course in moodle if they are not a site admin.
if (!is_siteadmin()) {
// Get the role id for a teacher.
$roles1 = get_roles_with_capability('mod/turnitintooltwo:grade');
$roles2 = get_roles_with_capability('mod/turnitintooltwo:addinstance');
$roles = array_intersect_key($roles1, $roles2);
$role = current($roles);
// Enrol $USER in the courses using the manual plugin.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance, $USER->id, $role->id);
} else {
// Enrol admin as an instructor incase they are not on the account.
$turnitintooltwouser = new turnitintooltwo_user($USER->id, "Instructor");
$turnitintooltwouser->join_user_to_class($tiicourseid);
}
if (!$insertid = $DB->insert_record('turnitintooltwo_courses', $turnitincourse)) {
turnitintooltwo_activitylog(get_string('migrationcoursecreatederror', 'turnitintooltwo',
$tiicourseid).' - '.$course->id, "REQUEST");
return 0;
} else {
turnitintooltwo_activitylog(get_string('migrationcoursecreated', 'turnitintooltwo').' - '.
$course->id.' ('.$tiicourseid.')', "REQUEST");
return $course;
}
} else {
turnitintooltwo_activitylog(get_string('migrationcoursecreateerror', 'turnitintooltwo', $tiicourseid), "REQUEST");
return 0;
}
}
/**
* Create the course in Turnitin
*
* @global type $DB
* @param object $course The course object
* @param int $ownerid The owner of the course
* @return object the turnitin course if created
*/
public function create_tii_course($course, $ownerid) {
global $DB;
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$class = new TiiClass();
$tiititle = $this->truncate_title( $course->fullname, TURNITIN_COURSE_TITLE_LIMIT );
$class->setTitle( $tiititle );
try {
$response = $turnitincall->createClass($class);
$newclass = $response->getClass();
$turnitincourse = new stdClass();
$turnitincourse->courseid = $course->id;
$turnitincourse->ownerid = $ownerid;
$turnitincourse->turnitin_cid = $newclass->getClassId();
$turnitincourse->turnitin_ctl = $course->fullname . " (Moodle TT)";
$turnitincourse->course_type = "TT";
if (empty($course->tii_rel_id)) {
$method = "insert_record";
} else {
$method = "update_record";
$turnitincourse->id = $course->tii_rel_id;
}
if (!$insertid = $DB->$method('turnitintooltwo_courses', $turnitincourse)) {
turnitintooltwo_print_error('classupdateerror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
exit();
}
if (empty($turnitincourse->id)) {
$turnitincourse->id = $insertid;
}
$coursetype = "TT";
$workflowcontext = "site";
turnitintooltwo_activitylog("Class created - ".$turnitincourse->courseid." | ".$turnitincourse->turnitin_cid.
" | ".$course->fullname . " (Moodle TT)" , "REQUEST");
return $turnitincourse;
} catch (Exception $e) {
$toscreen = true;
if ($workflowcontext == "cron") {
mtrace(get_string('pp_classcreationerror', 'turnitintooltwo'));
$toscreen = false;
}
$turnitincomms->handle_exceptions($e, 'classcreationerror', $toscreen);
}
}
/**
* Edit the course title in Turnitin
*
* @global type $DB
* @param var $course The course object
* @param string $coursetype whether the course is TT (Turnitintool) or PP (Plagiarism Plugin)
*/
public function edit_tii_course($course, $coursetype = "TT") {
global $DB;
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$class = new TiiClass();
$class->setClassId($course->turnitin_cid);
$title = $this->truncate_title( $course->fullname, TURNITIN_COURSE_TITLE_LIMIT, $coursetype );
$class->setTitle( $title );
// If a course end date is specified in Moodle then we set this in Turnitin with an additional month to
// account for the Turnitin viewer becoming read-only once the class end date passes.
if (!empty($course->enddate)) {
// The course end date must not be before the start date.
// Change the course end date if it is set earlier than today.
if ($course->enddate < strtotime('today')) {
$course->enddate = strtotime('today');
}
$enddate = strtotime('+1 month', $course->enddate);
$class->setEndDate(gmdate("Y-m-d\TH:i:s\Z", $enddate));
}
try {
$turnitincall->updateClass($class);
$turnitincourse = new stdClass();
$turnitintooltwocourse = $DB->get_record("turnitintooltwo_courses",
array("courseid" => $course->id, "course_type" => $coursetype));
$turnitincourse->id = $turnitintooltwocourse->id;
$turnitincourse->courseid = $course->id;
$turnitincourse->ownerid = $turnitintooltwocourse->ownerid;
$turnitincourse->turnitin_cid = $course->turnitin_cid;
$turnitincourse->turnitin_ctl = $course->fullname . " (Moodle ".$coursetype.")";
$turnitincourse->course_type = $coursetype;
if (!$insertid = $DB->update_record('turnitintooltwo_courses', $turnitincourse) && $coursetype != "PP") {
turnitintooltwo_print_error('classupdateerror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
exit();
} else {
turnitintooltwo_activitylog("Class edited - ".$turnitincourse->turnitin_ctl.
" (".$turnitincourse->id.")", "REQUEST");
}
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'classupdateerror');
}
}
/**
* Truncate the course and assignment titles to match Turnitin requirements and add a coursetype suffix on the end.
*
* @param string $title The course id on Turnitin
* @param int $limit The course title on Turnitin
* @param string $coursetype whether the course is TT (Turnitintooltwo) or PP (Plagiarism Plugin)
*/
public static function truncate_title($title, $limit, $coursetype = 'TT') {
$suffix = " (Moodle " . $coursetype . ")";
$limit = $limit - strlen($suffix);
$truncatedtitle = "";
if ( mb_strlen( $title, 'UTF-8' ) > $limit ) {
$truncatedtitle .= mb_substr( $title, 0, $limit - 3, 'UTF-8' ) . "...";
} else {
$truncatedtitle .= $title;
}
$truncatedtitle .= $suffix;
return $truncatedtitle;
}
/**
* Edit the course end date in Turnitin
*
* @global type $DB
* @param int $tiicourseid The course id on Turnitin
* @param int $tiicoursetitle The course title on Turnitin
* @param date $courseenddate The new course end date to be set on Turnitin
*/
public static function edit_tii_course_end_date($tiicourseid, $tiicoursetitle, $courseenddate) {
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$class = new TiiClass();
$class->setTitle($tiicoursetitle);
$class->setClassId($tiicourseid);
$class->setEndDate(gmdate("Y-m-d\TH:i:s\Z", $courseenddate));
try {
$turnitincall->updateClass($class);
return true;
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'classupdateerror');
}
}
/**
* Get the users from Turnitin for the specified role, gets the memberships
* and loops through them to get each user
*
* @param string $role the user has in the class
* @param string $idkey whether to use moodle or turnitin ids as the array key
* @return array $users
*/
public function get_tii_users_by_role($role = "Learner", $idkey = "tii") {
global $DB;
$users = array();
// Get Moodle Course Object.
$coursetype = turnitintooltwo_get_course_type($this->turnitintooltwo->legacy);
$course = $this->get_course_data($this->turnitintooltwo->course, $coursetype);
$classmembers = $this->get_class_memberships($course->turnitin_cid);
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$membership = new TiiMembership();
$membership->setMembershipIds($classmembers);
// Get Enrolled users from Turnitin.
try {
$response = $turnitincall->readMemberships($membership);
$readmemberships = $response->getMemberships();
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'membercheckerror');
}
// Add each user to an array.
foreach ($readmemberships as $readmembership) {
if ($readmembership->getRole() == $role) {
// Check user is a Moodle user. Otherwise we have to go to Turnitin for their name.
$moodleuserid = turnitintooltwo_user::get_moodle_user_id($readmembership->getUserId());
if (!empty($moodleuserid)) {
$user = $DB->get_record('user', array('id' => $moodleuserid));
$arraykey = ($idkey == "mdl") ? $user->id : $readmembership->getUserId();
$users[$arraykey]["firstname"] = $user->firstname;
$users[$arraykey]["lastname"] = $user->lastname;
$users[$arraykey]["membership_id"] = $readmembership->getMembershipId();
} else {
$user = new TiiUser();
$user->setUserId($readmembership->getUserId());
try {
$response = $turnitincall->readUser($user);
$readuser = $response->getUser();
$users[$readmembership->getUserId()]["firstname"] = $readuser->getFirstName();
$users[$readmembership->getUserId()]["lastname"] = $readuser->getLastName();
$users[$readmembership->getUserId()]["membership_id"] = $readmembership->getMembershipId();
} catch (Exception $e) {
// A read user exception occurs when users are inactive. Re-enrol user to make them active.
$membership = new TiiMembership();
$membership->setClassId($course->turnitin_cid);
$membership->setUserId($readmembership->getUserId());
$membership->setRole($role);
try {
$turnitincall->createMembership($membership);
} catch ( InnerException $e ) {
$turnitincomms->handle_exceptions($e, 'userjoinerror');
}
try {
$user = new TiiUser();
$user->setUserId($readmembership->getUserId());
$response = $turnitincall->readUser($user);
$readuser = $response->getUser();
$users[$readmembership->getUserId()]["firstname"] = $readuser->getFirstName();
$users[$readmembership->getUserId()]["lastname"] = $readuser->getLastName();
$users[$readmembership->getUserId()]["membership_id"] = $readmembership->getMembershipId();
} catch ( InnerException $e ) {
$turnitincomms->handle_exceptions($e, 'tiiusergeterror');
}
}
}
}
}
return $users;
}
/**
* Enrol All students on a course, checks to see if all the Moodle students
* are already enrolled and then enrols the students who aren't
*
* @param object $cm the course module
* @return boolean
*/
public function enrol_all_students($cm) {
// Get Moodle Course Object.
$coursetype = turnitintooltwo_get_course_type($this->turnitintooltwo->legacy);
$course = $this->get_course_data($this->turnitintooltwo->course, $coursetype);
$context = context_module::instance($cm->id);
// Get local course members.
$students = get_enrolled_users($context,
'mod/turnitintooltwo:submit', groups_get_activity_group($cm), 'u.id');
// Get the user ids of who is already enrolled and remove them from the students array.
$tiiclassmemberships = $this->get_class_memberships($course->turnitin_cid);
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$membership = new TiiMembership();
$membership->setMembershipIds($tiiclassmemberships);
try {
$response = $turnitincall->readMemberships($membership);
$readmemberships = $response->getMemberships();
foreach ($readmemberships as $readmembership) {
if ($readmembership->getRole() == "Learner") {
$moodleuserid = turnitintooltwo_user::get_moodle_user_id($readmembership->getUserId());
unset($students[$moodleuserid]);
}
}
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'membercheckerror');
}
// Get the suspended users.
$suspendedusers = get_suspended_userids($context, true);
// Enrol remaining unenrolled users to the course.
$members = array_keys($students);
foreach ($members as $member) {
// Don't include user if they are suspended.
$user = new turnitintooltwo_user($member, "Learner");
if (isset($suspendedusers[$user->id])) {
continue;
}
$user->join_user_to_class($course->turnitin_cid);
}
return true;
}
/**
* Get all the membership ids for a particular class
*
* @param int $tiicourseid the course id on Turnitin
* @return array $tiiclassmembers all the membership Ids for the class if successful
*/
private function get_class_memberships($tiicourseid) {
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
$membership = new TiiMembership();
$membership->setClassId($tiicourseid);
try {
$response = $turnitincall->findMemberships($membership);
$findmembership = $response->getMembership();
$tiiclassmembers = $findmembership->getMembershipIds();
return $tiiclassmembers;
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'membercheckerror');
}
}
/**
* Create assignment on Moodle, creates each individual part (and the relevant event)
* and in turn creates them on Turnitin
*
* @global type $USER
* @global type $DB
* @return int turnitintooltwo id
*/
public function create_moodle_assignment() {
global $USER, $DB;
$config = turnitintooltwo_admin_config();
// Get Moodle Course Object.
$course = $this->get_course_data($this->turnitintooltwo->course);
// If PHP UNIT tests are running and account/secretkey/apiurl are empty, just create basic object and return.
if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST) &&
(empty($config->accountid) || empty($config->secretkey) || empty($config->apiurl))) {
$turnitintooltwo = new stdClass();
$turnitintooltwo->timecreated = time();
$turnitintooltwo->timemodified = time();
$turnitintooltwo->course = $course->id;
$turnitintooltwo->name = "test V2";
$turnitintooltwo->dateformat = "d/m/Y";
$turnitintooltwo->usegrademark = 0;
$turnitintooltwo->gradedisplay = 0;
$turnitintooltwo->autoupdates = 0;
$turnitintooltwo->commentedittime = 0;
$turnitintooltwo->commentmaxsize = 0;
$turnitintooltwo->autosubmission = 0;
$turnitintooltwo->shownonsubmission = 0;
$turnitintooltwo->studentreports = 1;
$turnitintooltwo->grade = 0;
$turnitintooltwo->numparts = 1;
$turnitintooltwo->anon = 0;
$turnitintooltwo->allowlate = 0;
$turnitintooltwo->legacy = 0;
$turnitintooltwo->id = $DB->insert_record("turnitintooltwo", $turnitintooltwo);
return $turnitintooltwo->id;
}
// Get the Turnitin owner of this this Course or make user the owner if none.
$ownerid = $this->get_tii_owner($course->id);
if (!empty($ownerid)) {
$owner = new turnitintooltwo_user($ownerid, 'Instructor');
} else {
$owner = new turnitintooltwo_user($USER->id, 'Instructor');
}
// Setup or edit course in Turnitin.
if ($course->turnitin_cid == 0) {
$tiicoursedata = $this->create_tii_course($course, $owner->id);
$course->turnitin_cid = $tiicoursedata->turnitin_cid;
$course->turnitin_ctl = $tiicoursedata->turnitin_ctl;
} else {
$this->edit_tii_course($course);
$course->turnitin_ctl = $course->fullname . " (Moodle TT)";
}
$owner->join_user_to_class($course->turnitin_cid);
// Insert the default options for the assignment.
$this->turnitintooltwo->timecreated = time();
$this->turnitintooltwo->dateformat = "d/m/Y";
$this->turnitintooltwo->commentedittime = 1800;
$this->turnitintooltwo->commentmaxsize = 800;
$this->turnitintooltwo->autosubmission = 1;
$this->turnitintooltwo->gradedisplay = 2;
$this->turnitintooltwo->shownonsubmission = 1;
$this->turnitintooltwo->timemodified = time();
$this->turnitintooltwo->courseid = $course->id;
$this->turnitintooltwo->usegrademark = $config->usegrademark;
$toolid = $DB->insert_record("turnitintooltwo", $this->turnitintooltwo);
turnitintooltwo_activitylog("Turnitintool created (".$toolid.") - ".$this->turnitintooltwo->name , "REQUEST");
// Do the multiple Assignment creation on turnitin for each part.
for ($i = 1; $i <= $this->turnitintooltwo->numparts; $i++) {
// Set the assignment details to pass to the API.
$assignment = new TiiAssignment();
$assignment->setClassId($course->turnitin_cid);
$attribute = "partname".$i;
$tiititle = $this->turnitintooltwo->name." ".$this->turnitintooltwo->$attribute;
$tiititle = $this->truncate_title( $tiititle, TURNITIN_ASSIGNMENT_TITLE_LIMIT, 'TT' );
$assignment->setTitle( $tiititle );
$attribute = "dtstart".$i;
$assignment->setStartDate(gmdate("Y-m-d\TH:i:s\Z", $this->turnitintooltwo->$attribute));
$attribute = "dtdue".$i;
$assignment->setDueDate(gmdate("Y-m-d\TH:i:s\Z", $this->turnitintooltwo->$attribute));
$attribute = "dtpost".$i;
$assignment->setFeedbackReleaseDate(gmdate("Y-m-d\TH:i:s\Z", $this->turnitintooltwo->$attribute));
$assignment->setInstructions(strip_tags($this->turnitintooltwo->intro));
$assignment->setAuthorOriginalityAccess($this->turnitintooltwo->studentreports);
$assignment->setRubricId((!empty($this->turnitintooltwo->rubric)) ? $this->turnitintooltwo->rubric : '');
$assignment->setSubmitPapersTo($this->turnitintooltwo->submitpapersto);
$assignment->setResubmissionRule($this->turnitintooltwo->reportgenspeed);
$assignment->setBibliographyExcluded($this->turnitintooltwo->excludebiblio);
$assignment->setQuotedExcluded($this->turnitintooltwo->excludequoted);
$assignment->setSmallMatchExclusionType($this->turnitintooltwo->excludetype);
$assignment->setSmallMatchExclusionThreshold((int)$this->turnitintooltwo->excludevalue);
if ($config->useanon) {
$assignment->setAnonymousMarking($this->turnitintooltwo->anon);
}
$assignment->setAllowNonOrSubmissions($this->turnitintooltwo->allownonor);
$assignment->setLateSubmissionsAllowed($this->turnitintooltwo->allowlate);
if ($config->repositoryoption == ADMIN_REPOSITORY_OPTION_EXPANDED ||
$config->repositoryoption == ADMIN_REPOSITORY_OPTION_FORCE_INSTITUTIONAL) {
$institutioncheck = (isset($this->turnitintooltwo->institution_check)) ? $this->turnitintooltwo->institution_check : 0;
$assignment->setInstitutionCheck($institutioncheck);
}
$attribute = "maxmarks".$i;
$assignment->setMaxGrade((isset($this->turnitintooltwo->$attribute)) ? $this->turnitintooltwo->$attribute : 0);
$assignment->setSubmittedDocumentsCheck($this->turnitintooltwo->spapercheck);
$assignment->setInternetCheck($this->turnitintooltwo->internetcheck);
$assignment->setPublicationsCheck($this->turnitintooltwo->journalcheck);
$transmatch = (isset($this->turnitintooltwo->transmatch)) ? $this->turnitintooltwo->transmatch : 0;
$assignment->setTranslatedMatching($transmatch);
// Erater settings.
$assignment->setErater((isset($this->turnitintooltwo->erater)) ? $this->turnitintooltwo->erater : 0);
$eraterspelling = (isset($this->turnitintooltwo->erater_spelling)) ? $this->turnitintooltwo->erater_spelling : 0;
$assignment->setEraterSpelling($eraterspelling);
$eratergrammar = (isset($this->turnitintooltwo->erater_grammar)) ? $this->turnitintooltwo->erater_grammar : 0;
$assignment->setEraterGrammar($eratergrammar);
$eraterusage = (isset($this->turnitintooltwo->erater_usage)) ? $this->turnitintooltwo->erater_usage : 0;
$assignment->setEraterUsage($eraterusage);
$eratermechanics = (isset($this->turnitintooltwo->erater_mechanics)) ? $this->turnitintooltwo->erater_mechanics : 0;
$assignment->setEraterMechanics($eratermechanics);
$eraterstyle = (isset($this->turnitintooltwo->erater_style)) ? $this->turnitintooltwo->erater_style : 0;
$assignment->setEraterStyle($eraterstyle);
$eraterdictionary = (isset($this->turnitintooltwo->erater_dictionary)) ? $this->turnitintooltwo->erater_dictionary : 'en_US';
$assignment->setEraterSpellingDictionary($eraterdictionary);
$eraterhandbook = (isset($this->turnitintooltwo->erater_handbook)) ? $this->turnitintooltwo->erater_handbook : 0;
$assignment->setEraterHandbook($eraterhandbook);
// Create Assignment on Turnitin.
$newassignmentid = $this->create_tii_assignment($assignment, $toolid, $i);
// Save Part details.
$part = new stdClass();
$part->turnitintooltwoid = $toolid;
$attribute = "partname".$i;
$part->partname = $this->turnitintooltwo->$attribute;
$part->tiiassignid = $newassignmentid;
$attribute = "dtstart".$i;
$part->dtstart = $this->turnitintooltwo->$attribute;
$attribute = "dtdue".$i;
$part->dtdue = $this->turnitintooltwo->$attribute;
$attribute = "dtpost".$i;
$part->dtpost = $this->turnitintooltwo->$attribute;
$attribute = "maxmarks".$i;
$part->maxmarks = (empty($this->turnitintooltwo->$attribute)) ? 0 : $this->turnitintooltwo->$attribute;
$part->deleted = 0;
if (!$insert = $DB->insert_record('turnitintooltwo_parts', $part)) {
$DB->delete_records('turnitintooltwo', array('id' => $toolid));
turnitintooltwo_print_error('partdberror', 'turnitintooltwo', null, $i, __FILE__, __LINE__);
} else {
turnitintooltwo_activitylog("Moodle Assignment part created (".$insert.") - ".$part->tiiassignid , "REQUEST");
}
$this->create_event($toolid, $part->partname, $part->dtdue);
}
// Define grade settings.
$this->turnitintooltwo->id = $toolid;
turnitintooltwo_grade_item_update($this->turnitintooltwo);
return $this->turnitintooltwo->id;
}
/**
* Create event in Moodle for each part
*
* @global type $CFG
* @param int id of turnitintooltwo
* @param text name of part
* @param date part due date
*/
public function create_event($toolid, $partname, $duedate) {
global $CFG;
$properties = new stdClass();
$properties->name = $this->turnitintooltwo->name . ' - ' . $partname;
$intro = strip_pluginfile_content($this->turnitintooltwo->intro);
$intro = preg_replace("/<img[^>]+\>/i", "", $intro);
$properties->description = ($intro == null) ? '' : $intro;
$properties->courseid = $this->turnitintooltwo->course;
$properties->groupid = 0;
$properties->userid = 0;
$properties->modulename = 'turnitintooltwo';
$properties->instance = $toolid;
$properties->eventtype = 'due';
$properties->timestart = $duedate;
$properties->timeduration = 0;
require_once($CFG->dirroot.'/calendar/lib.php');
// Required parameters to support Moodle 3.3+ course overview block.
if ($CFG->branch >= 33) {
$properties->timesort = $duedate;
$properties->type = CALENDAR_EVENT_TYPE_ACTION;
}
$event = new calendar_event($properties);
return $event->update($properties, false);
}
/**
* Create Assignment on Turnitin and return id, delete the instance if it fails
*
* @global type $DB
* @param object $assignment add assignment instance
* @param var $toolid turnitintooltwo id
*/
public static function create_tii_assignment($assignment, $toolid, $partnumber,
$usecontext = "turnitintooltwo", $workflowcontext = "site") {
global $DB;
// Initialise Comms Object.
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
try {
$response = $turnitincall->createAssignment($assignment);
$newassignment = $response->getAssignment();
turnitintooltwo_activitylog("Part created as Turnitin Assignment (".$newassignment->getAssignmentId().
") - Tool Id: (".$toolid.") - Part num: (".$partnumber.")", "REQUEST");
if ($usecontext == "turnitintooltwo") {
$_SESSION["assignment_updated"][$toolid] = time();
}
return $newassignment->getAssignmentId();
} catch (Exception $e) {
if ($partnumber == 1 && $usecontext == "turnitintooltwo") {
$DB->delete_records('turnitintooltwo', array('id' => $toolid));
}
$toscreen = true;
if ($workflowcontext == "cron") {
mtrace(get_string('ppassignmentcreateerror', 'turnitintooltwo'));
$toscreen = false;
}
$turnitincomms->handle_exceptions($e, 'createassignmenterror', $toscreen);
}
}
/**
* Edit Assignment on Turnitin
*
* @param object $assignment edit assignment instance
*/
public function edit_tii_assignment($assignment, $workflowcontext = "site") {
// Initialise Comms Object.
$turnitincomms = new turnitintooltwo_comms();
$turnitincall = $turnitincomms->initialise_api();
try {
$turnitincall->updateAssignment($assignment);
$_SESSION["assignment_updated"][$assignment->getAssignmentId()] = time();
turnitintooltwo_activitylog("Turnitin Assignment part updated - id: ".$assignment->getAssignmentId(), "REQUEST");
return array('success' => true, 'tiiassignmentid' => $assignment->getAssignmentId());
} catch (Exception $e) {
$toscreen = true;
// Separate error handling for the Plagiarism plugin.
if ($workflowcontext == "cron") {
$error = new stdClass();
$error->title = $assignment->getTitle();
$error->assignmentid = $assignment->getAssignmentId();
$errorstr = get_string('ppassignmentediterror', 'turnitintooltwo', $error);
mtrace($errorstr);
$toscreen = false;
}
$turnitincomms->handle_exceptions($e, 'editassignmenterror', $toscreen);
// Return error string as we use this in the plagiarism plugin.
if ($workflowcontext == "cron") {
return array('success' => false, 'error' => $errorstr,
'tiiassignmentid' => $assignment->getAssignmentId());
} else {
return array('success' => false, 'error' => get_string('editassignmenterror', 'turnitintooltwo'));
}
}
}
/**
* Delete the assignment, parts and associated event from Moodle (not from Turnitin)
*
* @global type $CFG
* @global type $DB
* @param int $id instanced id of the turnitin assignment
* @return boolean
*/
public function delete_moodle_assignment($id) {
global $CFG, $DB;
$result = true;
// Get the Moodle Turnitintool (Assignment) Object.
if (!$turnitintooltwo = $DB->get_record("turnitintooltwo", array("id" => $id))) {
return true;
}
// Get Current Moodle Turnitin Tool parts and delete them.
$parts = $this->get_parts($id);
foreach ($parts as $part) {
$this->delete_moodle_assignment_part($id, $part->id);
turnitintooltwo_activitylog("Deleted assignment part - id (".$part->id." - ".$part->tiiassignid.")", "REQUEST");
}
// Delete events for this assignment / part.
$dbselect = " modulename = ? AND instance = ? ";