-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreportlib.php
More file actions
544 lines (479 loc) · 21.2 KB
/
reportlib.php
File metadata and controls
544 lines (479 loc) · 21.2 KB
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
<?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
* @subpackage programming
* @author thanhtri
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Access to internal script forbidden');
define('CHART_WITH', 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.
* Used to create the similarity report
* @param $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 $student_names: associative array id=>name of the students in this assignment. Not altered by the function.
* Passed by reference for performance only
* @param $cmid: course module id of the assignment
* @return the html_table object
*/
function plagiarism_programming_create_table_grouping_mode(&$list, &$student_names) {
$similarity_table = 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)) {
$similarity_table[$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
$similarity_table[$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 ($similarity_table as $s_id => $similarity_array) {
$row = new html_table_row();
// first cell
$cell = new html_table_cell();
$cell->text = $student_names[$s_id];
$row->cells[] = $cell;
// arrow cell
$cell = new html_table_cell();
$cell->text = '→';
$row->cells[] = $cell;
foreach ($similarity_array as $s2_id => $similarity) {
$cell = new html_table_cell();
$compare_link = html_writer::link('view_compare.php?id='.$similarity['id'], round($similarity['rate'], 2).'%',
array('class'=>'compare_link'));
$cell->text = plagiarism_programming_create_student_link($student_names[$s2_id], $s2_id).'<br/>'.$compare_link;
$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
* Used to create the similarity report
* @param $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 $student_names: associative array id=>name of the students in this assignment. Not altered by the function.
* Passed by reference for performance only
* @param $anchor: if anchor is specified, the anchored student will always appear on the left
* @return the html_table object
*/
function plagiarism_programming_create_table_list_mode(&$list, &$student_names, $anchor=null) {
$table = new html_table();
$table->attributes['class'] = 'plagiarism_programming_result_table generaltable';
$rownum = 1;
foreach ($list as $pair) {
$student1 = $student_names[$pair->student1_id];
$student2 = $student_names[$pair->student2_id];
if ($anchor && $anchor!=$pair->student1_id) {
$temp = $student1;
$student1 = $student2;
$student2 = $temp;
unset($temp);
}
$row = new html_table_row();
$cell = new html_table_cell($rownum++);
$row->cells[] = $cell;
$cell = new html_table_cell();
$cell->text = plagiarism_programming_create_student_link($student1, $pair->student1_id);
$row->cells[] = $cell;
$cell = new html_table_cell();
$cell->text = plagiarism_programming_create_student_link($student2, $pair->student2_id);
$row->cells[] = $cell;
$cell = new html_table_cell();
$cell->text = html_writer::link("view_compare.php?id=$pair->id", round($pair->similarity, 2).'%', array('class'=>'compare_link'));
$row->cells[] = $cell;
$mark = $pair->mark;
$row->attributes['class'] = ($mark=='Y')?'suspicious':(($mark=='N')?'normal':'');
$table->data[] = $row;
}
return $table;
}
function plagiarism_programming_create_diagram(&$cmid, &$list, &$student_names) {
require_once('graphviz/config.php');
require_once('graphviz/graph.php');
require_once('graphviz/lib.php');
drawForm($cmid);
$g = new graph();
$similarity_table = 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)) {
$similarity_table[$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
$similarity_table[$student2][$student1] =
array('rate'=>$pair->similarity,
'file'=>$pair->comparison,
'id'=>$pair->id,
'mark'=>$pair->mark);
}
}
$datasets = '';
$labels = array();
$sortedLabel = array();
$table = new html_table();
$table->attributes['class']='plagiarism_programming_result_table generaltable';
foreach ($similarity_table as $s_id => $similarity_array) {
$row = new html_table_row();
// first cell
$cell = new html_table_cell();
$labels[] = $student_names[$s_id];
$cell->text = $student_names[$s_id];
$row->cells[] = $cell;
// arrow cell
$cell = new html_table_cell();
$cell->text = '→';
$row->cells[] = $cell;
$data = '';
foreach ($similarity_array as $s2_id => $similarity) {
$cell = new html_table_cell();
$compare_link = html_writer::link('view_compare.php?id='.$similarity['id'], round($similarity['rate'], 2).'%',
array('class'=>'compare_link'));
$cell->text = plagiarism_programming_create_student_link($student_names[$s2_id], $s2_id).'<br/>'.$compare_link;
$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;
if (@!$sortedLabel[$s_id . "_" . $s2_id] && @!$sortedLabel[$s2_id . "_" . $s_id]) {
$data .= '["' . $student_names[$s_id] . '", "' . $student_names[$s2_id] . '", ' . round($similarity['rate'], 2) . "],";
$name_1 = str_replace(" ", "_", $student_names[$s_id]);
$name_2 = str_replace(" ", "_", $student_names[$s2_id]);
$g->addEdge($name_1, $name_2, "", round($similarity['rate'], 2),
round($similarity['rate'], 2), "678");
}
$sortedLabel[$s_id . "_" . $s2_id] = round($similarity['rate'], 2);
$sortedLabel[$s2_id . "_" . $s_id] = round($similarity['rate'], 2);
$rates[] = round($similarity['rate'], 2);
}
$datasets .= $data;
$table->data[] = $row;
}
showMoss($g, "");
$radar_chart_data = array ("labels" => $labels, "datasets" => $datasets);
$radar_str = json_encode($radar_chart_data);
return "";
}
/**
* Create the distribution graph of similarity rate of all the students
* @param $cmid: the course module id
* @param $tool: the name of the tool (either JPlag or MOSS)
* @param $similarity_type: either average (avg) or maximum (max)
* @return the html code for the graph
*/
function plagiarism_programming_create_chart($reportid, $similarity_type) {
global $DB;
$select = "reportid=$reportid";
// similarity depends on similarity type, "greatest" is supported in all Moodle except SQLServer
$result = ($similarity_type=='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]);
$max_student_num = max($histogram);
if ($max_student_num>0) {
$length_ratio = intval(floor(CHART_WITH/$max_student_num));
} else {
return '';
}
$div = '';
$report_url = new moodle_url(qualified_me());
foreach ($histogram as $key => $val) {
$upper = $key*10+10;
$lower = $key*10;
$range = $lower.'-'.$upper;
$pos_y = (9-$key)*(BAR_WIDTH+5).'px'; // 2 is the space between bars
$width = ($val*$length_ratio).'px';
// legend of the bar
$div .= html_writer::tag('div', $range, array('class'=>'legend', 'style'=>"top:$pos_y;width:40px"));
// the bar itself
$report_url->remove_params(array('upper_threshold', 'lower_threshold'));
$report_url->params(array('upper_threshold'=>$upper, 'lower_threshold'=>$lower));
// number of pairs
if ($val>0) {
$div .= html_writer::link($report_url->out(false), '', array('class'=>'bar', 'style'=>"top:$pos_y;width:$width"));
$left = ($width+5).'px';
$div .= html_writer::tag('div', $val,
array('class'=>'legend', 'style'=>"top:$pos_y;left:$left"));
}
}
$pos_y = (10*(BAR_WIDTH+5)-5).'px';
$width = CHART_WITH.'px';
//$div .= html_writer::tag('div', '', array('class'=>'bar', 'style'=>"top:$pos_y;width:$width;height:1px"));
$pos_y = (CHART_HEIGHT+10).'px';
$div .= html_writer::tag('div', get_string('pair', 'plagiarism_programming'),
array('class'=>'legend', 'style'=>"top:$pos_y;left:0px"));
return "<div class='canvas'>$div</div>";
}
/**
* Create HTML output of the lookup table in the top left section
*/
function plagiarism_programming_create_student_lookup_table(&$result_table, $is_teacher, &$student_names) {
global $USER, $DB;
$student_names = array();
if ($is_teacher) {
foreach ($result_table as $pair) {
$student_names[$pair->student1_id] = $pair->student1_id;
$student_names[$pair->student2_id] = $pair->student2_id;
}
} else {
foreach ($result_table as $pair) {
$student_names[$pair->student1_id] = "someone's";
$student_names[$pair->student2_id] = "someone's";
}
}
// find students' name if he is the lecturer
if ($is_teacher) {
$ids = array_keys($student_names);
$students = $DB->get_records_list('user', 'id', $ids, null, 'id,firstname,lastname');
foreach ($students as $student) {
$student_names[$student->id] = fullname($student);
}
} else { // if user is a student
$student_names[$USER->id] = 'Yours';
}
}
function plagiarism_programming_create_student_link($student_name, $student_id) {
$report_url = me();
return html_writer::link("$report_url&student=$student_id", $student_name,
array('class'=>'plagiarism_programming_student_link'));
}
function plagiarism_programming_get_suspicious_works($student_id, $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[]= $student_id;
$params[]= $student_id;
$params[]= 'Y';
return $DB->get_records_select('plagiarism_programming_reslt', $select, $params);
} else {
return array();
}
}
function plagiarism_programming_get_students_similarity_info($cmid, $student_id=null) {
global $DB, $detection_tools;
// get the enabled plugins
$setting = $DB->get_record('plagiarism_programming', array('cmid' => $cmid));
// get the latest report version
$reports = array();
foreach ($detection_tools 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 ($student_id!==null) {
$sql .= " AND (student1_id=? OR student2_id=?)";
$params[] = $student_id;
$params[] = $student_id;
}
$records = $DB->get_records_sql($sql, $params);
$students = array();
foreach ($records as $rec) {
foreach (array('student1_id', 'student2_id') as $student_id) {
if (isset($students[$rec->$student_id])) {
$max = max($students[$rec->$student_id]['max'], $rec->similarity);
$mark = ($rec->mark=='Y')?'Y':$students[$rec->$student_id]['mark'];
$detector = ($rec->similarity==$max)?$reports[$rec->reportid]->detector:$students[$rec->$student_id]['detector'];
$students[$rec->$student_id] = array('max'=>$max, 'mark'=>$mark, 'detector'=>$detector);
} else {
$students[$rec->$student_id] = array('max'=>$rec->similarity, 'mark'=>$rec->mark,
'detector'=>$reports[$rec->reportid]->detector);
}
}
}
return $students;
}
function plagiarism_programming_get_report_link($cmid, $student_id=null, $detector=null, $threshold=null) {
global $CFG;
$link = "$CFG->wwwroot/plagiarism/programming/view.php?cmid=$cmid";
if ($student_id) {
$link .= "&student=$student_id";
}
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 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 the report record created
*/
function plagiarism_programming_create_new_report($cmid, $detector) {
global $DB;
// create a new version of the report
$latest_report = plagiarism_programming_get_latest_report($cmid, $detector);
if ($latest_report) {
$version = $latest_report->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;
}
function plagiarism_programming_save_similarity_pair($pair_result) {
global $DB;
if (!ctype_digit($pair_result->student1_id)) {
$pair_result->additional_codefile_name = $pair_result->student1_id;
$pair_result->student1_id = 0;
} else if (!ctype_digit($pair_result->student2_id)) {
$pair_result->additional_codefile_name = $pair_result->student2_id;
$pair_result->student2_id = 0;
} else {
$pair_result->additional_codefile_name = null;
}
$DB->insert_record('plagiarism_programming_reslt', $pair_result);
}
function plagiarism_programming_transform_similarity_pair($similar_pairs) {
if (!is_array($similar_pairs)) { // only one object is passed
$pairs = array($similar_pairs);
} else {
$pairs = $similar_pairs;
}
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 $similar_pairs;
}
function plagiarism_programming_get_student_similarity_history($result, $time_sort='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 $time_sort";
$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;
}
function plagiarism_programming_delete_config($cmid) {
global $DB;
$setting = $DB->get_record('plagiarism_programming', array('cmid'=>$cmid));
$report_ids = $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_moss', array('settingid'=>$setting->id));
if (count($report_ids)>0) {
list($insql, $params) = $DB->get_in_or_equal(array_keys($report_ids));
$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));
}
}