From 4580b98ed5e4c9d1a09728851f8fc0f53ff6e7be Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Mon, 4 Dec 2023 18:14:24 -0600 Subject: [PATCH] Add a task name for the AchievementNotification.pm task and other minor cleanup. Also use the notes to pass the course id as in the other tasks. --- .../WeBWorK/Tasks/AchievementNotification.pm | 21 ++++++++++--------- lib/WeBWorK/AchievementEvaluator.pm | 4 ++-- .../AchievementNotificationEditor.pm | 4 +++- .../ContentGenerator/Instructor/JobManager.pm | 5 +++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/Mojolicious/WeBWorK/Tasks/AchievementNotification.pm b/lib/Mojolicious/WeBWorK/Tasks/AchievementNotification.pm index 810a59d15f..a030e7ee1c 100644 --- a/lib/Mojolicious/WeBWorK/Tasks/AchievementNotification.pm +++ b/lib/Mojolicious/WeBWorK/Tasks/AchievementNotification.pm @@ -31,30 +31,31 @@ use WeBWorK::SafeTemplate; # send student notification that they have earned an achievement sub run ($job, $mail_data) { - my $ce = eval { WeBWorK::CourseEnvironment->new({ courseName => $mail_data->{courseName} }); }; - return $job->fail("Could not construct course environment for $mail_data->{courseName}.") + my $courseID = $job->info->{notes}{courseID}; + + my $ce = eval { WeBWorK::CourseEnvironment->new({ courseName => $courseID }); }; + return $job->fail("Could not construct course environment for $courseID.") unless $ce; - $job->{language_handle} = - WeBWorK::Localize::getLoc($ce->{language} || 'en'); + $job->{language_handle} = WeBWorK::Localize::getLoc($ce->{language} || 'en'); my $db = WeBWorK::DB->new($ce->{dbLayout}); - return $job->fail($job->maketext("Could not obtain database connection for [_1].", $mail_data->{courseName})) + return $job->fail($job->maketext('Could not obtain database connection for [_1].', $courseID)) unless $db; - return $job->fail($job->maketext("Cannot notify student without an achievement.")) + return $job->fail($job->maketext('Cannot notify student without an achievement.')) unless $mail_data->{achievementID}; $mail_data->{achievement} = $db->getAchievement($mail_data->{achievementID}); - return $job->fail($job->maketext("Could not find achievement [_1].", $mail_data->{achievementID})) + return $job->fail($job->maketext('Could not find achievement [_1].', $mail_data->{achievementID})) unless $mail_data->{achievement}; my $result_message = eval { $job->send_achievement_notification($ce, $db, $mail_data) }; if ($@) { - $job->app->log->error($job->maketext("An error occurred while trying to send email: $@")); - return $job->fail($job->maketext("An error occurred while trying to send email: [_1]", $@)); + $job->app->log->error("An error occurred while trying to send email: $@"); + return $job->fail($job->maketext('An error occurred while trying to send email: [_1]', $@)); } $job->app->log->info("Message sent to $mail_data->{recipient}"); - return $job->finish($job->maketext("Message sent to [_1]", $mail_data->{recipient})); + return $job->finish($result_message); } sub send_achievement_notification ($job, $ce, $db, $mail_data) { diff --git a/lib/WeBWorK/AchievementEvaluator.pm b/lib/WeBWorK/AchievementEvaluator.pm index bd72708f12..812e161a81 100644 --- a/lib/WeBWorK/AchievementEvaluator.pm +++ b/lib/WeBWorK/AchievementEvaluator.pm @@ -257,13 +257,13 @@ sub checkForAchievements ($problem_in, $pg, $c, %options) { send_achievement_email => [ { recipient => $user_id, subject => 'Congratulations on earning a new achievement!', - courseName => $ce->{courseName}, achievementID => $achievement_id, setID => $set_id, nextLevelPoints => $nextLevelPoints || 0, pointsEarned => $achievementPoints, remote_host => $c->tx->remote_address || "UNKNOWN", - } ] + } ], + { notes => { courseID => $ce->{courseName} } } ) if ($ce->{mail}{achievementEmailFrom} && $achievement->email_template); } diff --git a/lib/WeBWorK/ContentGenerator/Instructor/AchievementNotificationEditor.pm b/lib/WeBWorK/ContentGenerator/Instructor/AchievementNotificationEditor.pm index 9f26f87011..01abffb81f 100644 --- a/lib/WeBWorK/ContentGenerator/Instructor/AchievementNotificationEditor.pm +++ b/lib/WeBWorK/ContentGenerator/Instructor/AchievementNotificationEditor.pm @@ -131,7 +131,7 @@ sub saveFileChanges ($c, $outputFilePath) { print $OUTPUTFILE $c->stash('achievementNotification'); close $OUTPUTFILE; }; - my $writeFileErrors = $@ if $@; + my $writeFileErrors = $@; # Catch errors in saving files, if ($writeFileErrors) { @@ -290,6 +290,8 @@ sub disable_handler ($c) { $c->stash('achievementID') )); } + + return; } 1; diff --git a/lib/WeBWorK/ContentGenerator/Instructor/JobManager.pm b/lib/WeBWorK/ContentGenerator/Instructor/JobManager.pm index 2b7ee3b0e9..e128e70cf6 100644 --- a/lib/WeBWorK/ContentGenerator/Instructor/JobManager.pm +++ b/lib/WeBWorK/ContentGenerator/Instructor/JobManager.pm @@ -28,8 +28,9 @@ use constant ACTION_FORMS => [ [ filter => x('Filter') ], [ sort => x('Sort') ], # All tasks added in the Mojolicious::WeBWorK module need to be listed here. use constant TASK_NAMES => { - lti_mass_update => x('LTI Mass Update'), - send_instructor_email => x('Send Instructor Email') + lti_mass_update => x('LTI Mass Update'), + send_instructor_email => x('Send Instructor Email'), + send_achievement_email => x('Send Achiement Email') }; # This constant is not used. It is here so that gettext adds these strings to the translation files.