From 4b947adb56b6939fc2dc8a3f7e14c1dedc026041 Mon Sep 17 00:00:00 2001 From: Nathan Wallach Date: Thu, 13 Apr 2023 18:42:22 +0300 Subject: [PATCH] Persistent problem data - modify to retrieve from $envir 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: https://github.com/openwebwork/pg/pull/809 --- lib/PGcore.pm | 30 ++++++++++++++++++++++-------- lib/WeBWorK/PG.pm | 5 +++++ macros/PG.pl | 16 +++++++++++++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/PGcore.pm b/lib/PGcore.pm index f711cc3686..88cfa015be 100755 --- a/lib/PGcore.pm +++ b/lib/PGcore.pm @@ -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 => [], @@ -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} }) { diff --git a/lib/WeBWorK/PG.pm b/lib/WeBWorK/PG.pm index 662925b53e..2d2e8c519f 100644 --- a/lib/WeBWorK/PG.pm +++ b/lib/WeBWorK/PG.pm @@ -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}, diff --git a/macros/PG.pl b/macros/PG.pl index 7fe996013b..a9e530eeb6 100644 --- a/macros/PG.pl +++ b/macros/PG.pl @@ -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 @@ -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;