Index: lib/LAE/zip.php
===================================================================
--- lib/LAE/zip.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ lib/LAE/zip.php	(.../branches/1.9.5-LAE1.0/AssignmentZip)	(revision 139)
@@ -0,0 +1,25 @@
+<?php
+
+// This provides a bolt-on to Moodle so you can easily add a download button
+// for a collection of files pretty much anywhere.
+
+require_once('xor.php');
+
+if(empty($SESSION->zipsecret)) {
+  $SESSION->zipsecret = md5(uniqid(rand(), true)) . md5(uniqid(rand(), true)) . md5(uniqid(rand(), true));
+}
+
+function file_collection_form($files_tozip, $collection_name) {
+  global $CFG, $SESSION;
+
+  $payload =      array('files' => $files_tozip, 'name' => $collection_name);
+  $encoded =    encrypt_and_encode(serialize($payload), $SESSION->zipsecret);
+
+  $output =       '<p><form method="post" action="' . $CFG->wwwroot . '/LAE/zipfile.php">';
+  $output .=      '<input type="hidden" name="t" value="' . $encoded . '"/>';
+  $output .=      '<input type="submit" name="download" value="Download all files (.zip)"/></form></p>';
+
+  return $output;
+}
+
+?>

Property changes on: lib/LAE/zip.php
___________________________________________________________________
Added: svn:keywords
   + Id

Index: lib/LAE/xor.php
===================================================================
--- lib/LAE/xor.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ lib/LAE/xor.php	(.../branches/1.9.5-LAE1.0/AssignmentZip)	(revision 139)
@@ -0,0 +1,57 @@
+<?php
+/**
+ * XOR encrypts a given string with a given key phrase.
+ *
+ * @param     string    $InputString    Input string
+ * @param     string    $KeyPhrase      Key phrase
+ * @return    string    Encrypted string
+ *
+ */
+
+function XORStrings($InputString, $KeyPhrase){
+    $KeyPhraseLength = strlen($KeyPhrase);
+
+    // Loop through input string
+    for ($i = 0; $i < strlen($InputString); $i++){
+
+      // Get key phrase character position
+      $rPos = $i % $KeyPhraseLength;
+
+      // Magic happens here:
+      $r = ord($InputString[$i]) ^ ord($KeyPhrase[$rPos]);
+
+      // Replace characters
+      $InputString[$i] = chr($r);
+    }
+
+    return $InputString;
+}
+
+// Helper functions, using base64 to
+// create readable encrypted texts:
+function XOREncode($InputString, $KeyPhrase){
+  $InputString = XORStrings($InputString, $KeyPhrase);
+  $InputString = base64_encode($InputString);
+  return $InputString;
+}
+
+function XORDecode($InputString, $KeyPhrase){
+  $InputString = base64_decode($InputString);
+  $InputString = XORStrings($InputString, $KeyPhrase);
+  return $InputString;
+}
+
+/* Additional methods for using XOR'd strings in URLs */
+function encrypt_and_encode($str) {
+  global $SESSION;
+
+  return XOREncode($str, $SESSION->zipsecret);
+}
+
+function decrypt_and_decode($str) {
+  global $SESSION;
+
+  return XORDecode($str, $SESSION->zipsecret);
+}
+
+?>

Property changes on: lib/LAE/xor.php
___________________________________________________________________
Added: svn:keywords
   + Id

Index: mod/assignment/lib.php
===================================================================
--- mod/assignment/lib.php	(.../vendor/moodle/1.9weekly)	(revision 139)
+++ mod/assignment/lib.php	(.../branches/1.9.5-LAE1.0/AssignmentZip)	(revision 139)
@@ -5,6 +5,8 @@
  * This class provides all the functionality for an assignment
  */
 
+require_once($CFG->dirroot . '/lib/LAE/zip.php');
+
 DEFINE ('ASSIGNMENT_COUNT_WORDS', 1);
 DEFINE ('ASSIGNMENT_COUNT_LETTERS', 2);
 
@@ -1158,7 +1160,6 @@
                'WHERE '.$where.'u.id IN ('.implode(',',$users).') ';
 
         $table->pagesize($perpage, count($users));
-
         ///offset used to calculate index of student in that particular query, needed for the pop up to know who's next
         $offset = $page * $perpage;
 
@@ -1166,6 +1167,25 @@
         $strgrade  = get_string('grade');
         $grademenu = make_grades_menu($this->assignment->grade);
 
