From f4373dd7780f1c7509658d77a8d38f3128209f9a Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Fri, 15 Nov 2024 19:03:12 -0800 Subject: [PATCH] minimize calls to grade_set --- lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm | 22 +++----- .../Authen/LTIAdvantage/SubmitGrade.pm | 23 +++----- lib/WeBWorK/Utils/Sets.pm | 55 +++++++++---------- 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm b/lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm index b90fe02388..14eb40d6be 100644 --- a/lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm +++ b/lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm @@ -30,7 +30,7 @@ use Digest::SHA qw(sha1_base64); use WeBWorK::Debug; use WeBWorK::Utils qw(wwRound); -use WeBWorK::Utils::Sets qw(grade_set grade_gateway grade_all_sets can_submit_LMS_score); +use WeBWorK::Utils::Sets qw(grade_all_sets can_submit_LMS_score); # This package contains utilities for submitting grades to the LMS sub new ($invocant, $c, $post_processing_mode = 0) { @@ -137,12 +137,11 @@ async sub submit_set_grade ($self, $userID, $setID) { my $ce = $c->{ce}; my $db = $c->{db}; - return - unless (can_submit_LMS_score($db, $ce, $userID, $setID) - || !$self->{post_processing_mode} && $ce->{LTIGradeOnSubmit} eq 'homework_always'); - my $user = $db->getUser($userID); - return 0 unless $user; + return unless $user; + + my $score = can_submit_LMS_score($db, $ce, $userID, $setID); + return unless ($score || !$self->{post_processing_mode} && $ce->{LTIGradeOnSubmit} eq 'homework_always'); my $userSet = $db->getMergedSet($userID, $setID); @@ -151,15 +150,8 @@ async sub submit_set_grade ($self, $userID, $setID) { $self->warning('lis_source_did is not available for this set.') if !$userSet->lis_source_did && ($ce->{debug_lti_grade_passback} || $self->{post_processing_mode}); - return await $self->submit_grade( - $userSet->lis_source_did, - scalar( - $userSet->assignment_type =~ /gateway/ - ? grade_gateway($db, $setID, $userID) - : grade_set($db, $userSet, $userID) - ), - $self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always' - ); + return await $self->submit_grade($userSet->lis_source_did, $score->{score}, + $self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always'); } # Submits a score of $score to the lms with $sourcedid as the identifier. diff --git a/lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm b/lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm index b41f15ffbe..fb3614e6a2 100644 --- a/lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm +++ b/lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm @@ -39,7 +39,7 @@ use Time::HiRes; use WeBWorK::Debug; use WeBWorK::Utils qw(wwRound); -use WeBWorK::Utils::Sets qw(grade_set grade_gateway grade_all_sets can_submit_LMS_score); +use WeBWorK::Utils::Sets qw(grade_all_sets can_submit_LMS_score); # This package contains utilities for submitting grades to the LMS via LTI 1.3. sub new ($invocant, $c, $post_processing_mode = 0) { @@ -216,12 +216,11 @@ async sub submit_set_grade ($self, $userID, $setID) { my $ce = $c->{ce}; my $db = $c->{db}; - return - unless (can_submit_LMS_score($db, $ce, $userID, $setID) - || !$self->{post_processing_mode} && $ce->{LTIGradeOnSubmit} eq 'homework_always'); - my $user = $db->getUser($userID); - return 0 unless $user; + return unless $user; + + my $score = can_submit_LMS_score($db, $ce, $userID, $setID); + return unless ($score || !$self->{post_processing_mode} && $ce->{LTIGradeOnSubmit} eq 'homework_always'); my $userSet = $db->getMergedSet($userID, $setID); @@ -229,16 +228,8 @@ async sub submit_set_grade ($self, $userID, $setID) { $self->warning('LMS user id is not available for this user.') unless $user->lis_source_did; $self->warning('LMS lineitem is not available for this set.') unless $userSet->lis_source_did; - return await $self->submit_grade( - $user->lis_source_did, - $userSet->lis_source_did, - ( - $userSet->assignment_type =~ /gateway/ - ? grade_gateway($db, $setID, $userID) - : (grade_set($db, $userSet, $userID))[ 0, 1 ] - ), - $self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always' - ); + return await $self->submit_grade($user->lis_source_did, $userSet->lis_source_did, $score->{totalRight}, + $score->{total}, $self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always'); } # Submits scoreGiven and scoreMaximum to the lms with $sourcedid as the identifier. diff --git a/lib/WeBWorK/Utils/Sets.pm b/lib/WeBWorK/Utils/Sets.pm index b71f17379b..dfc7da414c 100644 --- a/lib/WeBWorK/Utils/Sets.pm +++ b/lib/WeBWorK/Utils/Sets.pm @@ -28,7 +28,6 @@ our @EXPORT_OK = qw( format_set_name_display grade_set grade_gateway - set_attempted grade_all_sets is_restricted get_test_problem_position @@ -201,31 +200,20 @@ sub earliest_gateway_date ($db, $ce, $userSet) { } sub grade_all_sets ($db, $ce, $studentName) { - my @setIDs = $db->listUserSets($studentName); - my @userSetIDs = map { [ $studentName, $_ ] } @setIDs; - my @userSets = $db->getMergedSets(@userSetIDs); - - my $courseTotal = 0; + my @setIDs = $db->listUserSets($studentName); my $courseTotalRight = 0; + my $courseTotal = 0; - for my $userSet (@userSets) { - next unless (after($userSet->open_date())); - my $totalRight; - my $total; - my $criticalDate; - - if ($userSet->assignment_type() =~ /gateway/) { - ($totalRight, $total) = grade_gateway($db, $userSet->set_id, $studentName); - $criticalDate = earliest_gateway_date($db, $ce, $userSet) unless ($ce->{LTISendScoresAfterDate} eq 'never'); - } else { - ($totalRight, $total) = grade_set($db, $userSet, $studentName); - $criticalDate = get_LTISendScoresAfterDate($userSet, $ce) unless ($ce->{LTISendScoresAfterDate} eq 'never'); - } + for my $setID (@setIDs) { + my $score = can_submit_LMS_score($db, $ce, $studentName, $setID); + my $totalRight = $score->{totalRight}; + my $total = $score->{total}; + my $criticalDate = $score->{criticalDate} // ''; if ($ce->{LTISendScoresAfterDate} eq 'never' || $criticalDate && before($criticalDate)) { next if ($ce->{LTISendGradesEarlyThreshold} eq 'attempted' - && !set_attempted($db, $studentName, $userSet->set_id)); + && !set_attempted($db, $studentName, $setID)); next if ($ce->{LTISendGradesEarlyThreshold} ne 'attempted' && $total > 0 @@ -260,9 +248,21 @@ sub get_LTISendScoresAfterDate ($set, $ce) { } # Checks if the set is past the LTISendScoresAfterDate or has met the LTISendGradesEarlyThreshold +# Returns a reference to hash with keys totalRight, total, score, criticalDate if the set has met +# either condition and undef if not. sub can_submit_LMS_score ($db, $ce, $userID, $setID) { my $userSet = $db->getMergedSet($userID, $setID); + my $totalRight; + my $total; + if ($userSet->assignment_type() =~ /gateway/) { + ($totalRight, $total) = grade_gateway($db, $setID, $userID); + } else { + ($totalRight, $total) = (grade_set($db, $userSet, $userID))[ 0, 1 ]; + } + my $score = $total > 1 ? $totalRight / $total : 1; + my $return = { totalRight => $totalRight, total => $total, score => $score }; + if ($ce->{LTISendScoresAfterDate} ne 'never') { my $critical_date; if ($userSet->assignment_type() =~ /gateway/) { @@ -270,18 +270,13 @@ sub can_submit_LMS_score ($db, $ce, $userID, $setID) { } else { $critical_date = get_LTISendScoresAfterDate($userSet, $ce); } - return 1 if after($critical_date); + $return->{critical_date} = $critical_date; + return $return if after($critical_date); } - return set_attempted($db, $userID, $setID) if ($ce->{LTISendGradesEarlyThreshold} eq 'attempted'); - - my $score; - if ($userSet->assignment_type() =~ /gateway/) { - $score = grade_gateway($db, $setID, $userID); - } else { - $score = grade_set($db, $userSet, $userID); - } - return ($score >= $ce->{LTISendGradesEarlyThreshold}); + return $return + if ($ce->{LTISendGradesEarlyThreshold} eq 'attempted' && set_attempted($db, $userID, $setID) + || $score >= $ce->{LTISendGradesEarlyThreshold}); } sub is_restricted ($db, $set, $studentName) {