MDL-22893, assignment module should use filepicker and filemanager
authorDongsheng Cai <unoter@gmail.com>
Fri, 9 Jul 2010 09:21:52 +0000 (09:21 +0000)
committerDongsheng Cai <unoter@gmail.com>
Fri, 9 Jul 2010 09:21:52 +0000 (09:21 +0000)
mod/assignment/assignment.js
mod/assignment/lang/en/assignment.php
mod/assignment/lib.php
mod/assignment/renderer.php [new file with mode: 0644]
mod/assignment/type/upload/assignment.class.php
mod/assignment/type/upload/upload.php [new file with mode: 0644]
mod/assignment/type/upload/upload_form.php [new file with mode: 0644]
mod/assignment/type/uploadsingle/assignment.class.php
mod/assignment/type/uploadsingle/upload.php [new file with mode: 0644]
mod/assignment/type/uploadsingle/upload_form.php [new file with mode: 0644]

index db03ffa..80c34bb 100644 (file)
@@ -15,4 +15,23 @@ function saveNext(){
 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 39f67fe..b5bf7fa 100644 (file)
@@ -73,6 +73,8 @@ $string['duedate'] = 'Due date';
 $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['graded'] = 'Graded';
 $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'] = '<p>This is useful when the assignment is performed outside of Moodle.  It could be
-   something elsewhere on the web or face-to-face.</p><p>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.</p><p>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.</p>';
 $string['helponline'] = '<p>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.</p>
    <p>(If you are familiar with older versions of Moodle, this Assignment
    type does the same thing as the old Journal module used to do.)</p>';
-$string['helpupload'] = '<p>This type of assignment allows each participant to upload one or more files in any format. 
+$string['helpupload'] = '<p>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.</p>
    <p>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.</p>
    <p>Participants may also enter notes describing the submitted files, progress status or any other text information.</p>
    <p>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.</p>';
-$string['helpuploadsingle'] = '<p>This type of assignment allows each participant to upload a 
-   single file, of any type.</p> <p>This might be a Word processor document, an image, 
+$string['helpuploadsingle'] = '<p>This type of assignment allows each participant to upload a
+   single file, of any type.</p> <p>This might be a Word processor document, an image,
    a zipped web site, or anything you ask them to submit.</p>';
 $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['typeupload'] = 'Advanced uploading of files';
 $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 9600d73..9eaf7e7 100644 (file)
@@ -2039,33 +2039,6 @@ class assignment_base {
 
 } ////// 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)
 
diff --git a/mod/assignment/renderer.php b/mod/assignment/renderer.php
new file mode 100644 (file)
index 0000000..2671034
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * A custom renderer class that extends the plugin_renderer_base and
+ * is used by the assignment module.
+ *
+ * @package mod-assignment
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @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 = '<div id="'.$htmlid.'">';
+        $html .= $this->htmllize_tree($tree, $tree->dir);
+        $html .= '</div>';
+        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 = '<ul>';
+        foreach ($dir['subdirs'] as $subdir) {
+            $image = $this->output->pix_icon("/f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
+            $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
+        }
+        foreach ($dir['files'] as $file) {
+            $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/mod_assignment/submission/'.$file->get_itemid().'/'.$file->get_filepath().$file->get_filename(), true);
+            $filename = $file->get_filename();
+            $icon = substr(mimeinfo("icon", $filename), 0, -4);
+            $image = $this->output->pix_icon("/f/$icon", $filename, 'moodle', array('class'=>'icon'));
+            $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
+        }
+        $result .= '</ul>';
+
+        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 2a7486f..facc70f 100644 (file)
@@ -1,4 +1,28 @@
 <?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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 @@ class assignment_upload extends assignment_base {
 
 
     function view_upload_form() {
-        global $CFG, $USER;
+        global $CFG, $USER, $OUTPUT;
 
         $submission = $this->get_submission($USER->id);
 
@@ -165,10 +189,18 @@ class assignment_upload extends assignment_base {
         }
 
         if ($this->can_upload_file($submission)) {
-            $mform = new mod_assignment_upload_file_form('upload.php', $this);
-                       echo "<div class=\"uploadbox\">";
-               $mform->display();
-           echo "</div>";
+            $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 @@ class assignment_upload extends assignment_base {
 
 
     function print_student_answer($userid, $return=false){
-        global $CFG, $OUTPUT;
+        global $CFG, $OUTPUT, $PAGE;
 
         $submission = $this->get_submission($userid);
 
@@ -285,22 +317,10 @@ class assignment_upload extends assignment_base {
             $output .= '&nbsp;';
         }
 
-        $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 .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a>&nbsp;';
-            }
-
-        }
-        $output = '<div class="files">'.$output.'</div>';
-        $output .= '<br />';
+        $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 @@ class assignment_upload extends assignment_base {
      * @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 @@ class assignment_upload extends assignment_base {
 
         $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 .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
         }
 
@@ -344,38 +362,6 @@ class assignment_upload extends assignment_base {
 
         }
 
-        $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 .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';
-
-                if ($candelete) {
-                    $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;submissionid={$submission->id}&amp;mode=$mode&amp;offset=$offset";
-
-                    $output .= '<a href="'.$delurl.'">&nbsp;'
-                              .'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="" /></a> ';
-                }
-
-                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 .= '<br />';
-            }
-            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 @@ class assignment_upload extends assignment_base {
             }
         }
 
-        $output = '<div class="files">'.$output.'</div>';
+        $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 @@ class assignment_upload extends assignment_base {
     }
 
     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 @@ class assignment_upload extends assignment_base {
         $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 .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.$filename.'</a>';
-
-                    if ($candelete) {
-                        $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;action=response";
-
-                        $output .= '<a href="'.$delurl.'">&nbsp;'
-                                  .'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt=""/></a> ';
-                    }
-
-                    $output .= '&nbsp;';
-                }
-
-                $output = '<div class="responsefiles">'.$output.'</div>';
-            }
+            $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 @@ class assignment_upload extends assignment_base {
     }
 
 
-    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 @@ class assignment_upload extends assignment_base {
                 $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 @@ class assignment_upload extends assignment_base {
 
         $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 @@ class assignment_upload extends assignment_base {
         $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 @@ class assignment_upload extends assignment_base {
         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 @@ class assignment_upload extends assignment_base {
             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);
-
-                            redirect('view.php?id='.$this->cm->id);
-                        } else {
-                            $file->delete();
-                        }
-                    }
+            $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);
                 }
+
+                // 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 @@ class assignment_upload extends assignment_base {
         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 @@ class assignment_upload extends assignment_base {
         $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}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;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 @@ class assignment_upload extends assignment_base {
         $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 @@ class assignment_upload extends assignment_base {
         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
diff --git a/mod/assignment/type/upload/upload.php b/mod/assignment/type/upload/upload.php
new file mode 100644 (file)
index 0000000..866b007
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ *
+ * @package   mod-assignment
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @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();
diff --git a/mod/assignment/type/upload/upload_form.php b/mod/assignment/type/upload/upload_form.php
new file mode 100644 (file)
index 0000000..5f200c7
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package   mod-assignment
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @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 2c2fdb8..827d6f4 100644 (file)
@@ -1,9 +1,29 @@
 <?php
 
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
 /**
  * Extend the base assignment class for assignments where you upload a single file
  *
+ * @package   mod-assignment
+ * @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 +35,7 @@ class assignment_uploadsingle extends assignment_base {
 
         $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 +92,29 @@ class assignment_uploadsingle extends assignment_base {
 
 
     function view_upload_form() {
-        $mform = new mod_assignment_upload_file_form('upload.php', $this);
-               echo "<div class=\"uploadbox\">";
-        $mform->display();
-        echo "</div>";
+        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 +123,46 @@ class assignment_uploadsingle extends assignment_base {
             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);
-
-                    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);
-
-                        // 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('view.php?id='.$this->cm->id, get_string('uploadedfile'));
-                    }
+                $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 ($newfilename = $mform->get_new_filename('assignment_file')) {
+                    $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
+                        $submission->id, '/', $newfilename);
+
+                    $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);
+
+                    // 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 +182,6 @@ class assignment_uploadsingle extends assignment_base {
         $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 +209,7 @@ class assignment_uploadsingle extends assignment_base {
         }
 
         $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();
 
diff --git a/mod/assignment/type/uploadsingle/upload.php b/mod/assignment/type/uploadsingle/upload.php
new file mode 100644 (file)
index 0000000..8f724db
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ *
+ * @package   mod-assignment
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @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();
diff --git a/mod/assignment/type/uploadsingle/upload_form.php b/mod/assignment/type/uploadsingle/upload_form.php
new file mode 100644 (file)
index 0000000..2e6fe7c
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @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'));
+    }
+}