forked from thanhtri/plagiarism_programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportlib.php
608 lines (547 loc) · 21.3 KB
/
reportlib.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
<?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/>.
/**
* Functions to generate the report.
*
* Provide the site-wide setting and specific configuration for each assignment.
*
* @package plagiarism_programming
* @copyright 2015 thanhtri, 2019 Benedikt Schneider (@Nullmann)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Access to internal script forbidden');
require_once($CFG->dirroot . '/user/lib.php');
define('CHART_WIDTH', 800);
define('CHART_HEIGHT', 240);
define('BAR_WIDTH', 20);
/**
* Create the similarity table in grouping mode, in which each row lists all the similarity rate of a student to all others.
* This was renamed to matrix mode in version 2.
*
* @param Array $list A list of records of plagiarism_programming_reslt table (in DESC order) for the selected detector.
* Not altered by the function. Passed by reference for performance only.
* @param Array $studentnames Associative array id=>name of the students in this assignment. Not altered by the function.
* Passed by reference for performance only.
* @return $table The html_table object
*/
function plagiarism_programming_create_table_grouping_mode(&$list, &$studentnames) {
$similaritytable = array();
foreach ($list as $pair) {
// Make sure student1 id > student2 id to avoid repetition latter.
$student1 = max($pair->student1_id, $pair->student2_id);
$student2 = min($pair->student1_id, $pair->student2_id);
if (is_numeric($student1)) {
$similaritytable[$student1][$student2] = array(
'rate' => $pair->similarity,
'file' => $pair->comparison,
'id' => $pair->id,
'mark' => $pair->mark
);
}
if (is_numeric($student2)) { // Only add a line for student2 if it is a real student.
$similaritytable[$student2][$student1] = array(
'rate' => $pair->similarity,
'file' => $pair->comparison,
'id' => $pair->id,
'mark' => $pair->mark
);
}
}
$table = new html_table();
$table->attributes['class'] = 'plagiarism_programming_result_table generaltable';
foreach ($similaritytable as $studentid => $similarityarray) {
$row = new html_table_row();
// First cell.
$cell = new html_table_cell();
$cell->text = $studentnames[$studentid];
$row->cells[] = $cell;
// Arrow cell.
$cell = new html_table_cell();
$cell->text = '→';
$row->cells[] = $cell;
foreach ($similarityarray as $stud2id => $similarity) {
$cell = new html_table_cell();
$comparelink = html_writer::link('view_compare.php?id=' . $similarity['id'], round($similarity['rate'], 2) . '%', array(
'class' => 'compare_link'
));
$cell->text = plagiarism_programming_create_student_link($studentnames[$stud2id], $stud2id). '<br/>' . $comparelink;
$mark = $similarity['mark'];
$cell->attributes['class'] = ($mark == 'Y') ? 'suspicious' : (($mark == 'N') ? 'normal' : '');
$cell->attributes['class'] .= ' similar_pair';
$cell->attributes['pair'] = $similarity['id'];
$row->cells[] = $cell;
}
$table->data[] = $row;
}
return $table;
}
/**
* Create the similarity table in list mode, in which pairs of students are listed in descending similarity rate.
*
* @param Object $list A list of records of plagiarism_programming_reslt table in DESC order for the selected detector.
* Not altered by the function.
* Passed by reference for performance only
* @param Array $studentnames Associative array id=>name of the students in this assignment. Not altered by the function.
* Passed by reference for performance only
* @param Number $anchorstudentid ID of anchor student. If anchor is specified, the anchored student will always appear on the left.
* @param Boolean $isteacher
* @return $table The html_table object.
*/
function plagiarism_programming_create_table_list_mode(&$list, &$studentnames, $anchorstudentid = null, $isteacher, $seemark) {
$table = new html_table();
$table->attributes['class'] = 'plagiarism_programming_result_table generaltable';
$rownum = 1;
foreach ($list as $pair) {
// If an anchor is specified and the anchor is the 2nd student, make the anchor student1.
if ($anchorstudentid && $anchorstudentid != $pair->student1_id) {
$temp = $pair->student1_id;
$pair->student1_id = $pair->student2_id;
$pair->student2_id = $temp;
unset($temp);
}
$student1name = $studentnames[$pair->student1_id];
$student2name = $studentnames[$pair->student2_id];
$row = new html_table_row();
$cell = new html_table_cell($rownum ++);
$row->cells[] = $cell;
// First Row, student 1.
$cell = new html_table_cell();
$cell->text = $student1name;
$row->cells[] = $cell;
// Second Row, student 2.
$cell = new html_table_cell();
if ($isteacher) {
$cell->text = plagiarism_programming_create_student_link($student2name, $pair->student2_id);
} else {
$cell->text = $student2name;
}
$row->cells[] = $cell;
// Third Row, Percentage.
$cell = new html_table_cell();
if ($isteacher) {
$cell->text = html_writer::link("view_compare.php?id=$pair->id", round($pair->similarity, 2) . '%', array(
'class' => 'compare_link'
));
} else {
$cell->text = round($pair->similarity, 2) . '%';
}
$row->cells[] = $cell;
// Do not show the mark as suspicious or normal if a student is seeing the table.
if ($isteacher or $seemark) {
$mark = $pair->mark;
$row->attributes['class'] = ($mark == 'Y') ? 'suspicious' : (($mark == 'N') ? 'normal' : '');
}
$table->data[] = $row;
}
return $table;
}
/**
* Create the distribution graph of similarity rate of all the students
* @param Number $reportid ID of the report.
* @param String $similaritytype Either average (avg) or maximum (max)
* @return String The html code for the graph
*/
function plagiarism_programming_create_chart($reportid, $similaritytype, $setlowerthreshold, $setupperthreshold) {
global $DB;
$select = "reportid=$reportid";
// Similarity depends on similarity type, "greatest" is supported in all Moodle except SQLServer.
$result = ($similaritytype == 'avg') ? '(similarity1+similarity2)/2' : 'greatest(similarity1,similarity2)';
$similarities = $DB->get_fieldset_select('plagiarism_programming_reslt', $result, $select);
$histogram = array();
for ($i = 10; $i >= 0; $i --) {
$histogram[$i] = 0;
}
foreach ($similarities as $rate) {
$histogram[intval(floor($rate / 10))] ++;
}
// For 100% similar pairs, they are placed in interval 10, now they are merged with the 9th interval.
$histogram[9] += $histogram[10];
unset($histogram[10]);
$maxstudentnum = max($histogram);
if ($maxstudentnum > 0) {
$lengthratio = intval(floor(CHART_WIDTH / $maxstudentnum));
} else {
return '';
}
$div = '';
$reporturl = new moodle_url(qualified_me());
foreach ($histogram as $key => $val) {
$upper = $key * 10 + 10; // Upper threshold of current bar.
$lower = $key * 10; // Lower threshold of current bar.
$range = $lower . '-' . $upper;
$posy = (9 - $key) * (BAR_WIDTH + 5) . 'px'; // 2 is the space between bars.
$width = ($val * $lengthratio) . 'px';
// Range of the bar (numbers to the left on the y axis).
$div .= html_writer::tag('div', $range, array(
'class' => 'legend',
'style' => "top:$posy;width:40px"
));
// Draw the bar itself.
$reporturl->remove_params(array(
'upper_threshold',
'lower_threshold'
));
$reporturl->params(array(
'upper_threshold' => $upper,
'lower_threshold' => $lower
));
$left = "0px";
if ($val > 0) {
$class = 'bar';
if ($setlowerthreshold == $lower && $setupperthreshold == $upper) {
$class .= ' selected'; // Add "selected" class to bar if the bounds are set.
}
$div .= html_writer::link($reporturl->out(false), '', array(
'class' => $class,
'style' => "top:$posy;width:$width"
));
// Draw the number.
$left = (rtrim($width, "px") + 5) . 'px';
$div .= html_writer::tag('div', $val, array(
'class' => 'legend',
'style' => "top:$posy;left:$left"
));
}
}
$posy = (10 * (BAR_WIDTH + 5) - 5) . 'px';
$width = CHART_WIDTH . 'px';
$posy = (CHART_HEIGHT + 10) . 'px';
$div .= html_writer::tag('div', get_string('pair', 'plagiarism_programming'), array(
'class' => 'legend',
'style' => "top:$posy;left:0px"
));
return "<div class='canvas'>$div</div>";
}
/**
* Create HTML output of the lookup table in the top left section
* @param Object $resulttable
* @param Boolean $isteacher
* @param Array $studentnames
* @param Number $courseid
*/
function plagiarism_programming_create_student_lookup_table(&$resulttable, $isteacher, &$studentnames, $courseid) {
global $USER;
$studentnames = array();
if ($isteacher) {
foreach ($resulttable as $pair) {
$studentnames[$pair->student1_id] = $pair->student1_id;
$studentnames[$pair->student2_id] = $pair->student2_id;
}
} else {
foreach ($resulttable as $pair) {
$studentnames[$pair->student1_id] = get_string('another', 'plagiarism_programming');
$studentnames[$pair->student2_id] = get_string('another', 'plagiarism_programming');
}
}
// Find students' name if he is the lecturer.
if ($isteacher) {
// Gets all the users of the course with standard settings.
$students = user_get_participants($courseid, 0, 0, 0, 0, - 1, '');
foreach ($students as $student) {
$studentnames[$student->id] = fullname($student);
}
} else { // If user is a student.
$studentnames[$USER->id] = get_string('yours', 'plagiarism_programming');
}
}
/**
* Create link to detailed view of one student.
* @param String $studentname
* @param Number $studentid
* @return string
*/
function plagiarism_programming_create_student_link($studentname, $studentid) {
global $PAGE;
return html_writer::link("$PAGE->url&student=$studentid", $studentname, array(
'class' => 'plagiarism_programming_student_link'
));
}
/**
* Returns all submissions marked as suspicious
* @param Number $studentid
* @param Number $cmid course module id
* @return array|array
*/
function plagiarism_programming_get_suspicious_works($studentid, $cmid) {
global $DB;
// Get the latest report version.
$version = $DB->get_field('plagiarism_programming_rpt', 'max(version)', array(
'cmid' => $cmid
));
if ($version === null) {
return array();
}
$ids = $DB->get_fieldset_select('plagiarism_programming_rpt', 'id', "cmid=$cmid AND version=$version");
if (! empty($ids)) {
list ($insql, $params) = $DB->get_in_or_equal($ids);
$select = "reportid $insql AND (student1_id=? OR student2_id=?) AND mark=?";
$params[] = $studentid;
$params[] = $studentid;
$params[] = 'Y';
return $DB->get_records_select('plagiarism_programming_reslt', $select, $params);
} else {
return array();
}
}
/**
* Returns the similarity info.
* @param Number $cmid Course Module ID
* @param Number $studid
* @return array|mixed[][]|string[][]|NULL[][]
*/
function plagiarism_programming_get_students_similarity_info($cmid, $studid = null) {
global $DB, $detectiontools;
// Get the enabled plugins.
$setting = $DB->get_record('plagiarism_programming', array(
'cmid' => $cmid
));
// Get the latest report version.
$reports = array();
foreach ($detectiontools as $toolname => $toolinfo) {
if ($setting->$toolname) {
$report = plagiarism_programming_get_latest_report($cmid, $toolname);
if ($report) {
$reports[$report->id] = new stdClass();
$reports[$report->id]->detector = $toolname;
}
}
}
if (count($reports) == 0) {
return array();
}
list ($insql, $params) = $DB->get_in_or_equal(array_keys($reports));
$sql = "SELECT id, student1_id, student2_id, (similarity1+similarity2)/2 as similarity, mark, reportid
FROM {plagiarism_programming_reslt}
WHERE reportid $insql ";
if ($studid !== null) {
$sql .= " AND (student1_id=? OR student2_id=?)";
$params[] = $studid;
$params[] = $studid;
}
$records = $DB->get_records_sql($sql, $params);
$students = array();
foreach ($records as $rec) {
foreach (array(
'student1_id',
'student2_id'
) as $studid) {
if (isset($students[$rec->$studid])) {
$max = max($students[$rec->$studid]['max'], $rec->similarity);
$mark = ($rec->mark == 'Y') ? 'Y' : $students[$rec->$studid]['mark'];
$detector = ($rec->similarity == $max) ? $reports[$rec->reportid]->detector : $students[$rec->$studid]['detector'];
$students[$rec->$studid] = array(
'max' => $max,
'mark' => $mark,
'detector' => $detector
);
} else {
$students[$rec->$studid] = array(
'max' => $rec->similarity,
'mark' => $rec->mark,
'detector' => $reports[$rec->reportid]->detector
);
}
}
}
return $students;
}
/**
* Returns the URL of with optional filters.
* @param Number $cmid Course Module ID
* @param Number $studentid
* @param String $detector Moss or JPlag
* @param Number $threshold Minimum similarity to show.
* @return string
*/
function plagiarism_programming_get_report_link($cmid, $studentid = null, $detector = null, $threshold = null) {
global $CFG;
$link = "$CFG->wwwroot/plagiarism/programming/view.php?cmid=$cmid";
if ($studentid) {
$link .= "&student=$studentid";
}
if ($detector) {
$link .= "&detector=$detector";
}
if ($threshold !== null) {
$link .= "&lower_threshold=$threshold";
}
return $link;
}
/**
* Get the next version of the report for the specified assignment with the detector
*
* @param number $cmid
* the course module id of the assignment. If null, it will return the root directory of all the report
* @param number $detector
* the version of report. If null, it will return the directory of the latest report of this assignment
* @return $report The report record having the latest version.
*/
function plagiarism_programming_get_latest_report($cmid, $detector) {
global $DB;
$version = $DB->get_field('plagiarism_programming_rpt', 'max(version)', array(
'cmid' => $cmid,
'detector' => $detector
));
if ($version !== false) {
$report = $DB->get_record('plagiarism_programming_rpt', array(
'cmid' => $cmid,
'version' => $version,
'detector' => $detector
));
return $report;
} else {
return null;
}
}
/**
* Create the new report version
*
* @param number $cmid
* the course module id of the assignment. If null, it will return the root directory of all the report
* @param number $detector
* the version of report. If null, it will return the directory of the latest report of this assignment
* @return $report The report record created.
*/
function plagiarism_programming_create_new_report($cmid, $detector) {
global $DB;
// Create a new version of the report.
$latestreport = plagiarism_programming_get_latest_report($cmid, $detector);
if ($latestreport) {
$version = $latestreport->version + 1;
} else {
$version = 1;
}
$report = new stdClass();
$report->cmid = $cmid;
$report->time_created = time();
$report->version = $version;
$report->detector = $detector;
$report->id = $DB->insert_record('plagiarism_programming_rpt', $report);
return $report;
}
/**
* Save the similarity of one pair.
* @param Object $pairresult
*/
function plagiarism_programming_save_similarity_pair($pairresult) {
global $DB;
if (! ctype_digit($pairresult->student1_id)) {
$pairresult->additional_codefile_name = $pairresult->student1_id;
$pairresult->student1_id = 0;
} else if (! ctype_digit($pairresult->student2_id)) {
$pairresult->additional_codefile_name = $pairresult->student2_id;
$pairresult->student2_id = 0;
} else {
$pairresult->additional_codefile_name = null;
}
$DB->insert_record('plagiarism_programming_reslt', $pairresult);
}
/**
* Transforms a similarity pair (adds student id's?)
* @param Object $similarpairs
* @return $similarpairs
*/
function plagiarism_programming_transform_similarity_pair($similarpairs) {
if (! is_array($similarpairs)) { // Only one object is passed.
$pairs = array(
$similarpairs
);
} else {
$pairs = $similarpairs;
}
foreach ($pairs as $pair) {
if ($pair && ! empty($pair->additional_codefile_name)) {
if ($pair->student1_id == 0) {
$pair->student1_id = $pair->additional_codefile_name;
} else if ($pair->student2_id == 0) {
$pair->student2_id = $pair->additional_codefile_name;
}
}
}
// Objects are passed by reference and we only modify the object.
return $similarpairs;
}
/**
* Returns the history of one student.
* @param Object $result
* @param string $timesort
* @return array
*/
function plagiarism_programming_get_student_similarity_history($result, $timesort = 'desc') {
global $DB;
$report = $DB->get_record('plagiarism_programming_rpt', array(
'id' => $result->reportid
));
$params = array();
$sql = "SELECT result.*, time_created
FROM {plagiarism_programming_rpt} report
JOIN {plagiarism_programming_reslt} result
ON (report.id = result.reportid)
WHERE report.cmid=:cmid AND report.detector=:detector ";
if ($result->additional_codefile_name === null) {
$sql .= " AND result.additional_codefile_name IS NULL ";
} else {
$sql .= " AND result.additional_codefile_name = :addtional_name ";
$params['addtional_name'] = $result->additional_codefile_name;
}
$sql .= " AND ((result.student1_id=:student1_id1 AND result.student2_id=:student2_id1)
OR (result.student1_id=:student2_id2 AND result.student2_id=:student1_id2))
ORDER BY time_created $timesort";
$params += array(
'cmid' => $report->cmid,
'detector' => $report->detector,
'student1_id1' => $result->student1_id,
'student1_id2' => $result->student1_id,
'student2_id1' => $result->student2_id,
'student2_id2' => $result->student2_id
);
$pairs = $DB->get_records_sql($sql, $params);
return $pairs;
}
/**
* Deletes the config of an activity.
* @param Number $cmid Course Module ID
*/
function plagiarism_programming_delete_config($cmid) {
global $DB;
$setting = $DB->get_record('plagiarism_programming', array(
'cmid' => $cmid
));
$reportids = $DB->get_records_menu('plagiarism_programming', array(
'cmid' => $cmid
), '', 'id,cmid');
if ($setting) {
$DB->delete_records('plagiarism_programming_date', array(
'settingid' => $setting->id
));
$DB->delete_records('plagiarism_programming_jplag', array(
'settingid' => $setting->id
));
$DB->delete_records('plagiarism_programming_moss', array(
'settingid' => $setting->id
));
if (count($reportids) > 0) {
list ($insql, $params) = $DB->get_in_or_equal(array_keys($reportids));
$DB->delete_records('plagiarism_programming_rpt', array(
'cmid' => $setting->cmid
));
$DB->delete_records_select('plagiarism_programming_reslt', "reportid $insql", $params);
}
$DB->delete_records('plagiarism_programming', array(
'id' => $setting->id
));
}
}