### Eclipse Workspace Patch 1.0
#P CVS
Index: mod/assignment/submissions.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/submissions.php,v
retrieving revision 1.46
diff -u -r1.46 submissions.php
--- mod/assignment/submissions.php	5 Jun 2008 10:31:38 -0000	1.46
+++ mod/assignment/submissions.php	9 Jan 2009 10:12:33 -0000
@@ -6,6 +6,7 @@
     $id   = optional_param('id', 0, PARAM_INT);          // Course module ID
     $a    = optional_param('a', 0, PARAM_INT);           // Assignment ID
     $mode = optional_param('mode', 'all', PARAM_ALPHA);  // What mode are we in?
+    $download = optional_param('download' , 'none', PARAM_ALPHA); //ZIP download asked for?
 
     if ($id) {
         if (! $cm = get_coursemodule_from_id('assignment', $id)) {
@@ -40,6 +41,9 @@
     $assignmentclass = 'assignment_'.$assignment->assignmenttype;
     $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course);
 
-    $assignmentinstance->submissions($mode);   // Display or process the submissions
-
-?>
+    if($download == "zip") {
+        $assignmentinstance->download_submissions();
+    } else {
+        $assignmentinstance->submissions($mode);   // Display or process the submissions
+    }
+?>
\ No newline at end of file
Index: mod/assignment/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/lib.php,v
retrieving revision 1.373
diff -u -r1.373 lib.php
--- mod/assignment/lib.php	3 Jan 2009 13:16:59 -0000	1.373
+++ mod/assignment/lib.php	9 Jan 2009 10:12:33 -0000
@@ -1140,7 +1140,9 @@
             print_heading(get_string('nosubmitusers','assignment'));
             return true;
         }
-
+        if ($this->assignment->assignmenttype=='upload' || $this->assignment->assignmenttype=='online' || $this->assignment->assignmenttype=='uploadsingle') {
+            echo '<div style="text-align:right"><a href="submissions.php?id='.$this->cm->id.'&amp;download=zip">'.get_string('downloadall', 'assignment').'</div>';
+        }
     /// Construct the SQL
 
         if ($where = $table->get_sql_where()) {
Index: lang/en_utf8/assignment.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/assignment.php,v
retrieving revision 1.30
diff -u -r1.30 assignment.php
--- lang/en_utf8/assignment.php	9 Sep 2008 21:06:19 -0000	1.30
+++ lang/en_utf8/assignment.php	9 Jan 2009 10:12:33 -0000
@@ -38,6 +38,7 @@
 $string['deleteallsubmissions'] = 'Delete all submissions';
 $string['deletefilefailed'] = 'Deleting of file failed.';
 $string['description'] = 'Description';
+$string['downloadall'] = 'Download all submissions';
 $string['draft'] = 'Draft';
 $string['duedate'] = 'Due date';
 $string['duedateno'] = 'No due date';
Index: mod/assignment/type/online/assignment.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/type/online/assignment.class.php,v
retrieving revision 1.67
diff -u -r1.67 assignment.class.php
--- mod/assignment/type/online/assignment.class.php	11 Oct 2008 17:33:21 -0000	1.67
+++ mod/assignment/type/online/assignment.class.php	9 Jan 2009 10:12:33 -0000
@@ -281,6 +281,48 @@
     function portfolio_supported_formats() {
         return array(PORTFOLIO_FORMAT_PLAINHTML);
     }
+    function download_submissions() {
+        global $CFG, $DB;
+        require_once($CFG->libdir.'/filelib.php');
+        
+        $submissions = $this->get_submissions('','');
+        if (empty($submissions)) {
+            error("there are no submissions to download");
+        }
+        $filesforzipping = array();
+        $filename = "online_assignment.zip"; //final zip filename
+        $desttemp = $CFG->dataroot."/temp/". $this->course->id. "/assignment/".$this->assignment->id."/"; //location for temp files.
+        //online assignment can use html
+        $filextn=".html";
+
+        if (file_exists($desttemp)) {
+            remove_dir($desttemp);  //remove old files if they exist   
+        }
+        mkdir($desttemp,0777,true); //create fresh temp dir
+        
+        $groupmode = groupmode($this->course,$this->cm);
+        $groupid = 0;   // All users
+        if($groupmode) $groupid = get_current_group($this->course->id, $full = false);
+        foreach ($submissions as $submission) {
+            $a_userid = $submission->userid; //get userid
+            if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
+                $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
+                $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,firstname,lastname'); //get user firstname/lastname
+                $submissioncontent = "<html><body>". $submission->data1. "</body></html>";      //fetched from database
+                //get file name.html
+                $fileforzipname =  $a_user->firstname ."_". $a_user->lastname  . $filextn;
+                $fd = fopen($desttemp . $fileforzipname,'wb');   //create if not exist, write binary
+                fwrite( $fd, $submissioncontent);
+                fclose( $fd );
+                $filesforzipping[$fileforzipname] = $desttemp.$fileforzipname;
+            }    
+        }      //end of foreach
+        //zip files
+        $zipper = new zip_packer();
+        if ($zipper->archive_to_pathname($filesforzipping, $desttemp.$filename)) {
+            send_file($desttemp.$filename, $filename, 'default',0,false,true);
+        }
+    }
 }
 
 class mod_assignment_online_edit_form extends moodleform {
Index: mod/assignment/type/upload/assignment.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/type/upload/assignment.class.php,v
retrieving revision 1.66
diff -u -r1.66 assignment.class.php
--- mod/assignment/type/upload/assignment.class.php	13 Nov 2008 08:41:02 -0000	1.66
+++ mod/assignment/type/upload/assignment.class.php	9 Jan 2009 10:12:34 -0000
@@ -1041,7 +1041,53 @@
     function portfolio_exportable() {
         return true;
     }
-
+    function download_submissions() {
+        global $CFG,$DB;
+        require_once($CFG->libdir.'/filelib.php');
+
+        $submissions = $this->get_submissions('','');
+        if (empty($submissions)) {
+            error("there are no submissions to download");
+        }
+        $filesforzipping = array();
+        $filename = "assignment.zip"; //name of new zip file.
+        $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
+        $desttemp = $CFG->dataroot."/temp/". $this->course->id. "/assignment/".$this->assignment->id."/"; //location for temp files.
+        $fs = get_file_storage();
+        
+        if (file_exists($desttemp)) {
+            remove_dir($desttemp);  //remove old files if they exist   
+        }
+        mkdir($desttemp,0777,true); //create fresh temp dir
+
+        $groupmode = groupmode($this->course,$this->cm);
+        $groupid = 0;   // All users
+        if($groupmode) $groupid = get_current_group($this->course->id, $full = false);
+        foreach ($submissions as $submission) {
+            $a_userid = $submission->userid; //get userid
+            if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
+                $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
+                $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
+                
+                $files = $fs->get_area_files($this->context->id, 'assignment_submission', $a_userid, "timemodified", false);
+                foreach ($files as $file) {
+                    //get files new name.
+                    $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
+                    //get files old name
+                    if (!$file->copy_content_to($desttemp . $fileforzipname)) {
+                        error ("failed to copy file<br>" .$desttemp. $fileforzipname);
+                    }
+                    //save file name to array for zipping.
+                    $filesforzipping[$fileforzipname] = $desttemp.$fileforzipname;
+                }
+            }
+        } // end of foreach loop
+        //zip files
+        $zipper = new zip_packer();
+        if ($zipper->archive_to_pathname($filesforzipping, $desttemp.$filename)) {
+            send_file($desttemp.$filename, $filename, 'default',0,false,true);
+        }
+    }
 }
 
 class mod_assignment_upload_notes_form extends moodleform {
Index: mod/assignment/type/uploadsingle/assignment.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/type/uploadsingle/assignment.class.php,v
retrieving revision 1.43
diff -u -r1.43 assignment.class.php
--- mod/assignment/type/uploadsingle/assignment.class.php	16 Aug 2008 18:13:38 -0000	1.43
+++ mod/assignment/type/uploadsingle/assignment.class.php	9 Jan 2009 10:12:34 -0000
@@ -177,7 +177,53 @@
 
         send_stored_file($file, 0, 0, true); // download MUST be forced - security!
     }
