Skip to content

Commit

Permalink
MDL-56022 Assign: Make folders structure in the downloaded zip optional
Browse files Browse the repository at this point in the history
Special thanks to Matt McDermott for the contributing the Javascript
code to fix this issue.
  • Loading branch information
sbourget committed Oct 17, 2016
1 parent 6a69cda commit 1b467cb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
10 changes: 5 additions & 5 deletions mod/assign/feedback/file/importziplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ public function import_zip_files($assignment, $fileplugin) {
if ($this->is_file_modified($assignment, $user, $plugin, $filename, $unzippedfile)) {
$grade = $assignment->get_user_grade($user->id, true);

// In 3.1 the download structure of the submission files changed so that each student had their own
// In 3.1 the default download structure of the submission files changed so that each student had their own
// separate folder, the files were not renamed and the folder structure was kept. It is possible that
// a user downloaded the submission files in 3.0 (or earlier) and edited the zip to add feedback and
// in that time the site was updated to 3.1, the following code means that we will still support the
// old file structure. For more information please see - MDL-52489.
// a user downloaded the submission files in 3.0 (or earlier) and edited the zip to add feedback or
// changed the behavior back to the previous format, the following code means that we will still support the
// old file structure. For more information please see - MDL-52489 / MDL-56022.
$path = pathinfo($filename);
if ($path['dirname'] == '.') { // Old structure as students are not in separate folders.
if ($path['dirname'] == '.') { // Student submissions are not in separate folders.
$basename = $filename;
$dirname = "/";
$dirnamewslash = "/";
Expand Down
7 changes: 7 additions & 0 deletions mod/assign/gradingoptionsform.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public function definition() {
$mform->setDefault('showonlyactiveenrol', $instance['showonlyactiveenrol']);
}

// Place student downloads in seperate folders.
if ($instance['submissionsenabled']) {
$mform->addElement('checkbox', 'downloadasfolders', get_string('downloadasfolders', 'assign'), '', $dirtyclass);
$mform->addHelpButton('downloadasfolders', 'downloadasfolders', 'assign');
$mform->setDefault('downloadasfolders', $instance['downloadasfolders']);
}

// Hidden params.
$mform->addElement('hidden', 'contextid', $instance['contextid']);
$mform->setType('contextid', PARAM_INT);
Expand Down
2 changes: 2 additions & 0 deletions mod/assign/lang/en/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
$string['description'] = 'Description';
$string['downloadall'] = 'Download all submissions';
$string['download all submissions'] = 'Download all submissions in a zip file.';
$string['downloadasfolders'] = 'Download as separate folders';
$string['downloadasfolders_help'] = 'When enabled downloaded files will be placed in separate folders and files will not be renamed.';
$string['downloadselectedsubmissions'] = 'Download selected submissions';
$string['duedate'] = 'Due date';
$string['duedatecolon'] = 'Due date: {$a}';
Expand Down
74 changes: 52 additions & 22 deletions mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2902,28 +2902,51 @@ protected function download_submissions($userids = false) {
}

if ($submission) {
$downloadasfolders = get_user_preferences('assign_downloadasfolders', 1);
foreach ($this->submissionplugins as $plugin) {
if ($plugin->is_enabled() && $plugin->is_visible()) {
$pluginfiles = $plugin->get_files($submission, $student);
foreach ($pluginfiles as $zipfilepath => $file) {
$subtype = $plugin->get_subtype();
$type = $plugin->get_type();
$zipfilename = basename($zipfilepath);
$prefixedfilename = clean_filename($prefix .
'_' .
$subtype .
'_' .
$type .
'_');
if ($type == 'file') {
$pathfilename = $prefixedfilename . $file->get_filepath() . $zipfilename;
} else if ($type == 'onlinetext') {
$pathfilename = $prefixedfilename . '/' . $zipfilename;
} else {
$pathfilename = $prefixedfilename . '/' . $zipfilename;
if ($downloadasfolders) {
// Create a folder for each user for each assignment plugin.
// This is the default behavior for version of Moodle >= 3.1.
$submission->exportfullpath = true;
$pluginfiles = $plugin->get_files($submission, $student);
foreach ($pluginfiles as $zipfilepath => $file) {
$subtype = $plugin->get_subtype();
$type = $plugin->get_type();
$zipfilename = basename($zipfilepath);
$prefixedfilename = clean_filename($prefix .
'_' .
$subtype .
'_' .
$type .
'_');
if ($type == 'file') {
$pathfilename = $prefixedfilename . $file->get_filepath() . $zipfilename;
} else if ($type == 'onlinetext') {
$pathfilename = $prefixedfilename . '/' . $zipfilename;
} else {
$pathfilename = $prefixedfilename . '/' . $zipfilename;
}
$pathfilename = clean_param($pathfilename, PARAM_PATH);
$filesforzipping[$pathfilename] = $file;
}
} else {
// Create a single folder for all users of all assignment plugins.
// This was the default behavior for version of Moodle < 3.1.
$submission->exportfullpath = false;
$pluginfiles = $plugin->get_files($submission, $student);
foreach ($pluginfiles as $zipfilename => $file) {
$subtype = $plugin->get_subtype();
$type = $plugin->get_type();
$prefixedfilename = clean_filename($prefix .
'_' .
$subtype .
'_' .
$type .
'_' .
$zipfilename);
$filesforzipping[$prefixedfilename] = $file;
}
$pathfilename = clean_param($pathfilename, PARAM_PATH);
$filesforzipping[$pathfilename] = $file;
}
}
}
Expand Down Expand Up @@ -3679,6 +3702,7 @@ protected function view_grading_table() {
$showquickgrading = empty($controller) && $this->can_grade();
$quickgrading = get_user_preferences('assign_quickgrading', false);
$showonlyactiveenrolopt = has_capability('moodle/course:viewsuspendedusers', $this->context);
$downloadasfolders = get_user_preferences('assign_downloadasfolders', 1);

$markingallocation = $this->get_instance()->markingworkflow &&
$this->get_instance()->markingallocation &&
Expand Down Expand Up @@ -3715,7 +3739,8 @@ protected function view_grading_table() {
'markingworkflowopt'=>$markingworkflowoptions,
'markingallocationopt'=>$markingallocationoptions,
'showonlyactiveenrolopt'=>$showonlyactiveenrolopt,
'showonlyactiveenrol'=>$this->show_only_active_users());
'showonlyactiveenrol' => $this->show_only_active_users(),
'downloadasfolders' => $downloadasfolders);

$classoptions = array('class'=>'gradingoptionsform');
$gradingoptionsform = new mod_assign_grading_options_form(null,
Expand Down Expand Up @@ -6178,8 +6203,8 @@ protected function process_save_grading_options() {
'markingworkflowopt' => $markingworkflowoptions,
'markingallocationopt' => $markingallocationoptions,
'showonlyactiveenrolopt'=>$showonlyactiveenrolopt,
'showonlyactiveenrol'=>$this->show_only_active_users());

'showonlyactiveenrol' => $this->show_only_active_users(),
'downloadasfolders' => get_user_preferences('assign_downloadasfolders', 1));
$mform = new mod_assign_grading_options_form(null, $gradingoptionsparams);
if ($formdata = $mform->get_data()) {
set_user_preference('assign_perpage', $formdata->perpage);
Expand All @@ -6195,6 +6220,11 @@ protected function process_save_grading_options() {
if ($showquickgrading) {
set_user_preference('assign_quickgrading', isset($formdata->quickgrading));
}
if (isset($formdata->downloadasfolders)) {
set_user_preference('assign_downloadasfolders', 1); // Enabled.
} else {
set_user_preference('assign_downloadasfolders', 0); // Disabled.
}
if (!empty($showonlyactiveenrolopt)) {
$showonlyactiveenrol = isset($formdata->showonlyactiveenrol);
set_user_preference('grade_report_showonlyactiveenrol', $showonlyactiveenrol);
Expand Down
6 changes: 6 additions & 0 deletions mod/assign/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ M.mod_assign.init_grading_options = function(Y) {
Y.one('form.gradingoptionsform').submit();
});
}
var downloadasfolderselement = Y.one('#id_downloadasfolders');
if (downloadasfolderselement) {
downloadasfolderselement.on('change', function(e) {
Y.one('form.gradingoptionsform').submit();
});
}
});
};

Expand Down
7 changes: 6 additions & 1 deletion mod/assign/submission/file/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ public function get_files(stdClass $submission, stdClass $user) {
false);

foreach ($files as $file) {
$result[$file->get_filepath().$file->get_filename()] = $file;
// Do we return the full folder path or just the file name?
if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
$result[$file->get_filename()] = $file;
} else {
$result[$file->get_filepath().$file->get_filename()] = $file;
}
}
return $result;
}
Expand Down

0 comments on commit 1b467cb

Please sign in to comment.