--- lib.old	2010-04-25 01:03:37.000000000 +0100
+++ lib.php	2010-06-10 15:16:08.000000000 +0100
@@ -1,7 +1,7 @@
 <?PHP  // $Id$
 /**
  * assignment_base is the base class for assignment types
- *
+ *1-9-8+
  * This class provides all the functionality for an assignment
  */
 
@@ -1337,7 +1337,13 @@
             echo '</form>';
         }
         /// End of fast grading form
-
+         // print button offering zip file function to teacher
+         $options["id"] = "$cm->id";
+         $options["download"] = "zip";
+         echo '<div align="center">';
+         print_single_button("submissions.php",$options, "Zip & Download all submissions");
+         echo '</div>';
+ 
         /// Mini form for setting user preference
         echo '<div class="qgprefs">';
         echo '<form id="options" action="submissions.php?id='.$this->cm->id.'" method="post"><div>';
--- submissions.old	2006-08-29 03:01:31.000000000 +0100
+++ submissions.php	2010-06-10 15:25:57.000000000 +0100
@@ -6,7 +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)) {
             error("Course Module ID was incorrect");
@@ -40,6 +40,10 @@
     $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
+ }
 
 ?>
--- assignment.class.old	2010-03-08 00:03:06.000000000 +0000
+++ assignment.class.php	2010-06-10 14:55:20.000000000 +0100
@@ -260,6 +260,90 @@
         $mform->setDefault('var1', 0);
 
     }
+     function download_submissions() {
+ 	        global $CFG;
+ 		    $submit = $this->get_submissions('','');
+		if (empty($submissions))
+            	error("there are no submissions to download");
+ 
+ 	        $filesforzipping = array();
+ 	        $filesnewname = array();
+ 	        $desttemp = "";
+ 	        //create zip filename
+ 	        $filename = "online_assignment.zip";
+ 
+ 	        //online assignment can use html
+ 	        $file=".html";
+ 		
+ 
+ 
+         $course     = $this->course;
+         $assignment = $this->assignment;
+         $cm         = $this->cm;
+         $context    = get_context_instance(CONTEXT_MODULE, $cm->id);
+
+        $groupmode = groups_get_activity_groupmode($cm);
+        $groupid = groups_get_activity_group($cm, true);
+	
+ 	$count = 0;
+ 
+      
+ 
+ 
+ 
+ 	foreach ($submit as $tp) {
+ 		$a_userid = $tp->userid; //get userid
+ 		if ( (groups_is_member( $groupid,$a_userid)or !$groupmode or !$groupid)) {
+ 		$count = $count + 1;
+ 			
+ 		$a_assignid = $tp->assignment; //get name of this assignment for use in the file names.
+ 
+ 		$a_user = get_complete_user_data("id", $a_userid); //get user
+ 
+ 		$filearea = $this->file_area_name($a_userid);
+ 
+ 		$submission = $tp->data1;      //fetched from mysql database
+ 
+ 		$desttemp = $CFG->dataroot . "/" . substr($filearea, 0, strripos($filearea, "/")). "/temp/";
+ 					//get temp directory name
+ 
+ 			    if (!file_exists($desttemp)) { //create temp dir if it doesn't already exist.
+ 			        mkdir($desttemp,0777,true); 
+ 			    }
+ 
+ 	              require_once($CFG->libdir.'/filelib.php');
+ 
+ 	        //get file name.html
+ 	            $filesforzip = $desttemp . $a_user->firstname ."_". $a_user->lastname  . $file;
+ 
+ 
+ 	            $fd = fopen($filesforzip,'wb');   //create if not exist, write binary
+ 	              fwrite( $fd, $submission);
+ 	              fclose( $fd );
+ 
+ 	                //save file name to array for zipping.
+ 	                        $filesforzipping[] = $filesforzip;
+ 	      }    }      //end of foreach
+ 
+ 	        //zip files
+ 
+ 
+ 
+ 	        if ($count) zip_files($filesforzipping, $desttemp.$filename);  // check for no files
+ 
+ 	        //delete old temp files
+ 	        foreach ($filesforzipping as $filefor) {
+ 			   unlink($filefor);
+ 	        }
+ 
+ 	        //send file to user.
+ 	        if (file_exists($desttemp.$filename))
+		{
+		send_file($desttemp.$filename, $filename, 'default',0,false,true);
+ 	        }
+     }
+ 
+
 
 }
 --- assignment.class.old	2010-03-08 00:03:06.000000000 +0000
