-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.php
executable file
·7751 lines (6882 loc) · 347 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
/**
* @package turnitintool
* @copyright 2012 Turnitin
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Encryption value passed to the API
*/
defined("TURNITINTOOL_ENCRYPT") or define("TURNITINTOOL_ENCRYPT","0");
/**
* The pause in between API calls
*/
defined("TURNITINTOOL_LATENCY_SLEEP") or define("TURNITINTOOL_LATENCY_SLEEP","4");
/**
* API Error: Start Error Code
*/
defined("TURNITINTOOL_API_ERROR_START") or define("TURNITINTOOL_API_ERROR_START","100");
/**
* API Error: Database Error inserting unique ID into the database
*/
defined("TURNITINTOOL_DB_UNIQUEID_ERROR") or define("TURNITINTOOL_DB_UNIQUEID_ERROR","218");
/**
* API Error: Creating/Updating/Deleting assignment failed in fid 4
*/
defined("TURNITINTOOL_ASSIGNMENT_UPDATE_ERROR") or define("TURNITINTOOL_ASSIGNMENT_UPDATE_ERROR","411");
/**
* API Error: The assignment you are trying to access does not exist
* in Turnitin for this class
*/
defined("TURNITINTOOL_ASSIGNMENT_NOTEXIST_ERROR") or define("TURNITINTOOL_ASSIGNMENT_NOTEXIST_ERROR","206");
/**
* API Error: The assignment with the assignment id that you entered does
* not belong to the class with the class id you entered
*/
defined("TURNITINTOOL_ASSIGNMENT_WRONGCLASS_ERROR") or define("TURNITINTOOL_ASSIGNMENT_WRONGCLASS_ERROR","228");
/**
* API SRC Value: The src value defines the integration namespace area
* in the Turnitin integrations database tables
*/
defined("TURNITINTOOL_APISRC") or define("TURNITINTOOL_APISRC","12");
/**
* Include the loaderbar class file
*/
require_once(__DIR__."/loaderbar.php");
/**
* Include the comms class file
*/
require_once(__DIR__."/comms.php");
/**
* Include the calendar class file.
*/
require_once($CFG->dirroot . '/calendar/lib.php');
/**
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function turnitintool_supports($feature) {
defined("FEATURE_SHOW_DESCRIPTION") or define("FEATURE_SHOW_DESCRIPTION",null);
switch($feature) {
case FEATURE_GROUPS: return true;
case FEATURE_GROUPMEMBERSONLY: return true;
case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_GRADE_OUTCOMES: return true;
case FEATURE_BACKUP_MOODLE2: return true;
case FEATURE_SHOW_DESCRIPTION: return true;
default: return null;
}
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @global object
* @global object
* @param object $turnitintool add turnitintool instance
* @return int intance id
*/
function turnitintool_add_instance($turnitintool) {
global $USER,$CFG;
$turnitintool->timecreated = time();
$total=($turnitintool->numparts*2)+2;
// Get Moodle Course Object [[[[
if (!$course = turnitintool_get_record("course", "id", $turnitintool->course)) {
turnitintool_print_error('coursegeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
// ]]]]
// Find out if this Course already has a Turnitin Owner [[[[
if (!turnitintool_is_owner($course->id)) {
$owner=turnitintool_get_owner($course->id);
// If the Course has no Turnitin Owner ie above get owner returned NULL
if (is_null($owner)) {
$owner=$USER;
}
} else {
$owner=$USER;
}
// ]]]]
$loaderbar = null;
$tii = new turnitintool_commclass(turnitintool_getUID($owner),$owner->firstname,$owner->lastname,$owner->email,2,$loaderbar);
$tii->startSession();
// Set this user up with a Turnitin Account or check to see if an account has already been set up
// Either return the stored ID OR store the New Turnitin User ID then return it [[[[
$turnitinuser=turnitintool_usersetup($owner,get_string('userprocess','turnitintool'),$tii,$loaderbar); // PROC 1
if ($tii->getRerror()) {
if ($tii->getAPIunavailable()) {
turnitintool_print_error('apiunavailable','turnitintool',NULL,NULL,__FILE__,__LINE__);
} else {
turnitintool_print_error($tii->getRmessage(),NULL,NULL,NULL,__FILE__,__LINE__);
}
exit();
}
$turnitinuser_id=$turnitinuser->turnitin_uid;
// ]]]]
// Set this course up in Turnitin or check to see if it has been already
// Either return the stored ID OR store the New Turnitin Course ID then return it [[[[
$turnitincourse=turnitintool_classsetup($course,$owner,
get_string('classprocess','turnitintool'),$tii,$loaderbar); // PROC 2
$turnitincourse_id=$turnitincourse->turnitin_cid;
// ]]]]
// Insert the Submitted turnitin form data in the database and retreive the id [[[[
$turnitintool->timemodified=time();
// Insert the default options
$turnitintool->dateformat="d/m/Y"; // deprecated (Now using langconfig.php)
$turnitintool->usegrademark=1;
$turnitintool->gradedisplay=1;
$turnitintool->autoupdates=1;
$turnitintool->commentedittime=1800;
$turnitintool->commentmaxsize=800;
$turnitintool->autosubmission=1;
$turnitintool->shownonsubmission=1;
$turnitintool->courseid = $course->id; // compatibility with modedit assignment obj
$insertid=turnitintool_insert_record("turnitintool", $turnitintool);
// ]]]]
// Do the multiple Assignment creation on turnitin
//## We are creating an assignment for each Moodle Assignment Part [[[[
for ($i=1;$i<=$turnitintool->numparts;$i++) {
// Do the turnitin assignment set call to the API [[[[
$tiipost = new stdClass();
$tiipost->courseid=$course->id;
$tiipost->ctl=turnitintool_getCTL($course->id);
$tiipost->dtstart=time(); // Set as today and update to a date in the past if needed to later
$tiipost->dtdue=strtotime('+7 days');
$tiipost->dtpost=strtotime('+7 days');
$uniquestring=strtoupper(uniqid());
$tiipost->name=$turnitintool->name." - ".get_string('turnitinpart','turnitintool',$i).
" (".$uniquestring.")";
$tiipost->s_view_report=$turnitintool->studentreports;
$tiipost->max_points=($turnitintool->grade < 0) ? 100 : $turnitintool->grade;
$tiipost->anon=$turnitintool->anon;
$tiipost->report_gen_speed=$turnitintool->reportgenspeed;
$tiipost->late_accept_flag=$turnitintool->allowlate;
$tiipost->submit_papers_to=$turnitintool->submitpapersto;
$tiipost->s_paper_check=$turnitintool->spapercheck;
$tiipost->internet_check=$turnitintool->internetcheck;
$tiipost->journal_check=$turnitintool->journalcheck;
// Add in Exclude small matches, biblio, quoted etc 20102009
$tiipost->exclude_biblio=$turnitintool->excludebiblio;
$tiipost->exclude_quoted=$turnitintool->excludequoted;
$tiipost->exclude_value=$turnitintool->excludevalue;
$tiipost->exclude_type=$turnitintool->excludetype;
$tiipost->transmatch=$turnitintool->transmatch;
// Add erater settings
$tiipost->erater=(isset($turnitintool->erater)) ? $turnitintool->erater : 0;
$tiipost->erater_handbook=(isset($turnitintool->erater_handbook)) ? $turnitintool->erater_handbook : 0;
$tiipost->erater_dictionary=(isset($turnitintool->erater_dictionary)) ? $turnitintool->erater_dictionary : 'en_US';
$tiipost->erater_spelling=(isset($turnitintool->erater_spelling)) ? $turnitintool->erater_spelling : 0;
$tiipost->erater_grammar=(isset($turnitintool->erater_grammar)) ? $turnitintool->erater_grammar : 0;
$tiipost->erater_usage=(isset($turnitintool->erater_usage)) ? $turnitintool->erater_usage : 0;
$tiipost->erater_mechanics=(isset($turnitintool->erater_mechanics)) ? $turnitintool->erater_mechanics : 0;
$tiipost->erater_style=(isset($turnitintool->erater_style)) ? $turnitintool->erater_style : 0;
$tiipost->transmatch=(isset($turnitintool->transmatch)) ? $turnitintool->transmatch : 0;
// == Create the assignment with no IDs in order to retreive the correct ==
// == Assignment ID for future use. ==
// ## Needed because if one UID is used then all Must Be, ##
// ## we do not have the assignment ID as it has not been created yet ##
$tiipost->cid='';
$tiipost->assignid='';
$tii->createAssignment($tiipost,'INSERT',get_string('assignmentprocess','turnitintool',$i));
if ($tii->getRerror()) {
$reason=($tii->getAPIunavailable()) ? get_string('apiunavailable','turnitintool')
: $tii->getRmessage();
turnitintool_delete_records('turnitintool','id',$insertid);
turnitintool_print_error('<strong>'.get_string('inserterror','turnitintool')
.'</strong><br />'.$reason);
exit();
}
$tiipost->cid=turnitintool_getCID($course->id);
$part = new stdClass();
$part->tiiassignid=$tii->getAssignid();
$tiipost->assignid=$part->tiiassignid;
$tiipost->dtstart=$turnitintool->defaultdtstart;
$tiipost->dtdue=$turnitintool->defaultdtdue;
$tiipost->dtpost=$turnitintool->defaultdtpost;
$tiipost->currentassign=$tiipost->name;
$tiipost->name=str_replace(" (".$uniquestring.")"," (Moodle ".$part->tiiassignid.")",$tiipost->name);
// Now individualise the Assignment Name and set the date to allow dates in the past
$tii->createAssignment($tiipost,'UPDATE',get_string('assignmentindividualise',
'turnitintool',$i)); // PROC 3+
if ($tii->getRerror()) {
$reason=($tii->getAPIunavailable()) ? get_string('apiunavailable','turnitintool')
: $tii->getRmessage();
turnitintool_delete_records('turnitintool','id',$insertid);
turnitintool_print_error('<strong>'.get_string('inserterror','turnitintool')
.'</strong><br />'.$reason);
exit();
}
$part->turnitintoolid=$insertid;
$part->partname=get_string('turnitinpart','turnitintool',$i);
$part->dtstart=$turnitintool->defaultdtstart;
$part->dtdue=$turnitintool->defaultdtdue;
$part->dtpost=$turnitintool->defaultdtpost;
$part->maxmarks=($turnitintool->grade < 0) ? 100 : $turnitintool->grade;
$part->deleted=0;
if (!$insert=turnitintool_insert_record('turnitintool_parts',$part,false)) {
turnitintool_delete_records('turnitintool','id',$insertid);
turnitintool_print_error('partdberror','turnitintool',NULL,$i,__FILE__,__LINE__);
}
$event = new object();
$event->name = $turnitintool->name.' - '.$part->partname;
$event->description = ($turnitintool->intro==NULL) ? '' : $turnitintool->intro;
$event->courseid = $turnitintool->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'turnitintool';
$event->instance = $insertid;
$event->eventtype = 'due';
$event->timestart = $part->dtdue;
$event->timeduration = 0;
if(method_exists('calendar_event', 'create')){
calendar_event::create($event);
} else {
add_event($event);
}
// ]]]]
}
// Define grade settings in Moodle 1.9 and above
$turnitintool->id = $insertid;
turnitintool_grade_item_update( $turnitintool );
// ]]]]
$tii->endSession();
turnitintool_add_to_log($turnitintool->course, "add turnitintool", 'view.php?id='.$turnitintool->coursemodule, "Assignment created '$turnitintool->name'", $turnitintool->coursemodule);
return $insertid;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @global object
* @global object
* @param object $turnitintool update turnitintool instance
* @return bool success
*/
function turnitintool_update_instance($turnitintool) {
global $USER,$CFG, $DB;
$turnitintool->timemodified = time();
$turnitintool->id = $turnitintool->instance;
// Set the checkbox settings for updates
$turnitintool->erater_spelling = (isset($turnitintool->erater_spelling)) ? $turnitintool->erater_spelling : 0;
$turnitintool->erater_grammar = (isset($turnitintool->erater_grammar)) ? $turnitintool->erater_grammar : 0;
$turnitintool->erater_usage = (isset($turnitintool->erater_usage)) ? $turnitintool->erater_usage : 0;
$turnitintool->erater_mechanics = (isset($turnitintool->erater_mechanics)) ? $turnitintool->erater_mechanics : 0;
$turnitintool->erater_style = (isset($turnitintool->erater_style)) ? $turnitintool->erater_style : 0;
$turnitintool->transmatch = (isset($turnitintool->transmatch)) ? $turnitintool->transmatch : 0;
// Get Moodle Course Object [[[[
if (!$course = turnitintool_get_record("course", "id", $turnitintool->course)) {
turnitintool_print_error('coursegeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
// ]]]]
// Get Current Moodle Turnitin Tool Object (Assignment) [[[
if (!$turnitintoolnow = turnitintool_get_record("turnitintool", "id", $turnitintool->id)) {
turnitintool_print_error('turnitintoolgeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
// ]]]]
// Get Current Moodle Turnitin Tool Parts Object [[[
if (is_callable(array($DB,'sql_compare_text'))) {
$part_select_query = $DB->sql_compare_text('turnitintoolid').' = :turnitintoolid
AND '.$DB->sql_compare_text('deleted').' = :deleted';
if (!$parts = turnitintool_get_records_select("turnitintool_parts", $part_select_query,array('turnitintoolid' => $turnitintool->id, 'deleted' => 0),'id DESC')) {
turnitintool_print_error('partgeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
} else {
$part_select_query = "turnitintoolid='".$turnitintool->id."' AND deleted=0";
if (!$parts = turnitintool_get_records_select("turnitintool_parts", $part_select_query,'id DESC')) {
turnitintool_print_error('partgeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
}
// ]]]]
$partids=array_keys($parts);
$proc=0;
$total=$turnitintool->numparts+2;
if (count($partids)>$turnitintool->numparts) {
// Add the number of deletes needed
$total+=count($partids)-$turnitintool->numparts;
}
if ($turnitintoolnow->numparts<$turnitintool->numparts) {
// Add the number of Insert Individualising required
$total+=$turnitintool->numparts-count($partids);
}
if (turnitintool_is_owner($course->id)) {
$owner=$USER;
} else {
$owner=turnitintool_get_owner($course->id);
}
$loaderbar = null;
$tii = new turnitintool_commclass(turnitintool_getUID($owner),$owner->firstname,$owner->lastname,$owner->email,2,$loaderbar);
$tii->startSession();
if ($turnitintool->numparts<count($partids) AND turnitintool_count_records('turnitintool_submissions','turnitintoolid',$turnitintool->id)>0) {
turnitintool_print_error('reducepartserror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
} else if ($turnitintool->numparts<count($partids)) { // REDUCE THE NUMBER OF PARTS BY CHOPPING THE PARTS OFF THE END
for ($i=0;$i<count($partids);$i++) {
$n=$i+1;
if ($n>$turnitintool->numparts) {
$proc++;
// Get the Turnitin UIDs [[[[
$tiipost = new stdClass();
$tiipost->cid=turnitintool_getCID($course->id);
$tiipost->assignid=turnitintool_getAID($partids[$i]);
// ]]]]
// Do the turnitin assignment set call to the API [[[[
$tiipost->courseid=$course->id;
$tiipost->ctl=turnitintool_getCTL($course->id);
$tiipost->dtstart=time();
$tiipost->dtdue=strtotime('+1 week');
$tiipost->dtpost=strtotime('+1 week');
$tiipost->name=$turnitintool->name.' - '.turnitintool_partnamefromnum($partids[$i]).' (Moodle '.$tiipost->assignid.')';
$tii->deleteAssignment($tiipost,get_string('assignmentdeleteprocess','turnitintool',$n));
if ($tii->getRerror() AND $tii->getRcode()!=TURNITINTOOL_ASSIGNMENT_UPDATE_ERROR AND $tii->getRcode()!=TURNITINTOOL_ASSIGNMENT_NOTEXIST_ERROR) {
if (!$tii->getAPIunavailable()) {
$reason=($tii->getRcode()==TURNITINTOOL_DB_UNIQUEID_ERROR) ? get_string('assignmentdoesnotexist','turnitintool') : $tii->getRmessage();
} else {
$reason=get_string('apiunavailable','turnitintool');
}
turnitintool_print_error('<strong>'.get_string('deleteerror','turnitintool').'</strong><br />'.$reason);
exit();
} else {
if (!$submissions=turnitintool_delete_records('turnitintool_submissions','turnitintoolid',$turnitintool->id,'submission_part',$partids[$i])) {
turnitintool_print_error('submissiondeleteerror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
}
}
$part = new stdClass();
$part->id=$partids[$i];
$part->partname=turnitintool_partnamefromnum($part->id);
$part->deleted=1;
if (!$delete=turnitintool_update_record('turnitintool_parts',$part,false)) {
turnitintool_print_error('partdberror','turnitintool',NULL,$i,__FILE__,__LINE__);
}
if (is_callable(array($DB,'sql_compare_text'))) {
$event_delete_query = $DB->sql_compare_text('modulename').'=:turnitintool
AND '.$DB->sql_compare_text('instance').'=:instance
AND '.$DB->sql_compare_text('name').'=:name';
} else {
$event_delete_query = 'modulename=:turnitintool
AND instance=:instance
AND name=:name';
}
turnitintool_delete_records_select('event', $event_delete_query, array('turnitintool' => 'turnitintool', 'instance' => $turnitintool->id, 'name' => $turnitintool->name." - ".$part->partname));
// ]]]]
}
}
}
unset($tiipost);
for ($i=0;$i<$turnitintool->numparts;$i++) {
$n=$i+1;
$proc++;
// Update Turnitin Assignment via the API [[[[
$tiipost = new stdClass();
$tiipost->courseid=$course->id;
$tiipost->ctl=turnitintool_getCTL($course->id);
$thisid = isset($partids[$i]) ? $partids[$i] : null;
if ($i>=count($partids)) {
$tiipost->dtstart=time(); // Now
$tiipost->dtdue=strtotime('+1 week'); // 7 days time
$tiipost->dtpost=strtotime('+1 week'); // 7 days time
} else {
$tiipost->dtstart=$parts[$thisid]->dtstart;
$tiipost->dtdue=$parts[$thisid]->dtdue;
$tiipost->dtpost=$parts[$thisid]->dtpost;
}
$tiipost->s_view_report=$turnitintool->studentreports;
$tiipost->anon=$turnitintool->anon;
$tiipost->report_gen_speed=$turnitintool->reportgenspeed;
$tiipost->late_accept_flag=$turnitintool->allowlate;
$tiipost->submit_papers_to=$turnitintool->submitpapersto;
$tiipost->s_paper_check=$turnitintool->spapercheck;
$tiipost->internet_check=$turnitintool->internetcheck;
$tiipost->journal_check=$turnitintool->journalcheck;
// Add in Exclude small matches, biblio, quoted etc 20102009
$tiipost->exclude_biblio=$turnitintool->excludebiblio;
$tiipost->exclude_quoted=$turnitintool->excludequoted;
$tiipost->exclude_value=$turnitintool->excludevalue;
$tiipost->exclude_type=$turnitintool->excludetype;
// Add erater settings
$tiipost->erater=(isset($turnitintool->erater)) ? $turnitintool->erater : 0;
$tiipost->erater_handbook=(isset($turnitintool->erater_handbook)) ? $turnitintool->erater_handbook : 0;
$tiipost->erater_dictionary=(isset($turnitintool->erater_dictionary)) ? $turnitintool->erater_dictionary : 'en_US';
$tiipost->erater_spelling=(isset($turnitintool->erater_spelling)) ? $turnitintool->erater_spelling : 0;
$tiipost->erater_grammar=(isset($turnitintool->erater_grammar)) ? $turnitintool->erater_grammar : 0;
$tiipost->erater_usage=(isset($turnitintool->erater_usage)) ? $turnitintool->erater_usage : 0;
$tiipost->erater_mechanics=(isset($turnitintool->erater_mechanics)) ? $turnitintool->erater_mechanics : 0;
$tiipost->erater_style=(isset($turnitintool->erater_style)) ? $turnitintool->erater_style : 0;
$tiipost->transmatch=(isset($turnitintool->transmatch)) ? $turnitintool->transmatch : 0;
if (turnitintool_is_owner($course->id)) {
$owner=$USER;
} else {
$owner=turnitintool_get_owner($course->id);
}
if ($i<count($partids)) {
$individualise=false;
$partname=turnitintool_partnamefromnum($partids[$i]);
$tiipost->cid=turnitintool_getCID($course->id);
$tiipost->assignid=turnitintool_getAID($partids[$i]);
$tiipost->name=$turnitintool->name.' - '.$partname.' (Moodle '.$tiipost->assignid.')';
$tiipost->currentassign=$turnitintoolnow->name.' - '.turnitintool_partnamefromnum($partids[$i]).' (Moodle '.$tiipost->assignid.')';
$tii->createAssignment($tiipost,'UPDATE',get_string('assignmentupdate','turnitintool',$n));
} else {
$individualise=true;
$tiipost->cid='';
$tiipost->assignid='';
$tiipost->dtstart=strtotime("now"); // Set time to now and change to the correct date later to allow dates in the past
$tiipost->dtdue=strtotime("+1 day"); // Set time to now +1 day and change to the correct date later to allow dates in the past
$tiipost->dtpost=strtotime("+1 day"); // Set time to now +1 day and change to the correct date later to allow dates in the past
$partname=get_string('turnitinpart','turnitintool',$n);
$tiipost->name=$turnitintool->name.' - '.$partname;
$tii->createAssignment($tiipost,'INSERT',get_string('assignmentprocess','turnitintool',$n));
}
if ($tii->getRerror()) {
if ($tii->getAPIunavailable()) {
$reason=get_string('apiunavailable','turnitintool');
} else {
$reason=($tii->getRcode()==TURNITINTOOL_DB_UNIQUEID_ERROR) ? get_string('assignmentdoesnotexist','turnitintool') : $tii->getRmessage();
}
turnitintool_print_error('<strong>'.get_string('updateerror','turnitintool').'</strong><br />'.$reason);
exit();
}
$part = new stdClass();
$part->tiiassignid=$tii->getAssignid();
if ($individualise) {
$tiipost->cid=turnitintool_getCID($course->id);
$tiipost->assignid=$part->tiiassignid;
$tiipost->currentassign=$tiipost->name;
$tiipost->name.=' (Moodle '.$part->tiiassignid.')';
$tiipost->max_points=($turnitintool->grade < 0) ? 100 : $turnitintool->grade;
// Now individualise the Assignment Name and allow to set the date to any date even dates in the past for new assignment part
$proc++;
$tii->createAssignment($tiipost,'UPDATE',get_string('assignmentindividualise','turnitintool',$n)); // PROC 3+
if ($tii->getRerror()) {
$reason=($tii->getAPIunavailable()) ? get_string('apiunavailable','turnitintool') : $tii->getRmessage();
turnitintool_print_error('<strong>'.get_string('inserterror','turnitintool').'</strong><br />'.$reason);
exit();
}
}
$part->turnitintoolid=$turnitintool->id;
$part->partname=$partname;
$part->deleted=0;
if ($i>=count($partids)) {
$part->dtstart=time();
$part->dtdue=strtotime('+1 week');
$part->dtpost=strtotime('+1 week');
} else {
$part->dtdue=$parts[$thisid]->dtdue;
$part->dtpost=$parts[$thisid]->dtpost;
}
$part->dtstart=$tiipost->dtstart;
$part->dtdue=$tiipost->dtdue;
$part->dtpost=$tiipost->dtpost;
$event = new object();
$event->name = $turnitintool->name.' - '.$part->partname;
$event->description = $turnitintool->intro;
$event->courseid = $turnitintool->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'turnitintool';
$event->instance = $turnitintool->id;
$event->eventtype = 'due';
$event->timestart = $part->dtdue;
$event->timeduration = 0;
if ($i<count($partids)) {
$part->id=$partids[$i];
if (!$dbpart=turnitintool_update_record('turnitintool_parts',$part,false)) {
turnitintool_print_error('partdberror','turnitintool',NULL,$i,__FILE__,__LINE__);
exit();
}
} else {
$part->maxmarks=($turnitintool->grade < 0) ? 100 : $turnitintool->grade;
if (!$dbpart=turnitintool_insert_record('turnitintool_parts',$part,false)) {
turnitintool_print_error('partdberror','turnitintool',NULL,$i,__FILE__,__LINE__);
exit();
}
}
// Delete existing events for this assignment / part
$name = $turnitintoolnow->name.' - '.$part->partname;
// $DB is not available for Moodle 1.9
if (is_callable(array($DB,'sql_compare_text'))) {
$deletewhere = 'modulename = :modulename
AND '.$DB->sql_compare_text('instance').' = :id
AND '.$DB->sql_compare_text('name').' = :name';
} else {
$deletewhere = 'modulename = \'turnitintool\'
AND instance = \''.$turnitintool->id.'\'
AND name = \''.$name.'\'';
}
turnitintool_delete_records_select('event', $deletewhere, array('modulename' => 'turnitintool', 'id' => $turnitintool->id, 'name' => $name));
if(method_exists('calendar_event', 'create')){
calendar_event::create($event);
} else {
add_event($event);
}
unset($tiipost);
}
// ]]]]
$turnitintool->timemodified=time();
$update=turnitintool_update_record("turnitintool", $turnitintool);
// Define grade settings in Moodle 1.9 and above
turnitintool_grade_item_update( $turnitintool );
$cm=get_coursemodule_from_instance("turnitintool", $turnitintool->id, $turnitintool->course);
turnitintool_add_to_log($turnitintool->course, "update turnitintool", 'view.php?id='.$cm->id, "Assignment updated '$turnitintool->name'", $cm->id);
$tii->endSession();
return $update;
}
/**
* Create grade item for given assignment
*
* @param object $turnitintool object with extra cmidnumber (if available)
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function turnitintool_grade_item_update( $turnitintool, $grades=null ) {
global $CFG;
@include_once($CFG->dirroot."/lib/gradelib.php");
if ( function_exists( 'grade_update' ) ) {
$params = array();
$cm=get_coursemodule_from_instance("turnitintool", $turnitintool->id, $turnitintool->course);
$params['itemname'] = $turnitintool->name;
$params['idnumber'] = isset( $cm->idnumber ) ? $cm->idnumber : null;
if ($turnitintool->grade < 0) { // If we're using a grade scale
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$turnitintool->grade;
} else if ($turnitintool->grade > 0) { // If we are using a grade value
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $turnitintool->grade;
$params['grademin'] = 0;
} else { // If we aren't using a grade at all
$params['gradetype'] = GRADE_TYPE_NONE;
}
$lastpart=turnitintool_get_record('turnitintool_parts','turnitintoolid',$turnitintool->id,'','','','','max(dtpost)');
$lastpart=current($lastpart);
$params['hidden']=$lastpart;
$params['grademin'] = 0;
return grade_update('mod/turnitintool', $turnitintool->course, 'mod', 'turnitintool', $turnitintool->id, 0, $grades, $params);
}
return;
}
/**
* Not used but needed for instance name update via quick update name on the course home page
*
* @param stdClass $turnitintool
* @param integer $userid
* @param boolean $nullifnone
*/
function turnitintool_update_grades($turnitintool, $userid=0, $nullifnone=true) {
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @global object
* @global object
* @param int $id turnitintool instance id
* @return bool success
*/
function turnitintool_delete_instance($id) {
global $USER,$CFG;
$result = true;
// Get the Moodle Turnitintool (Assignment) and Course Object [[[[
if (!$turnitintool = turnitintool_get_record("turnitintool", "id", $id)) {
return false;
}
if (!$course = turnitintool_get_record("course", "id", $turnitintool->course)) {
return false;
}
// ]]]]
// Get Current Moodle Turnitin Tool Parts Object [[[
$parts = turnitintool_get_records("turnitintool_parts", "turnitintoolid", $turnitintool->id);
// ]]]]
$partids=array_keys($parts);
$total=count($partids)+2;
$proc=0;
foreach ($parts as $part) {
$proc++;
if (!$submissions=turnitintool_delete_records('turnitintool_submissions','turnitintoolid',$turnitintool->id,'submission_part',$part->id)) {
$result=false;
}
// ]]]]
# Delete any dependent records here #
if (!turnitintool_delete_records("turnitintool_parts", "id", $part->id)) {
$result = false;
}
}
$cm = get_coursemodule_from_instance( "turnitintool", $turnitintool->id, $turnitintool->course );
turnitintool_add_to_log( $turnitintool->course, "delete turnitintool", 'view.php?id='.$cm->id, 'Assignment deleted "'.$turnitintool->name.'"', $cm->id );
// Delete events for this assignment / part
turnitintool_delete_records('event', 'modulename','turnitintool','instance',$turnitintool->id);
if (!turnitintool_delete_records("turnitintool", "id", $turnitintool->id)) {
$result = false;
}
if ($oldcourses=turnitintool_get_records("turnitintool_courses")) {
foreach ($oldcourses as $oldcourse) { // General Clean Up
if (!turnitintool_count_records("course","id",$oldcourse->courseid)>0) {
// Delete the Turnitin Classes data if the Moodle courses no longer exists
turnitintool_delete_records("turnitintool_courses","courseid",$oldcourse->courseid);
}
if (!turnitintool_count_records("turnitintool","course",$oldcourse->courseid)>0) {
// Delete the Turnitin Class data if no more turnitin assignments exist in it
turnitintool_delete_records("turnitintool_courses","courseid",$oldcourse->courseid);
}
}
}
// Define grade settings in Moodle 1.9 and above
@include_once($CFG->dirroot."/lib/gradelib.php");
if (function_exists('grade_update')) {
$params['deleted'] = 1;
grade_update('mod/turnitintool', $turnitintool->course, 'mod', 'turnitintool', $turnitintool->id, 0, NULL, $params);
}
return $result;
}
/**
* This is a standard Moodle module that checks to make sure there are events for each activity
*
* @param var $courseid The ID of the course this activity belongs to (default 0 for all courses)
* @return bool success
*/
function turnitintool_refresh_events($courseid=0) {
if ($courseid == 0) {
if (!$turnitintools = turnitintool_get_records("turnitintool")) {
$result=true;
}
} else {
if (!$turnitintools = turnitintool_get_records("turnitintool", "course",$courseid)) {
$result=true;
}
}
$module = turnitintool_get_records_select('modules',"name='turnitintool'",NULL,'id');
$moduleid=current(array_keys($module));
foreach ($turnitintools as $turnitintool) {
$event = new stdClass();
$event->description = $turnitintool->intro;
if (!$parts = turnitintool_get_records("turnitintool_parts","turnitintoolid",$turnitintool->id)) {
$result=false;
}
foreach ($parts as $part) {
$event->timestart=$part->dtdue;
if ($events = turnitintool_get_record_select('event', "modulename='turnitintool' AND instance=".$turnitintool->id." AND name='".$turnitintool->name." - ".$part->partname."'")) {
$event->id = $events->id;
if(method_exists('calendar_event', 'update')){
$calendarevent = calendar_event::load($event->id);
$calendarevent->update($event);
} else {
update_event($event);
}
} else {
$event->courseid = $turnitintool->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'turnitintool';
$event->instance = $turnitintool->id;
$event->eventtype = 'due';
$event->timeduration = 0;
$event->name = $turnitintool->name.' - '.$part->partname;
$coursemodule = turnitintool_get_record('course_modules','module',$moduleid,'instance',$turnitintool->id);
$event->visible = $coursemodule->visible;
if(method_exists('calendar_event', 'create')){
calendar_event::create($event);
} else {
add_event($event);
}
}
}
$result=true;
}
return $result;
}
/**
* This is a standard Moodle module that prints out a summary of all activities
* of this kind in the My Moodle page for a user
*
* @param object $courses
* @param object $htmlarray
* @return bool success
*/
function turnitintool_print_overview($courses, &$htmlarray) {
global $USER, $CFG, $DB;
if (empty($courses) || !is_array($courses) || count($courses) == 0) {
return array();
}
if (!$turnitintools=get_all_instances_in_courses('turnitintool',$courses)) {
return;
}
$ids = array();
$tiidata=array();
foreach ($turnitintools as $key => $turnitintool) {
$now = time();
$parts=turnitintool_get_records_select('turnitintool_parts','turnitintoolid='.$turnitintool->id.' AND deleted=0',NULL,'id');
$context = turnitintool_get_context('MODULE', $turnitintool->coursemodule);
// Get Early and Late Date Boundries for each part of this assignment
$earlydate=0;
$latedate=0;
$partsarray=array();
foreach ($parts as $part) {
$earlydate = ($part->dtstart < $earlydate OR $earlydate==0) ? $part->dtstart : $earlydate;
$latedate = ($part->dtpost > $latedate) ? $part->dtpost : $latedate;
$partsarray[$part->id]['name']=$part->partname;
$partsarray[$part->id]['dtdue']=$part->dtdue;
if (has_capability('mod/turnitintool:grade', $context)) { // If user is a grader
$subquery=turnitintool_get_records_select('turnitintool_submissions','turnitintoolid='.$turnitintool->id.
' AND submission_part='.$part->id.' AND submission_objectid IS NOT NULL AND userid!=0',NULL,'','count(userid)');
$numsubmissions=key($subquery);
$gradequery=turnitintool_get_records_select('turnitintool_submissions','turnitintoolid='.$turnitintool->id.
' AND submission_part='.$part->id.' AND userid!=0 AND submission_grade IS NOT NULL',NULL,'','count(userid)');
$numgrades=key($gradequery);
$allusers=get_users_by_capability($context, 'mod/turnitintool:submit', 'u.id', '', '', '', 0, '', false);
$input = new stdClass();
$input->submitted=$numsubmissions;
$input->graded=$numgrades;
$input->total=count($allusers);
$input->gplural=($numgrades!=1) ? 's' : '';
$partsarray[$part->id]['status']=get_string('tutorstatus','turnitintool',$input);
} else { // If user is a student
if ($submission=turnitintool_get_record_select('turnitintool_submissions','turnitintoolid='.$turnitintool->id.
' AND submission_part='.$part->id.' AND userid='.$USER->id.' AND submission_objectid IS NOT NULL')) {
$input = new stdClass();
$input->modified=userdate($submission->submission_modified,get_string('strftimedatetimeshort','langconfig'));
$input->objectid=$submission->submission_objectid;
$partsarray[$part->id]['status']=get_string('studentstatus','turnitintool',$input);
} else {
$partsarray[$part->id]['status']=get_string('nosubmissions','turnitintool');
}
}
}
if ($earlydate <= $now AND $latedate >= $now) { // Turnitin Assignment Is Active for this user
$str = '<div class="turnitintool overview"><div class="name">'.get_string('modulename','turnitintool'). ': '.
'<a '.($turnitintool->visible ? '':' class="dimmed"').
'title="'.get_string('modulename','turnitintool').'" href="'.$CFG->wwwroot.
'/mod/turnitintool/view.php?id='.$turnitintool->coursemodule.'">'.
$turnitintool->name.'</a></div>';
foreach ($partsarray as $thispart) {
$str .= '<div class="info"><b>'.$thispart['name'].' - '.get_string('dtdue','turnitintool').': '.userdate($thispart['dtdue'],get_string('strftimedatetimeshort','langconfig'),$USER->timezone).'</b><br />
<i>'.$thispart['status'].'</i></div>';
}
$str .= '</div>';
if (empty($htmlarray[$turnitintool->course]['turnitintool'])) {
$htmlarray[$turnitintool->course]['turnitintool'] = $str;
} else {
$htmlarray[$turnitintool->course]['turnitintool'] .= $str;
}
}
}
}
/**
* A function to return a Turnitin User ID if one exists in turnitintool_users
* or returns NULL if we do not have a record for that user yet
*
* @param object $owner A data object for the owner user of the Turnitin Class
* @return var A Turnitin User ID or NULL
*/
function turnitintool_getUID($owner) {
if (is_null($owner) OR !$turnitintool_user = turnitintool_get_record("turnitintool_users", "userid", $owner->id)) {
return NULL;
} else {
return ( isset($turnitintool_user->turnitin_uid) AND $turnitintool_user->turnitin_uid > 0 )
? $turnitintool_user->turnitin_uid : NULL;
}
}
/**
* A function to return a Turnitin Class ID if one exists in turnitintool_courses
* or fails with an error if the record does not exist
*
* @param var $courseid The ID of the Moodle course to check against
* @return var A Turnitin Class ID
*/
function turnitintool_getCID($courseid) {
if (!$turnitintool_course = turnitintool_get_record("turnitintool_courses", "courseid", $courseid)) {
turnitintool_print_error('coursegeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
} else {
return $turnitintool_course->turnitin_cid;
}
}
/**
* A function to return the Turnitin Class Title if one exists in turnitintool_courses
* or fails with an error if the record does not exist
*
* @param var $courseid The ID of the Moodle course to check against
* @return var A Turnitin Class Title
*/
function turnitintool_getCTL($courseid) {
if (!$turnitintool_course = turnitintool_get_record("turnitintool_courses", "courseid", $courseid)) {
turnitintool_print_error('coursegeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
} else {
return $turnitintool_course->turnitin_ctl;
}
}
/**
* A function to return the Turnitin Assignment ID if one exists in turnitintool
* or fails with an error if the record does not exist
*
* @param var $courseid The ID of the Moodle assignment to check against
* @return var A Turnitin Assignment ID
*/
function turnitintool_getAID($partid) {
if (!$turnitintool_assign = turnitintool_get_record("turnitintool_parts", "id", $partid)) {
turnitintool_print_error('assigngeterror','turnitintool',NULL,NULL,__FILE__,__LINE__);
exit();
} else {
return $turnitintool_assign->tiiassignid;
}
}
/**
* Does a user setup up routine, if the user exists in turnitintool_users then the Turnitin User ID is returned for that user
* if not then A call to FID1 to Create the User is called and the ID is returned and stored in turnitintool_users
*
* This is required when user not present calls are made or when there may not yet be a class owner to make the call through the API
* specifically through reset course and add_instance
*
* @param object $userdata The moodle user object for the user we are setting up
* @param string $status The status message that is displayed in the loader bar if the user need to be created
* @param object $tii The turnitintool_commclass object is passed by reference
* @param object $loaderbar The turnitintool_loaderbarclass object is passed by reference can be NULL if no loader bar is to be used
* @return object A turnitintool_users data object that was either created or found during this call
*/
function turnitintool_usersetup($userdata,$status='',&$tii,&$loaderbar) {
global $CFG;