Skip to content

Commit

Permalink
simplify how can_submit_LMS_score is used, and use it earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Jordan authored and Alex Jordan committed Nov 11, 2024
1 parent d10ffa3 commit 2b5bc35
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
21 changes: 6 additions & 15 deletions lib/WeBWorK/Authen/LTIAdvanced/SubmitGrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ sub update_sourcedid ($self, $userID) {
}

# Checks if the set is past the LTISendScoresAfterDate or has met the LTISendGradesEarlyThreshold
sub can_submit_LMS_score ($self, $db, $ce, $userID, $setID) {
sub can_submit_LMS_score ($invocant, $db, $ce, $userID, $setID) {
my $userSet = $db->getMergedSet($userID, $setID);

if ($ce->{LTISendScoresAfterDate} ne 'never') {
Expand Down Expand Up @@ -164,6 +164,8 @@ async sub submit_set_grade ($self, $userID, $setID) {
my $ce = $c->{ce};
my $db = $c->{db};

return unless $self->can_submit_LMS_score($db, $ce, $userID, $setID);

my $user = $db->getUser($userID);
return 0 unless $user;

Expand All @@ -175,26 +177,15 @@ async sub submit_set_grade ($self, $userID, $setID) {
if !$userSet->lis_source_did && ($ce->{debug_lti_grade_passback} || $self->{post_processing_mode});

my $score;
my $criticalDate;

if ($userSet->assignment_type =~ /gateway/) {
$score = scalar(grade_gateway($db, $setID, $userID));
$criticalDate = earliest_gateway_date($db, $userSet, $ce->{LTISendScoresAfterDate})
unless ($ce->{LTISendScoresAfterDate} eq 'never');
$score = scalar(grade_gateway($db, $setID, $userID));
} else {
$score = grade_set($db, $userSet, $userID);
$criticalDate = get_set_date($userSet, $ce->{LTISendScoresAfterDate})
unless ($ce->{LTISendScoresAfterDate} eq 'never');
}

if ($ce->{LTISendScoresAfterDate} eq 'never' || $criticalDate && before($criticalDate)) {
return if ($ce->{LTISendGradesEarlyThreshold} eq 'attempted' && !set_attempted($db, $userID, $setID));
return if ($ce->{LTISendGradesEarlyThreshold} ne 'attempted' && $score < $ce->{LTISendGradesEarlyThreshold});
$score = grade_set($db, $userSet, $userID);
}

return await $self->submit_grade($userSet->lis_source_did, $score,
($ce->{LTISendScoresAfterDate} eq 'never' || before($criticalDate))
&& ($self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always'));
$self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always');
}

# Submits a score of $score to the lms with $sourcedid as the identifier.
Expand Down
20 changes: 4 additions & 16 deletions lib/WeBWorK/Authen/LTIAdvantage/SubmitGrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ async sub get_access_token ($self) {
}

# Checks if the set is past the LTISendScoresAfterDate or has met the LTISendGradesEarlyThreshold
sub can_submit_LMS_score ($self, $db, $ce, $userID, $setID) {
sub can_submit_LMS_score ($invocant, $db, $ce, $userID, $setID) {
my $userSet = $db->getMergedSet($userID, $setID);

if ($ce->{LTISendScoresAfterDate} ne 'never') {
Expand Down Expand Up @@ -243,6 +243,8 @@ async sub submit_set_grade ($self, $userID, $setID) {
my $ce = $c->{ce};
my $db = $c->{db};

return unless $self->can_submit_LMS_score($db, $ce, $userID, $setID);

my $user = $db->getUser($userID);
return 0 unless $user;

Expand All @@ -254,29 +256,15 @@ async sub submit_set_grade ($self, $userID, $setID) {

my $totalRight;
my $total;
my $criticalDate;

if ($userSet->assignment_type =~ /gateway/) {
($totalRight, $total) = grade_gateway($db, $setID, $userID);
$criticalDate = earliest_gateway_date($db, $userSet, $userID, $ce->{LTISendScoresAfterDate})
unless ($ce->{LTISendScoresAfterDate} eq 'never');
} else {
($totalRight, $total) = (grade_set($db, $userSet, $userID))[ 0, 1 ];
$criticalDate = get_set_date($userSet, $ce->{LTISendScoresAfterDate})
unless ($ce->{LTISendScoresAfterDate} eq 'never');
}

if ($ce->{LTISendScoresAfterDate} eq 'never' || $criticalDate && before($criticalDate)) {
return if ($ce->{LTISendGradesEarlyThreshold} eq 'attempted' && !set_attempted($db, $userID, $setID));
return
if ($ce->{LTISendGradesEarlyThreshold} ne 'attempted'
&& $total
&& $totalRight / $total < $ce->{LTISendGradesEarlyThreshold});
}

return await $self->submit_grade($user->lis_source_did, $userSet->lis_source_did, $totalRight, $total,
($ce->{LTISendScoresAfterDate} eq 'never' || before($criticalDate))
&& ($self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always'));
$self->{post_processing_mode} || $ce->{LTIGradeOnSubmit} ne 'homework_always');
}

# Submits scoreGiven and scoreMaximum to the lms with $sourcedid as the identifier.
Expand Down
13 changes: 11 additions & 2 deletions lib/WeBWorK/ContentGenerator/GatewayQuiz.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,18 @@ async sub pre_header_initialize ($c) {
}

# Try to update the student score on the LMS if that option is enabled.
if ($c->{submitAnswers} && $will{recordAnswers} && $ce->{LTIGradeMode} && $ce->{LTIGradeOnSubmit}) {
if (
$c->{submitAnswers}
&& $will{recordAnswers}
&& $ce->{LTIGradeMode}
&& ($ce->{LTIGradeOnSubmit} eq 'homework_always' && $ce->{LTIGradeMode} eq 'homework'
|| $ce->{LTIGradeOnSubmit}
&& $ce->{LTI}{ $ce->{LTIVersion} }{grader}->can_submit_LMS_score($db, $ce, $effectiveUserID, $setID)
)
)
{
my $grader = $ce->{LTI}{ $ce->{LTIVersion} }{grader}->new($c);
if ($ce->{LTIGradeMode} eq 'course' && $grader->can_submit_LMS_score($db, $ce, $effectiveUserID, $setID)) {
if ($ce->{LTIGradeMode} eq 'course') {
$LTIGradeResult = await $grader->submit_course_grade($effectiveUserID);
} elsif ($ce->{LTIGradeMode} eq 'homework') {
$LTIGradeResult = await $grader->submit_set_grade($effectiveUserID, $setID);
Expand Down
10 changes: 6 additions & 4 deletions lib/WeBWorK/Utils/ProblemProcessing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,14 @@ async sub process_and_log_answer ($c) {
if ($ce->{LTIGradeMode}) {
my $LMSname = $ce->{LTI}{ $ce->{LTIVersion} }{LMS_name};
my $LTIGradeResult = -1;
if ($ce->{LTIGradeOnSubmit}) {
if ($ce->{LTIGradeOnSubmit} eq 'homework_always' && $ce->{LTIGradeMode} eq 'homework'
|| $ce->{LTIGradeOnSubmit}
&& $ce->{LTI}{ $ce->{LTIVersion} }{grader}
->can_submit_LMS_score($db, $ce, $problem->user_id, $problem->set_id))
{
$LTIGradeResult = 0;
my $grader = $ce->{LTI}{ $ce->{LTIVersion} }{grader}->new($c);
if ($ce->{LTIGradeMode} eq 'course'
&& $grader->can_submit_LMS_score($db, $ce, $problem->user_id, $problem->set_id))
{
if ($ce->{LTIGradeMode} eq 'course') {
$LTIGradeResult = await $grader->submit_course_grade($problem->user_id);
} elsif ($ce->{LTIGradeMode} eq 'homework') {
$LTIGradeResult = await $grader->submit_set_grade($problem->user_id, $problem->set_id);
Expand Down

0 comments on commit 2b5bc35

Please sign in to comment.