Skip to content

Commit

Permalink
Persistent problem data - modify to retrieve from $envir
Browse files Browse the repository at this point in the history
and to allow update and get operations. The webwork2 side
will now store this in a special field.
Includes changes and suggestions from Dr. Glenn Rice.
See: openwebwork#809
  • Loading branch information
taniwallach committed Apr 19, 2023
1 parent 3803165 commit 0a4e3f8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
30 changes: 22 additions & 8 deletions lib/PGcore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ sub new {
POST_HEADER_ARRAY => [],
# PG_ANSWERS => [], # holds answers with labels # deprecated
# PG_UNLABELED_ANSWERS => [], # holds unlabeled ans. #deprecated -replaced by PG_ANSWERS_HASH
PG_ANSWERS_HASH => {}, # holds label=>answer pairs
PERSISTENCE_HASH => {}, # holds other data, besides answers, which persists during a session and beyond
answer_eval_count => 0,
answer_blank_count => 0,
PG_ANSWERS_HASH => {}, # holds label=>answer pairs

# Holds other data, besides answers, which persists during a session and beyond.
PERSISTENCE_HASH => $envir->{PERSISTENCE_HASH} // {}, # Main data, received from DB
PERSISTENCE_HASH_UPDATED => {}, # Keys whose updated values should be saved by the DB

answer_eval_count => 0,
answer_blank_count => 0,
unlabeled_answer_blank_count => 0,
unlabeled_answer_eval_count => 0,
KEPT_EXTRA_ANSWERS => [],
Expand Down Expand Up @@ -538,17 +542,27 @@ sub record_unlabeled_array_name {
}

sub store_persistent_data { # will store strings only (so far)
my $self = shift;
my $label = shift;
my @content = @_;
my ($self, $label, @values) = @_;
if (defined($self->{PERSISTENCE_HASH}->{$label})) {
warn "can' overwrite $label in persistent data";
} else {
$self->{PERSISTENCE_HASH}->{$label} = join("", @content); #need base64 encoding?
$self->{PERSISTENCE_HASH_UPDATED}->{$label} = 1;
$self->{PERSISTENCE_HASH}->{$label} = join("", @values);
}
$label;
}

sub update_persistent_data { # will store strings only (so far)
my ($self, $label, @values) = @_;
$self->{PERSISTENCE_HASH_UPDATED}->{$label} = 1;
$self->{PERSISTENCE_HASH}->{$label} = join("", @values);
}

sub get_persistent_data {
my ($self, $label) = @_;
return $self->{PERSISTENCE_HASH}->{$label};
}

sub check_answer_hash {
my $self = shift;
foreach my $key (keys %{ $self->{PG_ANSWERS_HASH} }) {
Expand Down
5 changes: 5 additions & 0 deletions lib/WeBWorK/PG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ sub defineProblemEnvironment ($pg_envir, $options = {}, $image_generator = undef
pastDue => $options->{pastDue} // 0,
answersAvailable => $options->{answersAvailable} // 0,
isInstructor => $options->{isInstructor} // 0,
PERSISTENCE_HASH => $options->{PERSISTENCE_HASH} // {},

# The next has marks what data was updated and needs to be saved
# by the front end.
PERSISTENCE_HASH_UPDATED => {},

inputs_ref => $options->{inputs_ref},

Expand Down
16 changes: 13 additions & 3 deletions macros/PG.pl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,18 @@ sub ANS_NUM_TO_NAME {
}

sub store_persistent_data {
$PG->store_persistent_data(@_); #needs testing
my ($label, @values) = @_;
$PG->store_persistent_data($label, @values);
}

sub update_persistent_data {
my ($label, @values) = @_;
$PG->update_persistent_data($label, @values);
}

sub get_persistent_data {
my ($label) = @_;
return $PG->get_persistent_data($label);
}

sub RECORD_FORM_LABEL { # this stores form data (such as sticky answers), but does nothing more
Expand Down Expand Up @@ -600,8 +611,7 @@ sub ENDDOCUMENT {
warn "$key is ", join("|", %{ $PG->{PG_ANSWERS_HASH}->{$key} });
}
}
push @KEPT_EXTRA_ANSWERS, keys %{ $PG->{PERSISTENCE_HASH} };
#Hackish way to store other persistence data

$PG->{flags}->{KEPT_EXTRA_ANSWERS} = \@KEPT_EXTRA_ANSWERS;
$PG->{flags}->{ANSWER_ENTRY_ORDER} = \@PG_ANSWER_ENTRY_ORDER;

Expand Down

0 comments on commit 0a4e3f8

Please sign in to comment.