Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / assignment / type / uploadsingle / assignment.class.php
blobcf71718524486c1df8c78304993197298352e6bf
1 <?php // $Id$
3 /**
4 * Extend the base assignment class for assignments where you upload a single file
6 */
7 class assignment_uploadsingle extends assignment_base {
10 function print_student_answer($userid, $return=false){
11 global $CFG, $USER;
13 $filearea = $this->file_area_name($userid);
15 $output = '';
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");
25 //died right here
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>';
34 return $output;
37 function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
38 parent::assignment_base($cmid, $assignment, $cm, $course);
39 $this->type = 'uploadsingle';
42 function view() {
44 global $USER;
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);
51 $this->view_header();
53 $this->view_intro();
55 $this->view_dates();
57 $filecount = $this->count_user_files($USER->id);
59 if ($submission = $this->get_submission()) {
60 if ($submission->timemarked) {
61 $this->view_feedback();
63 if ($filecount) {
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();
72 $this->view_footer();
76 function view_upload_form() {
77 global $CFG;
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').'" />';
92 echo '</fieldset>';
93 echo '</form>';
94 echo '</div>';
98 function upload() {
100 global $CFG, $USER;
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();
122 if ($submission) {
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'));
135 } else {
136 notify(get_string("uploadfailnoupdate", "assignment"));
138 } else {
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'));
149 } else {
150 notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
154 } else {
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);