Skip to content

Commit 3f42639

Browse files
authored
Merge pull request #2922 from somiaj/copy-aux-files-option
PGProblemEditor: Add option to copy auxiliary files.
2 parents 920897b + 33a5395 commit 3f42639

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,28 +704,34 @@ sub saveFileChanges ($c, $outputFilePath, $backup = 0) {
704704
}
705705

706706
# If the file is being saved as a new file in a new location, and the file is accompanied by auxiliary files
707-
# transfer them as well. If the file is a pg file, then assume there are auxiliary files. Copy all files not
708-
# ending in .pg from the original directory to the new one.
709-
if ($c->{action} eq 'save_as' && $outputFilePath =~ /\.pg/) {
707+
# transfer them as well. If the option 'copyAuxFiles' is set and the file is a pg file, then assume there are
708+
# auxiliary files. Copy all files not ending in .pg from the original directory to the new one.
709+
if ($c->{action} eq 'save_as' && $c->param('copyAuxFiles') && $outputFilePath =~ /\.pg/) {
710710
my $sourceDirectory = Mojo::File->new(($c->{sourceFilePath} || '') =~ s|/[^/]+\.pg$||r);
711711
my $outputDirectory = Mojo::File->new($outputFilePath =~ s|/[^/]+\.pg$||r);
712712

713713
# Only perform the copy if the output directory is an actual new location.
714714
if ($sourceDirectory ne $outputDirectory && -d $sourceDirectory) {
715+
my $filesCopied = 0;
715716
for my $file (@{ $sourceDirectory->list }) {
716717
# The .pg file being edited has already been transferred. Ignore any others in the directory.
717718
next if $file =~ /\.pg$/;
718719
my $toPath = $outputDirectory->child($file->basename);
719720
# Only copy regular files that are readable and that have not already been copied.
720721
if (-f $file && -r $file && !-e $toPath) {
721722
eval { $file->copy_to($toPath) };
722-
$c->addbadmessage($c->maketext('Error copying [_1] to [_2].', $file, $toPath)) if $@;
723+
if ($@) {
724+
$c->addbadmessage($c->maketext('Error copying [_1] to [_2].', $file, $toPath));
725+
} else {
726+
$filesCopied = 1;
727+
}
723728
}
724729
}
725730
$c->addgoodmessage($c->maketext(
726731
'Copied auxiliary files from [_1] to new location at [_2].',
727732
$sourceDirectory, $outputDirectory
728-
));
733+
))
734+
if $filesCopied;
729735
}
730736
}
731737

templates/ContentGenerator/Instructor/PGProblemEditor/save_as_form.html.ep

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
<%= hidden_field 'action.save_as.source_file' => $c->{editFilePath} =%>
5353
<%= hidden_field 'action.save_as.file_type' => $c->{file_type} =%>
5454
</div>
55+
<div class="form-check">
56+
% param('copyAuxFiles', 1) unless defined param('copyAuxFiles');
57+
<%= check_box copyAuxFiles => 1, id => 'copyAuxFiles', class => 'form-check-input' =%>
58+
<%= hidden_field copyAuxFiles => 0 =%>
59+
<%= label_for copyAuxFiles => maketext('Copy auxiliary files.'), class => 'form-check-label' =%>
60+
</div>
5561
% if ($can_add_problem_to_set) {
5662
<div class="form-check">
5763
<%= radio_button 'action.save_as.saveMode' => 'rename', id => 'action_save_as_saveMode_rename_id',

templates/HelpFiles/InstructorPGProblemEditor.html.ep

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
. 'append to the end to then end of the current set as a new problem, or create a problem that is not '
180180
. 'attached to a problem set.') =%>
181181
</p>
182+
<p>
183+
<%= maketext('When saving the problem in a new location (directory), by default all auxiliary files, such '
184+
. 'as images, or other files that do not end in ".pg", will also be copied to the new location. To '
185+
. 'stop this behavior, uncheck the "Copy auxiliary files" option.') =%>
186+
</p>
182187
<p>
183188
<%= maketext('You may want to create an unattached problem if you are using the current problem as a model '
184189
. 'for a new problem. You can add the new file to a homework set from the Library Browser or via the '

0 commit comments

Comments
 (0)