mirrored from git://git.moodle.org/moodle.git
/
assignment.class.php
190 lines (146 loc) · 7.62 KB
/
assignment.class.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
<?php // $Id$
/**
* Extend the base assignment class for assignments where you upload a single file
*
*/
class assignment_uploadsingle extends assignment_base {
function print_student_answer($userid, $return=false){
global $CFG, $USER;
$filearea = $this->file_area_name($userid);
$output = '';
if ($basedir = $this->file_area($userid)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $key => $file) {
require_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo('icon', $file);
if ($CFG->slasharguments) {
$ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
} else {
$ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
}
//died right here
//require_once($ffurl);
$output = '<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
'<a href="'.$ffurl.'" >'.$file.'</a><br />';
}
}
}
$output = '<div class="files">'.$output.'</div>';
return $output;
}
function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
parent::assignment_base($cmid, $assignment, $cm, $course);
$this->type = 'uploadsingle';
}
function view() {
global $USER;
$context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
require_capability('mod/assignment:view', $context);
add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
$this->view_header();
$this->view_intro();
$this->view_dates();
$filecount = $this->count_user_files($USER->id);
if ($submission = $this->get_submission()) {
if ($submission->timemarked) {
$this->view_feedback();
}
if ($filecount) {
print_simple_box($this->print_user_files($USER->id, true), 'center');
}
}
if (has_capability('mod/assignment:submit', $context) && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
$this->view_upload_form();
}
$this->view_footer();
}
function view_upload_form() {
global $CFG;
$struploadafile = get_string("uploadafile");
$maxbytes = $this->assignment->maxbytes == 0 ? $this->course->maxbytes : $this->assignment->maxbytes;
$strmaxsize = get_string('maxsize', '', display_size($maxbytes));
echo '<div style="text-align:center">';
echo '<form enctype="multipart/form-data" method="post" '.
"action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
echo '<fieldset class="invisiblefieldset">';
echo "<p>$struploadafile ($strmaxsize)</p>";
echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
require_once($CFG->libdir.'/uploadlib.php');
upload_print_form_fragment(1,array('newfile'),false,null,0,$this->assignment->maxbytes,false);
echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
echo '</fieldset>';
echo '</form>';
echo '</div>';
}
function upload() {
global $CFG, $USER;
require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
$this->view_header(get_string('upload'));
$filecount = $this->count_user_files($USER->id);
$submission = $this->get_submission($USER->id);
if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
if ($submission = $this->get_submission($USER->id)) {
//TODO: change later to ">= 0", to prevent resubmission when graded 0
if (($submission->grade > 0) and !$this->assignment->resubmit) {
notify(get_string('alreadygraded', 'assignment'));
}
}
$dir = $this->file_area_name($USER->id);
require_once($CFG->dirroot.'/lib/uploadlib.php');
$um = new upload_manager('newfile',true,false,$this->course,false,$this->assignment->maxbytes);
if ($um->process_file_uploads($dir)) {
$newfile_name = $um->get_new_filename();
if ($submission) {
$submission->timemodified = time();
$submission->numfiles = 1;
$submission->submissioncomment = addslashes($submission->submissioncomment);
unset($submission->data1); // Don't need to update this.
unset($submission->data2); // Don't need to update this.
if (update_record("assignment_submissions", $submission)) {
add_to_log($this->course->id, 'assignment', 'upload',
'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
$submission = $this->get_submission($USER->id);
$this->update_grade($submission);
$this->email_teachers($submission);
print_heading(get_string('uploadedfile'));
} else {
notify(get_string("uploadfailnoupdate", "assignment"));
}
} else {
$newsubmission = $this->prepare_new_submission($USER->id);
$newsubmission->timemodified = time();
$newsubmission->numfiles = 1;
if (insert_record('assignment_submissions', $newsubmission)) {
add_to_log($this->course->id, 'assignment', 'upload',
'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
$submission = $this->get_submission($USER->id);
$this->update_grade($submission);
$this->email_teachers($newsubmission);
print_heading(get_string('uploadedfile'));
} else {
notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
}
}
}
} else {
notify(get_string("uploaderror", "assignment")); //submitting not allowed!
}
print_continue('view.php?id='.$this->cm->id);
$this->view_footer();
}
function setup_elements(&$mform) {
global $CFG, $COURSE;
$ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
$mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
$mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
$mform->setDefault('resubmit', 0);
$mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
$mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
$mform->setDefault('emailteachers', 0);
$choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
$choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
$mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
$mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
}
}
?>