+    function download_submissions() {
+        global $CFG,$DB;
+        require_once($CFG->libdir.'/filelib.php');
 
+        $submissions = $this->get_submissions('','');
+        if (empty($submissions)) {
+            error("there are no submissions to download");
+        }
+        $filesforzipping = array();
+        $filename = "assignment.zip"; //name of new zip file.
+        $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
+        $desttemp = $CFG->dataroot."/temp/". $this->course->id. "/assignment/".$this->assignment->id."/"; //location for temp files.
+        $fs = get_file_storage();
+        
+        if (file_exists($desttemp)) {
+            remove_dir($desttemp);  //remove old files if they exist   
+        }
+        mkdir($desttemp,0777,true); //create fresh temp dir
+        
+        $groupmode = groupmode($this->course,$this->cm);
+        $groupid = 0;   // All users
+        if($groupmode) $groupid = get_current_group($this->course->id, $full = false);
+        foreach ($submissions as $submission) {
+            $a_userid = $submission->userid; //get userid
+            if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
+                $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
+                $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
+                
+                $files = $fs->get_area_files($this->context->id, 'assignment_submission', $a_userid, "timemodified", false);
+                foreach ($files as $file) {
+                    //get files new name.
+                    $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
+                    //get files old name
+                    if (!$file->copy_content_to($desttemp . $fileforzipname)) {
+                        error ("failed to copy file<br>" .$desttemp. $fileforzipname);
+                    }
+                    //save file name to array for zipping.
+                    $filesforzipping[$fileforzipname] = $desttemp.$fileforzipname;
+                }
+            }
+        } // End of foreach
+        //zip files
+        $zipper = new zip_packer();
+        if ($zipper->archive_to_pathname($filesforzipping, $desttemp.$filename)) {
+            send_file($desttemp.$filename, $filename, 'default',0,false,true);
+        }
+    }
 }
 
-?>
+?>
\ No newline at end of file
