From d7b315699eb277fe2ac05c5dd4eb0afe0c128497 Mon Sep 17 00:00:00 2001 From: Mike Panitz Date: Wed, 14 Sep 2011 12:31:46 -0700 Subject: [PATCH] Suggested fix for MDL-29369 (add 'incremental submission download') This provides a new link on the mod/assignment/submissions.php page (in display_submissions() ) that will download all files for any student who has uploaded/changed at least one file. This only works for the 'upload' assignment type This requires a new table be added (by hand, currently) --- mod/assignment/lang/en/assignment.php | 1 + mod/assignment/lib.php | 2 + mod/assignment/type/upload/assignment.class.php | 58 +++++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/mod/assignment/lang/en/assignment.php b/mod/assignment/lang/en/assignment.php index e9a9031..efe751f 100644 --- a/mod/assignment/lang/en/assignment.php +++ b/mod/assignment/lang/en/assignment.php @@ -68,6 +68,7 @@ $string['deleteallsubmissions'] = 'Delete all submissions'; $string['deletefilefailed'] = 'Deleting of file failed.'; $string['description'] = 'Description'; $string['downloadall'] = 'Download all assignments as a zip'; +$string['downloadallsincelastdownload'] = 'Download all new assignments (since last download) as a zip'; $string['draft'] = 'Draft'; $string['due'] = 'Assignment due'; $string['duedate'] = 'Due date'; diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 889ebc6..511a7a4 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -1479,6 +1479,8 @@ class assignment_base { } if ($hassubmission && ($this->assignment->assignmenttype=='upload' || $this->assignment->assignmenttype=='online' || $this->assignment->assignmenttype=='uploadsingle')) { //TODO: this is an ugly hack, where is the plugin spirit? (skodak) echo html_writer::start_tag('div', array('class' => 'mod-assignment-download-link')); + if( $this->assignment->assignmenttype=='upload' ) + echo html_writer::link(new moodle_url('/mod/assignment/submissions.php', array('id' => $this->cm->id, 'download' => 'zip', 'zipfilter'=>'sincelastdownload')), get_string('downloadallsincelastdownload', 'assignment')) .'
'; echo html_writer::link(new moodle_url('/mod/assignment/submissions.php', array('id' => $this->cm->id, 'download' => 'zip')), get_string('downloadall', 'assignment')); echo html_writer::end_tag('div'); } diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php index 3cbcc2f..8ecc440 100644 --- a/mod/assignment/type/upload/assignment.class.php +++ b/mod/assignment/type/upload/assignment.class.php @@ -1087,8 +1087,41 @@ class assignment_upload extends assignment_base { * creates a zip of all assignment submissions and sends a zip to the browser */ public function download_submissions() { - global $CFG,$DB; + global $CFG,$DB, $USER; require_once($CFG->libdir.'/filelib.php'); + + define( 'ZIP_FILTER_KEY', 'zipfilter'); + define( 'ZIP_FILTER_KEY_NONE', 'none' ); + define( 'ZIP_FILTER_SINCE_LAST_DOWNLOAD', 'sincelastdownload'); + + $zip_filter = optional_param( ZIP_FILTER_KEY, ZIP_FILTER_KEY_NONE, PARAM_ALPHA); + // We'll use this next one to update the last downloaded time regardless + + $dbman = $DB->get_manager(); + if( !$dbman->table_exists('assignmentsubmissionslastdownloaded') ) { + print_error( 'In order for this feature to work you must manually add a table named "assignmentsubmissionslastdownloaded" to the moodle table
In order for this feature to work you must manually add a table named "assignmentsubmissionslastdownloaded" to the moodle table (typically tables are actually named something like mdl_assignmentsubmissionslastdownloaded, if you went with the default installation options).
Within it create
Here\s + some SQL that should work for MySQL:
+CREATE TABLE `mdl_assignmentsubmissionslastdownloaded` (
+`id` bigint(20) NOT NULL AUTO_INCREMENT,
+`assignment_id` bigint(20) NOT NULL,
+`user_id` bigint(20) NOT NULL,
+`submissionslastdownloaded` bigint(20) NOT NULL DEFAULT \'0\',
+PRIMARY KEY (`id`),
+UNIQUE KEY `a_u` (`assignment_id`,`user_id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
+


Sorry, but this "incremental download" feature won\'t be able to work until that is done'); + exit(0); + } + + $zip_filter_conditions = array( 'assignment_id' => $this->assignment->id,'user_id' => $USER->id ); + // If we're not in 'filter' mode (or we are but there's no prior downloads) then download all submissions + $last_downloaded = 0; + // We need to know (for later update) if there's a current record or not, so always get it here: + $previous_submission_download = $DB->get_record('assignmentsubmissionslastdownloaded', $zip_filter_conditions); + if( $previous_submission_download != false + && $zip_filter == ZIP_FILTER_SINCE_LAST_DOWNLOAD ) + $last_downloaded = $previous_submission_download->submissionslastdownloaded; + $submissions = $this->get_submissions('',''); if (empty($submissions)) { print_error('errornosubmissions', 'assignment'); @@ -1110,6 +1143,9 @@ class assignment_upload extends assignment_base { $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 + if( (int)$submission->timemodified <= $last_downloaded) + continue; + $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false); foreach ($files as $file) { //get files new name. @@ -1118,10 +1154,26 @@ class assignment_upload extends assignment_base { $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext); //save file name to array for zipping. $filesforzipping[$fileforzipname] = $file; - } + } } } // end of foreach loop - if ($zipfile = assignment_pack_files($filesforzipping)) { + + // Always remember the most recent download, even if it wasn't filtered + if( $previous_submission_download != false ) { + $DB->set_field('assignmentsubmissionslastdownloaded', 'submissionslastdownloaded', (int)time(), $zip_filter_conditions); + } else { + // this is the first time we've seen this user/assignment combo - add a new record + $record = new stdClass(); + $record->assignment_id = $zip_filter_conditions['assignment_id']; + $record->user_id = $zip_filter_conditions['user_id']; + $record->submissionslastdownloaded = time(); + $res = $DB->insert_record('assignmentsubmissionslastdownloaded', $record, false); + } + + if( empty( $filesforzipping ) ) { + print_error("There are no (new) student submissions to download!"); + } + else if ($zipfile = assignment_pack_files($filesforzipping)) { send_temp_file($zipfile, $filename); //send file and delete after sending. } } -- 1.7.3.1.msysgit.0