886117b587d5849bd6800ad93c212ccde422d019
[moodle.git] / mod / assignment / type / uploadsingle / assignment.class.php
1 <?php // $Id$
3 /**
4  * Extend the base assignment class for assignments where you upload a single file
5  *
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 />';
29                 }
30             }
31         }
33         $output = '<div class="files">'.$output.'</div>';
34         return $output;
35     }
37     function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
38         parent::assignment_base($cmid, $assignment, $cm, $course);
39         $this->type = 'uploadsingle';
40     }
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();
62             }
63             if ($filecount) {
64                 print_simple_box($this->print_user_files($USER->id, true), 'center');
65             }
66         }
68         if (has_capability('mod/assignment:submit', $context)  && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
69             $this->view_upload_form();
70         }
72         $this->view_footer();
73     }
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>';
95     }
98     function upload() {
99         global $CFG, $USER, $DB;
101         require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
103         $this->view_header(get_string('upload'));
105         $filecount = $this->count_user_files($USER->id);
106         $submission = $this->get_submission($USER->id);
107         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
108             if ($submission = $this->get_submission($USER->id)) {
109                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
110                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
111                     notify(get_string('alreadygraded', 'assignment'));
112                 }
113             }
115             $dir = $this->file_area_name($USER->id);
117             require_once($CFG->dirroot.'/lib/uploadlib.php');
118             $um = new upload_manager('newfile',true,false,$this->course,false,$this->assignment->maxbytes);
119             if ($um->process_file_uploads($dir)) {
120                 $newfile_name = $um->get_new_filename();
121                 if ($submission) {
122                     $submission->timemodified = time();
123                     $submission->numfiles     = 1;
124                     $submission->submissioncomment = $submission->submissioncomment;
125                     unset($submission->data1);  // Don't need to update this.
126                     unset($submission->data2);  // Don't need to update this.
127                     if ($DB->update_record("assignment_submissions", $submission)) {
128                         add_to_log($this->course->id, 'assignment', 'upload',
129                                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
130                         $submission = $this->get_submission($USER->id);
131                         $this->update_grade($submission);
132                         $this->email_teachers($submission);
133                         print_heading(get_string('uploadedfile'));
134                     } else {
135                         notify(get_string("uploadfailnoupdate", "assignment"));
136                     }
137                 } else {
138                     $newsubmission = $this->prepare_new_submission($USER->id);
139                     $newsubmission->timemodified = time();
140                     $newsubmission->numfiles = 1;
141                     if ($DB->insert_record('assignment_submissions', $newsubmission)) {
142                         add_to_log($this->course->id, 'assignment', 'upload',
143                                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
144                         $submission = $this->get_submission($USER->id);
145                         $this->update_grade($submission);
146                         $this->email_teachers($newsubmission);
147                         print_heading(get_string('uploadedfile'));
148                     } else {
149                         notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
150                     }
151                 }
152             }
153         } else {
154             notify(get_string("uploaderror", "assignment")); //submitting not allowed!
155         }
157         print_continue('view.php?id='.$this->cm->id);
159         $this->view_footer();
160     }
162     function setup_elements(&$mform) {
163         global $CFG, $COURSE;
165         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
167         $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
168         $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
169         $mform->setDefault('resubmit', 0);
171         $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
172         $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
173         $mform->setDefault('emailteachers', 0);
175         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
176         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
177         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
178         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
180     }
182     function portfolio_exportable() {
183         return true;
184     }
188 ?>