Skip to content

Commit

Permalink
Modify addUserMultipleProblems of lib/WeBWorK/DB.pm to use
Browse files Browse the repository at this point in the history
   eval { $self->{problem_user}->insert_records([@problemList]); };
so that a single DB call makes all the inserts.
Test that no records for this user and set exist before making
that call.
  • Loading branch information
taniwallach committed Dec 1, 2022
1 parent 2c1c728 commit 48a068b
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions lib/WeBWorK/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,10 @@ sub addUserMultipleProblems {
my ($self, @problemList) = @_;

my $firstUserProblem = $problemList[0];
my $user_id = $firstUserProblem->user_id;
my $set_id = $firstUserProblem->set_id;

my ($nv_set_id, $versionNum) = grok_vsetID($firstUserProblem->set_id);
my ($nv_set_id, $versionNum) = grok_vsetID($set_id);
croak "addUserMultipleProblems does not support versioned sets"
if defined($versionNum);

Expand All @@ -2068,32 +2070,19 @@ sub addUserMultipleProblems {
my @tmp = ($firstUserProblem);
$self->checkArgs(\@tmp, qw/VREC:problem_user/);

# Just needed once
croak 'addMultipleUserProblems: user set ', $firstUserProblem->set_id, ' for user ', $firstUserProblem->user_id,
' not found'
unless $self->{set_user}->exists($firstUserProblem->user_id, $firstUserProblem->set_id);
croak "addMultipleUserProblems: user set $set_id for user $user_id not found"
unless $self->{set_user}->exists($user_id, $set_id);

my @sawRecordExists;
my @otherErrors;
# Test whether there are already problem records for this user in this set, as in that case
# inserting multiple records at once would trigger an error.
my $where = [ user_id_eq_set_id_eq => $user_id, $set_id ];

for my $userProblem (@problemList) {
eval { $self->{problem_user}->add($userProblem); };
if (my $ex = caught WeBWorK::DB::Ex::RecordExists) {
push(@sawRecordExists, $userProblem->problem_id);
} elsif ($@) {
push(@otherErrors, "for problem_id $userProblem->problem_id) saw error: $@\n");
}
}
if (@sawRecordExists || @otherErrors) {
croak join(
'',
@sawRecordExists
? join('',
'addUserMultipleProblems: the following user problems existed before ',
join(', ', @sawRecordExists), "\n")
: '',
@otherErrors
);
croak "addMultipleUserProblems cannot be run as set $set_id already contains some problems for user $user_id"
if $self->{problem_user}->count_where($where);

eval { $self->{problem_user}->insert_records([@problemList]); };
if ($@) {
croak "addUserMultipleProblems: $@";
}
}

Expand Down

0 comments on commit 48a068b

Please sign in to comment.