From de295cd9f72d1b24eb5e4498a89d7efff28413d9 Mon Sep 17 00:00:00 2001
From: Craig Mickelson <cmickelson@webcourseworks.com>
Date: Thu, 30 Jun 2011 17:22:15 -0500
Subject: [PATCH] 1.9.12 updated with the assignment patch

---
 mod/assignment/lib.php                             |    6 ++
 mod/assignment/submissions.php                     |    7 ++-
 mod/assignment/type/online/assignment.class.php    |   74 +++++++++++++++++++
 mod/assignment/type/upload/assignment.class.php    |   77 +++++++++++++++++++-
 .../type/uploadsingle/assignment.class.php         |   78 ++++++++++++++++++++
 5 files changed, 240 insertions(+), 2 deletions(-)

diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php
index 667c719..8e67aec 100644
--- a/mod/assignment/lib.php
+++ b/mod/assignment/lib.php
@@ -1353,6 +1353,12 @@ class assignment_base {
             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">';
diff --git a/mod/assignment/submissions.php b/mod/assignment/submissions.php
index 79e0caf..191c4b9 100644
--- a/mod/assignment/submissions.php
+++ b/mod/assignment/submissions.php
@@ -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,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
+    }
 
 ?>
diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php
index 515f0a3..5e5a67f 100644
--- a/mod/assignment/type/online/assignment.class.php
+++ b/mod/assignment/type/online/assignment.class.php
@@ -285,6 +285,80 @@ class mod_assignment_online_edit_form extends moodleform {
         // buttons
         $this->add_action_buttons();
     }
+    
+    function download_submissions() {
+        global $CFG;
+        $submit = $this->get_submissions('', '');
+
+        if (empty($submit)) {
+            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);
+        }
+    }
+
 }
 
 ?>
diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php
index d3bc484..c583ab1 100644
--- a/mod/assignment/type/upload/assignment.class.php
+++ b/mod/assignment/type/upload/assignment.class.php
@@ -5,7 +5,7 @@ define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted'); // student thinks it is fini
 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 {
@@ -1058,6 +1058,81 @@ class assignment_upload extends assignment_base {
 
     }
 
+    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);
+        }
+    }
+
 }
 
 class mod_assignment_upload_notes_form extends moodleform {
diff --git a/mod/assignment/type/uploadsingle/assignment.class.php b/mod/assignment/type/uploadsingle/assignment.class.php
index d3dad81..5922750 100644
--- a/mod/assignment/type/uploadsingle/assignment.class.php
+++ b/mod/assignment/type/uploadsingle/assignment.class.php
@@ -180,6 +180,84 @@ class assignment_uploadsingle extends assignment_base {
         $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);
+        }
+    }    
 
 }
 
-- 
1.7.3.4

