forked from turnitin/moodle-plagiarism_turnitin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
executable file
·3424 lines (2885 loc) · 161 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/>.
/*
* @package plagiarism_turnitin
* @copyright 2013 iParadigms LLC
*/
use Integrations\PhpSdk\TiiClass;
use Integrations\PhpSdk\TiiSubmission;
use Integrations\PhpSdk\TiiAssignment;
use Integrations\PhpSdk\TiiLTI;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
// Constants.
define('PLAGIARISM_TURNITIN_MAX_FILE_UPLOAD_SIZE', 104857600);
define('PLAGIARISM_TURNITIN_NUM_RECORDS_RETURN', 500);
define('PLAGIARISM_TURNITIN_CRON_SUBMISSIONS_LIMIT', 100);
define('PLAGIARISM_TURNITIN_REPORT_GEN_SPEED_NUM_RESUBMISSIONS', 3);
define('PLAGIARISM_TURNITIN_REPORT_GEN_SPEED_NUM_HOURS', 24);
define('PLAGIARISM_TURNITIN_MAX_FILENAME_LENGTH', 180);
define('PLAGIARISM_TURNITIN_COURSE_TITLE_LIMIT', 300);
// Admin Repository constants.
define('PLAGIARISM_TURNITIN_ADMIN_REPOSITORY_OPTION_STANDARD', 0);
define('PLAGIARISM_TURNITIN_ADMIN_REPOSITORY_OPTION_EXPANDED', 1);
define('PLAGIARISM_TURNITIN_ADMIN_REPOSITORY_OPTION_FORCE_STANDARD', 2);
define('PLAGIARISM_TURNITIN_ADMIN_REPOSITORY_OPTION_FORCE_NO', 3);
define('PLAGIARISM_TURNITIN_ADMIN_REPOSITORY_OPTION_FORCE_INSTITUTIONAL', 4);
// Submit Papers to Repository constants.
define('PLAGIARISM_TURNITIN_SUBMIT_TO_NO_REPOSITORY', 0);
define('PLAGIARISM_TURNITIN_SUBMIT_TO_STANDARD_REPOSITORY', 1);
define('PLAGIARISM_TURNITIN_SUBMIT_TO_INSTITUTIONAL_REPOSITORY', 2);
// Student privacy constants.
define('PLAGIARISM_TURNITIN_DEFAULT_PSEUDO_DOMAIN', '@tiimoodle.com');
define('PLAGIARISM_TURNITIN_DEFAULT_PSEUDO_FIRSTNAME', get_string('defaultcoursestudent'));
// Define accepted files if the module is not accepting any file type.
global $turnitinacceptedfiles;
$turnitinacceptedfiles = array('.doc', '.docx', '.ppt', '.pptx', '.pps', '.ppsx',
'.pdf', '.txt', '.htm', '.html', '.hwp', '.odt',
'.wpd', '.ps', '.rtf', '.xls', '.xlsx');
require_once($CFG->libdir.'/gradelib.php');
// Get global class.
require_once($CFG->dirroot.'/plagiarism/lib.php');
// Get helper methods.
require_once($CFG->dirroot.'/plagiarism/turnitin/locallib.php');
// Include plugin classes.
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_assignment.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_view.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_class.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_submission.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_comms.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/turnitin_user.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/digitalreceipt/pp_receipt_message.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/forms/turnitin_form.class.php');
// Include supported module specific code.
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/modules/turnitin_assign.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/modules/turnitin_forum.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/modules/turnitin_quiz.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/modules/turnitin_workshop.class.php');
require_once($CFG->dirroot.'/plagiarism/turnitin/classes/modules/turnitin_coursework.class.php');
class plagiarism_plugin_turnitin extends plagiarism_plugin {
/**
* Static variable to load the function from js files by using js_call_amd only once.
*/
private static $amdcomponentsloaded = false;
/**
* Get the fields to be used in the form to configure each activities Turnitin settings.
*
* @return array of settings fields.
*/
public function get_settings_fields() {
return array('use_turnitin', 'plagiarism_show_student_report', 'plagiarism_draft_submit',
'plagiarism_allow_non_or_submissions', 'plagiarism_submitpapersto', 'plagiarism_compare_student_papers',
'plagiarism_compare_internet', 'plagiarism_compare_journals', 'plagiarism_report_gen',
'plagiarism_compare_institution', 'plagiarism_exclude_biblio', 'plagiarism_exclude_quoted',
'plagiarism_exclude_matches', 'plagiarism_exclude_matches_value', 'plagiarism_rubric', 'plagiarism_erater',
'plagiarism_erater_handbook', 'plagiarism_erater_dictionary', 'plagiarism_erater_spelling',
'plagiarism_erater_grammar', 'plagiarism_erater_usage', 'plagiarism_erater_mechanics',
'plagiarism_erater_style', 'plagiarism_transmatch');
}
/**
* Get the configuration settings for the plagiarism plugin
*
* @return mixed if plugin is enabled then an array of config settings is returned or false if not
*/
public static function get_config_settings($modulename) {
$pluginconfig = get_config('plagiarism_turnitin', 'plagiarism_turnitin_'.$modulename);
return $pluginconfig;
}
/**
* @return mixed the admin config settings for the plugin
*/
public static function plagiarism_turnitin_admin_config() {
return get_config('plagiarism_turnitin');
}
/**
* Get the Turnitin settings for a module
*
* @param int $cmid - the course module id, if this is 0 the default settings will be retrieved
* @param bool $uselockedvalues - use locked values in place of saved values
* @return array of Turnitin settings for a module
*/
public function get_settings($cmid = null, $uselockedvalues = true) {
global $DB;
$defaults = $DB->get_records_menu('plagiarism_turnitin_config', array('cm' => null), '', 'name,value');
$settings = $DB->get_records_menu('plagiarism_turnitin_config', array('cm' => $cmid), '', 'name,value');
// Don't overwrite settings with locked values (only relevant on inital module creation).
if ($uselockedvalues == false) {
return $settings;
}
// Enforce site wide config locking.
foreach ($defaults as $key => $value) {
if (substr($key, -5) !== '_lock') {
continue;
}
if ($value != 1) {
continue;
}
$setting = substr($key, 0, -5);
$settings[$setting] = $defaults[$setting];
}
return $settings;
}
/**
* Get a list of the file upload errors.
*
* @param int $offset Number of records to skip.
* @param int $limit Max records to return.
* @param bool $count If true, returns a count of the total number of
* records.
* @access public
* @return array|int A list of records, or count when $count is true.
*/
public function get_file_upload_errors($offset = 0, $limit = 0, $count = false) {
global $DB;
$sql = "FROM {plagiarism_turnitin_files} PTF
LEFT JOIN {user} U ON U.id = PTF.userid
LEFT JOIN {course_modules} CM ON CM.id = PTF.cm
LEFT JOIN {modules} M ON CM.module = M.id
LEFT JOIN {course} C ON CM.course = C.id
WHERE PTF.statuscode = 'error'";
$countsql = "SELECT count(1) $sql";
$selectsql = "SELECT PTF.id, U.firstname, U.lastname, U.email, PTF.cm, M.name AS moduletype,
C.id AS courseid, C.fullname AS coursename, PTF.identifier, PTF.submissiontype,
PTF.errorcode, PTF.errormsg
$sql
ORDER BY PTF.id DESC";
if ($count) {
return $DB->count_records_sql($countsql);
}
return $DB->get_records_sql($selectsql, array(), $offset, $limit);
}
/**
* This function is called from the inbox in mod assign.
* This will alert the user to refresh the assignment when there has been a change in scores.
*
* @param $course - Course the module is part of
* @param $cm - Course module
* @return string
*/
public function update_status($course, $cm) {
return html_writer::div(get_string('turnitin_score_refresh_alert', 'plagiarism_turnitin'),
'turnitin_score_refresh_alert', array('id' => 'turnitin_score_refresh_alert'));
}
/**
* Check if plugin has been configured with Turnitin account details.
* @return boolean whether the plugin is configured for Turnitin.
**/
public function is_plugin_configured() {
$config = $this->plagiarism_turnitin_admin_config();
if (empty($config->plagiarism_turnitin_accountid) ||
empty($config->plagiarism_turnitin_apiurl) ||
empty($config->plagiarism_turnitin_secretkey)) {
return false;
}
return true;
}
/**
* Remove Turnitin class and assignment links from database
* so that new classes and assignments will be created.
*
* @param type $eventdata
* @return boolean
*/
public static function course_reset($eventdata) {
global $DB, $CFG;
$data = $eventdata->get_data();
$courseid = (int)$data['other']['reset_options']['courseid'];
$resetcourse = true;
$resetassign = 0;
$resetassignsubmissions = 0;
if (!empty($data['other']['reset_options']['reset_assign_submissions'])) {
$resetassign = $data['other']['reset_options']['reset_assign_submissions'];
$resetassignsubmissions = $resetassign;
}
$resetforumall = 0;
$resetforum = 0;
if (!empty($data['other']['reset_options']['reset_forum_all'])) {
$resetforumall = $data['other']['reset_options']['reset_forum_all'];
$resetforum = $resetforumall;
}
// Get the modules that support the Plagiarism plugin by whether they have a class file.
$supportedmods = array();
foreach (scandir($CFG->dirroot.'/plagiarism/turnitin/classes/modules/') as $filename) {
if (!in_array($filename, array(".", ".."))) {
$filenamear = explode('.', $filename);
$classnamear = explode('_', $filenamear[0]); // Split the class name.
$supportedmods[] = $classnamear[1]; // Set the module name.
}
}
foreach ($supportedmods as $supportedmod) {
$module = $DB->get_record('modules', array('name' => $supportedmod));
if ($module === false) {
continue;
}
// Get all the course modules that have Turnitin enabled.
$sql = "SELECT cm.id
FROM {course_modules} cm
RIGHT JOIN {plagiarism_turnitin_config} ptc ON cm.id = ptc.cm
WHERE cm.module = :moduleid
AND cm.course = :courseid
AND ptc.name = 'turnitin_assignid'";
$params = array('courseid' => $courseid, 'moduleid' => $module->id);
$modules = $DB->get_records_sql($sql, $params);
if (count($modules) > 0) {
$reset = "reset".$supportedmod;
if (!empty($$reset)) {
// Remove Plagiarism plugin submissions and assignment id from DB for this module.
foreach ($modules as $mod) {
$DB->delete_records('plagiarism_turnitin_files', array('cm' => $mod->id));
$DB->delete_records('plagiarism_turnitin_config', array('cm' => $mod->id, 'name' => 'turnitin_assignid'));
}
} else {
$resetcourse = false;
}
}
}
// If all turnitin enabled modules for this course have been reset.
// then remove the Turnitin course id from the database.
if ($resetcourse) {
$DB->delete_records('plagiarism_turnitin_courses', array('courseid' => $courseid));
}
return true;
}
/**
* Test whether we can connect to Turnitin.
*
* Initially only being used if a student is logged in before checking whether they have accepted the EULA.
*/
public function test_turnitin_connection($workflowcontext = 'site') {
$turnitincomms = new turnitin_comms();
$tiiapi = $turnitincomms->initialise_api();
$class = new TiiClass();
$class->setTitle('Test finding a class to see if connection works');
try {
$tiiapi->findClasses($class);
return true;
} catch (Exception $e) {
$turnitincomms->handle_exceptions($e, 'connecttesterror', false);
if ($workflowcontext == 'cron') {
mtrace(get_string('ppeventsfailedconnection', 'plagiarism_turnitin'));
}
return false;
}
}
/**
* Print the Turnitin student disclosure inside the submission page for students to see
*
* @global $OUTPUT
* @global $USER
* @global $CFG
* @param $cmid
* @return string
*/
public function print_disclosure($cmid) {
global $OUTPUT, $USER, $DB;
static $tiiconnection;
$config = $this->plagiarism_turnitin_admin_config();
$output = '';
// Get course details.
$cm = get_coursemodule_from_id('', $cmid);
$moduletiienabled = $this->get_config_settings('mod_'.$cm->modname);
// Exit if Turnitin is not being used for this activity type.
if (empty($moduletiienabled)) {
return '';
}
$plagiarismsettings = $this->get_settings($cmid);
// Check Turnitin is enabled for this current module.
if (empty($plagiarismsettings['use_turnitin'])) {
return '';
}
$this->load_page_components();
// Show resubmission warning - but not for mod forum.
if ($cm->modname != 'forum') {
$tiisubmissions = $DB->get_records('plagiarism_turnitin_files', array('userid' => $USER->id, 'cm' => $cm->id));
$tiisubmissions = current($tiisubmissions);
if ($tiisubmissions) {
$genparams = $this->plagiarism_get_report_gen_speed_params();
$output .= html_writer::tag('div', get_string('reportgenspeed_resubmission', 'plagiarism_turnitin', $genparams), array('class' => 'tii_genspeednote'));
}
}
// Show agreement.
if (!empty($config->plagiarism_turnitin_agreement)) {
$contents = format_text($config->plagiarism_turnitin_agreement, FORMAT_MOODLE, array("noclean" => true));
$output .= $OUTPUT->box($contents, 'generalbox boxaligncenter', 'intro');
}
// Exit here if the plugin is not configured for Turnitin.
if (!$this->is_plugin_configured()) {
return $output;
}
// Show EULA if necessary and we have a connection to Turnitin.
if (empty($tiiconnection)) {
$tiiconnection = $this->test_turnitin_connection();
}
if ($tiiconnection) {
$coursedata = $this->get_course_data($cm->id, $cm->course);
$user = new turnitin_user($USER->id, "Learner");
$user->join_user_to_class($coursedata->turnitin_cid);
$eulaaccepted = ($user->useragreementaccepted == 0) ? $user->get_accepted_user_agreement() : $user->useragreementaccepted;
if ($eulaaccepted != 1) {
$eulalink = html_writer::tag('span',
get_string('turnitinppulapre', 'plagiarism_turnitin'),
array('class' => 'pp_turnitin_eula_link tii_tooltip', 'id' => 'rubric_manager_form')
);
$eulaignoredclass = ($eulaaccepted == 0) ? ' pp_turnitin_eula_ignored' : '';
$eula = html_writer::tag('div', $eulalink, array('class' => 'pp_turnitin_eula'.$eulaignoredclass,
'data-userid' => $user->id));
$form = turnitin_view::output_launch_form(
"useragreement",
0,
$user->tiiuserid,
"Learner",
get_string('turnitinppulapre', 'plagiarism_turnitin'),
false
);
$form .= " ".get_string('noscriptula', 'plagiarism_turnitin');
$noscripteula = html_writer::tag('noscript', $form, array('class' => 'warning turnitin_ula_noscript'));
}
// Show EULA launcher and form placeholder.
if (!empty($eula)) {
$output .= $eula.$noscripteula;
$turnitincomms = new turnitin_comms();
$turnitincall = $turnitincomms->initialise_api();
$customdata = array("disable_form_change_checker" => true,
"elements" => array(array('html', $OUTPUT->box('', '', 'useragreement_inputs'))));
$eulaform = new turnitin_form($turnitincall->getApiBaseUrl().TiiLTI::EULAENDPOINT, $customdata,
'POST', $target = 'eulaWindow', array('id' => 'eula_launch'));
$output .= $OUTPUT->box($eulaform->display(), 'tii_useragreement_form', 'useragreement_form');
}
}
if ($config->plagiarism_turnitin_usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
// Update assignment in case rubric is not stored in Turnitin yet.
$this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
$rubricviewlink = html_writer::tag('span',
get_string('launchrubricview', 'plagiarism_turnitin'),
array('class' => 'rubric_view rubric_view_pp_launch_upload tii_tooltip',
'data-courseid' => $cm->course,
'data-cmid' => $cm->id,
'title' => get_string('launchrubricview',
'plagiarism_turnitin'), 'id' => 'rubric_manager_form'
)
);
$rubricviewlink = html_writer::tag('div', $rubricviewlink, array('class' => 'row_rubric_view'));
$output .= html_writer::tag('div', $rubricviewlink, array('class' => 'tii_links_container tii_disclosure_links'));
}
return $output;
}
/**
* Load JS needed by the page.
*/
public function load_page_components() {
global $PAGE, $CFG;
// The function from js files by using js_call_amd will be loaded only once.
if (static::$amdcomponentsloaded) {
return;
}
$PAGE->requires->js_call_amd('plagiarism_turnitin/open_viewer', 'origreport_open');
$PAGE->requires->js_call_amd('plagiarism_turnitin/open_viewer', 'grademark_open');
$PAGE->requires->js_call_amd('plagiarism_turnitin/peermark', 'peermarkLaunch');
$PAGE->requires->js_call_amd('plagiarism_turnitin/rubric', 'rubric');
// Moodle 4.3 uses a new Modal dialog that is not compatible with older versions of Moodle. Depending on the user's
// version of Moodle, we will use the supported versin of Modal dialog
if ($CFG->version >= 2023100900) {
$PAGE->requires->js_call_amd('plagiarism_turnitin/newEulaLaunch', 'newEulaLaunch');
} else {
$PAGE->requires->js_call_amd('plagiarism_turnitin/eulaLaunch', 'eulaLaunch');
}
$PAGE->requires->js_call_amd('plagiarism_turnitin/resend_submission', 'resendSubmission');
$PAGE->requires->string_for_js('closebutton', 'plagiarism_turnitin');
$PAGE->requires->string_for_js('loadingdv', 'plagiarism_turnitin');
if (!static::$amdcomponentsloaded) {
static::$amdcomponentsloaded = true;
}
}
/**
* Get Moodle and Turnitin Course data
*/
public function get_course_data($cmid, $courseid, $workflowcontext = 'site') {
$coursedata = turnitin_assignment::get_course_data($courseid, $workflowcontext);
// Get add from querystring to work out module type.
$add = optional_param('add', '', PARAM_TEXT);
if (empty($coursedata->turnitin_cid)) {
// Course may have existed in a previous incarnation of this plugin.
// Get this and save it in courses table if so.
if ($turnitincid = $this->get_previous_course_id($cmid, $courseid)) {
$coursedata->turnitin_cid = $turnitincid;
$coursedata = $this->migrate_previous_course($coursedata, $turnitincid);
} else {
// Otherwise create new course in Turnitin if it doesn't exist.
if ($cmid == 0) {
$tiicoursedata = $this->create_tii_course($cmid, $add, $coursedata, $workflowcontext);
} else {
$cm = get_coursemodule_from_id('', $cmid);
$tiicoursedata = $this->create_tii_course($cmid, $cm->modname, $coursedata, $workflowcontext);
}
$coursedata->turnitin_cid = (!empty($tiicoursedata->turnitin_cid)) ? $tiicoursedata->turnitin_cid : null;
$coursedata->turnitin_ctl = (!empty($tiicoursedata->turnitin_ctl)) ? $tiicoursedata->turnitin_ctl : "";
}
}
return $coursedata;
}
/**
*
* @global type $CFG
* @param type $linkarray
* @return type
*/
public function get_links($linkarray) {
global $CFG, $DB, $OUTPUT, $USER;
$output = "";
// Don't show links for certain file types as they won't have been submitted to Turnitin.
if (!empty($linkarray["file"])) {
$file = $linkarray["file"];
$filearea = $file->get_filearea();
$nonsubmittingareas = array("feedback_files", "introattachment");
if (in_array($filearea, $nonsubmittingareas)) {
return $output;
}
}
$component = (!empty($linkarray['component'])) ? $linkarray['component'] : "";
// Exit if this is a quiz and quizzes are disabled.
if ($component == "qtype_essay" && empty($this->get_config_settings('mod_quiz'))) {
return $output;
}
// If this is a quiz, retrieve the cmid
if ($component == "qtype_essay" && !empty($linkarray['area']) && empty($linkarray['cmid'])) {
$questions = question_engine::load_questions_usage_by_activity($linkarray['area']);
// Try to get cm using the questions owning context.
$context = $questions->get_owning_context();
if (empty($linkarray['cmid']) && $context->contextlevel == CONTEXT_MODULE) {
$linkarray['cmid'] = $context->instanceid;
}
}
// Set static variables.
static $cm;
static $forum;
if (empty($cm)) {
$cm = get_coursemodule_from_id('', $linkarray["cmid"]);
if ($cm->modname == 'forum') {
if (! $forum = $DB->get_record("forum", array("id" => $cm->instance))) {
print_error('invalidforumid', 'forum');
}
}
}
static $config;
if (empty($config)) {
$config = $this->plagiarism_turnitin_admin_config();
}
// Retrieve the plugin settings for this module.
static $plagiarismsettings = null;
if (is_null($plagiarismsettings)) {
$plagiarismsettings = $this->get_settings($linkarray["cmid"]);
}
// Is this plugin enabled for this activity type.
static $moduletiienabled;
if (empty($moduletiienabled)) {
$moduletiienabled = $this->get_config_settings('mod_'.$cm->modname);
}
// Exit if Turnitin is not being used for this module or activity type.
if (empty($moduletiienabled) || empty($plagiarismsettings['use_turnitin'])) {
return $output;
}
static $moduledata;
if (empty($moduledata)) {
$moduledata = $DB->get_record($cm->modname, array('id' => $cm->instance));
}
static $context;
if (empty($context)) {
$context = context_course::instance($cm->course);
}
static $coursedata;
if (empty($coursedata)) {
$coursedata = $this->get_course_data($cm->id, $cm->course);
}
// Create module object.
$moduleclass = "turnitin_".$cm->modname;
$moduleobject = new $moduleclass;
// Work out if logged in user is a tutor on this module.
static $istutor;
if (empty($istutor)) {
$istutor = $moduleobject->is_tutor($context);
}
// Define the timestamp for updating Peermark Assignments.
if (empty($_SESSION["updated_pm"][$cm->id]) && $config->plagiarism_turnitin_enablepeermark) {
$_SESSION["updated_pm"][$cm->id] = (time() - (60 * 5));
}
// If a text submission has been made, we can only display links for current attempts so don't show links previous attempts.
// This will need to be reworked when linkarray contains submission id.
static $contentdisplayed;
if ($cm->modname == 'assign' && !empty($linkarray["content"]) && $contentdisplayed == true) {
return $output;
}
if ((!empty($linkarray["file"]) || !empty($linkarray["content"])) && !empty($linkarray["cmid"])) {
$this->load_page_components();
$identifier = '';
$itemid = 0;
// Get File or Content information.
$submittinguser = $linkarray['userid'];
if (!empty($linkarray["file"])) {
$identifier = $file->get_pathnamehash();
$itemid = $file->get_itemid();
$submissiontype = 'file';
} else if (!empty($linkarray["content"])) {
// Get turnitin text content details.
$submissiontype = 'text_content';
if ($cm->modname == 'forum') {
$submissiontype = 'forum_post';
} else if ($cm->modname == 'quiz') {
$submissiontype = 'quiz_answer';
}
$content = $moduleobject->set_content($linkarray, $cm);
$identifier = ($submissiontype === 'quiz_answer') ? sha1($content.$linkarray["itemid"]) : sha1($content);
}
// Group submissions where all students have to submit sets userid to 0.
if ($linkarray['userid'] == 0 && !$istutor) {
$linkarray['userid'] = $USER->id;
}
/*
The author will be incorrect if an instructor submits on behalf of a student who is in a group.
To get around this, we get the group ID, get the group members and set the author as the first student in the group.
*/
$plagiarismfile = null;
$moodlesubmission = $DB->get_record('assign_submission', array('id' => $itemid), 'id, groupid');
if ((!empty($moodlesubmission->groupid)) && ($cm->modname == "assign")) {
$plagiarismfiles = $DB->get_records('plagiarism_turnitin_files', ['itemid' => $itemid, 'cm' => $cm->id, 'identifier' => $identifier],
'lastmodified DESC', '*', 0, 1);
$plagiarismfile = reset($plagiarismfiles);
$author = $plagiarismfile->userid;
$linkarray['userid'] = $author;
} else {
// Get correct user id that submission is for rather than who submitted, this only affects file submissions
// post Moodle 2.7 which is problematic as teachers can submit on behalf of students.
$author = $linkarray['userid'];
if ($itemid != 0) {
$author = $moduleobject->get_author($itemid);
$linkarray['userid'] = (!empty($author)) ? $author : $linkarray['userid'];
}
}
// Show the EULA for a student if necessary.
if ($linkarray["userid"] == $USER->id) {
$eula = "";
static $userid;
if (empty($userid)) {
$userid = 0;
}
// Show EULA if necessary and we have a connection to Turnitin.
if ($userid != $linkarray["userid"]) {
static $eulashown;
if (empty($eulashown)) {
$eulashown = false;
}
$user = new turnitin_user($USER->id, "Learner");
$success = $user->join_user_to_class($coursedata->turnitin_cid);
// Variable $success is false if there is no Turnitin connection and null if user has previously been enrolled.
if ((is_null($success) || $success === true) && $eulashown == false) {
$eulaaccepted = ($user->useragreementaccepted == 0) ? $user->get_accepted_user_agreement() : $user->useragreementaccepted;
$userid = $linkarray["userid"];
if ($eulaaccepted != 1) {
$eulalink = html_writer::tag('span',
get_string('turnitinppulapost', 'plagiarism_turnitin'),
array('class' => 'pp_turnitin_eula_link tii_tooltip', 'id' => 'rubric_manager_form')
);
$eula = html_writer::tag('div', $eulalink, array('class' => 'pp_turnitin_eula', 'data-userid' => $user->id));
}
// Show EULA launcher and form placeholder.
if (!empty($eula)) {
$output .= $eula;
$turnitincomms = new turnitin_comms();
$turnitincall = $turnitincomms->initialise_api();
$customdata = array("disable_form_change_checker" => true,
"elements" => array(array('html', $OUTPUT->box('', '', 'useragreement_inputs'))));
$eulaform = new turnitin_form(
$turnitincall->getApiBaseUrl().TiiLTI::EULAENDPOINT,
$customdata,
'POST',
$target = 'eulaWindow',
array('id' => 'eula_launch')
);
$output .= $OUTPUT->box($eulaform->display(), 'tii_useragreement_form', 'useragreement_form');
$eulashown = true;
}
}
}
}
// Check whether submission is a group submission - only applicable to assignment and coursework module.
// If it's a group submission then other users in the group should be able to see the originality score
// They can not open the DV though.
$submissionusers = array($linkarray["userid"]);
switch ($cm->modname) {
case "assign":
if ($moduledata->teamsubmission) {
$assignment = new assign($context, $cm, null);
if ($group = $assignment->get_submission_group($linkarray["userid"])) {
$users = groups_get_members($group->id);
$submissionusers = array_keys($users);
}
}
break;
case "coursework":
if ($moduledata->use_groups) {
$coursework = new \mod_coursework\models\coursework($moduledata->id);
$user = $DB->get_record('user', array('id' => $linkarray["userid"]));
$user = mod_coursework\models\user::find($user);
if ($group = $coursework->get_student_group($user)) {
$users = groups_get_members($group->id);
$submissionusers = array_keys($users);
}
}
}
// Proceed to displaying links for submissions.
if ($istutor || in_array($USER->id, $submissionusers)) {
// Prevent text content links being displayed for previous attempts as we have no way of getting the data.
if (!empty($linkarray["content"]) && $linkarray["userid"] == $USER->id) {
$contentdisplayed = true;
}
// Get turnitin file details.
if (is_null($plagiarismfile)) {
$plagiarismfiles = $DB->get_records('plagiarism_turnitin_files', array('userid' => $linkarray["userid"],
'cm' => $linkarray["cmid"], 'identifier' => $identifier),
'lastmodified DESC', '*', 0, 1);
$plagiarismfile = current($plagiarismfiles);
}
// Populate gradeitem query.
$gradeitemqueryarray = array(
'iteminstance' => $cm->instance,
'itemmodule' => $cm->modname,
'courseid' => $cm->course,
'itemnumber' => 0
);
// Get grade item and work out whether grades have been released for viewing.
$gradesreleased = true;
if ($gradeitem = $DB->get_record('grade_items', $gradeitemqueryarray)) {
switch ($gradeitem->hidden) {
case 1:
$gradesreleased = false;
break;
default:
$gradesreleased = ($gradeitem->hidden >= time()) ? false : true;
break;
}
// Give Marking workflow higher priority than gradebook hidden date.
if ($cm->modname == 'assign' && !empty($moduledata->markingworkflow)) {
$gradesreleased = $DB->record_exists(
'assign_user_flags',
array(
'userid' => $linkarray["userid"],
'assignment' => $cm->instance,
'workflowstate' => 'released'
));
}
}
$currentgradequery = false;
if ($gradeitem) {
$currentgradequery = $moduleobject->get_current_gradequery($linkarray["userid"], $cm->instance, $gradeitem->id);
}
// Display links to OR, GradeMark and show relevant errors.
if ($plagiarismfile) {
if ($plagiarismfile->statuscode == 'success' || ($plagiarismfile->statuscode == 'error' && $plagiarismfile->errorcode == 13)) {
if ($istutor || $linkarray["userid"] == $USER->id) {
$output .= html_writer::tag('div',
$OUTPUT->pix_icon('turnitin-icon',
get_string('turnitinid', 'plagiarism_turnitin').': '.$plagiarismfile->externalid,
'plagiarism_turnitin', array('class' => 'icon_size')).
get_string('turnitinid', 'plagiarism_turnitin').': '.$plagiarismfile->externalid,
array('class' => 'turnitin_status'));
}
// Show Originality Report score and link.
if (($istutor || (in_array($USER->id, $submissionusers) && $plagiarismsettings["plagiarism_show_student_report"])) &&
((is_null($plagiarismfile->orcapable) || $plagiarismfile->orcapable == 1) && !is_null($plagiarismfile->similarityscore))) {
// Show score.
if ($plagiarismfile->statuscode == "pending") {
$orscorehtml = html_writer::tag('div', ' ', array('title' => get_string('pending', 'plagiarism_turnitin'),
'class' => 'tii_tooltip origreport_score score_colour score_colour_'));
} else {
// Put EN flag if translated matching is on and that is the score used.
$transmatch = ($plagiarismfile->transmatch == 1) ? ' EN' : '';
if (is_null($plagiarismfile->similarityscore)) {
$score = ' ';
$titlescore = get_string('pending', 'plagiarism_turnitin');
$class = 'score_colour_';
} else {
$score = $plagiarismfile->similarityscore.'%';
$titlescore = $plagiarismfile->similarityscore.'% '.get_string('similarity', 'plagiarism_turnitin');
$class = 'score_colour_'.round($plagiarismfile->similarityscore, -1);
}
$orscorehtml = html_writer::tag('div', $score.$transmatch,
array('title' => $titlescore, 'class' => 'tii_tooltip origreport_score score_colour '.$class));
}
// Put in div placeholder for DV launch form.
$orscorehtml .= html_writer::tag('div', '', array('class' => 'launch_form origreport_form_'.$plagiarismfile->externalid));
// Add url for launching DV from Forum post.
if ($cm->modname == 'forum') {
$orscorehtml .= html_writer::tag('div', $CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"],
array('class' => 'origreport_forum_launch origreport_forum_launch_'.$plagiarismfile->externalid));
}
// This class is applied so that only the user who submitted or a tutor can open the DV.
$useropenclass = ($USER->id == $linkarray["userid"] || $istutor) ? 'pp_origreport_open' : '';
// Output container for OR Score.
$ordivclass = 'row_score pp_origreport '.$useropenclass.' origreport_'.$plagiarismfile->externalid.'_'.$linkarray["cmid"];
$output .= html_writer::tag('div', $orscorehtml, array('class' => $ordivclass, 'tabindex' => '0', 'role' => 'link'));
}
if (($plagiarismfile->orcapable == 0 && !is_null($plagiarismfile->orcapable))) {
$notorlink = html_writer::tag('div', 'x', array('title' => get_string('notorcapable', 'plagiarism_turnitin'),
'class' => 'tii_tooltip score_colour score_colour_ score_no_orcapable'));
// This class is applied so that only the user who submitted or a tutor can open the DV.
$useropenclass = ($USER->id == $linkarray["userid"] || $istutor) ? 'pp_origreport_open' : '';
$output .= html_writer::tag('div', $notorlink, array('class' => 'row_score pp_origreport '.$useropenclass));
}
// Check if blind marking is on and revealidentities is not set yet.
$blindon = (!empty($moduledata->blindmarking) && empty($moduledata->revealidentities));
// Check if a grade exists - as $currentgradequery->grade defaults to -1.
$gradeexists = false;
if (isset($currentgradequery->grade)) {
if ($currentgradequery->grade >= 0) {
$gradeexists = true;
}
}
// Can grade and feedback be released to this student yet?
$released = ((!$blindon) && ($gradesreleased && (!empty($plagiarismfile->gm_feedback) || $gradeexists)));
// Show link to open grademark.
if ($config->plagiarism_turnitin_usegrademark && ($istutor || ($linkarray["userid"] == $USER->id && $released))
&& !empty($gradeitem)) {
// Output grademark icon.
$gmicon = html_writer::tag('div', $OUTPUT->pix_icon('icon-edit',
get_string('grademark', 'plagiarism_turnitin'), 'plagiarism_turnitin'),
array('title' => get_string('grademark', 'plagiarism_turnitin'),
'class' => 'pp_grademark_open tii_tooltip grademark_'.$plagiarismfile->externalid.
'_'.$linkarray["cmid"], 'tabindex' => '0', 'role' => 'link'
));
// Put in div placeholder for DV launch form.
$gmicon .= html_writer::tag('div', '', array('class' => 'launch_form grademark_form_'.$plagiarismfile->externalid));
$output .= html_writer::tag('div', $gmicon, array('class' => 'grade_icon'));
}
// Indicate whether student has viewed the feedback.
if ($istutor) {
$readicon = "--";
if (isset($plagiarismfile->externalid)) {
$studentread = (!empty($plagiarismfile->student_read)) ? $plagiarismfile->student_read : 0;
if ($studentread > 0) {
$readicon = $OUTPUT->pix_icon('icon-student-read',
get_string('student_read', 'plagiarism_turnitin').' '.userdate($studentread),
'plagiarism_turnitin');
} else {
$readicon = $OUTPUT->pix_icon('icon-dot', get_string('student_notread', 'plagiarism_turnitin'),
'plagiarism_turnitin');
}
}
$output .= html_writer::tag('div', $readicon, array('class' => 'student_read_icon'));
}
// Show link to view rubric for student.
if (!$istutor && $config->plagiarism_turnitin_usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
// Update assignment in case rubric is not stored in Turnitin yet.
$this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
$rubricviewlink = html_writer::tag('span', '',
array('class' => 'rubric_view rubric_view_pp_launch tii_tooltip',
'data-courseid' => $cm->course,
'data-cmid' => $cm->id,
'title' => get_string('launchrubricview',
'plagiarism_turnitin'), 'id' => 'rubric_view_launch'
)
);
$rubricviewlink = html_writer::tag('div', $rubricviewlink, array('class' => 'row_rubric_view'));
$output .= $rubricviewlink;
}
if ($config->plagiarism_turnitin_enablepeermark) {
// If this module is already on Turnitin then refresh and get Peermark Assignments.
if (!empty($plagiarismsettings['turnitin_assignid'])) {
if ($_SESSION["updated_pm"][$cm->id] <= (time() - (60 * 2))) {
$this->refresh_peermark_assignments($cm, $plagiarismsettings['turnitin_assignid']);
$turnitinassignment = new turnitin_assignment($cm->instance);
$_SESSION["peermark_assignments"][$cm->id] = $turnitinassignment->get_peermark_assignments($plagiarismsettings['turnitin_assignid']);
$_SESSION["updated_pm"][$cm->id] = time();
}
// Determine if we have any active Peermark Assignments.
static $peermarksactive;
if (!isset($peermarksactive)) {
$peermarksactive = false;
foreach ($_SESSION["peermark_assignments"][$cm->id] as $peermarkassignment) {
if (time() > $peermarkassignment->dtstart) {
$peermarksactive = true;
break;
}
}
}
// Show Peermark Reviews link.
if (($istutor && count($_SESSION["peermark_assignments"][$cm->id]) > 0) ||
(!$istutor && $peermarksactive)) {
$peermarkreviewslink = html_writer::tag('span', '',
array('title' => get_string('launchpeermarkreviews', 'plagiarism_turnitin'),
'class' => 'peermark_reviews_pp_launch tii_tooltip', 'id' => 'peermark_reviews_form')
);
$output .= html_writer::tag('div', $peermarkreviewslink, array('class' => 'row_peermark_reviews'));
}
}
}
} else if ($plagiarismfile->statuscode == 'error') {
// Deal with legacy error issues.
$errorcode = (isset($plagiarismfile->errorcode)) ? $plagiarismfile->errorcode : 0;
if ($errorcode == 0 && $submissiontype == 'file') {
if ($file->get_filesize() > PLAGIARISM_TURNITIN_MAX_FILE_UPLOAD_SIZE) {
$errorcode = 2;
$plagiarismfile->errorcode = 2;
}
}
// Show error message if there is one.
if ($errorcode == 0) {
$langstring = ($istutor) ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
$errorstring = empty($plagiarismfile->errormsg) ? get_string($langstring, 'plagiarism_turnitin') : $plagiarismfile->errormsg;
} else {
$errorstring = get_string(
'errorcode'.$plagiarismfile->errorcode,
'plagiarism_turnitin',
array(
'maxfilesize' => display_size(PLAGIARISM_TURNITIN_MAX_FILE_UPLOAD_SIZE),
'externalid' => $plagiarismfile->externalid
));
}
$erroricon = html_writer::tag('div', $OUTPUT->pix_icon('x-red', $errorstring, 'plagiarism_turnitin'),
array('title' => $errorstring,
'class' => 'tii_tooltip tii_error_icon'));
// Attach error text or resubmit link after icon depending on whether user is a student/teacher.