+++ assignment.class.php	2010-06-10 14:53:42.000000000 +0100
@@ -5,7 +5,7 @@
 define('ASSIGNMENT_STATUS_CLOSED', 'closed');       // teacher prevents more submissions
 
 /**
- * Extend the base assignment class for assignments where you upload a single file
+ * Extend the base assignment class for assignments where you upload multiple files
  *
  */
 class assignment_upload extends assignment_base {
@@ -1057,6 +1057,91 @@
         $mform->setDefault('var4', 1);
 
     }
+     function download_submissions() {
+ 	        global $CFG;
+ 
+ 	        $submissions = $this->get_submissions('','');
+        	if (empty($submissions))
+            	error("there are no submissions to download");
+        	
+
+ 
+ 	        $filesforzipping = array();
+ 	        $filesnewname = array();
+ 	        $desttemp = "";
+ 
+ 	        //create prefix of new filename
+ 		$filenewname = clean_filename($this->assignment->name. "_");
+ 		$course     = $this->course;
+ 		$assignment = $this->assignment;
+ 		$cm         = $this->cm;
+ 		$context    = get_context_instance(CONTEXT_MODULE, $cm->id);
+        	$groupmode = groups_get_activity_groupmode($cm);
+        	$groupid = groups_get_activity_group($cm, true);
+ 		$count = 0;
+ 
+ 
+ 	        foreach ($submissions as $submission) {
+ 			    
+ 
+ 
+ 		$a_userid = $submission->userid; //get userid
+ 		if ( (groups_is_member( $groupid,$a_userid)or !$groupmode or !$groupid)) {
+ 		$count = $count + 1;
+ 
+ 			    $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
+ 
+ 			    $a_user = get_complete_user_data("id", $a_userid); //get user
+ 
+ 	            $filearea = $this->file_area_name($a_userid);
+ 
+ 			    $desttemp = $CFG->dataroot . "/" . substr($filearea, 0, strrpos($filearea, "/")). "/temp/"; //get temp directory name
+ 
+ 			    if (!file_exists($desttemp)) { //create temp dir if it doesn't already exist.
+ 			        mkdir($desttemp,0777,true); //can create a tree .........
+ 			    }
+ 
+ 	            if ($basedir = $this->file_area($a_userid)) {
+ 	                if ($files = get_directory_list($basedir)) {
+ 
+ 	                    foreach ($files as $key => $file) {
+ 	                        require_once($CFG->libdir.'/filelib.php');
+ 				$filecleaned = clean_filename($file);  //remove any slashes
+ 
+ 	                        //get files new name.
+ 	   $filesforzip = $desttemp . $a_user->username . "_" . $filenewname . "_" . $filecleaned;  
+ 
+ 
+ 	                        //get files old name
+ 	                        $fileold = $CFG->dataroot . "/" . $filearea . "/" . $file;
+ 
+ 	                        if (!copy($fileold, $filesforzip)) {
+ 						        error ("failed to copy file<br>" . $filesforzip . "<br>" .$fileold);
+ 						    }
+ 
+ 	                        //save file name to array for zipping.
+ 	                        $filesforzipping[] = $filesforzip;
+ 	                    }
+ 	                }
+ 	            }
+ 	    }    }		// end of foreach loop
+ 
+ 	        //zip files
+ 	        $filename = "assignment.zip"; //name of new zip file.
+ 	        if ($count) zip_files($filesforzipping, $desttemp.$filename);
+ 
+ 	        //delete old temp files
+ 	        foreach ($filesforzipping as $filefor) {
+ 			   unlink($filefor);
+ 	        }
+ 
+ 	        //send file to user.
+ 	        if (file_exists($desttemp.$filename))
+		{
+		send_file($desttemp.$filename, $filename, 'default',0,false,true);
+ 	        }
+ 	    }
+ 
 
 }
 --- assignment.class.old	2009-11-20 16:36:31.000000000 +0000
