@@ -174,6 +174,19 @@ public function update_status($course, $cm) {
174
174
return '' ;
175
175
}
176
176
177
+ /**
178
+ * Check if plugin has been configured with Turnitin account details.
179
+ * @return boolean whether the plugin is configured for Turnitin.
180
+ **/
181
+ public function is_plugin_configured () {
182
+ $ config = turnitintooltwo_admin_config ();
183
+ if (empty ($ config ->accountid ) || empty ($ config ->apiurl ) || empty ($ config ->secretkey )) {
184
+ return false ;
185
+ }
186
+
187
+ return true ;
188
+ }
189
+
177
190
/**
178
191
* Save the form data associated with the plugin
179
192
*
@@ -229,9 +242,8 @@ public function get_form_elements_module($mform, $context, $modulename = "") {
229
242
// Get Course module id and values.
230
243
$ cmid = optional_param ('update ' , null , PARAM_INT );
231
244
232
- // Check Turnitin is configured.
233
- $ config = turnitintooltwo_admin_config ();
234
- if (empty ($ config ->accountid ) || empty ($ config ->apiurl ) || empty ($ config ->secretkey )) {
245
+ // Return no form if the plugin isn't configured.
246
+ if (!$ this ->is_plugin_configured ()) {
235
247
return ;
236
248
}
237
249
@@ -265,11 +277,10 @@ public function get_form_elements_module($mform, $context, $modulename = "") {
265
277
$ turnitinpluginview = new turnitinplugin_view ();
266
278
$ plagiarismvalues ["plagiarism_rubric " ] = ( !empty ($ plagiarismvalues ["plagiarism_rubric " ]) ) ? $ plagiarismvalues ["plagiarism_rubric " ] : 0 ;
267
279
268
- // Create/Edit course in Turnitin and join user to class.
269
- $ course = $ this ->get_course_data ($ cmid , $ COURSE ->id );
270
-
271
280
// We don't require the settings form on Moodle 3.3's bulk completion feature.
272
- if ($ PAGE ->pagetype != 'course-editbulkcompletion ' ) {
281
+ if ($ PAGE ->pagetype != 'course-editbulkcompletion ' && $ PAGE ->pagetype != 'course-editdefaultcompletion ' ) {
282
+ // Create/Edit course in Turnitin and join user to class.
283
+ $ course = $ this ->get_course_data ($ cmid , $ COURSE ->id );
273
284
$ turnitinpluginview ->add_elements_to_settings_form ($ mform , $ course , "activity " , $ cmid , $ plagiarismvalues ["plagiarism_rubric " ]);
274
285
}
275
286
@@ -436,6 +447,11 @@ public function print_disclosure($cmid) {
436
447
$ output = $ OUTPUT ->box ($ contents , 'generalbox boxaligncenter ' , 'intro ' );
437
448
}
438
449
450
+ // Exit here if the plugin is not configured for Turnitin.
451
+ if (!$ this ->is_plugin_configured ()) {
452
+ return $ output ;
453
+ }
454
+
439
455
// Show EULA if necessary and we have a connection to Turnitin.
440
456
if (empty ($ tiiconnection )) {
441
457
$ tiiconnection = $ this ->test_turnitin_connection ();
@@ -679,12 +695,22 @@ public function get_links($linkarray) {
679
695
$ linkarray ['userid ' ] = $ USER ->id ;
680
696
}
681
697
682
- // Get correct user id that submission is for rather than who submitted, this only affects file submissions
683
- // post Moodle 2.7 which is problematic as teachers can submit on behalf of students.
684
- $ author = $ linkarray ['userid ' ];
685
- if ($ itemid != 0 ) {
686
- $ author = $ moduleobject ->get_author ($ itemid );
687
- $ linkarray ['userid ' ] = (!empty ($ author )) ? $ author : $ linkarray ['userid ' ];
698
+ /*
699
+ The author will be incorrect if an instructor submits on behalf of a student who is in a group.
700
+ To get around this, we get the group ID, get the group members and set the author as the first student in the group.
701
+ */
702
+ $ moodlesubmission = $ DB ->get_record ('assign_submission ' , array ('id ' => $ itemid ), 'id, groupid ' );
703
+ if ((!empty ($ moodlesubmission ->groupid )) && ($ cm ->modname == "assign " )) {
704
+ $ author = $ this ->get_first_group_author ($ cm ->course , $ moodlesubmission ->groupid );
705
+ $ linkarray ['userid ' ] = $ author ;
706
+ } else {
707
+ // Get correct user id that submission is for rather than who submitted, this only affects file submissions
708
+ // post Moodle 2.7 which is problematic as teachers can submit on behalf of students.
709
+ $ author = $ linkarray ['userid ' ];
710
+ if ($ itemid != 0 ) {
711
+ $ author = $ moduleobject ->get_author ($ itemid );
712
+ $ linkarray ['userid ' ] = (!empty ($ author )) ? $ author : $ linkarray ['userid ' ];
713
+ }
688
714
}
689
715
690
716
// Show the EULA for a student if necessary.
@@ -1239,10 +1265,18 @@ private function update_submission($cm, $submissionid, $tiisubmission) {
1239
1265
if ($ file = $ fs ->get_file_by_hash ($ submissiondata ->identifier )) {
1240
1266
$ itemid = $ file ->get_itemid ();
1241
1267
1242
- $ submission = $ DB ->get_records ('assign_submission ' , array ('assignment ' => $ cm ->instance ,
1243
- 'userid ' => $ submissiondata ->userid ), 'id DESC ' , 'id, attemptnumber ' , '0 ' , '1 ' );
1244
- $ item = current ($ submission );
1268
+ $ assignmentdata = array ("assignment " => $ cm ->instance );
1269
+
1270
+ // Check whether submission is a group submission.
1271
+ $ groupid = $ this ->check_group_submission ($ cm , $ submissiondata ->userid );
1272
+ if ($ groupid ) {
1273
+ $ assignmentdata ['groupid ' ] = $ groupid ;
1274
+ } else {
1275
+ $ assignmentdata ['userid ' ] = $ submissiondata ->userid ;
1276
+ }
1277
+ $ submission = $ DB ->get_records ('assign_submission ' , $ assignmentdata , 'id DESC ' , 'id, attemptnumber ' , '0 ' , '1 ' );
1245
1278
1279
+ $ item = current ($ submission );
1246
1280
if ($ item ->id != $ itemid ) {
1247
1281
$ gbupdaterequired = false ;
1248
1282
}
@@ -1443,6 +1477,48 @@ private function update_grade($cm, $submission, $userid, $type = 'submission') {
1443
1477
return $ return ;
1444
1478
}
1445
1479
1480
+ /**
1481
+ * Check if this is a group submission.
1482
+ */
1483
+ public function check_group_submission ($ cm , $ userid ) {
1484
+ global $ CFG , $ DB ;
1485
+
1486
+ $ moduledata = $ DB ->get_record ($ cm ->modname , array ('id ' => $ cm ->instance ));
1487
+ if (!empty ($ moduledata ->teamsubmission )) {
1488
+ require_once ($ CFG ->dirroot . '/mod/assign/locallib.php ' );
1489
+ $ context = context_course::instance ($ cm ->course );
1490
+
1491
+ $ assignment = new assign ($ context , $ cm , null );
1492
+ $ group = $ assignment ->get_submission_group ($ userid );
1493
+
1494
+ return $ group ->id ;
1495
+ }
1496
+
1497
+ return false ;
1498
+ }
1499
+
1500
+ /*
1501
+ * Related user ID will be NULL if an instructor submits on behalf of a student who is in a group.
1502
+ * To get around this, we get the group ID, get the group members and set the author as the first student in the group.
1503
+
1504
+ * @param int $cmid - The course ID.
1505
+ * @param int $groupid - The ID of the Moodle group that we're getting from.
1506
+ * @return int $author The Moodle user ID that we'll be using for the author.
1507
+ */
1508
+ private function get_first_group_author ($ cmid , $ groupid ) {
1509
+ static $ context ;
1510
+ if (empty ($ context )) {
1511
+ $ context = context_course::instance ($ cmid );
1512
+ }
1513
+
1514
+ $ groupmembers = groups_get_members ($ groupid , "u.id " );
1515
+ foreach ($ groupmembers as $ author ) {
1516
+ if (!has_capability ('mod/assign:grade ' , $ context , $ author ->id )) {
1517
+ return $ author ->id ;
1518
+ }
1519
+ }
1520
+ }
1521
+
1446
1522
/**
1447
1523
* Create a course within Turnitin
1448
1524
*/
@@ -1491,6 +1567,11 @@ public function create_tii_course($cmid, $modname, $coursedata, $workflowcontext
1491
1567
public function refresh_peermark_assignments ($ cm , $ tiiassignmentid ) {
1492
1568
global $ DB ;
1493
1569
1570
+ // Return here if the plugin is not configured for Turnitin.
1571
+ if (!$ this ->is_plugin_configured ()) {
1572
+ return ;
1573
+ }
1574
+
1494
1575
// Initialise Comms Object.
1495
1576
$ turnitincomms = new turnitin_comms ();
1496
1577
$ turnitincall = $ turnitincomms ->initialise_api ();
@@ -2223,12 +2304,22 @@ public function event_handler($eventdata) {
2223
2304
$ submitter = $ eventdata ['userid ' ];
2224
2305
$ author = (!empty ($ eventdata ['relateduserid ' ])) ? $ eventdata ['relateduserid ' ] : $ eventdata ['userid ' ];
2225
2306
2307
+ /*
2308
+ Related user ID will be NULL if an instructor submits on behalf of a student who is in a group.
2309
+ To get around this, we get the group ID, get the group members and set the author as the first student in the group.
2310
+ */
2311
+ if ((empty ($ eventdata ['relateduserid ' ])) && ($ eventdata ['other ' ]['modulename ' ] == 'assign ' )) {
2312
+ $ moodlesubmission = $ DB ->get_record ('assign_submission ' , array ('id ' => $ eventdata ['objectid ' ]), 'id, groupid ' );
2313
+ if (!empty ($ moodlesubmission ->groupid )) {
2314
+ $ author = $ this ->get_first_group_author ($ cm ->course , $ moodlesubmission ->groupid );
2315
+ }
2316
+ }
2317
+
2226
2318
// Get actual text content and files to be submitted for draft submissions.
2227
2319
// As this won't be present in eventdata for certain event types.
2228
2320
if ($ eventdata ['other ' ]['modulename ' ] == 'assign ' && $ eventdata ['eventtype ' ] == "assessable_submitted " ) {
2229
2321
// Get content.
2230
- $ moodlesubmission = $ DB ->get_record ('assign_submission ' , array ('assignment ' => $ cm ->instance ,
2231
- 'userid ' => $ author , 'id ' => $ eventdata ['objectid ' ]), 'id ' );
2322
+ $ moodlesubmission = $ DB ->get_record ('assign_submission ' , array ('id ' => $ eventdata ['objectid ' ]), 'id ' );
2232
2323
if ($ moodletextsubmission = $ DB ->get_record ('assignsubmission_onlinetext ' ,
2233
2324
array ('submission ' => $ moodlesubmission ->id ), 'onlinetext ' )) {
2234
2325
$ eventdata ['other ' ]['content ' ] = $ moodletextsubmission ->onlinetext ;
0 commit comments