### 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 13 Jan 2009 03:26:00 -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 13 Jan 2009 03:26:00 -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 '
'.get_string('downloadall', 'assignment').'
'; + } /// Construct the SQL if ($where = $table->get_sql_where()) { @@ -3237,4 +3239,44 @@ default: return null; } } -?> + +/** + * generate zip file from array of given files + * @param array $filesforzipping - array of files to pass into archive_to_pathname + * @return path of temp file - note this returned file does not have a .zip extension - it is a temp file. + */ +function assignment_pack_files($filesforzipping) { + global $CFG; + //create path for new zip file. + $tempzip = tempnam($CFG->dataroot.'/temp/', 'assignment_'); + //zip files + $zipper = new zip_packer(); + if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) { + return $tempzip; + } + return false; +} +//TODO - this is a copy of the function my_mktempdir in admin/uploadpicture.php - it would be good to have as a core function. +/** + * Create a unique temporary directory with a given prefix name, + * inside a given directory, with given permissions. Return the + * full path to the newly created temp directory. + * + * @param string $dir where to create the temp directory. + * @param string $prefix prefix for the temp directory name (default '') + * @param string $mode permissions for the temp directory (default 700) + * + * @return string The full path to the temp directory. + */ +function assignment_create_temp_dir($dir, $prefix='', $mode=0700) { + if (substr($dir, -1) != '/') { + $dir .= '/'; + } + + do { + $path = $dir.$prefix.mt_rand(0, 9999999); + } while (!mkdir($path, $mode)); + + return $path; +} +?> \ No newline at end of file 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 13 Jan 2009 03:26:00 -0000 @@ -281,6 +281,42 @@ 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 + $tempdir = assignment_create_temp_dir($CFG->dataroot."/temp/", "assignment".$this->assignment->id); //location for temp files. + //online assignment can use html + $filextn=".html"; + + $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 = "". $submission->data1. ""; //fetched from database + //get file name.html + $fileforzipname = $a_user->username . "_" . clean_filename($this->assignment->name) . $filextn; + $fd = fopen($tempdir . $fileforzipname,'wb'); //create if not exist, write binary + fwrite( $fd, $submissioncontent); + fclose( $fd ); + $filesforzipping[$fileforzipname] = $tempdir.$fileforzipname; + } + } //end of foreach + if ($zipfile = assignment_pack_files($filesforzipping)) { + remove_dir($tempdir); //remove old tempdir with individual files. + send_temp_file($zipfile, $filename); //send file and delete after sending. + } + } } 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 13 Jan 2009 03:26:00 -0000 @@ -1041,7 +1041,47 @@ 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 + $tempdir = assignment_create_temp_dir($CFG->dataroot."/temp/", "assignment".$this->assignment->id); //location for temp files. + $fs = get_file_storage(); + + $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($tempdir . $fileforzipname)) { + error ("failed to copy file
" .$tempdir. $fileforzipname); + } + //save file name to array for zipping. + $filesforzipping[$fileforzipname] = $tempdir.$fileforzipname; + } + } + } // end of foreach loop + if ($zipfile = assignment_pack_files($filesforzipping)) { + remove_dir($tempdir); //remove old tempdir with individual files. + send_temp_file($zipfile, $filename); //send file and delete after sending. + } + } } class mod_assignment_upload_notes_form extends moodleform { @@ -1091,6 +1131,4 @@ } } - - -?> +?> \ No newline at end of file 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 13 Jan 2009 03:26:00 -0000 @@ -178,6 +178,50 @@ send_stored_file($file, 0, 0, true); // download MUST be forced - security! } + /** + * creates a zip of all assignment submissions and sends a zip to the browser + */ + 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 + $tempdir = assignment_create_temp_dir($CFG->dataroot."/temp/", "assignment".$this->assignment->id); //location for temp files. + $fs = get_file_storage(); + + $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($tempdir . $fileforzipname)) { + error ("failed to copy file
" .$tempdir. $fileforzipname); + } + //save file name to array for zipping. + $filesforzipping[$fileforzipname] = $tempdir.$fileforzipname; + } + } + } // End of foreach + if ($zipfile = assignment_pack_files($filesforzipping)) { + remove_dir($tempdir); //remove old tempdir with individual files. + send_temp_file($zipfile, $filename); //send file and delete after sending. + } + } } -?> +?> \ No newline at end of file