+++ assignment.class.php	2010-06-10 14:54:47.000000000 +0100
@@ -180,7 +180,82 @@
         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
 
     }
+     function download_submissions() {
+ 	        global $CFG;
+ 
+ 	        $submissions = $this->get_submissions('','');
+ 		if (empty($submissions))
+            	error("there are no submissions to download");
 
+ 	        $filesforzipping = array();
+ 	        $filesnewname = array();
+ 	        $desttemp = "";
+ 
+ 	        //create prefix of new filename
+ 		$filenewname = clean_filename($this->assignment->name. "_");
+ 		$course     = $this->course;
+ 		$assignment = $this->assignment;
+ 		$cm         = $this->cm;
+ 		$context    = get_context_instance(CONTEXT_MODULE, $cm->id);
+ 		$groupmode = groups_get_activity_groupmode($cm);
+        	$groupid = groups_get_activity_group($cm, true);
+ 		$count = 0;
+ 
+ 	        foreach ($submissions as $submission) {
+ 		$a_userid = $submission->userid; //get userid
+ 		if ( (groups_is_member( $groupid,$a_userid)or !$groupmode or !$groupid)) {
+ 		$count = $count + 1;
+ 
+ 			    $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
+ 
+ 			    $a_user = get_complete_user_data("id", $a_userid); //get user
+ 
+ 	            $filearea = $this->file_area_name($a_userid);
+ 
+ 			    $desttemp = $CFG->dataroot . "/" . substr($filearea, 0, strrpos($filearea, "/")). "/temp/"; //get temp directory name
+ 
+ 			    if (!file_exists($desttemp)) { //create temp dir if it doesn't already exist.
+ 			        mkdir($desttemp);
+ 			    }
+ 
+ 	            if ($basedir = $this->file_area($a_userid)) {
+ 	                if ($files = get_directory_list($basedir)) {
+ 	                    foreach ($files as $key => $file) {
+ 	                        require_once($CFG->libdir.'/filelib.php');
+ 
+ 	                        //get files new name.
+ 	                        $filesforzip = $desttemp . $a_user->username . "_" . $filenewname . "_" . $file;
+ 
+ 	                        //get files old name
+ 	                        $fileold = $CFG->dataroot . "/" . $filearea . "/" . $file;
+ 
+ 	                        if (!copy($fileold, $filesforzip)) {
+ 						        error ("failed to copy file<br>" . $filesforzip . "<br>" .$fileold);
+ 						    }
+ 
+ 	                        //save file name to array for zipping.
+ 	                        $filesforzipping[] = $filesforzip;
+ 	                    }
+ 	                }
+ 	            }
+ 	        }   }     // End of foreach
+ 
+ 	        //zip files
+ 	        $filename = "assignment.zip"; //name of new zip file.
+ 	        if ($count) zip_files($filesforzipping, $desttemp.$filename);
+ 		// skip if no files zipped
+ 	        //delete old temp files
+ 	        foreach ($filesforzipping as $filefor) {
+ 			   unlink($filefor);
+ 	        }
+ 
+ 	        //send file to user.
+ 	        if (file_exists($desttemp.$filename))
+		{
+ 	           send_file($desttemp.$filename, $filename, 'default',0,false,true);
+ 	        }
+ 	    }
+ 
 }
 
 ?>

