Skip to content

Commit

Permalink
Add a task name for the AchievementNotification.pm task and other min…
Browse files Browse the repository at this point in the history
…or cleanup.

Also use the notes to pass the course id as in the other tasks.
  • Loading branch information
drgrice1 committed Dec 6, 2023
1 parent dc9221f commit e9f4ff0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
21 changes: 11 additions & 10 deletions lib/Mojolicious/WeBWorK/Tasks/AchievementNotification.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/WeBWorK/AchievementEvaluator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -290,6 +290,8 @@ sub disable_handler ($c) {
$c->stash('achievementID')
));
}

return;
}

1;
5 changes: 3 additions & 2 deletions lib/WeBWorK/ContentGenerator/Instructor/JobManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e9f4ff0

Please sign in to comment.