Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions lib/WeBWorK/Utils/CourseManagement.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use WeBWorK::Utils::Instructor qw(assignSetsToUsers);
our @EXPORT_OK = qw(
listCourses
listArchivedCourses
statArchivedCourses
addCourse
renameCourse
retitleCourse
Expand Down Expand Up @@ -110,38 +109,42 @@ sub listCourses {

=item listArchivedCourses($ce)

Lists the courses which have been archived (end in .tar.gz).
Lists the courses which have been archived (end in .tar.gz). The courses found
are returned as a hash whose keys are the course ids and the values are
references to hashes containing the C<filename> (the basename of the file
including the .tar.gz extension) and file C<size>. For example,

{
myTestCourse => {
filename => 'myTestCourse.tar.gz',
size => '605 KB'
}
}

=cut

sub listArchivedCourses {
my ($ce) = @_;
my $archivesDir = path("$ce->{webworkDirs}{courses}/$ce->{admin_course_id}/archives");
surePathToFile($ce->{webworkDirs}{courses}, "$archivesDir/test"); # Ensure archives directory exists.
return @{ $archivesDir->list->grep(qr/\.tar\.gz$/)->map('basename') };
}

=item statArchivedCourses($ce)
my $archives = $archivesDir->list->grep(qr/\.tar\.gz$/i);

File info for the courses which have been archived (end in .tar.gz).

=cut

sub statArchivedCourses {
my ($ce) = @_;
my @archives = listArchivedCourses($ce);
my $archivesDir = path("$ce->{webworkDirs}{courses}/$ce->{admin_course_id}/archives");
my %return;
for (@archives) {
my @stat = stat("$archivesDir/$_");
my $size = $stat[7];
for (@$archives) {
my $size = $_->stat->size;
my @units = qw(B KB MB GB);
my $unit_idx = 0;
while ($size >= 1024 && $unit_idx < @units - 1) {
while ($size >= 1024 && $unit_idx < $#units) {
$size /= 1024;
$unit_idx++;
++$unit_idx;
}
$return{ $_ =~ s/\.tar\.gz$//ir } = { filename => $_, size => sprintf("%s %s", int($size), $units[$unit_idx]) };
my $basename = $_->basename;
my $round = 10**($unit_idx > 0 ? $unit_idx - 1 : 0);
$return{ $basename =~ s/\.tar\.gz$//ir } = {
filename => $basename,
size => sprintf("%s %s", int($size * $round) / $round, $units[$unit_idx])
};
}
return %return;
}
Expand Down
5 changes: 3 additions & 2 deletions templates/ContentGenerator/CourseAdmin.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
) =%>
</p>
<ol>
% for (sort { lc($a) cmp lc($b) } listArchivedCourses($ce)) {
<li><%= $_ %></li>
% my %courseArchives = listArchivedCourses($ce);
% for (sort { lc($a) cmp lc($b) } keys %courseArchives) {
<li><%= "$_ ($courseArchives{$_}{size})" %></li>
% }
</ol>
% }
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
% use WeBWorK::Utils::CourseManagement qw(statArchivedCourses);
% use WeBWorK::Utils::CourseManagement qw(listArchivedCourses);
%
<h2><%= maketext('Unarchive Course') %> <%= $c->helpMacro('AdminUnarchiveCourse') %></h2>
%
% # Find courses which have been archived.
% my %stat = statArchivedCourses($ce);
% my @courseIDs = sort { lc($a) cmp lc($b) } keys %stat;
% my %courseArchives = listArchivedCourses($ce);
% my @courseIDs = sort { lc($a) cmp lc($b) } keys %courseArchives;
%
% if (@courseIDs) {
<%= form_for current_route, method => 'POST', begin =%>
Expand All @@ -15,18 +15,22 @@
%
<div class="col-lg-10 col-md-11">
<div class="row mb-2">
<%= label_for 'unarchive_courseID' => maketext('Course ID:'), class => 'col-lg-3 col-md-4 col-form-label fw-bold' =%>
<%= label_for 'unarchive_courseID' => maketext('Course ID:'),
class => 'col-lg-3 col-md-4 col-form-label fw-bold' =%>
<div class="col-lg-9 col-md-8">
<%= select_field
unarchive_courseID => [map {["$_ ($stat{$_}{size})" => $stat{$_}{filename}]} @courseIDs],
id => 'unarchive_courseID',
class => 'form-select',
size => 10
unarchive_courseID => [
map { [ "$_ ($courseArchives{$_}{size})" => $courseArchives{$_}{filename} ] } @courseIDs
],
id => 'unarchive_courseID',
class => 'form-select',
size => 10
=%>
</div>
</div>
<div class="row mb-2 align-items-center">
<%= label_for new_courseID => maketext('New ID:'), class => 'col-lg-3 col-md-4 col-form-label fw-bold' =%>
<%= label_for new_courseID => maketext('New ID:'),
class => 'col-lg-3 col-md-4 col-form-label fw-bold' =%>
<div class="col-lg-9 col-md-8">
<%= text_field new_courseID => '',
id => 'new_courseID',
Expand Down