|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00024 require_once($CFG->dirroot.'/mod/assignment/lib.php'); 00025 require_once(dirname(__FILE__).'/upload_form.php'); 00026 00027 class assignment_uploadsingle extends assignment_base { 00028 00029 00030 function print_student_answer($userid, $return=false){ 00031 global $CFG, $USER, $OUTPUT; 00032 00033 $fs = get_file_storage(); 00034 $browser = get_file_browser(); 00035 00036 $output = ''; 00037 00038 if ($submission = $this->get_submission($userid)) { 00039 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { 00040 foreach ($files as $file) { 00041 $filename = $file->get_filename(); 00042 $found = true; 00043 $mimetype = $file->get_mimetype(); 00044 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename); 00045 $output .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a><br />'; 00046 $output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment)); 00047 $output .='<br/>'; 00048 } 00049 } 00050 } 00051 00052 $output = '<div class="files">'.$output.'</div>'; 00053 return $output; 00054 } 00055 00056 function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) { 00057 parent::assignment_base($cmid, $assignment, $cm, $course); 00058 $this->type = 'uploadsingle'; 00059 } 00060 00061 function view() { 00062 00063 global $USER, $OUTPUT; 00064 00065 $context = get_context_instance(CONTEXT_MODULE,$this->cm->id); 00066 require_capability('mod/assignment:view', $context); 00067 00068 add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id); 00069 00070 $this->view_header(); 00071 00072 $this->view_intro(); 00073 00074 $this->view_dates(); 00075 00076 $filecount = false; 00077 00078 if ($submission = $this->get_submission($USER->id)) { 00079 $filecount = $this->count_user_files($submission->id); 00080 if ($submission->timemarked) { 00081 $this->view_feedback(); 00082 } 00083 if ($filecount) { 00084 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter'); 00085 } 00086 } 00087 00088 if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) { 00089 $this->view_upload_form(); 00090 } 00091 00092 $this->view_footer(); 00093 } 00094 00095 00096 function process_feedback() { 00097 if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data? 00098 return false; 00099 } 00100 $userid = required_param('userid', PARAM_INT); 00101 $offset = required_param('offset', PARAM_INT); 00102 $mform = $this->display_submission($offset, $userid, false); 00103 parent::process_feedback($mform); 00104 } 00105 00106 function print_responsefiles($userid, $return=false) { 00107 global $CFG, $USER, $OUTPUT, $PAGE; 00108 00109 $mode = optional_param('mode', '', PARAM_ALPHA); 00110 $offset = optional_param('offset', 0, PARAM_INT); 00111 00112 $output = $OUTPUT->box_start('responsefiles'); 00113 00114 $candelete = $this->can_manage_responsefiles(); 00115 $strdelete = get_string('delete'); 00116 00117 $fs = get_file_storage(); 00118 $browser = get_file_browser(); 00119 00120 if ($submission = $this->get_submission($userid)) { 00121 $renderer = $PAGE->get_renderer('mod_assignment'); 00122 $output .= $renderer->assignment_files($this->context, $submission->id, 'response'); 00123 $output .= $OUTPUT->box_end(); 00124 } 00125 00126 if ($return) { 00127 return $output; 00128 } 00129 echo $output; 00130 } 00131 00132 function can_manage_responsefiles() { 00133 if (has_capability('mod/assignment:grade', $this->context)) { 00134 return true; 00135 } else { 00136 return false; 00137 } 00138 } 00139 00140 function view_upload_form() { 00141 global $OUTPUT, $USER; 00142 echo $OUTPUT->box_start('uploadbox'); 00143 $fs = get_file_storage(); 00144 // edit files in another page 00145 if ($submission = $this->get_submission($USER->id)) { 00146 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { 00147 $str = get_string('editthisfile', 'assignment'); 00148 } else { 00149 $str = get_string('uploadafile', 'assignment'); 00150 } 00151 } else { 00152 $str = get_string('uploadafile', 'assignment'); 00153 } 00154 echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get'); 00155 echo $OUTPUT->box_end(); 00156 } 00157 00158 00159 function upload($mform) { 00160 $action = required_param('action', PARAM_ALPHA); 00161 switch ($action) { 00162 case 'uploadresponse': 00163 $this->upload_responsefile($mform); 00164 break; 00165 case 'uploadfile': 00166 $this->upload_file($mform); 00167 } 00168 } 00169 00170 function upload_file($mform) { 00171 global $CFG, $USER, $DB, $OUTPUT; 00172 $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); 00173 if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) { 00174 redirect($viewurl); 00175 } 00176 00177 $submission = $this->get_submission($USER->id); 00178 $filecount = 0; 00179 if ($submission) { 00180 $filecount = $this->count_user_files($submission->id); 00181 } 00182 if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) { 00183 if ($submission = $this->get_submission($USER->id)) { 00184 //TODO: change later to ">= 0", to prevent resubmission when graded 0 00185 if (($submission->grade > 0) and !$this->assignment->resubmit) { 00186 redirect($viewurl, get_string('alreadygraded', 'assignment')); 00187 } 00188 } 00189 00190 if ($formdata = $mform->get_data()) { 00191 $fs = get_file_storage(); 00192 $submission = $this->get_submission($USER->id, true); //create new submission if needed 00193 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id); 00194 00195 if ($newfilename = $mform->get_new_filename('assignment_file')) { 00196 $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission', 00197 $submission->id, '/', $newfilename); 00198 00199 $updates = new stdClass(); //just enough data for updating the submission 00200 $updates->timemodified = time(); 00201 $updates->numfiles = 1; 00202 $updates->id = $submission->id; 00203 $DB->update_record('assignment_submissions', $updates); 00204 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); 00205 $this->update_grade($submission); 00206 $this->email_teachers($submission); 00207 00208 // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection) 00209 $eventdata = new stdClass(); 00210 $eventdata->modulename = 'assignment'; 00211 $eventdata->cmid = $this->cm->id; 00212 $eventdata->itemid = $submission->id; 00213 $eventdata->courseid = $this->course->id; 00214 $eventdata->userid = $USER->id; 00215 $eventdata->file = $file; 00216 events_trigger('assessable_file_uploaded', $eventdata); 00217 } 00218 00219 redirect($viewurl, get_string('uploadedfile')); 00220 } else { 00221 redirect($viewurl, get_string('uploaderror', 'assignment')); //submitting not allowed! 00222 } 00223 } 00224 00225 redirect($viewurl); 00226 } 00227 00228 function upload_responsefile($mform) { 00229 global $CFG, $USER, $OUTPUT, $PAGE; 00230 00231 $userid = required_param('userid', PARAM_INT); 00232 $mode = required_param('mode', PARAM_ALPHA); 00233 $offset = required_param('offset', PARAM_INT); 00234 00235 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url. 00236 00237 if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) { 00238 $fs = get_file_storage(); 00239 $submission = $this->get_submission($userid, true); //create new submission if needed 00240 $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id); 00241 00242 if ($newfilename = $mform->get_new_filename('assignment_file')) { 00243 $file = $mform->save_stored_file('assignment_file', $this->context->id, 00244 'mod_assignment', 'response',$submission->id, '/', $newfilename); 00245 } 00246 redirect($returnurl, get_string('uploadedfile')); 00247 } else { 00248 redirect($returnurl, get_string('uploaderror', 'assignment')); //submitting not allowed! 00249 } 00250 } 00251 00252 function setup_elements(&$mform) { 00253 global $CFG, $COURSE; 00254 00255 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes')); 00256 00257 $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions); 00258 $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment'); 00259 $mform->setDefault('resubmit', 0); 00260 00261 $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions); 00262 $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment'); 00263 $mform->setDefault('emailteachers', 0); 00264 00265 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes); 00266 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')'; 00267 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices); 00268 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes); 00269 00270 $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id); 00271 plagiarism_get_form_elements_module($mform, $course_context); 00272 } 00273 00274 function portfolio_exportable() { 00275 return true; 00276 } 00277 00278 function send_file($filearea, $args) { 00279 global $CFG, $DB, $USER; 00280 require_once($CFG->libdir.'/filelib.php'); 00281 00282 require_login($this->course, false, $this->cm); 00283 00284 if ($filearea !== 'submission' && $filearea !== 'response') { 00285 return false; 00286 } 00287 00288 $submissionid = (int)array_shift($args); 00289 00290 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) { 00291 return false; 00292 } 00293 00294 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) { 00295 return false; 00296 } 00297 00298 $relativepath = implode('/', $args); 00299 $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath; 00300 00301 $fs = get_file_storage(); 00302 00303 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { 00304 return false; 00305 } 00306 00307 send_stored_file($file, 0, 0, true); // download MUST be forced - security! 00308 } 00309 00310 function extend_settings_navigation($node) { 00311 global $CFG, $USER, $OUTPUT; 00312 00313 // get users submission if there is one 00314 $submission = $this->get_submission(); 00315 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) { 00316 $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked); 00317 } else { 00318 $editable = false; 00319 } 00320 00321 // If the user has submitted something add some related links and data 00322 if (isset($submission->numfiles) AND $submission->numfiles) { 00323 // Add a view link to the settings nav 00324 $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); 00325 $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING); 00326 if (!empty($submission->timemodified)) { 00327 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified)); 00328 $submissionnode->text = preg_replace('#([^,])\s#', '$1 ', $submissionnode->text); 00329 $submissionnode->add_class('note'); 00330 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) { 00331 $submissionnode->add_class('early'); 00332 } else { 00333 $submissionnode->add_class('late'); 00334 } 00335 } 00336 } 00337 00338 // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav 00339 if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) { 00340 $fs = get_file_storage(); 00341 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { 00342 $filenode = $node->add(get_string('submission', 'assignment')); 00343 foreach ($files as $file) { 00344 $filename = $file->get_filename(); 00345 $mimetype = $file->get_mimetype(); 00346 $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename); 00347 $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype), '')); 00348 } 00349 } 00350 } 00351 } 00352 00356 function download_submissions() { 00357 global $CFG,$DB; 00358 require_once($CFG->libdir.'/filelib.php'); 00359 00360 $submissions = $this->get_submissions('',''); 00361 if (empty($submissions)) { 00362 print_error('errornosubmissions', 'assignment'); 00363 } 00364 $filesforzipping = array(); 00365 $fs = get_file_storage(); 00366 00367 $groupmode = groups_get_activity_groupmode($this->cm); 00368 $groupid = 0; // All users 00369 $groupname = ''; 00370 if ($groupmode) { 00371 $groupid = groups_get_activity_group($this->cm, true); 00372 $groupname = groups_get_group_name($groupid).'-'; 00373 } 00374 $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file. 00375 foreach ($submissions as $submission) { 00376 $a_userid = $submission->userid; //get userid 00377 if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) { 00378 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names. 00379 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname 00380 00381 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false); 00382 foreach ($files as $file) { 00383 //get files new name. 00384 $fileext = strstr($file->get_filename(), '.'); 00385 $fileoriginal = str_replace($fileext, '', $file->get_filename()); 00386 $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext); 00387 //save file name to array for zipping. 00388 $filesforzipping[$fileforzipname] = $file; 00389 } 00390 } 00391 } // End of foreach 00392 if ($zipfile = assignment_pack_files($filesforzipping)) { 00393 send_temp_file($zipfile, $filename); //send file and delete after sending. 00394 } 00395 } 00396 00405 public function is_submitted_with_required_data($submission) { 00406 return ($submission->timemodified AND $submission->numfiles > 0); 00407 } 00408 } 00409 00410 class mod_assignment_uploadsingle_response_form extends moodleform { 00411 function definition() { 00412 $mform = $this->_form; 00413 $instance = $this->_customdata; 00414 00415 // visible elements 00416 $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options); 00417 00418 // hidden params 00419 $mform->addElement('hidden', 'id', $instance->cm->id); 00420 $mform->setType('id', PARAM_INT); 00421 $mform->addElement('hidden', 'contextid', $instance->contextid); 00422 $mform->setType('contextid', PARAM_INT); 00423 $mform->addElement('hidden', 'action', 'uploadresponse'); 00424 $mform->setType('action', PARAM_ALPHA); 00425 $mform->addElement('hidden', 'mode', $instance->mode); 00426 $mform->setType('mode', PARAM_ALPHA); 00427 $mform->addElement('hidden', 'offset', $instance->offset); 00428 $mform->setType('offset', PARAM_INT); 00429 $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh); 00430 $mform->setType('forcerefresh', PARAM_INT); 00431 $mform->addElement('hidden', 'userid', $instance->userid); 00432 $mform->setType('userid', PARAM_INT); 00433 00434 // buttons 00435 $this->add_action_buttons(false, get_string('uploadthisfile')); 00436 } 00437 }