-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresemble_analyze.php
128 lines (109 loc) · 4.07 KB
/
resemble_analyze.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
<?php
require_once('../../config.php');
require_once('lib.php');
require_once("../../lib/filelib.php");
require_once('resemble_analyze_lib.php');
$a = optional_param('a', 0, PARAM_INT); // programming ID
$group = optional_param('group', 0, PARAM_INT);
$action = optional_param('action', 0, PARAM_CLEAN);
$max = optional_param('max', 0, PARAM_INT);
$lowest = optional_param('lowest', 0, PARAM_INT);
//from package.php
if (! $programming = get_record('programming', 'id', $a)) {
error('Course module is incorrect');
}
if (! $course = get_record('course', 'id', $programming->course)) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('programming', $programming->id, $course->id)) {
error('Course Module ID was incorrect');
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_login($course->id);
require_capability('mod/programming:updateresemble', $context);
add_to_log($course->id, 'programming', 'resemble_analyze', me(), $programming->id);
/// Print the page header
$pagename = get_string('resemble_analyze', 'programming');
// cross-site xmlhttprequest is not allowed :(
//$CFG->scripts[] = 'resemble_analyze.js';
include_once('pageheader.php');
/// Print tabs
$currenttab = 'resemble';
$currenttab2 = 'resemble-analyze';
include_once('tabs.php');
/// Print page content
if ($action) {
if ($group != 0) {
$users = get_group_users($group);
} else {
$mygroupid = mygroupid($course->id);
if ($mygroupid) {
$users = get_group_users($mygroupid);
} else {
$users = False;
}
}
$sql = "SELECT * FROM {$CFG->prefix}programming_submits WHERE programmingid={$programming->id}";
if (is_array($users)) {
$sql .= ' AND userid IN ('.implode(',', array_keys($users)).')';
}
$sql .= ' ORDER BY timemodified DESC';
$submits = get_records_sql($sql);
$users = array();
$latestsubmits = array();
if (is_array($submits)) {
foreach ($submits as $submit) {
if (in_array($submit->userid, $users)) continue;
$users[] = $submit->userid;
$latestsubmits[] = $submit;
}
}
$sql = 'SELECT * FROM '.$CFG->prefix.'user WHERE id IN ('.implode(',', $users).')';
$users = get_records_sql($sql);
// create dir
$dirname = $CFG->dataroot.'/temp';
if (!file_exists($dirname)) {
mkdir($dirname, 0777) or ('Failed to create dir');
}
$dirname .= '/programming';
if (!file_exists($dirname)) {
mkdir($dirname, 0777) or ('Failed to create dir');
}
$dirname .= '/'.$programming->id;
if (file_exists($dirname)) {
if (is_dir($dirname)) {
fulldelete($dirname) or error('Failed to remove dir contents');
//rmdir($dirname) or error('Failed to remove dir');
} else {
unlink($dirname) or error('Failed to delete file');
}
}
mkdir($dirname, 0700) or error('Failed to create dir');
$files = array();
// write files
$exts = array('.txt', '.c', '.cxx', '.java', '.java', '.pas', '.py', '.cs');
foreach ($latestsubmits as $submit) {
$ext = $exts[$submit->language];
$filename = "{$dirname}/{$submit->userid}-{$submit->id}{$ext}";
$files[] = $filename;
$f = fopen($filename, 'w');
fwrite($f, $submit->code);
fwrite($f, "\r\n");
fclose($f);
}
//echo "dir is $dirname <br />";
$cwd = getcwd();
chdir($dirname);
$url = array();
exec("perl $cwd/moss.pl -u {$CFG->programming_moss_userid} *", $url);
$url = $url[count($url)-1];
echo "See result $url <br />";
// remove temp
fulldelete($dirname);
parse_result($programming->id, $url, $max, $lowest);
} else {
include_once('resemble_analyze.tpl.php');
}
/// Finish the page
print_footer($course);
?>