Skip to content

Commit

Permalink
Persistent problem data - directly into a new database field
Browse files Browse the repository at this point in the history
  • Loading branch information
taniwallach committed Apr 13, 2023
1 parent 607a817 commit 61bf8fe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/WeBWorK/ContentGenerator/GatewayQuiz.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ deal with versioning sets
=cut

use Mojo::Promise;
use Mojo::JSON qw(encode_json decode_json);

use WeBWorK::PG::ImageGenerator;
# Use the ContentGenerator formatDateTime, not the version in Utils.
Expand Down Expand Up @@ -1455,6 +1456,18 @@ async sub getProblemHTML ($c, $effectiveUser, $set, $formFields, $mergedProblem)
# So rewarn them and let the global warning handler take care of it.
warn $pg->{warnings} if $pg->{warnings};

# Store persistent problem data from the PERSISTENCE_HASH
my $pureProblem =
$c->db->getProblemVersion($mergedProblem->user_id, $setID, $setVersionNumber, $mergedProblem->problem_id);
my $json_data = decode_json($pureProblem->{problem_data} || '{}');
for my $key (keys %{ $pg->{PERSISTENCE_HASH} }) {
$json_data->{$key} = $pg->{PERSISTENCE_HASH}{$key};
}
$pureProblem->problem_data(encode_json($json_data));
if (!$c->db->putProblemVersion($pureProblem)) {
warn "failed to save problem data";
}

if ($pg->{flags}{error_flag}) {
push @{ $c->{errors} },
{
Expand Down
14 changes: 14 additions & 0 deletions lib/WeBWorK/ContentGenerator/Problem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use WeBWorK::Utils::LanguageAndDirection qw(get_problem_lang_and_dir);
use WeBWorK::AchievementEvaluator;
use WeBWorK::HTML::AttemptsTable;

use Mojo::JSON qw(encode_json decode_json);

# GET/POST Parameters for this module
#
# Standard params:
Expand Down Expand Up @@ -625,6 +627,18 @@ async sub pre_header_initialize ($c) {

debug('end pg processing');

# Store persistent problem data from the PERSISTENCE_HASH
my $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id);

my $json_data = decode_json($pureProblem->{problem_data} || '{}');
for my $key (keys %{ $pg->{PERSISTENCE_HASH} }) {
$json_data->{$key} = $pg->{PERSISTENCE_HASH}{$key};
}
$pureProblem->problem_data(encode_json($json_data));
if (!$db->putUserProblem($pureProblem)) {
warn "failed to save problem data";
}

$pg->{body_text} .= $c->hidden_field(
num_attempts => $problem->num_correct + $problem->num_incorrect + ($c->{submitAnswers} ? 1 : 0),
id => 'num_attempts'
Expand Down
2 changes: 2 additions & 0 deletions lib/WeBWorK/DB/Record/UserProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ BEGIN {
sub_status => { type => "FLOAT" },
# a field for flags which need to be set
flags => { type => "TEXT" },
# additional stored data for this problem, internally uses JSON:
problem_data => { type => "MEDIUMTEXT" },
);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/WeBWorK/Utils/Rendering.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ sub constructPGOptions ($ce, $user, $set, $problem, $psvn, $formFields, $transla
$options{num_of_correct_ans} = $problem->num_correct;
$options{num_of_incorrect_ans} = $problem->num_incorrect;

# Persistent problem data
$options{PERSISTENCE_HASH} = decode_json($problem->problem_data || '{}');

# Language
$options{language} = $ce->{language};
$options{language_subroutine} = WeBWorK::Localize::getLoc($options{language});
Expand Down Expand Up @@ -260,6 +263,10 @@ sub renderPG ($c, $effectiveUser, $set, $problem, $psvn, $formFields, $translati
map { $_ => $pg->{pgcore}{PG_alias}{resource_list}{$_}{uri}{content} }
keys %{ $pg->{pgcore}{PG_alias}{resource_list} }
};
$ret->{PERSISTENCE_HASH} = {
map { $_ => $pg->{pgcore}{PERSISTENCE_HASH}{$_} }
keys %{ $pg->{pgcore}{PERSISTENCE_HASH} }
};
}

# Save the problem source. This is used by Caliper::Entity. Why?
Expand Down

0 comments on commit 61bf8fe

Please sign in to comment.