diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-02-13 17:23:43 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-02-16 20:23:12 +0000 |
commit | 684405c7808cc71567f52cbba6d595326438b876 (patch) | |
tree | ab0c9860fca6057b7dadd80855b2916512533835 | |
parent | b82662a6173e7f20ed19f80b4fae3c405bcff4ea (diff) |
Store questionnaire data as soon as opened.
This means the questionnaire is considered 'answered' as soon as
a (HTML) link is clicked, which I think is okay. Then filling in
the questionnaire form will update the same questionnaire.
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Questionnaire.pm | 61 | ||||
-rw-r--r-- | t/app/controller/questionnaire.t | 17 | ||||
-rw-r--r-- | templates/web/base/questionnaire/index.html | 2 |
3 files changed, 58 insertions, 22 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Questionnaire.pm b/perllib/FixMyStreet/App/Controller/Questionnaire.pm index dd180dff8..58848f546 100755 --- a/perllib/FixMyStreet/App/Controller/Questionnaire.pm +++ b/perllib/FixMyStreet/App/Controller/Questionnaire.pm @@ -27,13 +27,13 @@ finds out if this user has answered the "ever reported" question before. =cut sub check_questionnaire : Private { - my ( $self, $c ) = @_; + my ( $self, $c, $unanswered ) = @_; my $questionnaire = $c->stash->{questionnaire}; my $problem = $questionnaire->problem; - if ( $questionnaire->whenanswered ) { + if ( $unanswered && $questionnaire->whenanswered ) { my $problem_url = $c->cobrand->base_url_for_report( $problem ) . $problem->url; my $contact_url = $c->uri_for( "/contact" ); my $message = sprintf(_("You have already answered this questionnaire. If you have a question, please <a href='%s'>get in touch</a>, or <a href='%s'>view your problem</a>.\n"), $contact_url, $problem_url); @@ -47,6 +47,10 @@ sub check_questionnaire : Private { $c->stash->{problem} = $problem; $c->stash->{answered_ever_reported} = $problem->user->answered_ever_reported; $c->stash->{been_fixed} = $c->get_param('been_fixed') || ''; + + # In case they've already visited the questionnaire page, so take what was stored then + my $old_state = $c->stash->{old_state} = $questionnaire->old_state || $problem->state; + $c->stash->{was_fixed} = FixMyStreet::DB::Result::Problem->fixed_states()->{$old_state}; } =head2 submit @@ -145,35 +149,51 @@ sub submit_creator_fixed : Private { return 1; } -sub submit_standard : Private { +sub record_state_change : Private { my ( $self, $c ) = @_; - $c->forward( '/tokens/load_questionnaire', [ $c->get_param('token') ] ); - $c->forward( 'check_questionnaire' ); - $c->forward( 'process_questionnaire' ); + return unless $c->stash->{been_fixed}; my $problem = $c->stash->{problem}; - my $old_state = $problem->state; + my $old_state = $c->stash->{old_state}; my $new_state = ''; - $new_state = 'fixed - user' if $c->stash->{been_fixed} eq 'Yes' && + $new_state = 'fixed - user' if $c->stash->{been_fixed} eq 'Yes' && FixMyStreet::DB::Result::Problem->open_states()->{$old_state}; $new_state = 'fixed - user' if $c->stash->{been_fixed} eq 'Yes' && FixMyStreet::DB::Result::Problem->closed_states()->{$old_state}; $new_state = 'confirmed' if $c->stash->{been_fixed} eq 'No' && FixMyStreet::DB::Result::Problem->fixed_states()->{$old_state}; + $c->stash->{new_state} = $new_state; # Record state change, if there was one if ( $new_state ) { $problem->state( $new_state ); $problem->lastupdate( \'current_timestamp' ); - } - - # If it's not fixed and they say it's still not been fixed, record time update - if ( $c->stash->{been_fixed} eq 'No' && + } elsif ($problem->state ne $old_state) { + $problem->state( $old_state ); + $problem->lastupdate( \'current_timestamp' ); + } elsif ( $c->stash->{been_fixed} eq 'No' && FixMyStreet::DB::Result::Problem->open_states->{$old_state} ) { + # If it's not fixed and they say it's still not been fixed, record time update $problem->lastupdate( \'current_timestamp' ); } + $problem->update; + $c->stash->{questionnaire}->update({ + whenanswered => \'current_timestamp', + old_state => $old_state, + new_state => $c->stash->{been_fixed} eq 'Unknown' ? 'unknown' : ($new_state || $old_state), + }); +} + +sub submit_standard : Private { + my ( $self, $c ) = @_; + + $c->forward( '/tokens/load_questionnaire', [ $c->get_param('token') ] ); + $c->forward( 'check_questionnaire' ); + $c->forward( 'process_questionnaire' ); + $c->forward( 'record_state_change' ); + # Record questionnaire response my $reported = undef; $reported = 1 if $c->stash->{reported} eq 'Yes'; @@ -181,14 +201,13 @@ sub submit_standard : Private { my $q = $c->stash->{questionnaire}; $q->update( { - whenanswered => \'current_timestamp', ever_reported => $reported, - old_state => $old_state, - new_state => $c->stash->{been_fixed} eq 'Unknown' ? 'unknown' : ($new_state || $old_state), } ); + my $problem = $c->stash->{problem}; + # Record an update if they've given one, or if there's a state change - if ( $new_state || $c->stash->{update} ) { + if ( $c->stash->{new_state} || $c->stash->{update} ) { my $update = $c->stash->{update} || _('Questionnaire filled in by problem reporter'); $update = $c->model('DB::Comment')->new( { @@ -197,8 +216,8 @@ sub submit_standard : Private { user => $problem->user, text => $update, state => 'confirmed', - mark_fixed => $new_state eq 'fixed - user' ? 1 : 0, - mark_open => $new_state eq 'confirmed' ? 1 : 0, + mark_fixed => $c->stash->{new_state} eq 'fixed - user' ? 1 : 0, + mark_open => $c->stash->{new_state} eq 'confirmed' ? 1 : 0, lang => $c->stash->{lang_code}, cobrand => $c->cobrand->moniker, cobrand_data => '', @@ -218,7 +237,6 @@ sub submit_standard : Private { if ($c->stash->{been_fixed} eq 'No' || $c->stash->{been_fixed} eq 'Unknown') && $c->stash->{another} eq 'Yes'; $problem->update; - $c->stash->{new_state} = $new_state; $c->stash->{template} = 'questionnaire/completed.html'; } @@ -242,7 +260,7 @@ sub process_questionnaire : Private { if ($c->stash->{been_fixed} eq 'No' || $c->stash->{been_fixed} eq 'Unknown') && !$c->stash->{another}; push @errors, _('Please provide some explanation as to why you\'re reopening this report') - if $c->stash->{been_fixed} eq 'No' && $c->stash->{problem}->is_fixed() && !$c->stash->{update}; + if $c->stash->{been_fixed} eq 'No' && $c->stash->{was_fixed} && !$c->stash->{update}; $c->forward('/photo/process_photo'); push @errors, $c->stash->{photo_error} @@ -260,7 +278,8 @@ sub process_questionnaire : Private { # Sent here from email token action. Simply load and display questionnaire. sub show : Private { my ( $self, $c ) = @_; - $c->forward( 'check_questionnaire' ); + $c->forward( 'check_questionnaire', [ 'unanswered' ] ); + $c->forward( 'record_state_change' ); $c->forward( 'display' ); } diff --git a/t/app/controller/questionnaire.t b/t/app/controller/questionnaire.t index 0dcfdaba6..75542d759 100644 --- a/t/app/controller/questionnaire.t +++ b/t/app/controller/questionnaire.t @@ -108,6 +108,23 @@ foreach my $test ( }; } +subtest "If been_fixed is provided in the URL" => sub { + $mech->get_ok("/Q/" . $token->token . "?been_fixed=Yes"); + $mech->content_contains('id="been_fixed_yes" value="Yes" checked'); + $report->discard_changes; + is $report->state, 'fixed - user'; + $questionnaire->discard_changes; + is $questionnaire->old_state, 'confirmed'; + is $questionnaire->new_state, 'fixed - user'; + $mech->submit_form_ok({ with_fields => { been_fixed => 'Unknown', reported => 'Yes', another => 'No' } }); + $report->discard_changes; + is $report->state, 'confirmed'; + $questionnaire->discard_changes; + is $questionnaire->old_state, 'confirmed'; + is $questionnaire->new_state, 'unknown'; + $questionnaire->update({ whenanswered => undef, ever_reported => undef, old_state => undef, new_state => undef }); +}; + $mech->get_ok("/Q/" . $token->token); $mech->title_like( qr/Questionnaire/ ); $mech->submit_form_ok( ); diff --git a/templates/web/base/questionnaire/index.html b/templates/web/base/questionnaire/index.html index 2f5b9b277..4305f05c9 100644 --- a/templates/web/base/questionnaire/index.html +++ b/templates/web/base/questionnaire/index.html @@ -28,7 +28,7 @@ [% END %] <p>1. -[% loc('An update marked this problem as fixed.') IF problem.is_fixed %] +[% loc('An update marked this problem as fixed.') IF was_fixed %] [% loc('Has this problem been fixed?') %] </p> |