From 9122e00a1fd12e63bbd86fd570e8d987998441f2 Mon Sep 17 00:00:00 2001 From: Jaimos Skriletz Date: Fri, 16 Aug 2024 07:41:58 -0600 Subject: [PATCH] Show newly imported/created achievements to the Achievement List. This ensures that any imported or created achievement is shown in the achievement list (currently you have to reload the page or apply a new filter to see them). This also honors any currently filtered list by adding the new achievements to the current filtered list (vs just showing all achievements after an import). --- .../Instructor/AchievementList.pm | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/WeBWorK/ContentGenerator/Instructor/AchievementList.pm b/lib/WeBWorK/ContentGenerator/Instructor/AchievementList.pm index 300350f78c..7a59fda322 100644 --- a/lib/WeBWorK/ContentGenerator/Instructor/AchievementList.pm +++ b/lib/WeBWorK/ContentGenerator/Instructor/AchievementList.pm @@ -402,7 +402,8 @@ sub create_handler ($c) { $db->addUserAchievement($userAchievement); # Add to local list of achievements - push @{ $c->{allAchievementIDs} }, $newAchievementID; + push @{ $c->{allAchievementIDs} }, $newAchievementID; + push @{ $c->{visibleAchievementIDs} }, $newAchievementID; return (0, $c->maketext("Failed to create new achievement: [_1]", $@)) if $@; @@ -414,11 +415,12 @@ sub import_handler ($c) { my $ce = $c->ce; my $db = $c->db; - my $fileName = $c->param('action.import.source'); - my $assign = $c->param('action.import.assign'); - my @users = $db->listUsers; - my %allAchievementIDs = map { $_ => 1 } @{ $c->{allAchievementIDs} }; - my $filePath = $ce->{courseDirs}{achievements} . '/' . $fileName; + my $fileName = $c->param('action.import.source'); + my $assign = $c->param('action.import.assign'); + my @users = $db->listUsers; + my %allAchievementIDs = map { $_ => 1 } @{ $c->{allAchievementIDs} }; + my %visibleAchievementIDs = map { $_ => 1 } @{ $c->{visibleAchievementIDs} }; + my $filePath = $ce->{courseDirs}{achievements} . '/' . $fileName; # Open file name my $fh = Mojo::File->new($filePath)->open('<:encoding(UTF-8)') @@ -430,6 +432,10 @@ sub import_handler ($c) { while (my $data = $csv->getline($fh)) { my $achievement_id = $$data[0]; + + # Add imported achievement to visible list even if it already exists. + $visibleAchievementIDs{$achievement_id} = 1; + # Skip achievements that already exist next if $db->existsAchievement($achievement_id); @@ -474,8 +480,8 @@ sub import_handler ($c) { $fh->close; - $c->{allAchievementIDs} = [ keys %allAchievementIDs ]; - + $c->{allAchievementIDs} = [ keys %allAchievementIDs ]; + $c->{visibleAchievementIDs} = [ keys %visibleAchievementIDs ]; return (1, $c->maketext('Imported [quant,_1,achievement].', $count)); }