diff --git a/htdocs/js/AchievementItems/achievementitems.js b/htdocs/js/AchievementItems/achievementitems.js index 567dea616c..267d27f495 100644 --- a/htdocs/js/AchievementItems/achievementitems.js +++ b/htdocs/js/AchievementItems/achievementitems.js @@ -1,17 +1,33 @@ (() => { for (const setSelect of document.querySelectorAll('select[data-problems]')) { setSelect.addEventListener('change', () => { - const max = parseInt(Array.from(setSelect.querySelectorAll('option')) - .find((option) => option.value === setSelect.value)?.dataset.max ?? '0'); + const problemIds = JSON.parse( + Array.from(setSelect.querySelectorAll('option')).find((option) => option.value === setSelect.value) + ?.dataset.problemIds ?? '[]' + ); - document.querySelectorAll(`#${setSelect.dataset.problems} option`).forEach((option, index) => { - option.style.display = index < max ? '' : 'none'; - }); + const problemSelect = document.getElementById(setSelect.dataset.problems); + if (problemSelect) { + for (const option of problemSelect.querySelectorAll('option')) option.remove(); + for (const id of problemIds) { + const option = document.createElement('option'); + option.value = id; + option.text = id; + problemSelect.add(option); + } + } // This is only used by the "Box of Transmogrification". - document.querySelectorAll(`#${setSelect.dataset.problems2} option`).forEach((option, index) => { - option.style.display = index < max ? '' : 'none'; - }); + const problemSelect2 = document.getElementById(setSelect.dataset.problems2); + if (problemSelect2) { + for (const option of problemSelect2.querySelectorAll('option')) option.remove(); + for (const id of problemIds) { + const option = document.createElement('option'); + option.value = id; + option.text = id; + problemSelect2.add(option); + } + } }); } })(); diff --git a/lib/WeBWorK/AchievementItems/AddNewTestGW.pm b/lib/WeBWorK/AchievementItems/AddNewTestGW.pm index b101982a4a..2352311eaf 100644 --- a/lib/WeBWorK/AchievementItems/AddNewTestGW.pm +++ b/lib/WeBWorK/AchievementItems/AddNewTestGW.pm @@ -31,21 +31,21 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my $db = $c->db; - my $effectiveUserName = $c->param('effectiveUser') // $c->param('user'); - my @unfilteredsets = $db->getMergedSets(map { [ $effectiveUserName, $_ ] } $db->listUserSets($effectiveUserName)); my @openGateways; # Find the template sets of open gateway quizzes. - for my $set (@unfilteredsets) { + for my $set (@$sets) { push(@openGateways, [ format_set_name_display($set->set_id) => $set->set_id ]) if $set->assignment_type =~ /gateway/ && $set->set_id !~ /,v\d+$/ && between($set->open_date, $set->due_date); } + return unless @openGateways; + return $c->c( $c->tag('p', $c->maketext('Add a new version for which test?')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/DoubleProb.pm b/lib/WeBWorK/AchievementItems/DoubleProb.pm index fe7184460e..6ba044e239 100644 --- a/lib/WeBWorK/AchievementItems/DoubleProb.pm +++ b/lib/WeBWorK/AchievementItems/DoubleProb.pm @@ -18,6 +18,8 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to make a problem worth double. +use Mojo::JSON qw(encode_json); + use WeBWorK::Utils qw(between x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { @@ -28,31 +30,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # Construct a dropdown with open sets and another with problems. - # Javascript ensures the appropriate number of problems are shown for the selected set. + # Javascript ensures the appropriate problems are shown for the selected set. - my @openSets; - my $maxProblems = 0; + my (@openSets, @initialProblemIDs); for my $i (0 .. $#$sets) { - if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default') { + if (between($sets->[$i]->open_date, $sets->[$i]->due_date) + && $sets->[$i]->assignment_type eq 'default' + && @{ $setProblemIds->{ $sets->[$i]->set_id } }) + { push( @openSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id, - data => { max => $setProblemCount->[$i] } + data => { problem_ids => encode_json($setProblemIds->{ $sets->[$i]->set_id }) } ] ); - $maxProblems = $setProblemCount->[$i] if $setProblemCount->[$i] > $maxProblems; + @initialProblemIDs = @{ $setProblemIds->{ $sets->[$i]->set_id } } unless @initialProblemIDs; } } - my @problemIDs; - - for my $i (1 .. $maxProblems) { - push(@problemIDs, [ $i => $i, $i > $openSets[0][3]{max} ? (style => 'display:none') : () ]); - } + return unless @openSets; return $c->c( $c->tag( @@ -71,7 +71,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { $c, id => 'dbp_problem_id', label_text => $c->maketext('Problem Number'), - values => \@problemIDs, + values => \@initialProblemIDs, menu_container_attr => { class => 'col-3' } ) )->join(''); diff --git a/lib/WeBWorK/AchievementItems/DoubleSet.pm b/lib/WeBWorK/AchievementItems/DoubleSet.pm index c67c1e7055..c1d7a297f6 100644 --- a/lib/WeBWorK/AchievementItems/DoubleSet.pm +++ b/lib/WeBWorK/AchievementItems/DoubleSet.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -36,6 +36,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Choose the set which you would like to be worth twice as much.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/DuplicateProb.pm b/lib/WeBWorK/AchievementItems/DuplicateProb.pm index 0cff78c1de..ecc1e72052 100644 --- a/lib/WeBWorK/AchievementItems/DuplicateProb.pm +++ b/lib/WeBWorK/AchievementItems/DuplicateProb.pm @@ -18,6 +18,8 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to turn one problem into another problem +use Mojo::JSON qw(encode_json); + use WeBWorK::Utils qw(between x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { @@ -28,30 +30,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # Show open sets and allow for a choice of two problems from the set. + # Javascript ensures the appropriate problems are shown for the selected set. - my @openSets; - my $maxProblems = 0; + my (@openSets, @initialProblemIDs); for my $i (0 .. $#$sets) { - if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default') { + if (between($sets->[$i]->open_date, $sets->[$i]->due_date) + && $sets->[$i]->assignment_type eq 'default' + && @{ $setProblemIds->{ $sets->[$i]->set_id } }) + { push( @openSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id, - data => { max => $setProblemCount->[$i] } + data => { problem_ids => encode_json($setProblemIds->{ $sets->[$i]->set_id }) } ] ); - $maxProblems = $setProblemCount->[$i] if $setProblemCount->[$i] > $maxProblems; + @initialProblemIDs = @{ $setProblemIds->{ $sets->[$i]->set_id } } unless @initialProblemIDs; } } - my @problemIDs; - - for my $i (1 .. $maxProblems) { - push(@problemIDs, [ $i => $i, $i > $openSets[0][3]{max} ? (style => 'display:none') : () ]); - } + return unless @openSets; return $c->c( $c->tag( @@ -78,7 +79,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { WeBWorK::AchievementItems::form_popup_menu_row( $c, id => 'tran_problem_id', - values => \@problemIDs, + values => \@initialProblemIDs, label_text => $c->maketext('Copy this Problem'), menu_container_attr => { class => 'col-2 ps-0' }, add_container => 0 @@ -86,7 +87,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { WeBWorK::AchievementItems::form_popup_menu_row( $c, id => 'tran_problem_id2', - values => \@problemIDs, + values => \@initialProblemIDs, label_text => $c->maketext('To this Problem'), menu_container_attr => { class => 'col-2 ps-0' }, add_container => 0 diff --git a/lib/WeBWorK/AchievementItems/ExtendDueDate.pm b/lib/WeBWorK/AchievementItems/ExtendDueDate.pm index f1b521a706..7ce497db1d 100644 --- a/lib/WeBWorK/AchievementItems/ExtendDueDate.pm +++ b/lib/WeBWorK/AchievementItems/ExtendDueDate.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -36,6 +36,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Choose the set whose close date you would like to extend.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/ExtendDueDateGW.pm b/lib/WeBWorK/AchievementItems/ExtendDueDateGW.pm index fb2e04893e..f29d4eaa73 100644 --- a/lib/WeBWorK/AchievementItems/ExtendDueDateGW.pm +++ b/lib/WeBWorK/AchievementItems/ExtendDueDateGW.pm @@ -29,21 +29,21 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my $db = $c->db; - my $effectiveUserName = $c->param('effectiveUser') // $c->param('user'); - my @unfilteredsets = $db->getMergedSets(map { [ $effectiveUserName, $_ ] } $db->listUserSets($effectiveUserName)); my @openGateways; # Find the template sets for open tests. - for my $set (@unfilteredsets) { + for my $set (@$sets) { push(@openGateways, [ format_set_name_display($set->set_id) => $set->set_id ]) if $set->assignment_type =~ /gateway/ && $set->set_id !~ /,v\d+$/ && between($set->open_date, $set->due_date); } + return unless @openGateways; + return $c->c( $c->tag('p', $c->maketext('Extend the close date for which test?')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/FullCreditProb.pm b/lib/WeBWorK/AchievementItems/FullCreditProb.pm index bca88938c7..96cb798b91 100644 --- a/lib/WeBWorK/AchievementItems/FullCreditProb.pm +++ b/lib/WeBWorK/AchievementItems/FullCreditProb.pm @@ -18,6 +18,8 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to give full credit on a single problem +use Mojo::JSON qw(encode_json); + use WeBWorK::Utils qw(between x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { @@ -28,31 +30,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # Construct a dropdown with open sets and another with problems. - # Javascript ensures the appropriate number of problems are shown for the selected set. + # Javascript ensures the appropriate problems are shown for the selected set. - my @openSets; - my $maxProblems = 0; + my (@openSets, @initialProblemIDs); for my $i (0 .. $#$sets) { - if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default') { + if (between($sets->[$i]->open_date, $sets->[$i]->due_date) + && $sets->[$i]->assignment_type eq 'default' + && @{ $setProblemIds->{ $sets->[$i]->set_id } }) + { push( @openSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id, - data => { max => $setProblemCount->[$i] } + data => { problem_ids => encode_json($setProblemIds->{ $sets->[$i]->set_id }) } ] ); - $maxProblems = $setProblemCount->[$i] if $setProblemCount->[$i] > $maxProblems; + @initialProblemIDs = @{ $setProblemIds->{ $sets->[$i]->set_id } } unless @initialProblemIDs; } } - my @problemIDs; - - for my $i (1 .. $maxProblems) { - push(@problemIDs, [ $i => $i, $i > $openSets[0][3]{max} ? (style => 'display:none') : () ]); - } + return unless @openSets; return $c->c( $c->tag( @@ -71,7 +71,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { $c, id => 'fcp_problem_id', label_text => $c->maketext('Problem Number'), - values => \@problemIDs, + values => \@initialProblemIDs, menu_container_attr => { class => 'col-3' } ) )->join(''); diff --git a/lib/WeBWorK/AchievementItems/FullCreditSet.pm b/lib/WeBWorK/AchievementItems/FullCreditSet.pm index 812916adac..4dcdd4fd70 100644 --- a/lib/WeBWorK/AchievementItems/FullCreditSet.pm +++ b/lib/WeBWorK/AchievementItems/FullCreditSet.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -36,6 +36,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Please choose the set for which all problems should be given full credit.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/HalfCreditProb.pm b/lib/WeBWorK/AchievementItems/HalfCreditProb.pm index 859f768a34..17dd547500 100644 --- a/lib/WeBWorK/AchievementItems/HalfCreditProb.pm +++ b/lib/WeBWorK/AchievementItems/HalfCreditProb.pm @@ -18,6 +18,8 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to give half credit on a single problem. +use Mojo::JSON qw(encode_json); + use WeBWorK::Utils qw(between x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { @@ -28,31 +30,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # Construct a dropdown with open sets and another with problems. - # Javascript ensures the appropriate number of problems are shown for the selected set. + # Javascript ensures the appropriate problems are shown for the selected set. - my @openSets; - my $maxProblems = 0; + my (@openSets, @initialProblemIDs); for my $i (0 .. $#$sets) { - if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default') { + if (between($sets->[$i]->open_date, $sets->[$i]->due_date) + && $sets->[$i]->assignment_type eq 'default' + && @{ $setProblemIds->{ $sets->[$i]->set_id } }) + { push( @openSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id, - data => { max => $setProblemCount->[$i] } + data => { problem_ids => encode_json($setProblemIds->{ $sets->[$i]->set_id }) } ] ); - $maxProblems = $setProblemCount->[$i] if $setProblemCount->[$i] > $maxProblems; + @initialProblemIDs = @{ $setProblemIds->{ $sets->[$i]->set_id } } unless @initialProblemIDs; } } - my @problemIDs; - - for my $i (1 .. $maxProblems) { - push(@problemIDs, [ $i => $i, $i > $openSets[0][3]{max} ? (style => 'display:none') : () ]); - } + return unless @openSets; return $c->c( $c->tag( @@ -70,7 +70,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { WeBWorK::AchievementItems::form_popup_menu_row( $c, id => 'hcp_problem_id', - values => \@problemIDs, + values => \@initialProblemIDs, label_text => $c->maketext('Problem Number'), menu_container_attr => { class => 'col-3' } ) diff --git a/lib/WeBWorK/AchievementItems/HalfCreditSet.pm b/lib/WeBWorK/AchievementItems/HalfCreditSet.pm index d3a9ce07b0..3ca427a96c 100644 --- a/lib/WeBWorK/AchievementItems/HalfCreditSet.pm +++ b/lib/WeBWorK/AchievementItems/HalfCreditSet.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -36,6 +36,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Please choose the set for which all problems should have half credit added.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/ReducedCred.pm b/lib/WeBWorK/AchievementItems/ReducedCred.pm index c6cec053ac..8455584319 100644 --- a/lib/WeBWorK/AchievementItems/ReducedCred.pm +++ b/lib/WeBWorK/AchievementItems/ReducedCred.pm @@ -32,7 +32,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -40,6 +40,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Choose the set which you would like to enable partial credit for.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/ResetIncorrectAttempts.pm b/lib/WeBWorK/AchievementItems/ResetIncorrectAttempts.pm index 0a6cce6306..a0d180a679 100644 --- a/lib/WeBWorK/AchievementItems/ResetIncorrectAttempts.pm +++ b/lib/WeBWorK/AchievementItems/ResetIncorrectAttempts.pm @@ -18,6 +18,8 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to reset number of incorrect attempts. +use Mojo::JSON qw(encode_json); + use WeBWorK::Utils qw(between x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { @@ -28,31 +30,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # Construct a dropdown with open sets and another with problems. - # Javascript ensures the appropriate number of problems are shown for the selected set. + # Javascript ensures the appropriate problems are shown for the selected set. - my @openSets; - my $maxProblems = 0; + my (@openSets, @initialProblemIDs); for my $i (0 .. $#$sets) { - if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default') { + if (between($sets->[$i]->open_date, $sets->[$i]->due_date) + && $sets->[$i]->assignment_type eq 'default' + && @{ $setProblemIds->{ $sets->[$i]->set_id } }) + { push( @openSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id, - data => { max => $setProblemCount->[$i] } + data => { problem_ids => encode_json($setProblemIds->{ $sets->[$i]->set_id }) } ] ); - $maxProblems = $setProblemCount->[$i] if $setProblemCount->[$i] > $maxProblems; + @initialProblemIDs = @{ $setProblemIds->{ $sets->[$i]->set_id } } unless @initialProblemIDs; } } - my @problemIDs; - - for my $i (1 .. $maxProblems) { - push(@problemIDs, [ $i => $i, $i > $openSets[0][3]{max} ? (style => 'display:none') : () ]); - } + return unless @openSets; return $c->c( $c->tag( @@ -73,7 +73,7 @@ sub print_form ($self, $sets, $setProblemCount, $c) { $c, id => 'ria_problem_id', label_text => $c->maketext('Problem Number'), - values => \@problemIDs, + values => \@initialProblemIDs, menu_container_attr => { class => 'col-3' } ) )->join(''); diff --git a/lib/WeBWorK/AchievementItems/ResurrectGW.pm b/lib/WeBWorK/AchievementItems/ResurrectGW.pm index 3a3bab10be..545503de1f 100644 --- a/lib/WeBWorK/AchievementItems/ResurrectGW.pm +++ b/lib/WeBWorK/AchievementItems/ResurrectGW.pm @@ -18,10 +18,7 @@ use Mojo::Base 'WeBWorK::AchievementItems', -signatures; # Item to extend the due date on a gateway -use strict; -use warnings; - -use WeBWorK::Utils qw(x nfreeze_base64 thaw_base64 format_set_name_display); +use WeBWorK::Utils qw(after x nfreeze_base64 thaw_base64 format_set_name_display); sub new ($class) { return bless { @@ -34,26 +31,29 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my $db = $c->db; - my $effectiveUserName = $c->param('effectiveUser') // $c->param('user'); - my @unfilteredsets = $db->getMergedSets(map { [ $effectiveUserName, $_ ] } $db->listUserSets($effectiveUserName)); - my @sets; + my @closed_gateway_sets; # Find the template sets of gateway quizzes. - for my $set (@unfilteredsets) { - push(@sets, [ format_set_name_display($set->set_id) => $set->set_id ]) - if ($set->assignment_type =~ /gateway/ && $set->set_id !~ /,v\d+$/); + for my $set (@$sets) { + push(@closed_gateway_sets, [ format_set_name_display($set->set_id) => $set->set_id ]) + if $set->assignment_type =~ /gateway/ + && $set->set_id !~ /,v\d+$/ + && (after($set->due_date) + || ($set->reduced_scoring_date && after($set->reduced_scoring_date))); } + return unless @closed_gateway_sets; + return $c->c( $c->tag('p', $c->maketext('Resurrect which test?')), WeBWorK::AchievementItems::form_popup_menu_row( $c, id => 'resgw_gw_id', label_text => $c->maketext('Test Name'), - values => \@sets, + values => \@closed_gateway_sets, menu_attr => { dir => 'ltr' } ) )->join(''); diff --git a/lib/WeBWorK/AchievementItems/ResurrectHW.pm b/lib/WeBWorK/AchievementItems/ResurrectHW.pm index 4303059730..478ab195b6 100644 --- a/lib/WeBWorK/AchievementItems/ResurrectHW.pm +++ b/lib/WeBWorK/AchievementItems/ResurrectHW.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # List all of the sets that are closed or past their reduced scoring date. my @closedSets; @@ -37,9 +37,11 @@ sub print_form ($self, $sets, $setProblemCount, $c) { push(@closedSets, [ format_set_name_display($sets->[$i]->set_id) => $sets->[$i]->set_id ]) if $sets->[$i]->assignment_type eq 'default' && (after($sets->[$i]->due_date) - || ($sets->[$i]->reduced_scoring_date && after($$sets[$i]->reduced_scoring_date))); + || ($sets->[$i]->reduced_scoring_date && after($sets->[$i]->reduced_scoring_date))); } + return unless @closedSets; + return $c->c( $c->tag('p', $c->maketext('Choose the set which you would like to resurrect.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/SuperExtendDueDate.pm b/lib/WeBWorK/AchievementItems/SuperExtendDueDate.pm index 08719c3c18..6c4e641a8a 100644 --- a/lib/WeBWorK/AchievementItems/SuperExtendDueDate.pm +++ b/lib/WeBWorK/AchievementItems/SuperExtendDueDate.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { my @openSets; for my $i (0 .. $#$sets) { @@ -36,6 +36,8 @@ sub print_form ($self, $sets, $setProblemCount, $c) { if (between($sets->[$i]->open_date, $sets->[$i]->due_date) && $sets->[$i]->assignment_type eq 'default'); } + return unless @openSets; + return $c->c( $c->tag('p', $c->maketext('Choose the set whose close date you would like to extend.')), WeBWorK::AchievementItems::form_popup_menu_row( diff --git a/lib/WeBWorK/AchievementItems/Surprise.pm b/lib/WeBWorK/AchievementItems/Surprise.pm index c750812f69..2a5bbdd7e8 100644 --- a/lib/WeBWorK/AchievementItems/Surprise.pm +++ b/lib/WeBWorK/AchievementItems/Surprise.pm @@ -28,7 +28,7 @@ sub new ($class) { }, $class; } -sub print_form ($self, $sets, $setProblemCount, $c) { +sub print_form ($self, $sets, $setProblemIds, $c) { # The form opens the file "suprise_message.txt" in the achievements # folder and prints the contents of the file. diff --git a/lib/WeBWorK/ContentGenerator/Achievements.pm b/lib/WeBWorK/ContentGenerator/Achievements.pm index 0e126d5ab6..5115610299 100644 --- a/lib/WeBWorK/ContentGenerator/Achievements.pm +++ b/lib/WeBWorK/ContentGenerator/Achievements.pm @@ -93,31 +93,27 @@ sub getAchievementItemsData ($c) { my $userID = $c->{studentName}; - my (@items, %itemCounts, @sets, @setProblemCount); + my (@items, %itemCounts, @sets, %setProblemIds); if ($c->ce->{achievementItemsEnabled} && $c->{achievementItems}) { # Remove count data so @items is structured as originally designed. for my $item (@{ $c->{achievementItems} }) { push(@items, $item->[0]); - $itemCounts{ $item->[0]->id() } = $item->[1]; + $itemCounts{ $item->[0]->id } = $item->[1]; } - # Achievement items only make sense for regular homeworks. So filter gateways out. for my $set ($db->getMergedSets(map { [ $userID, $_ ] } $db->listUserSets($userID))) { - push(@sets, $set) if ($set->assignment_type() eq 'default'); - } - - # Generate an array of problem counts. - for (my $i = 0; $i <= $#sets; $i++) { - $setProblemCount[$i] = WeBWorK::Utils::max($db->listUserProblems($userID, $sets[$i]->set_id)); + push(@sets, $set); + $setProblemIds{ $set->set_id } = [ map { $_->[2] } + $db->listUserProblemsWhere({ user_id => $userID, set_id => $set->set_id }, 'problem_id') ]; } } return ( - items => \@items, - itemCounts => \%itemCounts, - sets => \@sets, - setProblemCount => \@setProblemCount + items => \@items, + itemCounts => \%itemCounts, + sets => \@sets, + setProblemIds => \%setProblemIds ); } diff --git a/templates/ContentGenerator/Achievements/achievement_items.html.ep b/templates/ContentGenerator/Achievements/achievement_items.html.ep index b224d90b9f..68518c00e9 100644 --- a/templates/ContentGenerator/Achievements/achievement_items.html.ep +++ b/templates/ContentGenerator/Achievements/achievement_items.html.ep @@ -16,34 +16,37 @@

<%= maketext($item->name) %>

% }

<%= maketext($item->description) %>

+ % my $form = $item->print_form($sets, $setProblemIds, $c); % # Print a modal popup for each item which contains the form necessary to get the data to use the item. <%= link_to maketext('Use Reward') => '#modal_' . $item->id, role => 'button', - class => 'btn btn-secondary', + class => 'btn btn-secondary' . ($form ? '' : ' disabled'), id => 'popup_' . $item->id, - data => { bs_toggle => 'modal' } =%> -