Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised handling of PERSISTENCE_HASH data #809

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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