Skip to content

Commit 6f6aa10

Browse files
committed
Show both the course id and the archive file when listing course archives in the admin course.
The previous setup did not take into account what would happen if multiple archive files contained the same course id. So instead of the `listArchivedCourse` method returning a hash of the form ```perl { myTestCourse => { filename => 'myTestCourse.tar.gz', size => '605 KB' } } ``` it now returns a hash of the form ```perl { 'myTestCourse.tar.gz' => { courseID => 'myTestCourse', size => '605 KB', lastModified => 1778667472 } } ``` Note that the `lastModified` key is only included because that is what is saved in the cache file, and that is just directly returned if the archive file has not been modified. Although, at this point it is not used elsewhere. Both the courseID and the filename are then displayed. The format displayed is `myTestCourse (myTestCourse.tar.gz, 605 KB)`. If there is also an archive file `myTestCourseAlt.tar.gz` then it will also be listed as something like`myTestCourse (myTestCourseAlt.tar.gz, 1.6 MB)`.
1 parent bf8b07e commit 6f6aa10

3 files changed

Lines changed: 20 additions & 30 deletions

File tree

lib/WeBWorK/Utils/CourseManagement.pm

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ sub listCourses {
111111
=item listArchivedCourses($ce)
112112
113113
Lists the courses which have been archived (end in .tar.gz). The courses found
114-
are returned as a hash whose keys are the course ids and the values are
115-
references to hashes containing the C<filename> (the basename of the file
116-
including the .tar.gz extension) and file C<size>. For example,
114+
are returned as a hash whose keys are the filenames (the basename of the file
115+
including the .tar.gz extension) and the values are references to hashes
116+
containing the C<courseID>, file C<size>, and C<lastModified> time. For example,
117117
118118
{
119-
myTestCourse => {
120-
filename => 'myTestCourse.tar.gz',
121-
size => '605 KB'
119+
'myTestCourse.tar.gz' => {
120+
courseID => 'myTestCourse',
121+
size => '605 KB',
122+
lastModified => 1778667472
122123
}
123124
}
124125
@@ -134,23 +135,19 @@ sub listArchivedCourses {
134135
my $archiveDataFile = $archivesDir->child('archive-cache.json');
135136
my $archiveData = eval { decode_json($archiveDataFile->slurp) } || {};
136137

137-
my $archiveDataUpdated = 0;
138+
my %updatedArchiveData;
138139
my %return;
139140
for (@$archives) {
140141
my $basename = $_->basename;
141142
my $lastModified = $_->stat->mtime;
142143

143144
if ($archiveData->{$basename} && $archiveData->{$basename}{lastModified} >= $lastModified) {
144-
$return{ $archiveData->{$basename}{courseID} } = {
145-
filename => $basename,
146-
size => $archiveData->{$basename}{size}
147-
}
148-
if defined $archiveData->{$basename}{courseID};
145+
$updatedArchiveData{$basename} = $archiveData->{$basename};
146+
$return{$basename} = $updatedArchiveData{$basename} if defined $archiveData->{$basename}{courseID};
149147
next;
150148
}
151149

152-
$archiveDataUpdated = 1;
153-
$archiveData->{$basename} = { lastModified => $lastModified };
150+
$updatedArchiveData{$basename} = { lastModified => $lastModified };
154151

155152
my $archive = Archive::Tar->new($_);
156153
my %top_level;
@@ -164,20 +161,12 @@ sub listArchivedCourses {
164161
}
165162
my ($currCourseID) = keys %top_level;
166163

167-
$archiveData->{$basename}{courseID} = $currCourseID;
168-
$archiveData->{$basename}{size} = getHumanReadableFileSize($_);
169-
$return{$currCourseID} = { filename => $basename, size => $archiveData->{$basename}{size} };
170-
}
171-
172-
my %archives = map { $_->basename => 1 } @$archives;
173-
for (keys %$archiveData) {
174-
unless ($archives{$_}) {
175-
delete $archiveData->{$_};
176-
$archiveDataUpdated = 1;
177-
}
164+
$updatedArchiveData{$basename}{courseID} = $currCourseID;
165+
$updatedArchiveData{$basename}{size} = getHumanReadableFileSize($_);
166+
$return{$basename} = $updatedArchiveData{$basename};
178167
}
179168

180-
$archiveDataFile->spew(encode_json($archiveData)) if $archiveDataUpdated;
169+
$archiveDataFile->spew(encode_json(\%updatedArchiveData));
181170

182171
return %return;
183172
}

templates/ContentGenerator/CourseAdmin.html.ep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<ol>
6666
% my %courseArchives = listArchivedCourses($ce);
6767
% for (sort { lc($a) cmp lc($b) } keys %courseArchives) {
68-
<li><%= "$_ ($courseArchives{$_}{size})" %></li>
68+
<li><%= "$courseArchives{$_}{courseID} ($_, $courseArchives{$_}{size})" %></li>
6969
% }
7070
</ol>
7171
% }

templates/ContentGenerator/CourseAdmin/unarchive_course_form.html.ep

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
%
55
% # Find courses which have been archived.
66
% my %courseArchives = listArchivedCourses($ce);
7-
% my @courseIDs = sort { lc($a) cmp lc($b) } keys %courseArchives;
7+
% my @archiveFiles = sort { lc($a) cmp lc($b) } keys %courseArchives;
88
%
9-
% if (@courseIDs) {
9+
% if (@archiveFiles) {
1010
<%= form_for current_route, method => 'POST', begin =%>
1111
<%= $c->hidden_authen_fields =%>
1212
<%= $c->hidden_fields('subDisplay') =%>
@@ -21,7 +21,8 @@
2121
<div class="col-lg-9 col-md-8">
2222
<%= select_field
2323
unarchive_courseID =>
24-
[ map { [ "$_ ($courseArchives{$_}{size})" => $courseArchives{$_}{filename} ] } @courseIDs ],
24+
[ map { [ "$courseArchives{$_}{courseID} ($_, $courseArchives{$_}{size})" => $_ ] }
25+
@archiveFiles ],
2526
id => 'unarchive_courseID',
2627
class => 'form-select',
2728
size => 10 =%>

0 commit comments

Comments
 (0)