Skip to content

Commit c70ae20

Browse files
committed
Use a more proper fix.
Remove the improper access of the direct hash keys in the database records in the fieldHTML method. Instead check that the record object `can` access the fields.
1 parent 5d7b7fd commit c70ae20

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/WeBWorK/ContentGenerator/Instructor/ProblemSetDetail.pm

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -852,20 +852,17 @@ sub fieldHTML ($c, $userID, $setID, $problemID, $globalRecord, $userRecord, $fie
852852
my @uVals;
853853
my @bVals;
854854
for my $f (split(/:/, $field)) {
855-
# Hmm. This directly references the data in the record rather than calling the access method, thereby
856-
# avoiding errors if the access method is undefined. That seems a bit suspect, but it's used below so we'll
857-
# leave it here.
858-
push(@gVals, $globalRecord->{$f});
859-
push(@uVals, $userRecord ? $userRecord->{$f} : '');
855+
push(@gVals, $globalRecord->can($f) ? $globalRecord->$f : undef);
856+
push(@uVals, $userRecord && $userRecord->can($f) ? $userRecord->$f : undef);
860857
push(@bVals, '');
861858
}
862859
# I don't like this, but combining multiple values is a bit messy
863860
$globalValue = (grep {defined} @gVals) ? join(':', (map { defined ? $_ : '' } @gVals)) : undef;
864861
$userValue = (grep {defined} @uVals) ? join(':', (map { defined ? $_ : '' } @uVals)) : undef;
865862
$blankfield = join(':', @bVals);
866863
} else {
867-
$globalValue = $globalRecord->{$field};
868-
$userValue = $userRecord ? $userRecord->{$field} : '';
864+
$globalValue = $globalRecord->can($field) ? $globalRecord->$field : undef;
865+
$userValue = $userRecord && $userRecord->can($field) ? $userRecord->$field : undef;
869866
}
870867

871868
# Use defined instead of value in order to allow 0 to printed, e.g. for the 'value' field.
@@ -957,12 +954,16 @@ sub fieldHTML ($c, $userID, $setID, $problemID, $globalRecord, $userRecord, $fie
957954
my @part_values;
958955
for (@fields) {
959956
push(@part_values,
960-
$forUsers && $userRecord && $userRecord->$_ ne '' ? $userRecord->$_ : $globalRecord->$_);
957+
($forUsers && $userRecord && $userRecord->can($_) && $userRecord->$_ ne '')
958+
? $userRecord->$_
959+
: $globalRecord->$_);
961960
}
962961
$value = join(':', @part_values);
963962
} elsif (!$value) {
964963
$value =
965-
$forUsers && $userRecord && $userRecord->$field ne '' ? $userRecord->$field : $globalRecord->$field;
964+
($forUsers && $userRecord && $userRecord->can($field) && $userRecord->$field ne '')
965+
? $userRecord->$field
966+
: $globalRecord->$field;
966967
}
967968

968969
$inputType = $c->select_field(

0 commit comments

Comments
 (0)