-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.php
More file actions
129 lines (112 loc) · 4.19 KB
/
package.php
File metadata and controls
129 lines (112 loc) · 4.19 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
<?PHP
require_once("../../config.php");
require_once("lib.php");
require_once("../../lib/filelib.php");
$a = optional_param('a', 0, PARAM_INT); // programming ID
$group = optional_param('group', 0, PARAM_INT);
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:viewotherprogram', $context);
add_to_log($course->id, 'programming', 'package', me(), $programming->id);
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
foreach ($latestsubmits as $submit) {
if ($submit->language == 1) $ext = '.c';
elseif ($submit->language == 2) $ext = '.cxx';
$filename = $dirname.'/'.$users[$submit->userid]->username.'-'.$submit->id.$ext;
$files[] = $filename;
$f = fopen($filename, 'w');
fwrite($f, $submit->code);
fwrite($f, "\r\n");
fclose($f);
}
// zip file
// eli changed ! 2009-8-18 22:37:12
$dest = $CFG->dataroot.'/'.$course->id;
if (!file_exists($dest)) {
mkdir($dest, 0777) or error('Failed to create dir');
}
$dest .= '/programming-'.$programming->id;
if ($group === 0) {
$dest .= '-all';
} else {
$group_obj = get_current_group($course->id, True);
$dest .= '-'.$group_obj->name;
}
$dest .= '.zip';
if (file_exists($dest)) {
unlink($dest) or error("Failed to delete dest file");
}
zip_files($files, $dest);
// remove temp
fulldelete($dirname);
$g = $group === 0 ? 'all' : $group_obj->name;
$filelink = $CFG->wwwroot.($CFG->slasharguments ? '/file.php/' : '/file.php?file=/').$course->id.'/programming-'.$programming->id.'-'.$g.'.zip';
$count = count($files);
$referer = $_SERVER['HTTP_REFERER'];
/// Print the page header
$pagename = get_string('package', 'programming');
include_once('pageheader.php');
echo '<div class="maincontent generalbox">';
echo '<p>'.get_string('packagesuccess', 'programming').'</p>';
echo "<p><a href='$filelink'>".get_string('download', 'programming').'</a></p>';
echo "<p><a href='$referer'>".get_string('return', 'programming').'</a></p>';
echo '</div>';
/// Finish the page
print_footer($course);
?>