diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Questionnaire.pm')
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Questionnaire.pm | 61 |
1 files changed, 40 insertions, 21 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' ); } |