+
+        if (class_exists('ZipArchive')) {
+          $files_tozip = array(); // zip assignment files
+
+          require_once($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php');
+          $assignmentclass = 'assignment_'.$assignment->assignmenttype;
+          $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course);
+
+          foreach (assignment_get_all_submissions($assignment, '', 'DESC') as $sub) {
+            $filearea = $assignmentinstance->file_area_name($sub->userid);
+            $fullpath = $CFG->dataroot . '/' . $filearea;
+            $subuser = get_record('user', 'id', $sub->userid);
+
+            if ($files = get_directory_list($fullpath, 'responses'))
+              foreach ($files as $key => $file)
+                $files_tozip[$file] = array('path' => $fullpath . "/" . $file . " ", 'author' => $subuser->firstname . " " . $subuser->lastname);
+          }
+        }
+
         if (($ausers = get_records_sql($select.$sql.$sort, $table->get_page_start(), $table->get_page_size())) !== false) {
             $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, array_keys($ausers));
             foreach ($ausers as $auser) {
@@ -1299,7 +1319,7 @@
                     }
                 }
 
-				$userlink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $auser->id . '&amp;course=' . $course->id . '">' . fullname($auser) . '</a>';
+                                $userlink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $auser->id . '&amp;course=' . $course->id . '">' . fullname($auser) . '</a>';
                 $row = array($picture, $userlink, $grade, $comment, $studentmodified, $teachermodified, $status, $finalgrade);
                 if ($uses_outcomes) {
                     $row[] = $outcomes;
@@ -1336,6 +1356,10 @@
         }
         /// End of fast grading form
 
+        if(class_exists('ZipArchive') && sizeof($files_tozip) > 1) {
+          echo file_collection_form($files_tozip, $assignment->name);
+        }
+
         /// Mini form for setting user preference
         echo '<div class="qgprefs">';
         echo '<form id="options" action="submissions.php?id='.$this->cm->id.'" method="post"><div>';
@@ -2881,11 +2905,11 @@
     $strreviewed = get_string('reviewed','assignment');
 
 
-    // NOTE: we do all possible database work here *outside* of the loop to ensure this scales 
-    
+    // NOTE: we do all possible database work here *outside* of the loop to ensure this scales
+
     // build up and array of unmarked submissions indexed by assigment id/ userid
     // for use where the user has grading rights on assigment
-    $rs = get_recordset_sql("SELECT id, assignment, userid 
+    $rs = get_recordset_sql("SELECT id, assignment, userid
                             FROM {$CFG->prefix}assignment_submissions
                             WHERE teacher = 0 AND timemarked = 0
                             AND assignment IN (". implode(',', $assignmentids).")");
@@ -2899,8 +2923,8 @@
 
     // get all user submissions, indexed by assigment id
     $mysubmissions = get_records_sql("SELECT assignment, timemarked, teacher, grade
-                                      FROM {$CFG->prefix}assignment_submissions 
-                                      WHERE userid = {$USER->id} AND 
+                                      FROM {$CFG->prefix}assignment_submissions
+                                      WHERE userid = {$USER->id} AND
                                       assignment IN (".implode(',', $assignmentids).")");
 
     foreach ($assignments as $assignment) {
Index: LAE/zipfile.php
===================================================================
--- LAE/zipfile.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ LAE/zipfile.php	(.../branches/1.9.5-LAE1.0/AssignmentZip)	(revision 139)
@@ -0,0 +1,55 @@
+<?php
+
+// This script creates a zip file from a specially encoded list
+
+require_once('../config.php');
+require_once($CFG->dirroot . '/lib/LAE/xor.php');
+
+$err_base = 'Could not create .zip file: ';
+
+if(!isset($_POST['t'])) {
+  die($err_base . 'Your request did not contain the list of source files.');
+ } else {
+  $str = decrypt_and_decode($_POST['t']);
+
+  if(!($zipinfo = unserialize($str))) {
+    die($err_base . 'Unable to decode the list of source files you provided.');
+  }
+ }
+
+// unpack
+$zip_files      = $zipinfo['files'];
+$name           = $zipinfo['name'];
+
+// set up file name
+$name           = preg_replace("/[^a-zA-Z0-9]/", '', $name);
+$zip            = new ZipArchive();
+$id             = uniqid();
+$date           = date('m-d-y');
+$zip_name       = 'Moodle_' . $name . '_' . $date;
+$zip_file       = "$zip_name.zip";
+$zip_path       = $CFG->dataroot . "/temp/" . $zip_file;
+
+// build the .zip
+if($zip->open($zip_path, ZIPARCHIVE::CREATE) !== TRUE) {
+  die($err_base . "Check validity of and permissions on $zip_path");
+} else {
+  $count = 0;
+  foreach($zip_files as $filename => $fileinfo) {
+    $fullname = $name . '/' . $fileinfo['author'] . '_' . $filename;
+    $zip->addFile(chop($fileinfo['path']), $fullname);
+    $count++;
+  }
+
+  $zip->close();
+}
+
+// send file
+// TODO: Let web server stream the file instead of tying up a PHP process
+header('Content-Type: application/zip');
+header('Content-Disposition: attachment; filename="' . $zip_file . '"');
+readfile($zip_path);
+
+// clean up
+unlink($zip_path);
+?>

Property changes on: LAE/zipfile.php
___________________________________________________________________
Added: svn:keywords
   + Id

