diff options
author | Matthew Somerville <matthew@mysociety.org> | 2011-05-21 00:12:42 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2011-05-21 00:12:42 +0100 |
commit | c804762c2fa0b855fca4c2388c6a47f09913b966 (patch) | |
tree | 09b90e602aa3239fc8dc4a4046afbe63e8edec9b | |
parent | 4af1e3d75ae247fd7d6a9910087fc36e1a9659f5 (diff) |
Start of questionnaire migration, displays mostly okay, submission not done, nor problem display.
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Questionnaire.pm | 228 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Tokens.pm | 20 | ||||
-rwxr-xr-x | templates/web/default/questionnaire/error.html | 8 | ||||
-rw-r--r-- | templates/web/default/questionnaire/index.html | 114 | ||||
-rw-r--r-- | templates/web/default/report/_main.html | 13 | ||||
-rw-r--r-- | templates/web/default/report/display.html | 12 |
7 files changed, 384 insertions, 13 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Questionnaire.pm b/perllib/FixMyStreet/App/Controller/Questionnaire.pm new file mode 100755 index 000000000..986543e9d --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Questionnaire.pm @@ -0,0 +1,228 @@ +package FixMyStreet::App::Controller::Questionnaire; + +use Moose; +use namespace::autoclean; +#use Utils; +#use Error qw(:try); +#use CrossSell; +#use mySociety::Locale; + +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Questionnaire - Catalyst Controller + +=head1 DESCRIPTION + +Catalyst Controller. + +=head1 METHODS + +=cut + +sub load_questionnaire : Private { + my ( $self, $c ) = @_; + + my $questionnaire = $c->model('DB::Questionnaire')->find( + { id => $c->stash->{id} }, + { prefetch => 'problem' } + ); + $c->stash->{questionnaire} = $questionnaire; + + my $problem_id = $questionnaire->problem_id; + + if ( $questionnaire->whenanswered ) { + my $problem_url = $c->uri_for( "/report/$problem_id" ); + my $contact_url = $c->uri_for( "/contact" ); + $c->stash->{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); + $c->stash->{template} = 'questionnaire/error.html'; + $c->detach; + } + + # FIXME problem fetched information + # extract(epoch from confirmed) as time, extract(epoch from whensent-confirmed) as whensent + # state in ('confirmed','fixed') + $c->stash->{problem} = $questionnaire->problem; + # throw Error::Simple(_("I'm afraid we couldn't locate your problem in the database.\n")) unless $problem; + + $c->stash->{answered_ever_reported} = $c->model('DB::Questionnaire')->count( + { 'problem.user_id' => $c->stash->{problem}->user_id, + ever_reported => { '!=', undef }, + }, + { join => 'problem' } + ); +} + +sub submit : Path('submit') { + my ( $self, $c ) = @_; + + $c->forward( '/tokens/load_questionnaire_id', [ $c->req->params->{token} ] ); + $c->forward( 'load_questionnaire' ); + + my $questionnaire = $c->stash->{questionnaire}; + my $problem = $questionnaire->problem; + + $c->stash->{num_questionnaire} = $c->model('DB::Questionnaire')->count( + { problem_id => $problem->id } + ); + + map { $c->stash->{$_} = $c->req->params->{$_} || '' } qw(been_fixed reported another update); + # EHA questionnaires done for you + if ($c->cobrand->moniker eq 'emptyhomes') { + $c->stash->{another} = $c->stash->{num_questionnaire}==1 ? 'Yes' : 'No'; + } + + my @errors; + push @errors, _('Please state whether or not the problem has been fixed') unless $c->stash->{been_fixed}; + my $ask_ever_reported = $c->cobrand->ask_ever_reported; + if ($ask_ever_reported) { + push @errors, _('Please say whether you\'ve ever reported a problem to your council before') unless $c->stash->{reported} || $c->stash->{answered_ever_reported}; + } + push @errors, _('Please indicate whether you\'d like to receive another questionnaire') + 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' && $problem->state eq 'fixed' && !$c->stash->{update}; + if (@errors) { + $c->stash->{errors} = [ @errors ]; + $c->detach( 'display' ); + } + +# my $fh = $q->upload('photo'); +# my $image; +# if ($fh) { +# my $err = Page::check_photo($q, $fh); +# push @errors, $err if $err; +# try { +# $image = Page::process_photo($fh) unless $err; +# } catch Error::Simple with { +# my $e = shift; +# push(@errors, "That image doesn't appear to have uploaded correctly ($e), please try again."); +# }; +# } +# push @errors, _('Please provide some text as well as a photo') +# if $image && !$input{update}; +# return display_questionnaire($q, @errors) if @errors; +# +# my $new_state = ''; +# $new_state = 'fixed' if $input{been_fixed} eq 'Yes' && $problem->{state} eq 'confirmed'; +# $new_state = 'confirmed' if $input{been_fixed} eq 'No' && $problem->{state} eq 'fixed'; +# +# # Record state change, if there was one +# dbh()->do("update problem set state=?, lastupdate=ms_current_timestamp() +# where id=?", {}, $new_state, $problem->{id}) +# if $new_state; +# +# # If it's not fixed and they say it's still not been fixed, record time update +# dbh()->do("update problem set lastupdate=ms_current_timestamp() +# where id=?", {}, $problem->{id}) +# if $input{been_fixed} eq 'No' && $problem->{state} eq 'confirmed'; +# +# # Record questionnaire response +# my $reported = $input{reported} +# ? ($input{reported} eq 'Yes' ? 't' : ($input{reported} eq 'No' ? 'f' : undef)) +# : undef; +# dbh()->do('update questionnaire set whenanswered=ms_current_timestamp(), +# ever_reported=?, old_state=?, new_state=? where id=?', {}, +# $reported, $problem->{state}, $input{been_fixed} eq 'Unknown' +# ? 'unknown' +# : ($new_state ? $new_state : $problem->{state}), +# $questionnaire->{id}); +# +# # Record an update if they've given one, or if there's a state change +# my $name = $problem->{anonymous} ? undef : $problem->{name}; +# my $update = $input{update} ? $input{update} : _('Questionnaire filled in by problem reporter'); +# Utils::workaround_pg_bytea("insert into comment +# (problem_id, name, email, website, text, state, mark_fixed, mark_open, photo, lang, cobrand, cobrand_data, confirmed) +# values (?, ?, ?, '', ?, 'confirmed', ?, ?, ?, ?, ?, ?, ms_current_timestamp())", 7, +# $problem->{id}, $name, $problem->{email}, $update, +# $new_state eq 'fixed' ? 't' : 'f', $new_state eq 'confirmed' ? 't' : 'f', +# $image, $mySociety::Locale::lang, $cobrand, $c->cobrand->extra_data +# ) +# if $new_state || $input{update}; +# +# # If they've said they want another questionnaire, mark as such +# dbh()->do("update problem set send_questionnaire = 't' where id=?", {}, $problem->{id}) +# if ($input{been_fixed} eq 'No' || $input{been_fixed} eq 'Unknown') && $input{another} eq 'Yes'; +# dbh()->commit(); +# +# my $out; +# my $message; +# my $advert_outcome = 1; +# if ($input{been_fixed} eq 'Unknown') { +# $message = _(<<EOF); +# <p>Thank you very much for filling in our questionnaire; if you +# get some more information about the status of your problem, please come back to the +# site and leave an update.</p> +# EOF +# } elsif ($new_state eq 'confirmed' || (!$new_state && $problem->{state} eq 'confirmed')) { +# my $wtt_url = Cobrand::writetothem_url($cobrand, $c->cobrand->extra_data); +# $wtt_url = "http://www.writetothem.com" if (! $wtt_url); +# $message = sprintf(_(<<EOF), $wtt_url); +# <p style="font-size:150%%">We're sorry to hear that. We have two suggestions: why not try +# <a href="%s">writing direct to your councillor(s)</a> +# or, if it's a problem that could be fixed by local people working together, +# why not <a href="http://www.pledgebank.com/new">make and publicise a pledge</a>? +# </p> +# EOF +# $advert_outcome = 0; +# } else { +# $message = _(<<EOF); +# <p style="font-size:150%">Thank you very much for filling in our questionnaire; glad to hear it's been fixed.</p> +# EOF +# } +# $out = $message; +# my $display_advert = Cobrand::allow_crosssell_adverts($cobrand); +# if ($display_advert && $advert_outcome) { +# $out .= CrossSell::display_advert($q, $problem->{email}, $problem->{name}, +# council => $problem->{council}); +# } +# my %vars = (message => $message); +# my $template_page = Page::template_include('questionnaire-completed', $q, Page::template_root($q), %vars); +# return $template_page if ($template_page); +# return $out; +} + +# Sent here from email token action. Simply load and display questionnaire. +sub index : Private { + my ( $self, $c ) = @_; + $c->forward( 'load_questionnaire' ); + $c->forward( 'display' ); +} + +# Displays the questionnaire, either after bad submission or from email token +sub display : Private { + my ( $self, $c ) = @_; + + $c->stash->{template} = 'questionnaire/index.html'; + + my $problem = $c->stash->{questionnaire}->problem; + + my $problem_text = ''; # Page::display_problem_text($c->fake_q, $problem); # FIXME This needs to be in the template + $c->stash->{updates} = ''; # FIXME Should be database ResultSet of problem's pdates + $c->stash->{map_start_html} = FixMyStreet::Map::display_map( + $c->fake_q, + latitude => $problem->latitude, + longitude => $problem->longitude, + pins => [ [ $problem->latitude, $problem->longitude, $problem->state eq 'fixed' ? 'green' : 'red' ] ], + pre => $problem_text, + ); + $c->stash->{map_js} = FixMyStreet::Map::header_js(); + $c->stash->{cobrand_form_elements} = $c->cobrand->form_elements('questionnaireForm'); +} + +=head1 AUTHOR + +Matthew Somerville + +=head1 LICENSE + +Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +Licensed under the Affero GPL. + +=cut + +__PACKAGE__->meta->make_immutable; + +1; + diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index 9ec187c7d..9041fa731 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -157,7 +157,7 @@ sub format_problem_for_display : Private { $c->stash->{banner} = $c->cobrand->generate_problem_banner($problem); - $c->stash->{allow_photo_upload} = $c->cobrand->allow_photo_display; + $c->stash->{allow_photo_upload} = $c->cobrand->allow_photo_display; # FIXME? $c->stash->{cobrand_alert_fields} = $c->cobrand->form_elements( '/alerts' ); $c->stash->{cobrand_update_fields} = $c->cobrand->form_elements( '/updateForm' ); diff --git a/perllib/FixMyStreet/App/Controller/Tokens.pm b/perllib/FixMyStreet/App/Controller/Tokens.pm index 1c3d89b54..7053edc95 100644 --- a/perllib/FixMyStreet/App/Controller/Tokens.pm +++ b/perllib/FixMyStreet/App/Controller/Tokens.pm @@ -156,6 +156,24 @@ sub confirm_update : Path('/C') { return 1; } +sub load_questionnaire_id : Private { + my ( $self, $c, $token_code ) = @_; + + # Set up error handling + $c->stash->{error_template} = 'questionnaire/error.html'; + $c->stash->{message} = _("I'm afraid we couldn't validate that token. If you've copied the URL from an email, please check that you copied it exactly.\n"); + + my $auth_token = $c->forward( 'load_auth_token', [ $token_code, 'questionnaire' ] ); + $c->stash->{id} = $auth_token->data; + $c->stash->{token} = $token_code; +} + +sub questionnaire : Path('/Q') : Args(1) { + my ( $self, $c, $token_code ) = @_; + $c->forward( 'load_questionnaire_id', [ $token_code ] ); + $c->forward( '/questionnaire/index'); +} + =head2 load_auth_token my $auth_token = @@ -193,7 +211,7 @@ Display an error page saying that there is something wrong with the token. sub token_error : Private { my ( $self, $c ) = @_; - $c->stash->{template} = 'tokens/error.html'; + $c->stash->{template} = $c->stash->{error_template} || 'tokens/error.html'; $c->detach; } diff --git a/templates/web/default/questionnaire/error.html b/templates/web/default/questionnaire/error.html new file mode 100755 index 000000000..12aa8e170 --- /dev/null +++ b/templates/web/default/questionnaire/error.html @@ -0,0 +1,8 @@ +[% INCLUDE 'header.html', title = loc('Error') %] + +<h1>[% loc('Error') %]</h1> + +<p>[% message %]</p> + +[% INCLUDE 'footer.html' %] + diff --git a/templates/web/default/questionnaire/index.html b/templates/web/default/questionnaire/index.html new file mode 100644 index 000000000..142a8b909 --- /dev/null +++ b/templates/web/default/questionnaire/index.html @@ -0,0 +1,114 @@ +[% INCLUDE 'header.html', title = loc('Questionnaire') %] + +# FIXME The below should be in the template, by including a map template. +[% map_start_html %] + +[% INCLUDE 'report/updates.html' %] + +</div> +<div id="side"> + +<h1>[% loc('Questionnaire') %]</h1> + +<form method="post" action="/questionnaire/submit" id="questionnaire" +[%- IF c.cobrand.allow_photo_upload -%] + enctype="multipart/form-data" +[%- END -%] +> + +<input type="hidden" name="token" value="[% token | html %]"> + +[% IF c.cobrand.moniker == 'emptyhomes' %] +[% IF num_questionnaires == 1 %] + _(<<EOF); +<p>Getting empty homes back into use can be difficult. You shouldn't expect +the property to be back into use yet. But a good council will have started work +and should have reported what they have done on the website. If you are not +satisfied with progress or information from the council, now is the right time +to say. You may also want to try contacting some other people who may be able +to help. For advice on how to do this and other useful information please +go to <a href="http://www.emptyhomes.com/getinvolved/campaign.html">http://www.emptyhomes.com/getinvolved/campaign.html</a>.</p> +EOF +[% ELSE %] + _(<<EOF); +<p>Getting empty homes back into use can be difficult, but by now a good council +will have made a lot of progress and reported what they have done on the +website. Even so properties can remain empty for many months if the owner is +unwilling or the property is in very poor repair. If nothing has happened or +you are not satisfied with the progress the council is making, now is the right +time to say so. We think it's a good idea to contact some other people who +may be able to help or put pressure on the council For advice on how to do +this and other useful information please go to <a +href="http://www.emptyhomes.com/getinvolved/campaign.html">http://www.emptyhomes.com/getinvolved/campaign.html</a>.</p> +EOF +[% END %] +[% END %] + +<p> +[% loc('The details of your problem are available on the right hand side of this page.') %] +[% loc('Please take a look at the updates that have been left.') IF updates %] +</p> + +[% IF errors %] +<ul class="error"> +<li>[% errors.join("</li>\n<li>") %]</li> +</ul> +[% END %] + +<p> +[% loc('An update marked this problem as fixed.') IF problem.state == 'fixed' %] +[% loc('Has this problem been fixed?') %] +</p> + +<p> +<input type="radio" name="been_fixed" id="been_fixed_yes" value="Yes"[% ' checked' IF been_fixed == 'Yes' %]> +<label for="been_fixed_yes">[% loc('Yes') %]</label> +<input type="radio" name="been_fixed" id="been_fixed_no" value="No"[% ' checked' IF been_fixed == 'No' %]> +<label for="been_fixed_no">[% loc('No') %]</label> +<input type="radio" name="been_fixed" id="been_fixed_unknown" value="Unknown"[% ' checked' IF been_fixed == 'Unknown' %]> +<label for="been_fixed_unknown">[% loc('Don’t know') %]</label> +</p> + +[% UNLESS answered_ever_reported %] +<p>[% loc('Have you ever reported a problem to a council before, or is this your first time?') %]</p> +<p> +<input type="radio" name="reported" id="reported_yes" value="Yes"[% ' checked' IF reported == 'Yes' %]> +<label for="reported_yes">[% loc('Reported before') %]</label> +<input type="radio" name="reported" id="reported_no" value="No"[% ' checked' IF reported == 'No' %]> +<label for="reported_no">[% loc('First time') %]</label> +</p> +[% END %] + +<p>[% loc('If you wish to leave a public update on the problem, please enter it here +(please note it will not be sent to the council). For example, what was +your experience of getting the problem fixed?') %]</p> + +<p><textarea name="update" style="max-width:90%" rows="7" cols="30">[% update | html %]</textarea></p> + +[% IF c.cobrand.allow_photo_upload %] +<div id="fileupload_normalUI"> + <label for="form_photo">[% loc('Photo:') %]</label> + <input type="file" name="photo" id="form_photo"> +</div> +[% END %] + +[% IF c.cobrand.moniker != 'emptyhomes' %] +<div id="another_qn"> + <p>[% loc('Would you like to receive another questionnaire in 4 weeks, reminding you to check the status?') %]</p> + <p> + <input type="radio" name="another" id="another_yes" value="Yes"[% ' checked' IF another == 'Yes' %]> + <label for="another_yes">[% loc('Yes') %]</label> + <input type="radio" name="another" id="another_no" value="No"[% ' checked' IF another == 'No' %]> + <label for="another_no">[% loc('No') %]</label> + </p> +</div> +[% END %] + +<p><input type="submit" name="submit" value="[% loc('Submit questionnaire') %]"></p> + +</form> + +</div> + +[% INCLUDE 'footer.html' %] + diff --git a/templates/web/default/report/_main.html b/templates/web/default/report/_main.html new file mode 100644 index 000000000..42cb4895d --- /dev/null +++ b/templates/web/default/report/_main.html @@ -0,0 +1,13 @@ +<h1>[% problem_title %]</h1> + +<p><em>[% meta | html %]</em></p> + +[% FOREACH line IN split_into_lines( problem.detail ) %] +<p> +[% line | html %] +</p> +[% END %] + +[% INCLUDE 'report/photo.html' object=problem center=1 %] + + diff --git a/templates/web/default/report/display.html b/templates/web/default/report/display.html index 60efae453..558b89692 100644 --- a/templates/web/default/report/display.html +++ b/templates/web/default/report/display.html @@ -17,17 +17,7 @@ </p> [% END %] -<h1>[% problem_title %]</h1> - -<p><em>[% meta | html %]</em></p> - -[% FOREACH line IN split_into_lines( problem.detail ) %] -<p> -[% line | html %] -</p> -[% END %] - -[% INCLUDE 'report/photo.html' object=problem center=1 %] +[% INCLUDE 'report/_main.html' %] <p align="right"> <small> |