forked from turnitin/moodle-plagiarism_turnitin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
2828 lines (2400 loc) · 139 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 turnitintooltwo
* @copyright 2013 iParadigms LLC
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
define('PLAGIARISM_TURNITIN_NUM_RECORDS_RETURN', 500);
define('PLAGIARISM_TURNITIN_CRON_SUBMISSIONS_LIMIT', 50);
// 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');
global $tiipp;
$tiipp = new stdClass();
$tiipp->in_use = true;
// Required classes from Moodle.
if ($CFG->branch < 28) {
require_once($CFG->libdir.'/pluginlib.php');
}
require_once($CFG->libdir.'/gradelib.php');
// Get global class.
require_once($CFG->dirroot.'/plagiarism/lib.php');
// Require classes from mod/turnitintooltwo
require_once($CFG->dirroot.'/mod/turnitintooltwo/lib.php');
require_once($CFG->dirroot.'/mod/turnitintooltwo/turnitintooltwo_view.class.php');
// Include plugin classes
require_once(__DIR__."/turnitinplugin_view.class.php");
require_once(__DIR__.'/classes/turnitin_class.class.php');
require_once(__DIR__.'/classes/turnitin_submission.class.php');
require_once(__DIR__.'/classes/turnitin_comms.class.php');
require_once(__DIR__.'/classes/digitalreceipt/pp_receipt_message.php');
// Include supported module specific code
require_once(__DIR__.'/classes/modules/turnitin_assign.class.php');
require_once(__DIR__.'/classes/modules/turnitin_forum.class.php');
require_once(__DIR__.'/classes/modules/turnitin_workshop.class.php');
class plagiarism_plugin_turnitin extends plagiarism_plugin {
/**
* 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) {
global $DB;
$pluginconfig = get_config('plagiarism', 'turnitin_use_'.$modulename);
return $pluginconfig;
}
/**
* Get the Turnitin settings for a module
*
* @param int $cm_id the course module id, if this is 0 the default settings will be retrieved
* @return array of Turnitin settings for a module
*/
public function get_settings($cmid = 0) {
global $DB;
$defaults = $DB->get_records_menu('plagiarism_turnitin_config', array('cm' => 0), '', 'name,value');
$settings = $DB->get_records_menu('plagiarism_turnitin_config', array('cm' => $cmid), '', 'name,value');
// 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);
}
/**
* Save the form data associated with the plugin
*
* @global type $DB
* @param object $data the form data to save
*/
public function save_form_elements($data) {
global $DB;
$moduletiienabled = $this->get_config_settings('mod_'.$data->modulename);
if (empty($moduletiienabled)) {
return;
}
$settingsfields = $this->get_settings_fields();
// Get current values.
$plagiarismvalues = $this->get_settings($data->coursemodule);
foreach ($settingsfields as $field) {
if (isset($data->$field)) {
$optionfield = new object();
$optionfield->cm = $data->coursemodule;
$optionfield->name = $field;
$optionfield->value = $data->$field;
if (isset($plagiarismvalues[$field])) {
$optionfield->id = $DB->get_field('plagiarism_turnitin_config', 'id',
(array('cm' => $data->coursemodule, 'name' => $field)));
if (!$DB->update_record('plagiarism_turnitin_config', $optionfield)) {
turnitintooltwo_print_error('defaultupdateerror', 'plagiarism_turnitin', null, null, __FILE__, __LINE__);
}
} else {
if (!$DB->insert_record('plagiarism_turnitin_config', $optionfield)) {
turnitintooltwo_print_error('defaultinserterror', 'plagiarism_turnitin', null, null, __FILE__, __LINE__);
}
}
}
}
}
/**
* Add the Turnitin settings form to an add/edit activity page
*
* @param object $mform
* @param object $context
* @return type
*/
public function get_form_elements_module($mform, $context, $modulename = "") {
global $DB, $COURSE;
if (has_capability('plagiarism/turnitin:enable', $context)) {
// Get Course module id and values.
$cmid = optional_param('update', 0, PARAM_INT);
// Check if plagiarism plugin is enabled for this module if provided.
if (!empty($modulename)) {
$moduletiienabled = $this->get_config_settings($modulename);
if (empty($moduletiienabled)) {
return;
}
}
$plagiarismvalues = $this->get_settings($cmid);
$plagiarismelements = $this->get_settings_fields();
$turnitinpluginview = new turnitinplugin_view();
$plagiarismvalues["plagiarism_rubric"] = ( !empty($plagiarismvalues["plagiarism_rubric"]) ) ?
$plagiarismvalues["plagiarism_rubric"] : 0;
// Create/Edit course in Turnitin and join user to class.
$course = $this->get_course_data($cmid, $COURSE->id);
$turnitinpluginview->add_elements_to_settings_form($mform, $course, "activity", $cmid, $plagiarismvalues["plagiarism_rubric"]);
// Disable all plagiarism elements if turnitin is not enabled.
foreach ($plagiarismelements as $element) {
if ($element <> 'use_turnitin') { // Ignore this var.
$mform->disabledIf($element, 'use_turnitin', 'eq', 0);
}
}
// Check if files have already been submitted and disable exclude biblio and quoted if turnitin is enabled.
if ($cmid != 0) {
if ($DB->record_exists('plagiarism_turnitin_files', array('cm' => $cmid))) {
$mform->disabledIf('plagiarism_exclude_biblio', 'use_turnitin');
$mform->disabledIf('plagiarism_exclude_quoted', 'use_turnitin');
}
}
// Set the default value for each option as the value we have stored.
foreach ($plagiarismelements as $element) {
if (isset($plagiarismvalues[$element])) {
$mform->setDefault($element, $plagiarismvalues[$element]);
}
}
}
}
/**
* 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(\core\event\course_reset_ended $event) {
global $DB;
$eventdata = $event->get_data();
$courseid = (int)$eventdata['courseid'];
$resetcourse = true;
$resetassign = (isset($eventdata['other']['reset_options']['reset_assign_submissions'])) ?
$eventdata['other']['reset_options']['reset_assign_submissions'] : 0;
$resetforum = (isset($eventdata['other']['reset_options']['reset_forum_all'])) ?
$eventdata['other']['reset_options']['reset_forum_all'] : 0;
// Get the modules that support the Plagiarism plugin by whether they have a class file.
$supportedmods = array();
foreach(scandir(__DIR__.'/classes/modules/') as $filename){
if (!in_array($filename, array(".",".."))) {
$filename_ar = explode('.', $filename);
$classname_ar = explode('_', $filename_ar[0]); // $filename_ar[0] is class name.
$supportedmods[] = $classname_ar[1]; // $classname_ar[1] is module name.
}
}
foreach ($supportedmods as $supportedmod) {
$module = $DB->get_record('modules', array('name' => $supportedmod));
// 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('turnitintooltwo_courses', array('courseid' => $courseid, 'course_type' => 'PP'));
}
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 {
$response = $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 type $DB
* @global type $OUTPUT
* @param type $cmid
* @return type
*/
public function print_disclosure($cmid) {
global $DB, $OUTPUT, $USER, $PAGE, $CFG;
static $tiiconnection;
if (empty($tiiconnection)) {
$tiiconnection = $this->test_turnitin_connection();
}
$config = turnitintooltwo_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 agreement.
if (!empty($config->agreement)) {
$contents = format_text($config->agreement, FORMAT_MOODLE, array("noclean" => true));
$output = $OUTPUT->box($contents, 'generalbox boxaligncenter', 'intro');
}
// Show EULA if necessary and we have a connection to Turnitin.
if ($tiiconnection) {
$coursedata = $this->get_course_data($cm->id, $cm->course);
$user = new turnitintooltwo_user($USER->id, "Learner");
$user->join_user_to_class($coursedata->turnitin_cid);
$eulaaccepted = ($user->user_agreement_accepted == 0) ? $user->get_accepted_user_agreement() : $user->user_agreement_accepted;
if ($eulaaccepted != 1) {
// Moodle strips out form and script code for forum posts so we have to do the Eula Launch differently.
$ula_link = html_writer::link($CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$cmid.'&cmd=useragreement&view_context=box_solid',
$OUTPUT->pix_icon('tiiIcon', '', 'plagiarism_turnitin', array('class' => 'icon_size_large')).'<br/>'.
get_string('turnitinppulapre', 'plagiarism_turnitin'),
array("class" => "pp_turnitin_eula_link"));
$eulaignoredclass = ($eulaaccepted == 0) ? ' pp_turnitin_ula_ignored' : '';
$ula = html_writer::tag('div', $ula_link, array('class' => 'pp_turnitin_ula js_required'.$eulaignoredclass,
'data-userid' => $user->id));
$noscriptula = html_writer::tag('noscript',
turnitintooltwo_view::output_dv_launch_form("useragreement", 0, $user->tii_user_id,
"Learner", get_string('turnitinppulapre', 'plagiarism_turnitin'), false)." ".
get_string('noscriptula', 'plagiarism_turnitin'),
array('class' => 'warning turnitin_ula_noscript'));
}
// Show EULA launcher and form placeholder.
if (!empty($ula)) {
$output .= $ula.$noscriptula;
$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 turnitintooltwo_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->usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
// Update assignment in case rubric is not stored in Turnitin yet.
$tiiassignment = $this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
$rubricviewlink = html_writer::tag('div', html_writer::link(
$CFG->wwwroot.'/plagiarism/turnitin/ajax.php?cmid='.$cm->id.
'&action=rubricview&view_context=box',
get_string('launchrubricview', 'plagiarism_turnitin'),
array('class' => 'rubric_view_pp_launch', 'id' => 'rubric_view_launch',
'title' => get_string('launchrubricview', 'plagiarism_turnitin'))).
html_writer::tag('span', '',
array('class' => 'launch_form', 'id' => 'rubric_view_form')),
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 $CFG, $PAGE;
$jsurl = new moodle_url('/mod/turnitintooltwo/jquery/turnitintooltwo.js');
$PAGE->requires->js($jsurl);
$jsurl = new moodle_url('/plagiarism/turnitin/jquery/turnitin_module.js');
$PAGE->requires->js($jsurl);
$jsurl = new moodle_url('/mod/turnitintooltwo/jquery/jquery.colorbox.js');
$PAGE->requires->js($jsurl);
$jsurl = new moodle_url('/mod/turnitintooltwo/jquery/jquery.tooltipster.js');
$PAGE->requires->js($jsurl);
$PAGE->requires->string_for_js('closebutton', 'plagiarism_turnitin');
$PAGE->requires->string_for_js('loadingdv', 'plagiarism_turnitin');
}
/**
* Get Moodle and Turnitin Course data
*/
public function get_course_data($cmid, $courseid, $workflowcontext = 'site') {
$coursedata = turnitintooltwo_assignment::get_course_data($courseid, 'PP', $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 = $tiicoursedata->turnitin_cid;
$coursedata->turnitin_ctl = $tiicoursedata->turnitin_ctl;
}
}
return $coursedata;
}
/**
*
* @global type $CFG
* @param type $linkarray
* @return type
*/
public function get_links($linkarray) {
global $CFG, $DB, $OUTPUT, $PAGE, $USER;
// 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;
}
}
// 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 $plagiarismsettings;
if (empty($plagiarismsettings)) {
$plagiarismsettings = $this->get_settings($linkarray["cmid"]);
}
// Exit if Turnitin is not being used for this module.
if (empty($plagiarismsettings['use_turnitin'])) {
return;
}
static $config;
if (empty($config)) {
$config = turnitintooltwo_admin_config();
}
static $moduletiienabled;
if (empty($moduletiienabled)) {
$moduletiienabled = $this->get_config_settings('mod_'.$cm->modname);
}
// Exit if Turnitin is not being used for this activity type.
if (empty($moduletiienabled)) {
return;
}
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->enablepeermark) {
$_SESSION["updated_pm"][$cm->id] = (time() - (60 * 5));
}
$output = "";
// 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 = ($cm->modname == "forum") ? 'forum_post' : 'text_content';
$content = $moduleobject->set_content($linkarray, $cm);
$identifier = sha1($content);
}
// Group submissions where all students have to submit sets userid to 0;
if ($linkarray['userid'] == 0 && !$istutor) {
$linkarray['userid'] = $USER->id;
}
// 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'];
}
$output .= $OUTPUT->box_start('tii_links_container');
// Show the EULA for a student if necessary.
if ($linkarray["userid"] == $USER->id && empty($plagiarismfile->externalid)) {
$eula = "";
static $userid;
if (empty($userid)) {
$userid = 0;
}
// Condition added to test for Moodle 2.7 as it calls this function twice.
if ($CFG->branch >= 27 || $userid != $linkarray["userid"]) {
// Show EULA if necessary and we have a connection to Turnitin.
static $eulashown;
if (empty($eulashown)) {
$eulashown = false;
}
$user = new turnitintooltwo_user($USER->id, "Learner");
$success = $user->join_user_to_class($coursedata->turnitin_cid);
// $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->user_agreement_accepted == 0) ? $user->get_accepted_user_agreement() : $user->user_agreement_accepted;
$userid = $linkarray["userid"];
if ($eulaaccepted != 1) {
$eula_link = html_writer::link($CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"].
'&cmd=useragreement&view_context=box_solid',
$OUTPUT->pix_icon('tiiIcon', '', 'plagiarism_turnitin', array('class' => 'icon_size_large')).'<br/>'.
get_string('turnitinppulapost', 'plagiarism_turnitin'),
array("class" => "pp_turnitin_eula_link"));
$eula = html_writer::tag('div', $eula_link, array('class' => 'pp_turnitin_ula js_required', '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 turnitintooltwo_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 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"]);
if ($cm->modname == "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);
}
}
}
// 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
$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') {
if ($istutor || $linkarray["userid"] == $USER->id) {
$output .= html_writer::tag('div',
$OUTPUT->pix_icon('tiiIcon',
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))) {
// 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 .= $OUTPUT->box_start('row_score pp_origreport '.$useropenclass.' origreport_'.
$plagiarismfile->externalid.'_'.$linkarray["cmid"],
$CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"]);
// Show score.
if ($plagiarismfile->statuscode == "pending") {
$output .= 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);
}
$output .= 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.
$output .= $OUTPUT->box('', 'launch_form origreport_form_'.$plagiarismfile->externalid);
// Add url for launching DV from Forum post.
if ($cm->modname == 'forum') {
$output .= $OUTPUT->box($CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"],
'origreport_forum_launch origreport_forum_launch_'.$plagiarismfile->externalid);
}
$output .= $OUTPUT->box_end(true);
}
if (($plagiarismfile->orcapable == 0 && !is_null($plagiarismfile->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 .= $OUTPUT->box_start('row_score pp_origreport '.$useropenclass, '');
$output .= html_writer::tag('div', 'x', array('title' => get_string('notorcapable', 'plagiarism_turnitin'),
'class' => 'tii_tooltip score_colour score_colour_ score_no_orcapable'));
$output .= $OUTPUT->box_end(true);
}
//Check if blind marking is on and revealidentities is not set yet.
$blindon = (!empty($moduledata->blindmarking) && empty($moduledata->revealidentities));
// Can grade and feedback be released to this student yet?
$released = ((!$blindon) && ($gradesreleased && (!empty($plagiarismfile->gm_feedback) || isset($currentgradequery->grade))));
// Show link to open grademark.
if ($config->usegrademark && ($istutor || ($linkarray["userid"] == $USER->id && $released))
&& !empty($gradeitem)) {
// Output grademark icon.
$output .= $OUTPUT->box_start('grade_icon', '');
$output .= 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"],
'id' => $CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"]));
// Add url for launching DV from Forum post.
if ($cm->modname == 'forum') {
$output .= $OUTPUT->box($CFG->wwwroot.'/plagiarism/turnitin/extras.php?cmid='.$linkarray["cmid"],
'grademark_forum_launch grademark_forum_launch_'.$plagiarismfile->externalid);
}
// Put in div placeholder for DV launch form.
$output .= $OUTPUT->box('', 'launch_form grademark_form_'.$plagiarismfile->externalid);
$output .= $OUTPUT->box_end(true);
}
// Indicate whether student has viewed the feedback.
if ($istutor) {
if (isset($plagiarismfile->externalid)) {
$studentread = (!empty($plagiarismfile->student_read)) ? $plagiarismfile->student_read : 0;
if ($studentread > 0) {
$output .= $OUTPUT->pix_icon('icon-student-read',
get_string('student_read', 'plagiarism_turnitin').' '.userdate($studentread),
'plagiarism_turnitin', array("class" => "student_read_icon"));
} else {
$output .= $OUTPUT->pix_icon('icon-dot', get_string('student_notread', 'plagiarism_turnitin'),
'plagiarism_turnitin', array("class" => "student_read_icon"));
}
} else {
$output .= "--";
}
}
// Show link to view rubric for student.
if (!$istutor && $config->usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
// Update assignment in case rubric is not stored in Turnitin yet.
$tiiassignment = $this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
$rubricviewlink = html_writer::tag('div', html_writer::link(
$CFG->wwwroot.'/plagiarism/turnitin/ajax.php?cmid='.$cm->id.
'&action=rubricview&view_context=box', ' ',
array('class' => 'tii_tooltip rubric_view_pp_launch', 'id' => 'rubric_view_launch',
'title' => get_string('launchrubricview', 'plagiarism_turnitin'))).
html_writer::tag('span', '',
array('class' => 'launch_form', 'id' => 'rubric_view_form')),
array('class' => 'row_rubric_view'));
$output .= $rubricviewlink;
}
if ($config->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']);
$turnitintooltwoassignment = new turnitintooltwo_assignment($cm->instance, '', 'PP');
$_SESSION["peermark_assignments"][$cm->id] =
$turnitintooltwoassignment->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 = $OUTPUT->box_start('row_peermark_reviews', '');
$peermarkreviewslink .= html_writer::link($CFG->wwwroot.'/plagiarism/turnitin/ajax.php?cmid='.$cm->id.
'&action=peermarkreviews&view_context=box', '',
array('title' => get_string('launchpeermarkreviews', 'plagiarism_turnitin'),
'class' => 'peermark_reviews_pp_launch tii_tooltip'));
$peermarkreviewslink .= html_writer::tag('span', '', array('class' => 'launch_form',
'id' => 'peermark_reviews_form'));
$peermarkreviewslink .= $OUTPUT->box_end(true);
$output .= $peermarkreviewslink;
}
}
}
} 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() > TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE) {
$errorcode = 2;
}
}
// Show error message if there is one.
if ($errorcode == 0) {
$langstring = ($istutor) ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
$errorstring = (isset($plagiarismfile->errormsg)) ?
get_string($langstring, 'plagiarism_turnitin') : $plagiarismfile->errormsg;
} else {
$errorstring = get_string('errorcode'.$plagiarismfile->errorcode,
'turnitintooltwo', display_size(TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE));
}
$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.
// Don't attach resubmit link if the user has not accepted the EULA.
if (!$istutor) {
$output .= html_writer::tag('div', $erroricon.' '.$errorstring, array('class' => 'warning clear'));
} else if ($errorcode == 3) {
$output .= html_writer::tag('div', $erroricon, array('class' => 'clear'));
} else {
$output .= html_writer::tag('div', $erroricon.' '.get_string('resubmittoturnitin', 'plagiarism_turnitin'),
array('class' => 'clear pp_resubmit_link',
'id' => 'pp_resubmit_'.$plagiarismfile->id));
$output .= html_writer::tag('div',
$OUTPUT->pix_icon('loading', $errorstring, 'plagiarism_turnitin').' '.
get_string('resubmitting', 'plagiarism_turnitin'),
array('class' => 'pp_resubmitting hidden'));
// Pending status for after resubmission.
$statusstr = get_string('turnitinstatus', 'plagiarism_turnitin').': '.get_string('pending', 'plagiarism_turnitin');
$output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')).$statusstr,
array('class' => 'turnitin_status hidden'));
// Show hidden data for potential forum post resubmissions
if ($submissiontype == 'forum_post' && !empty($linkarray["content"])) {
$output .= html_writer::tag('div', $linkarray["content"],
array('class' => 'hidden', 'id' => 'content_'.$plagiarismfile->id));
}
if ($cm->modname == 'forum') {
// Get forum data from the query string as we'll need this to recreate submission event.
$querystrid = optional_param('id', 0, PARAM_INT);
$discussionid = optional_param('d', 0, PARAM_INT);
$reply = optional_param('reply', 0, PARAM_INT);
$edit = optional_param('edit', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$output .= html_writer::tag('div', $querystrid.'_'.$discussionid.'_'.$reply.'_'.$edit.'_'.$delete,
array('class' => 'hidden', 'id' => 'forumdata_'.$plagiarismfile->id));
}
}
} else if ($plagiarismfile->statuscode == 'deleted'){
$errorcode = (isset($plagiarismfile->errorcode)) ? $plagiarismfile->errorcode : 0;
if ($errorcode == 0) {
$langstring = ($istutor) ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
$errorstring = (isset($plagiarismfile->errormsg)) ?
get_string($langstring, 'plagiarism_turnitin') : $plagiarismfile->errormsg;
} else {
$errorstring = get_string('errorcode'.$plagiarismfile->errorcode,
'turnitintooltwo', display_size(TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE));
}
$statusstr = get_string('turnitinstatus', 'plagiarism_turnitin').': '.get_string('deleted', 'plagiarism_turnitin').'<br />';
$statusstr .= get_string('because', 'plagiarism_turnitin').'<br />"'.$errorstring.'"';
$output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')).$statusstr,
array('class' => 'turnitin_status'));
} else {
$statusstr = get_string('turnitinstatus', 'plagiarism_turnitin').': '.get_string('pending', 'plagiarism_turnitin');
$output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')).$statusstr,
array('class' => 'turnitin_status'));
}
}
else {
// Add Error if the user has not accepted EULA for submissions made before instant submission was removed.
$eulaerror = "";
if ($linkarray["userid"] != $USER->id && $submittinguser == $author && $istutor) {
// There is a moodle plagiarism bug where get_links is called twice, the first loop is incorrect and is killing
// this functionality. Have to check that user exists here first else there will be a fatal error.
if ($mdl_user = $DB->get_record('user', array('id' => $linkarray["userid"]))) {
// We need to check for security that the user is actually on the course.
if ($moduleobject->user_enrolled_on_course($context, $linkarray["userid"])) {
$user = new turnitintooltwo_user($linkarray["userid"], "Learner");
if ($user->user_agreement_accepted != 1) {
$erroricon = html_writer::tag('div', $OUTPUT->pix_icon('doc-x-grey', get_string('errorcode3', 'plagiarism_turnitin'),
'plagiarism_turnitin'),