4 * Extend the base assignment class for assignments where you upload a single file
7 class assignment_uploadsingle
extends assignment_base
{
10 function print_student_answer($userid, $return=false){
13 $filearea = $this->file_area_name($userid);
17 if ($basedir = $this->file_area($userid)) {
18 if ($files = get_directory_list($basedir)) {
19 require_once($CFG->libdir
.'/filelib.php');
20 foreach ($files as $key => $file) {
22 $icon = mimeinfo('icon', $file);
23 $ffurl = get_file_url("$filearea/$file");
26 //require_once($ffurl);
27 $output = '<img src="'.$CFG->pixpath
.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
28 '<a href="'.$ffurl.'" >'.$file.'</a><br />';
33 $output = '<div class="files">'.$output.'</div>';
37 function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
38 parent
::assignment_base($cmid, $assignment, $cm, $course);
39 $this->type
= 'uploadsingle';
46 $context = get_context_instance(CONTEXT_MODULE
,$this->cm
->id
);
47 require_capability('mod/assignment:view', $context);
49 add_to_log($this->course
->id
, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment
->id
, $this->cm
->id
);
57 $filecount = $this->count_user_files($USER->id
);
59 if ($submission = $this->get_submission()) {
60 if ($submission->timemarked
) {
61 $this->view_feedback();
64 print_simple_box($this->print_user_files($USER->id
, true), 'center');
68 if (has_capability('mod/assignment:submit', $context) && $this->isopen() && (!$filecount ||
$this->assignment
->resubmit ||
!$submission->timemarked
)) {
69 $this->view_upload_form();
76 function view_upload_form() {
78 $struploadafile = get_string("uploadafile");
80 $maxbytes = $this->assignment
->maxbytes
== 0 ?
$this->course
->maxbytes
: $this->assignment
->maxbytes
;
81 $strmaxsize = get_string('maxsize', '', display_size($maxbytes));
83 echo '<div style="text-align:center">';
84 echo '<form enctype="multipart/form-data" method="post" '.
85 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
86 echo '<fieldset class="invisiblefieldset">';
87 echo "<p>$struploadafile ($strmaxsize)</p>";
88 echo '<input type="hidden" name="id" value="'.$this->cm
->id
.'" />';
89 require_once($CFG->libdir
.'/uploadlib.php');
90 upload_print_form_fragment(1,array('newfile'),false,null,0,$this->assignment
->maxbytes
,false);
91 echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
102 require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE
, $this->cm
->id
));
104 $this->view_header(get_string('upload'));
106 $filecount = $this->count_user_files($USER->id
);
107 $submission = $this->get_submission($USER->id
);
108 if ($this->isopen() && (!$filecount ||
$this->assignment
->resubmit ||
!$submission->timemarked
)) {
109 if ($submission = $this->get_submission($USER->id
)) {
110 //TODO: change later to ">= 0", to prevent resubmission when graded 0
111 if (($submission->grade
> 0) and !$this->assignment
->resubmit
) {
112 notify(get_string('alreadygraded', 'assignment'));
116 $dir = $this->file_area_name($USER->id
);
118 require_once($CFG->dirroot
.'/lib/uploadlib.php');
119 $um = new upload_manager('newfile',true,false,$this->course
,false,$this->assignment
->maxbytes
);
120 if ($um->process_file_uploads($dir)) {
121 $newfile_name = $um->get_new_filename();
123 $submission->timemodified
= time();
124 $submission->numfiles
= 1;
125 $submission->submissioncomment
= addslashes($submission->submissioncomment
);
126 unset($submission->data1
); // Don't need to update this.
127 unset($submission->data2
); // Don't need to update this.
128 if (update_record("assignment_submissions", $submission)) {
129 add_to_log($this->course
->id
, 'assignment', 'upload',
130 'view.php?a='.$this->assignment
->id
, $this->assignment
->id
, $this->cm
->id
);
131 $submission = $this->get_submission($USER->id
);
132 $this->update_grade($submission);
133 $this->email_teachers($submission);
134 print_heading(get_string('uploadedfile'));
136 notify(get_string("uploadfailnoupdate", "assignment"));
139 $newsubmission = $this->prepare_new_submission($USER->id
);
140 $newsubmission->timemodified
= time();
141 $newsubmission->numfiles
= 1;
142 if (insert_record('assignment_submissions', $newsubmission)) {
143 add_to_log($this->course
->id
, 'assignment', 'upload',
144 'view.php?a='.$this->assignment
->id
, $this->assignment
->id
, $this->cm
->id
);
145 $submission = $this->get_submission($USER->id
);
146 $this->update_grade($submission);
147 $this->email_teachers($newsubmission);
148 print_heading(get_string('uploadedfile'));
150 notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
155 notify(get_string("uploaderror", "assignment")); //submitting not allowed!
158 print_continue('view.php?id='.$this->cm
->id
);
160 $this->view_footer();
163 function setup_elements(&$mform) {
164 global $CFG, $COURSE;
166 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
168 $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
169 $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
170 $mform->setDefault('resubmit', 0);
172 $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
173 $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
174 $mform->setDefault('emailteachers', 0);
176 $choices = get_max_upload_sizes($CFG->maxbytes
, $COURSE->maxbytes
);
177 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes
).')';
178 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
179 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes
);