MDL-11515:
[moodle-linuxchix.git] / mod / assignment / type / uploadsingle / assignment.class.php
blob53d6e48af484d86d163fc35bff052db79cdd6d82
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)) {
20 foreach ($files as $key => $file) {
21 require_once($CFG->libdir.'/filelib.php');
23 $icon = mimeinfo('icon', $file);
25 if ($CFG->slasharguments) {
26 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
27 } else {
28 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
30 //died right here
31 //require_once($ffurl);
32 $output = '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
33 '<a href="'.$ffurl.'" >'.$file.'</a><br />';
38 $output = '<div class="files">'.$output.'</div>';
39 return $output;
42 function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
43 parent::assignment_base($cmid, $assignment, $cm, $course);
47 function view() {
49 global $USER;
51 $context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
52 require_capability('mod/assignment:view', $context);
54 add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
56 $this->view_header();
58 $this->view_intro();
60 $this->view_dates();
62 $filecount = $this->count_user_files($USER->id);
64 if ($submission = $this->get_submission()) {
65 if ($submission->timemarked) {
66 $this->view_feedback();
68 if ($filecount) {
69 print_simple_box($this->print_user_files($USER->id, true), 'center');
73 if (has_capability('mod/assignment:submit', $context) && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
74 $this->view_upload_form();
77 $this->view_footer();
81 function view_upload_form() {
82 global $CFG;
83 $struploadafile = get_string("uploadafile");
84 $strmaxsize = get_string("maxsize", "", display_size($this->assignment->maxbytes));
86 echo '<div style="text-align:center">';
87 echo '<form enctype="multipart/form-data" method="post" '.
88 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
89 echo '<fieldset class="invisiblefieldset">';
90 echo "<p>$struploadafile ($strmaxsize)</p>";
91 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
92 require_once($CFG->libdir.'/uploadlib.php');
93 upload_print_form_fragment(1,array('newfile'),false,null,0,$this->assignment->maxbytes,false);
94 echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
95 echo '</fieldset>';
96 echo '</form>';
97 echo '</div>';
101 function upload() {
103 global $CFG, $USER;
105 require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
107 $this->view_header(get_string('upload'));
109 $filecount = $this->count_user_files($USER->id);
110 $submission = $this->get_submission($USER->id);
111 if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
112 if ($submission = $this->get_submission($USER->id)) {
113 //TODO: change later to ">= 0", to prevent resubmission when graded 0
114 if (($submission->grade > 0) and !$this->assignment->resubmit) {
115 notify(get_string('alreadygraded', 'assignment'));
119 $dir = $this->file_area_name($USER->id);
121 require_once($CFG->dirroot.'/lib/uploadlib.php');
122 $um = new upload_manager('newfile',true,false,$this->course,false,$this->assignment->maxbytes);
123 if ($um->process_file_uploads($dir)) {
124 $newfile_name = $um->get_new_filename();
125 if ($submission) {
126 $submission->timemodified = time();
127 $submission->numfiles = 1;
128 $submission->submissioncomment = addslashes($submission->submissioncomment);
129 unset($submission->data1); // Don't need to update this.
130 unset($submission->data2); // Don't need to update this.
131 if (update_record("assignment_submissions", $submission)) {
132 add_to_log($this->course->id, 'assignment', 'upload',
133 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
134 $this->email_teachers($submission);
135 print_heading(get_string('uploadedfile'));
136 } else {
137 notify(get_string("uploadfailnoupdate", "assignment"));
139 } else {
140 $newsubmission = $this->prepare_new_submission($USER->id);
141 $newsubmission->timemodified = time();
142 $newsubmission->numfiles = 1;
143 if (insert_record('assignment_submissions', $newsubmission)) {
144 add_to_log($this->course->id, 'assignment', 'upload',
145 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
146 $this->email_teachers($newsubmission);
147 print_heading(get_string('uploadedfile'));
148 } else {
149 notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
153 } else {
154 notify(get_string("uploaderror", "assignment")); //submitting not allowed!
157 print_continue('view.php?id='.$this->cm->id);
159 $this->view_footer();
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);