aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2011-05-26 18:54:48 +0100
committerMatthew Somerville <matthew@mysociety.org>2011-05-26 18:54:48 +0100
commit67cfdc9bf5948bec4c4f88e19ac1b4e7370a995f (patch)
treea8ea53899f31f6d99194cd3a05cdd5d61e68760a
parent4308906bc2695f1fd034b8f5f1a6be657e6d4dfe (diff)
Questionnaire submission code.
-rw-r--r--conf/httpd.conf4
-rwxr-xr-xperllib/FixMyStreet/App/Controller/Questionnaire.pm249
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm3
-rw-r--r--perllib/FixMyStreet/App/View/Web.pm12
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm1
-rw-r--r--templates/web/default/questionnaire/completed.html28
-rw-r--r--templates/web/default/questionnaire/index.html6
-rw-r--r--templates/web/default/tokens/confirm_alert.html2
-rw-r--r--templates/web/default/tokens/confirm_problem.html2
-rw-r--r--templates/web/default/tokens/confirm_update.html2
10 files changed, 169 insertions, 140 deletions
diff --git a/conf/httpd.conf b/conf/httpd.conf
index 84d24bb4a..4f96593af 100644
--- a/conf/httpd.conf
+++ b/conf/httpd.conf
@@ -51,7 +51,7 @@ RewriteRule ^(.+)/$ $1 [R=permanent]
# Confirmation tokens
RewriteRule ^/[Aa]/([0-9A-Za-z]{16,18}).*$ /alert.cgi?token=$1 [QSA,L]
RewriteRule ^/[Cc]/([0-9A-Za-z]{16,18}).*$ /confirm.cgi?type=update;token=$1 [QSA,L]
-RewriteRule ^/[Qq]/([0-9A-Za-z]{16,18}).*$ /questionnaire.cgi?token=$1 [QSA,L]
+# RewriteRule ^/[Qq]/([0-9A-Za-z]{16,18}).*$ /questionnaire.cgi?token=$1 [QSA,L]
RewriteRule ^/[Ff]/([0-9A-Za-z]{16,18}).*$ /flickr.cgi?token=$1 [L]
RewriteRule ^/[Tt]/([0-9A-Za-z]{16,18}).*$ /tms-signup.cgi?token=$1 [L]
@@ -111,7 +111,7 @@ RewriteRule ^/flickr(.*) /flickr.cgi$1 [L]
RewriteRule ^/fun(.*) /fun.cgi$1 [L]
# RewriteRule ^/json(.*) /json.cgi$1 [L]
# RewriteRule ^/photo(.*) /photo.cgi$1 [L]
-RewriteRule ^/questionnaire(.*) /questionnaire.cgi$1 [L]
+# RewriteRule ^/questionnaire(.*) /questionnaire.cgi$1 [L]
# RewriteRule ^/reports(.*) /reports.cgi$1 [L]
# RewriteRule ^/rss(.*) /rss.cgi$1 [L]
RewriteRule ^/test(.*) /test.cgi$1 [L]
diff --git a/perllib/FixMyStreet/App/Controller/Questionnaire.pm b/perllib/FixMyStreet/App/Controller/Questionnaire.pm
index 1b1e5e547..25d24c212 100755
--- a/perllib/FixMyStreet/App/Controller/Questionnaire.pm
+++ b/perllib/FixMyStreet/App/Controller/Questionnaire.pm
@@ -2,10 +2,7 @@ package FixMyStreet::App::Controller::Questionnaire;
use Moose;
use namespace::autoclean;
-#use Utils;
-#use Error qw(:try);
-#use CrossSell;
-#use mySociety::Locale;
+use Utils;
BEGIN { extends 'Catalyst::Controller'; }
@@ -15,12 +12,20 @@ FixMyStreet::App::Controller::Questionnaire - Catalyst Controller
=head1 DESCRIPTION
-Catalyst Controller.
+Deals with report questionnaires.
=head1 METHODS
=cut
+=head2 load_questionnaire
+
+Loads the questionnaire from the database, and checks it still needs answering
+and is in the right state. Also finds out if this user has answered the
+"ever reported" question before.
+
+=cut
+
sub load_questionnaire : Private {
my ( $self, $c ) = @_;
@@ -40,20 +45,23 @@ sub load_questionnaire : Private {
$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;
+ unless ( $questionnaire->problem->state eq 'confirmed' || $questionnaire->problem->state eq 'fixed' ) {
+ $c->stash->{message} = _("I'm afraid we couldn't locate your problem in the database.\n");
+ $c->stash->{template} = 'questionnaire/error.html';
+ $c->detach;
+ }
- $c->stash->{answered_ever_reported} = $c->model('DB::Questionnaire')->count(
- { 'problem.user_id' => $c->stash->{problem}->user_id,
- ever_reported => { '!=', undef },
- },
- { join => 'problem' }
- );
+ $c->stash->{problem} = $questionnaire->problem;
+ $c->stash->{answered_ever_reported} = $questionnaire->problem->user->answered_ever_reported;
}
+=head2 submit
+
+If someone submits a questionnaire - either a full style one (when we'll have a
+token), or the mini own-report one (when we'll have a problem ID).
+
+=cut
+
sub submit : Path('submit') {
my ( $self, $c ) = @_;
@@ -108,127 +116,113 @@ sub submit_standard : Private {
$c->forward( '/tokens/load_questionnaire_id', [ $c->req->params->{token} ] );
$c->forward( 'load_questionnaire' );
- my $questionnaire = $c->stash->{questionnaire};
- my $problem = $questionnaire->problem;
+ my $problem = $c->stash->{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->{num_questionnaire} = $c->model('DB::Questionnaire')->count(
+ { problem_id => $problem->id }
+ );
$c->stash->{another} = $c->stash->{num_questionnaire}==1 ? 'Yes' : 'No';
}
+ $c->forward( 'process_questionnaire' );
+
+ my $new_state = '';
+ $new_state = 'fixed' if $c->stash->{been_fixed} eq 'Yes' && $problem->{state} eq 'confirmed';
+ $new_state = 'confirmed' if $c->stash->{been_fixed} eq 'No' && $problem->{state} eq 'fixed';
+
+ # Record state change, if there was one
+ if ( $new_state ) {
+ $problem->state( $new_state );
+ $problem->lastupdate( \'ms_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' && $problem->state eq 'confirmed' ) {
+ $problem->lastupdate( \'ms_current_timestamp()' );
+ }
+
+ # Record questionnaire response
+ my $reported = undef;
+ $reported = 't' if $c->stash->{reported} eq 'Yes';
+ $reported = 'f' if $c->stash->{reported} eq 'No';
+
+ my $q = $c->stash->{questionnaire};
+ $q->whenanswered( \'ms_current_timestamp()' );
+ $q->ever_reported( $reported );
+ $q->old_state( $problem->state );
+ $q->new_state( $c->stash->{been_fixed} eq 'Unknown' ? 'unknown' : ($new_state ? $new_state : $problem->state) );
+ $q->update;
+
+ # Record an update if they've given one, or if there's a state change
+ if ( $new_state || $c->stash->{update} ) {
+ my $update = $c->stash->{update} || _('Questionnaire filled in by problem reporter');
+ $update = $c->model('DB::Comment')->new(
+ {
+ problem => $problem,
+ name => $problem->name,
+ user => $problem->user,
+ text => $c->stash->{update},
+ state => 'confirmed',
+ mark_fixed => $new_state eq 'fixed' ? 1 : 0,
+ mark_open => $new_state eq 'confirmed' ? 1 : 0,
+ lang => $c->stash->{lang_code},
+ cobrand => $c->cobrand->moniker,
+ cobrand_data => $c->cobrand->extra_update_data,
+ confirmed => \'ms_current_timestamp()',
+ anonymous => $problem->anonymous,
+ }
+ );
+ if ( my $fileid = $c->stash->{upload_fileid} ) {
+ my $file = file( $c->config->{UPLOAD_CACHE}, "$fileid.jpg" );
+ my $blob = $file->slurp;
+ $file->remove;
+ $update->photo($blob);
+ }
+ $update->insert;
+ }
+
+ # If they've said they want another questionnaire, mark as such
+ $problem->send_questionnaire( 1 )
+ 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';
+}
+
+sub process_questionnaire : Private {
+ my ( $self, $c ) = @_;
+
+ map { $c->stash->{$_} = $c->req->params->{$_} || '' } qw(been_fixed reported another update);
+
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 state whether or not the problem has been fixed')
+ unless $c->stash->{been_fixed};
+
+ if ($c->cobrand->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 $c->stash->{been_fixed} eq 'No' && $c->stash->{problem}->state eq 'fixed' && !$c->stash->{update};
+
+ $c->forward('/report/new/process_photo');
+ push @errors, $c->stash->{photo_error}
+ if $c->stash->{photo_error};
+
+ push @errors, _('Please provide some text as well as a photo')
+ if $c->stash->{upload_fileid} && !$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.
@@ -238,7 +232,12 @@ sub index : Private {
$c->forward( 'display' );
}
-# Displays the questionnaire, either after bad submission or from email token
+=head2 display
+
+Displays a questionnaire, either after bad submission or directly from email token.
+
+=cut
+
sub display : Private {
my ( $self, $c ) = @_;
@@ -250,11 +249,10 @@ sub display : Private {
map { Utils::truncate_coordinate($_) }
( $problem->latitude, $problem->longitude );
- my $updates = $c->model('DB::Comment')->search(
+ $c->stash->{updates} = $c->model('DB::Comment')->search(
{ problem_id => $problem->id, state => 'confirmed' },
{ order_by => 'confirmed' }
);
- $c->stash->{updates} = $updates;
FixMyStreet::Map::display_map(
$c,
@@ -266,7 +264,6 @@ sub display : Private {
colour => $problem->state eq 'fixed' ? 'green' : 'red',
} ],
);
- $c->stash->{cobrand_form_elements} = $c->cobrand->form_elements('questionnaireForm');
}
=head2 creator_fixed
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index e113517f9..736af16b1 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -881,9 +881,6 @@ sub generate_map : Private {
map { Utils::truncate_coordinate($_) }
( $c->stash->{latitude}, $c->stash->{longitude} );
- # Forms that allow photos need a different enctype
- my $allow_photo_upload = $c->cobrand->allow_photo_upload;
-
# Don't do anything if the user skipped the map
unless ( $c->req->param('skipped') ) {
FixMyStreet::Map::display_map(
diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm
index 61d7c6ca5..8f1aaa085 100644
--- a/perllib/FixMyStreet/App/View/Web.pm
+++ b/perllib/FixMyStreet/App/View/Web.pm
@@ -16,7 +16,7 @@ __PACKAGE__->config(
ENCODING => 'utf8',
render_die => 1,
expose_methods => [
- 'loc', 'nget', 'tprintf', 'display_crossell_advert', 'prettify_epoch',
+ 'loc', 'nget', 'tprintf', 'display_crosssell_advert', 'prettify_epoch',
'split_into_lines',
],
);
@@ -70,16 +70,16 @@ sub tprintf {
return sprintf $format, @args;
}
-=head2 display_crossell_advert
+=head2 display_crosssell_advert
- [% display_crossell_advert( email, name ) %]
+ [% display_crosssell_advert( email, name ) %]
Displays a crosssell advert if permitted by the cobrand.
=cut
-sub display_crossell_advert {
- my ( $self, $c, $email, $name ) = @_;
+sub display_crosssell_advert {
+ my ( $self, $c, $email, $name, %data ) = @_;
return unless $c->cobrand->allow_crosssell_adverts();
@@ -87,7 +87,7 @@ sub display_crossell_advert {
my $q = { site => $c->cobrand->moniker, };
$q->{site} = 'fixmystreet' if $q->{site} eq 'default';
- return CrossSell::display_advert( $q, $email, $name );
+ return CrossSell::display_advert( $q, $email, $name, %data );
}
=head2 Page::prettify_epoch
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 228e93267..30314ffa3 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -114,4 +114,5 @@ sub answered_ever_reported {
return $has_answered->count > 0;
}
+
1;
diff --git a/templates/web/default/questionnaire/completed.html b/templates/web/default/questionnaire/completed.html
new file mode 100644
index 000000000..0dbd2828d
--- /dev/null
+++ b/templates/web/default/questionnaire/completed.html
@@ -0,0 +1,28 @@
+[% advert_outcome = 1 %]
+
+[% IF been_fixed == 'Unknown' %]
+
+[% loc('<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>') %]
+
+[% ELSIF new_state == 'confirmed' OR (!new_state AND problem.state == 'confirmed') %]
+
+[% tprintf( loc('<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>'), c.cobrand.writetothem_url || http://www.writetothem.com %]
+
+[% advert_outcome = 0 %]
+
+[% ELSE %]
+
+[% loc('<p style="font-size:150%">Thank you very much for filling in our questionnaire; glad to hear it's been fixed.</p>') %]
+
+[% END %]
+
+[% IF advert_outcome %]
+ [% display_crosssell_advert( problem.user.email, problem.name, { council = problem.council } ) %]
+[% END %]
+
diff --git a/templates/web/default/questionnaire/index.html b/templates/web/default/questionnaire/index.html
index f0e65de4d..6d102bac9 100644
--- a/templates/web/default/questionnaire/index.html
+++ b/templates/web/default/questionnaire/index.html
@@ -21,6 +21,8 @@
<input type="hidden" name="token" value="[% token | html %]">
+[% c.cobrand.form_elements('questionnaireForm') %]
+
[% IF c.cobrand.moniker == 'emptyhomes' %]
[% IF num_questionnaires == 1 %]
_(<<EOF);
@@ -90,6 +92,10 @@ your experience of getting the problem fixed?') %]</p>
[% IF c.cobrand.allow_photo_upload %]
<div id="fileupload_normalUI">
+ [% IF upload_fileid %]
+ <p>[% loc('You have already attached a photo to this report, attaching another one will replace it.') %]</p>
+ <input type="hidden" name="upload_fileid" value="[% upload_fileid %]">
+ [% END %]
<label for="form_photo">[% loc('Photo:') %]</label>
<input type="file" name="photo" id="form_photo">
</div>
diff --git a/templates/web/default/tokens/confirm_alert.html b/templates/web/default/tokens/confirm_alert.html
index 07694cbb6..d23a658ec 100644
--- a/templates/web/default/tokens/confirm_alert.html
+++ b/templates/web/default/tokens/confirm_alert.html
@@ -12,6 +12,6 @@
[% END %]
</p>
-[% display_crossell_advert( alert.user.email, alert.user.name ) %]
+[% display_crosssell_advert( alert.user.email, alert.user.name ) %]
[% INCLUDE 'footer.html' %]
diff --git a/templates/web/default/tokens/confirm_problem.html b/templates/web/default/tokens/confirm_problem.html
index cc3fb6400..756958380 100644
--- a/templates/web/default/tokens/confirm_problem.html
+++ b/templates/web/default/tokens/confirm_problem.html
@@ -17,6 +17,6 @@
%]
</p>
-[% display_crossell_advert( problem.user.email, problem.name ) %]
+[% display_crosssell_advert( problem.user.email, problem.name ) %]
[% INCLUDE 'footer.html' %]
diff --git a/templates/web/default/tokens/confirm_update.html b/templates/web/default/tokens/confirm_update.html
index cae702073..25a1235f6 100644
--- a/templates/web/default/tokens/confirm_update.html
+++ b/templates/web/default/tokens/confirm_update.html
@@ -15,6 +15,6 @@
[% END %]
</p>
-[% display_crossell_advert( update.user.email, update.name ) %]
+[% display_crosssell_advert( update.user.email, update.name ) %]
[% INCLUDE 'footer.html' %]