Index: assignment.js ========================================================= --- assignment.js (revision 1.3) +++ assignment.js Thu Jul 08 16:27:59 WST 2010 @@ -15,4 +15,23 @@ function initNext(nextid, usserid) { assignment.nextid = nextid; assignment.userid = userid; -} \ No newline at end of file +} + +M.mod_assignment = {}; + +M.mod_assignment.init_tree = function(Y, expand_all, htmlid) { + Y.use('yui2-treeview', function(Y) { + var tree = new YAHOO.widget.TreeView(htmlid); + + tree.subscribe("clickEvent", function(node, event) { + // we want normal clicking which redirects to url + return false; + }); + + if (expand_all) { + tree.expandAll(); + } + + tree.render(); + }); +} Index: lang/en/assignment.php ========================================================= --- lang/en/assignment.php (revision 1.13) +++ lang/en/assignment.php Fri Jul 09 11:47:13 WST 2010 @@ -73,6 +73,8 @@ $string['duedateno'] = 'No due date'; $string['early'] = '{$a} early'; $string['editmysubmission'] = 'Edit my submission'; +$string['editthesefiles'] = 'Edit these files'; +$string['editthisfile'] = 'Update this file'; $string['addsubmission'] = 'Add submission'; $string['emailstudents'] = 'Email alerts to students'; $string['emailteachermail'] = '{$a->username} has updated their assignment submission @@ -102,22 +104,22 @@ $string['guestnosubmit'] = 'Sorry, guests are not allowed to submit an assignment. You have to log in/ register before you can submit your answer.'; $string['guestnoupload'] = 'Sorry, guests are not allowed to upload'; $string['helpoffline'] = '

This is useful when the assignment is performed outside of Moodle. It could be - something elsewhere on the web or face-to-face.

Students can see a description of the assignment, - but can\'t upload files or anything. Grading works normally, and students will get notifications of + something elsewhere on the web or face-to-face.

Students can see a description of the assignment, + but can\'t upload files or anything. Grading works normally, and students will get notifications of their grades.

'; $string['helponline'] = '

This assignment type asks users to edit a text, using the normal editing tools. Teachers can grade them online, and even add inline comments or changes.

(If you are familiar with older versions of Moodle, this Assignment type does the same thing as the old Journal module used to do.)

'; -$string['helpupload'] = '

This type of assignment allows each participant to upload one or more files in any format. +$string['helpupload'] = '

This type of assignment allows each participant to upload one or more files in any format. These might be a Word processor documents, images, a zipped web site, or anything you ask them to submit.

This type also allows you to upload multiple response files. Response files can be also uploaded before submission which can be used to give each participant different file to work with.

Participants may also enter notes describing the submitted files, progress status or any other text information.

Submission of this type of assignment must be manually finalised by the participant. You can review the current status at any time, unfinished assignments are marked as Draft. You can revert any ungraded assignment back to draft status.

'; -$string['helpuploadsingle'] = '

This type of assignment allows each participant to upload a - single file, of any type.

This might be a Word processor document, an image, +$string['helpuploadsingle'] = '

This type of assignment allows each participant to upload a + single file, of any type.

This might be a Word processor document, an image, a zipped web site, or anything you ask them to submit.

'; $string['hideintro'] = 'Hide description before available date'; $string['hideintro_help'] = 'If enabled, the assignment description is hidden before the "Available from" date. Only the assignment name is displayed.'; @@ -182,6 +184,8 @@ $string['typeuploadsingle'] = 'Upload a single file'; $string['unfinalize'] = 'Revert to draft'; $string['unfinalizeerror'] = 'An error occurred and that submission could not be reverted to draft'; +$string['uploadafile'] = 'Upload a file'; +$string['uploadfiles'] = 'Upload files'; $string['uploadbadname'] = 'This filename contained strange characters and couldn\'t be uploaded'; $string['uploadedfiles'] = 'uploaded files'; $string['uploaderror'] = 'An error happened while saving the file on the server'; Index: lib.php ========================================================= --- lib.php (revision 1.467) +++ lib.php Thu Jul 08 13:10:30 WST 2010 @@ -2039,33 +2039,6 @@ } ////// End of the assignment_base class -/** - * @package mod-assignment - * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class mod_assignment_upload_file_form extends moodleform { - function definition() { - $mform = $this->_form; - $instance = $this->_customdata; - - //TODO: improve upload size checking - $mform->setMaxFileSize($instance->assignment->maxbytes); - - // visible elements - $mform->addElement('file', 'newfile', get_string('uploadafile')); - - // hidden params - $mform->addElement('hidden', 'id', $instance->cm->id); - $mform->setType('id', PARAM_INT); - $mform->addElement('hidden', 'action', 'uploadfile'); - $mform->setType('action', PARAM_ALPHA); - - // buttons - $this->add_action_buttons(false, get_string('uploadthisfile')); - } -} - class mod_assignment_online_grading_form extends moodleform { // TODO: why "online" in the name of this class? (skodak) Index: renderer.php ========================================================= --- renderer.php Fri Jul 09 11:17:56 WST 2010 +++ renderer.php Fri Jul 09 11:17:56 WST 2010 @@ -0,0 +1,83 @@ +. + +/** + * A custom renderer class that extends the plugin_renderer_base and + * is used by the assignment module. + * + * @package mod-assignment + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + **/ +class mod_assignment_renderer extends plugin_renderer_base { + + /** + * @return string + */ + public function assignment_files($context, $itemid) { + return $this->render(new assignment_files($context, $itemid)); + } + + public function render_assignment_files(assignment_files $tree) { + $module = array('name'=>'mod_assignment_files', 'fullpath'=>'/mod/assignment/assignment.js', 'requires'=>array('yui2-treeview')); + $htmlid = 'assignment_files_tree_'.uniqid(); + $this->page->requires->js_init_call('M.mod_assignment.init_tree', array(true, $htmlid)); + $html = '
'; + $html .= $this->htmllize_tree($tree, $tree->dir); + $html .= '
'; + return $html; + } + + /** + * Internal function - creates htmls structure suitable for YUI tree. + */ + protected function htmllize_tree($tree, $dir) { + global $CFG; + $yuiconfig = array(); + $yuiconfig['type'] = 'html'; + + if (empty($dir['subdirs']) and empty($dir['files'])) { + return ''; + } + $result = ''; + + return $result; + } +} + +class assignment_files implements renderable { + public $context; + public $dir; + public function __construct($context, $itemid) { + global $USER; + $this->context = $context; + $fs = get_file_storage(); + $this->dir = $fs->get_area_tree($this->context->id, 'mod_assignment', 'submission', $itemid); + } +} Index: type/upload/assignment.class.php ========================================================= --- type/upload/assignment.class.php (revision 1.112) +++ type/upload/assignment.class.php Fri Jul 09 11:53:07 WST 2010 @@ -1,4 +1,28 @@ . + +/** + * Assignment upload type implementation + * + * @package mod-assignment + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(__FILE__).'/upload_form.php'); require_once($CFG->libdir . '/portfoliolib.php'); require_once($CFG->dirroot . '/mod/assignment/lib.php'); @@ -155,7 +179,7 @@ function view_upload_form() { - global $CFG, $USER; + global $CFG, $USER, $OUTPUT; $submission = $this->get_submission($USER->id); @@ -165,10 +189,18 @@ } if ($this->can_upload_file($submission)) { - $mform = new mod_assignment_upload_file_form('upload.php', $this); - echo "
"; - $mform->display(); - echo "
"; + $fs = get_file_storage(); + // edit files in another page + if ($submission = $this->get_submission($USER->id)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { + $str = get_string('editthesefiles', 'assignment'); + } else { + $str = get_string('uploadfiles', 'assignment'); + } + } else { + $str = get_string('uploadfiles', 'assignment'); + } + echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id)), $str, 'get'); } } @@ -267,7 +299,7 @@ function print_student_answer($userid, $return=false){ - global $CFG, $OUTPUT; + global $CFG, $OUTPUT, $PAGE; $submission = $this->get_submission($userid); @@ -285,22 +317,10 @@ $output .= ' '; } - $fs = get_file_storage(); - $browser = get_file_browser(); - - if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { - - foreach ($files as $file) { - $filename = $file->get_filename(); - $found = true; - $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename); - $output .= ''.$mimetype.''.s($filename).' '; - } - - } - $output = '
'.$output.'
'; - $output .= '
'; + $renderer = $PAGE->get_renderer('mod_assignment'); + $output = $OUTPUT->box_start('files').$output; + $output .= $renderer->assignment_files($this->context, $submission->id); + $output .= $OUTPUT->box_end(); return $output; } @@ -314,7 +334,7 @@ * @return string optional */ function print_user_files($userid=0, $return=false) { - global $CFG, $USER, $OUTPUT; + global $CFG, $USER, $OUTPUT, $PAGE; $mode = optional_param('mode', '', PARAM_ALPHA); $offset = optional_param('offset', 0, PARAM_INT); @@ -330,10 +350,8 @@ $submission = $this->get_submission($userid); - $candelete = $this->can_delete_files($submission); - $strdelete = get_string('delete'); - - if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) { // only during grading + // only during grading + if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) { $output .= ''.get_string('draft', 'assignment').':
'; } @@ -344,38 +362,6 @@ } - $fs = get_file_storage(); - $browser = get_file_browser(); - - if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { - $button = new portfolio_add_button(); - foreach ($files as $file) { - $filename = $file->get_filename(); - $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename); - $output .= ''.$mimetype.''.s($filename).''; - - if ($candelete) { - $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&file=".rawurlencode($filename)."&userid=$userid&submissionid={$submission->id}&mode=$mode&offset=$offset"; - - $output .= ' ' - .' '; - } - - if (has_capability('mod/assignment:exportownsubmission', $this->context)) { - $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php'); - $button->set_format_by_file($file); - $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); - } - $output .= '
'; - } - if (count($files) > 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) { - $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php'); - $button->reset_formats(); // reset what we set before, since it's multi-file - $output .= $button->to_html(); - } - } - if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page if ($this->can_unfinalize($submission)) { $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset); @@ -386,7 +372,10 @@ } } - $output = '
'.$output.'
'; + $renderer = $PAGE->get_renderer('mod_assignment'); + $output = $OUTPUT->box_start('files').$output; + $output .= $renderer->assignment_files($this->context, $submission->id); + $output .= $OUTPUT->box_end(); if ($return) { return $output; @@ -395,7 +384,7 @@ } function print_responsefiles($userid, $return=false) { - global $CFG, $USER, $OUTPUT; + global $CFG, $USER, $OUTPUT, $PAGE; $mode = optional_param('mode', '', PARAM_ALPHA); $offset = optional_param('offset', 0, PARAM_INT); @@ -409,27 +398,10 @@ $browser = get_file_browser(); if ($submission = $this->get_submission($userid)) { - if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "timemodified", false)) { - foreach ($files as $file) { - $filename = $file->get_filename(); - $found = true; - $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/response/'.$submission->id.'/'.$filename); - - $output .= ''.$mimetype.''.$filename.''; - - if ($candelete) { - $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&file=".rawurlencode($filename)."&userid=$userid&mode=$mode&offset=$offset&action=response"; - - $output .= ' ' - .' '; - } - - $output .= ' '; - } - - $output = '
'.$output.'
'; - } + $renderer = $PAGE->get_renderer('mod_assignment'); + $output = $OUTPUT->box_start('responsefiles').$output; + $output .= $renderer->assignment_files($this->context, $submission->id); + $output .= $OUTPUT->box_end(); } if ($return) { @@ -439,7 +411,13 @@ } - function upload() { + /** + * Upload files + * upload_file function requires moodle form instance and file manager options + * @param object $mform + * @param array $options + */ + function upload($mform = null, $filemanager_options = null) { $action = required_param('action', PARAM_ALPHA); switch ($action) { @@ -456,7 +434,7 @@ $this->upload_responsefile(); break; case 'uploadfile': - $this->upload_file(); + $this->upload_file($mform, $filemanager_options); case 'savenotes': case 'editnotes': $this->upload_notes(); @@ -470,7 +448,7 @@ $action = required_param('action', PARAM_ALPHA); - $returnurl = 'view.php?id='.$this->cm->id; + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); $mform = new mod_assignment_upload_notes_form(); @@ -486,7 +464,8 @@ $mform->set_data($defaults); if ($mform->is_cancelled()) { - redirect('view.php?id='.$this->cm->id); + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); + redirect($returnurl); } if (!$this->can_update_notes($submission)) { @@ -560,11 +539,10 @@ die; } - function upload_file() { + function upload_file($mform, $options) { global $CFG, $USER, $DB, $OUTPUT; - $returnurl = 'view.php?id='.$this->cm->id; - + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); $filecount = $this->count_user_files($USER->id); $submission = $this->get_submission($USER->id); @@ -576,42 +554,38 @@ die; } - $mform = new mod_assignment_upload_file_form('upload.php', $this); - if ($mform->get_data()) { + if ($formdata = $mform->get_data()) { $fs = get_file_storage(); - $filename = $mform->get_new_filename('newfile'); - if ($filename !== false) { - $submission = $this->get_submission($USER->id, true); //create new submission if needed - if (!$fs->file_exists($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename)) { - if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename, false, $USER->id)) { - $updates = new object(); - $updates->id = $submission->id; - $updates->timemodified = time(); - if ($DB->update_record('assignment_submissions', $updates)) { - add_to_log($this->course->id, 'assignment', 'upload', - 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); - $this->update_grade($submission); - if (!$this->drafts_tracked()) { - $this->email_teachers($submission); - } - - // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection) - $eventdata = new object(); - $eventdata->modulename = 'assignment'; - $eventdata->cmid = $this->cm->id; - $eventdata->itemid = $submission->id; - $eventdata->courseid = $this->course->id; - $eventdata->userid = $USER->id; - $eventdata->file = $file; - events_trigger('assessable_file_uploaded', $eventdata); + $submission = $this->get_submission($USER->id, true); //create new submission if needed + $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id); + $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id); + $updates = new object(); + $updates->id = $submission->id; + $updates->timemodified = time(); + if ($DB->update_record('assignment_submissions', $updates)) { + add_to_log($this->course->id, 'assignment', 'upload', + 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); + $this->update_grade($submission); + if (!$this->drafts_tracked()) { + $this->email_teachers($submission); + } - redirect('view.php?id='.$this->cm->id); - } else { - $file->delete(); - } - } + // send files to event system + $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id); + // Let Moodle know that assessable files were uploaded (eg for plagiarism detection) + $eventdata = new object(); + $eventdata->modulename = 'assignment'; + $eventdata->cmid = $this->cm->id; + $eventdata->itemid = $submission->id; + $eventdata->courseid = $this->course->id; + $eventdata->userid = $USER->id; + if ($files) { + $eventdata->files = $files; } + events_trigger('assessable_file_uploaded', $eventdata); } + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); + redirect($returnurl); } $this->view_header(get_string('upload')); @@ -675,7 +649,7 @@ global $USER, $DB, $OUTPUT; $confirm = optional_param('confirm', 0, PARAM_BOOL); - $returnurl = 'view.php?id='.$this->cm->id; + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); $submission = $this->get_submission($USER->id); if (!$this->can_finalize($submission)) { @@ -729,7 +703,7 @@ $userid = optional_param('userid', 0, PARAM_INT); $mode = required_param('mode', PARAM_ALPHA); $offset = required_param('offset', PARAM_INT); - $returnurl = "submissions.php?id={$this->cm->id}&userid=$userid&mode=$mode&offset=$offset&forcerefresh=1"; + $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1)); // create but do not add student submission date $submission = $this->get_submission($userid, true, true); @@ -808,7 +782,7 @@ $offset = required_param('offset', PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_BOOL); - $returnurl = "submissions.php?id={$this->cm->id}&userid=$userid&mode=$mode&offset=$offset"; + $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset)); if (!$this->can_manage_responsefiles()) { redirect($returnurl); @@ -851,11 +825,11 @@ if (empty($mode)) { $urlreturn = 'view.php'; $optionsreturn = array('id'=>$this->cm->id); - $returnurl = 'view.php?id='.$this->cm->id; + $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); } else { $urlreturn = 'submissions.php'; $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid); - $returnurl = "submissions.php?id={$this->cm->id}&offset=$offset&mode=$mode&userid=$userid"; + $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid)); } if (!$submission = $this->get_submission($userid) // incorrect submission Index: type/upload/upload.php ========================================================= --- type/upload/upload.php Fri Jul 09 11:35:03 WST 2010 +++ type/upload/upload.php Fri Jul 09 11:35:03 WST 2010 @@ -0,0 +1,81 @@ +. + +/** + * + * @package mod-assignment + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config.php'); +require_once(dirname(__FILE__).'/upload_form.php'); +require_once(dirname(__FILE__).'/assignment.class.php'); +require_once("$CFG->dirroot/repository/lib.php"); + +$contextid = required_param('contextid', PARAM_INT); + +$url = new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$contextid)); + +list($context, $course, $cm) = get_context_info_array($contextid); + +require_login($course, true, $cm); +if (isguestuser()) { + die(); +} + +if (!$assignment = $DB->get_record('assignment', array('id'=>$cm->instance))) { + print_error('invalidid', 'assignment'); +} + +$PAGE->set_url($url); +$PAGE->set_context($context); +$title = strip_tags($course->fullname.': '.get_string('modulename', 'assignment').': '.format_string($assignment->name,true)); +$PAGE->set_title($title); +$PAGE->set_heading($title); + +$instance = new assignment_upload($cm->id, $assignment, $cm, $course); +$submission = $instance->get_submission($USER->id, true); + +$filemanager_options = array('subdirs'=>1, 'maxbytes'=>$assignment->maxbytes, 'maxfiles'=>$assignment->var1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); + +$mform = new mod_assignment_upload_form(null, array('contextid'=>$contextid, 'options'=>$filemanager_options)); + +if ($mform->is_cancelled()) { + redirect(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id))); +} else if ($formdata = $mform->get_data()) { + $instance->upload($mform, $filemanager_options); + die; +} + +echo $OUTPUT->header(); + +echo $OUTPUT->box_start('generalbox'); +if ($instance->can_upload_file($submission)) { + $data = new stdclass; + // move submission files to user draft area + $data = file_prepare_standard_filemanager($data, 'files', $filemanager_options, $context, 'mod_assignment', 'submission', $submission->id); + // set file manager itemid, so it will find the files in draft area + $mform->set_data($data); + $mform->display(); +} else { + echo $OUTPUT->notification(get_string('uploaderror', 'assignment')); + echo $OUTPUT->continue_button(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id))); +} +echo $OUTPUT->box_end(); + +echo $OUTPUT->footer(); Index: type/upload/upload_form.php ========================================================= --- type/upload/upload_form.php Thu Jul 08 15:34:53 WST 2010 +++ type/upload/upload_form.php Thu Jul 08 15:34:53 WST 2010 @@ -0,0 +1,40 @@ +. + +/** + * @package mod-assignment + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_assignment_upload_form extends moodleform { + function definition() { + $mform = $this->_form; + $instance = $this->_customdata; + + // visible elements + $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance['options']); + + // hidden params + $mform->addElement('hidden', 'contextid', $instance['contextid']); + $mform->setType('contextid', PARAM_INT); + $mform->addElement('hidden', 'action', 'uploadfile'); + $mform->setType('action', PARAM_ALPHA); + + // buttons + $this->add_action_buttons(true, get_string('savechanges', 'admin')); + } +} Index: type/uploadsingle/assignment.class.php ========================================================= --- type/uploadsingle/assignment.class.php (revision 1.66) +++ type/uploadsingle/assignment.class.php Fri Jul 09 11:49:01 WST 2010 @@ -1,9 +1,31 @@ . + /** * Extend the base assignment class for assignments where you upload a single file * + * @since 2.0 + * @package moodlecore + * @subpackage repository + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +require_once($CFG->dirroot.'/mod/assignment/lib.php'); +require_once(dirname(__FILE__).'/upload_form.php'); + class assignment_uploadsingle extends assignment_base { @@ -15,7 +37,7 @@ $output = ''; - if ($submission = $this->get_submission($USER->id)) { + if ($submission = $this->get_submission($userid)) { if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); @@ -72,18 +94,29 @@ function view_upload_form() { - $mform = new mod_assignment_upload_file_form('upload.php', $this); - echo "
"; - $mform->display(); - echo "
"; + global $OUTPUT, $USER; + echo $OUTPUT->box_start('uploadbox'); + $fs = get_file_storage(); + // edit files in another page + if ($submission = $this->get_submission($USER->id)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { + $str = get_string('editthisfile', 'assignment'); + } else { + $str = get_string('uploadafile', 'assignment'); + } + } else { + $str = get_string('uploadafile', 'assignment'); + } + echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id)), $str, 'get'); + echo $OUTPUT->box_end(); } - function upload() { + function upload($mform) { global $CFG, $USER, $DB, $OUTPUT; - + $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id)); if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) { - redirect('view.php?id='.$this->cm->id); + redirect($viewurl); } $filecount = $this->count_user_files($USER->id); @@ -92,48 +125,46 @@ if ($submission = $this->get_submission($USER->id)) { //TODO: change later to ">= 0", to prevent resubmission when graded 0 if (($submission->grade > 0) and !$this->assignment->resubmit) { - redirect('view.php?id='.$this->cm->id, get_string('alreadygraded', 'assignment')); + redirect($viewurl, get_string('alreadygraded', 'assignment')); } } - $mform = new mod_assignment_upload_file_form('upload.php', $this); - if ($mform->get_data()) { + if ($formdata = $mform->get_data()) { $fs = get_file_storage(); - $filename = $mform->get_new_filename('newfile'); - if ($filename !== false) { - $submission = $this->get_submission($USER->id, true); //create new submission if needed - $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id); + $submission = $this->get_submission($USER->id, true); //create new submission if needed + $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id); - if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename, false, $USER->id)) { - $updates = new object(); //just enough data for updating the submission - $updates->timemodified = time(); - $updates->numfiles = 1; - $updates->id = $submission->id; - $DB->update_record('assignment_submissions', $updates); - add_to_log($this->course->id, 'assignment', 'upload', - 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); - $this->update_grade($submission); - $this->email_teachers($submission); + if ($newfilename = $mform->get_new_filename('assignment_file')) { + $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission', + $submission->id, '/', $newfilename); - // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection) - $eventdata = new object(); - $eventdata->modulename = 'assignment'; - $eventdata->cmid = $this->cm->id; - $eventdata->itemid = $submission->id; - $eventdata->courseid = $this->course->id; - $eventdata->userid = $USER->id; - $eventdata->file = $file; - events_trigger('assessable_file_uploaded', $eventdata); + $updates = new object(); //just enough data for updating the submission + $updates->timemodified = time(); + $updates->numfiles = 1; + $updates->id = $submission->id; + $DB->update_record('assignment_submissions', $updates); + add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); + $this->update_grade($submission); + $this->email_teachers($submission); - redirect('view.php?id='.$this->cm->id, get_string('uploadedfile')); - } + // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection) + $eventdata = new object(); + $eventdata->modulename = 'assignment'; + $eventdata->cmid = $this->cm->id; + $eventdata->itemid = $submission->id; + $eventdata->courseid = $this->course->id; + $eventdata->userid = $USER->id; + $eventdata->file = $file; + events_trigger('assessable_file_uploaded', $eventdata); } + + redirect($viewurl, get_string('uploadedfile')); } else { - redirect('view.php?id='.$this->cm->id, get_string('uploaderror', 'assignment')); //submitting not allowed! + redirect($viewurl, get_string('uploaderror', 'assignment')); //submitting not allowed! } } - redirect('view.php?id='.$this->cm->id); + redirect($viewurl); } function setup_elements(&$mform) { @@ -153,7 +184,6 @@ $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')'; $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices); $mform->setDefault('maxbytes', $CFG->assignment_maxbytes); - } function portfolio_exportable() { @@ -181,7 +211,7 @@ } $relativepath = implode('/', $args); - $fullpath = "/$this->context->id/mod_assignment/submission/$submissionid/$relativepath"; + $fullpath = '/'.$this->context->id.'/mod_assignment/submission/'.$submissionid.'/'.$relativepath; $fs = get_file_storage(); Index: type/uploadsingle/upload.php ========================================================= --- type/uploadsingle/upload.php Fri Jul 09 11:35:17 WST 2010 +++ type/uploadsingle/upload.php Fri Jul 09 11:35:17 WST 2010 @@ -0,0 +1,67 @@ +. + +/** + * + * @package mod-assignment + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config.php'); +require_once(dirname(__FILE__).'/upload_form.php'); +require_once(dirname(__FILE__).'/assignment.class.php'); +require_once("$CFG->dirroot/repository/lib.php"); + +$contextid = required_param('contextid', PARAM_INT); + +$url = new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$contextid)); + +list($context, $course, $cm) = get_context_info_array($contextid); + +if (!$assignment = $DB->get_record('assignment', array('id'=>$cm->instance))) { + print_error('invalidid', 'assignment'); +} + +require_login($course, true, $cm); +if (isguestuser()) { + die(); +} +$instance = new assignment_uploadsingle($cm->id, $assignment, $cm, $course); + +$PAGE->set_url($url); +$PAGE->set_context($context); +$title = strip_tags($course->fullname.': '.get_string('modulename', 'assignment').': '.format_string($assignment->name,true)); +$PAGE->set_title($title); +$PAGE->set_heading($title); + +$options = array('subdirs'=>0, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); + +$mform = new mod_assignment_uploadsingle_form(null, array('contextid'=>$contextid, 'options'=>$options)); + +if ($mform->is_cancelled()) { + redirect(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id))); +} else if ($formdata = $mform->get_data()) { + $instance->upload($mform, $options); + redirect(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id))); +} + +echo $OUTPUT->header(); +echo $OUTPUT->box_start('generalbox'); +$mform->display(); +echo $OUTPUT->box_end(); +echo $OUTPUT->footer(); Index: type/uploadsingle/upload_form.php ========================================================= --- type/uploadsingle/upload_form.php Thu Jul 08 15:15:11 WST 2010 +++ type/uploadsingle/upload_form.php Thu Jul 08 15:15:11 WST 2010 @@ -0,0 +1,42 @@ +. + +/** + * @package mod-assignment + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_assignment_uploadsingle_form extends moodleform { + function definition() { + $mform = $this->_form; + $instance = $this->_customdata; + + // visible elements + //$mform->addElement('filemanager', 'newfile', get_string('uploadafile')); + //$mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance['options']); + $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance['options']); + + // hidden params + $mform->addElement('hidden', 'contextid', $instance['contextid']); + $mform->setType('contextid', PARAM_INT); + $mform->addElement('hidden', 'action', 'uploadfile'); + $mform->setType('action', PARAM_ALPHA); + + // buttons + $this->add_action_buttons(true, get_string('savechanges', 'admin')); + } +}