Skip to content

Commit

Permalink
minimize calls to grade_set
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Jordan committed Nov 16, 2024
1 parent 22457bc commit 60ffcf8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 59 deletions.
20 changes: 6 additions & 14 deletions lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
Expand Down
21 changes: 6 additions & 15 deletions lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,20 @@ 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);

$self->warning("Submitting grade for user $userID and set $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.
Expand Down
55 changes: 25 additions & 30 deletions lib/WeBWorK/Utils/Sets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -260,28 +248,35 @@ 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/) {
$critical_date = earliest_gateway_date($db, $ce, $userSet);
} 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) {
Expand Down

0 comments on commit 60ffcf8

Please sign in to comment.