From 782dd66c50d5c33cc38f5ae69d7ad4e5b47a7ed8 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. --- lib/PGcore.pm | 23 +++++++++++++++++++---- lib/WeBWorK/PG.pm | 1 + macros/PG.pl | 13 ++++++++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/PGcore.pm b/lib/PGcore.pm index f711cc3686..5e245e0a45 100755 --- a/lib/PGcore.pm +++ b/lib/PGcore.pm @@ -73,10 +73,13 @@ 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} // {}, + + answer_eval_count => 0, + answer_blank_count => 0, unlabeled_answer_blank_count => 0, unlabeled_answer_eval_count => 0, KEPT_EXTRA_ANSWERS => [], @@ -549,6 +552,18 @@ sub store_persistent_data { # will store strings only (so far) $label; } +sub update_persistent_data { # will store strings only (so far) + my $self = shift; + my $label = shift; + $self->{PERSISTENCE_HASH}->{$label} = join("", @_); +} + +sub get_persistent_data { + my $self = shift; + my $label = shift; + 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..1f5c55534d 100644 --- a/lib/WeBWorK/PG.pm +++ b/lib/WeBWorK/PG.pm @@ -242,6 +242,7 @@ 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} // {}, inputs_ref => $options->{inputs_ref}, diff --git a/macros/PG.pl b/macros/PG.pl index 7fe996013b..3e05c07338 100644 --- a/macros/PG.pl +++ b/macros/PG.pl @@ -307,7 +307,15 @@ sub ANS_NUM_TO_NAME { } sub store_persistent_data { - $PG->store_persistent_data(@_); #needs testing + $PG->store_persistent_data(@_); +} + +sub update_persistent_data { + $PG->update_persistent_data(@_); +} + +sub get_persistent_data { + return ($PG->get_persistent_data(shift)); } sub RECORD_FORM_LABEL { # this stores form data (such as sticky answers), but does nothing more @@ -600,8 +608,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;