<commit_before>package FixMyStreet::App::Controller::Questionnaire;

use Moose;
use namespace::autoclean;
use Utils;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Questionnaire - Catalyst Controller

=head1 DESCRIPTION

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 ) = @_;

    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;
    }

    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->{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 ) = @_;

    if ( $c->req->params->{token} ) {
        $c->forward('submit_standard');
    } elsif ( $c->req->params->{problem} ) {
        $c->forward('submit_creator_fixed');
    } else {
        return;
    }

    return 1;
}

sub submit_creator_fixed : Private {
    my ( $self, $c ) = @_;

    my @errors;

    map { $c->stash->{$_} = $c->req->params->{$_} || '' } qw(reported problem);

    push @errors, _('Please say whether you\'ve ever reported a problem to your council before') unless $c->stash->{reported};

    $c->stash->{problem_id} = $c->stash->{problem};
    $c->stash->{errors} = \@errors;
    $c->detach( 'creator_fixed' ) if scalar @errors;

    my $questionnaire = $c->model( 'DB::Questionnaire' )->find_or_new(
        {
            problem_id => $c->stash->{problem},
            old_state  => 'confirmed',
            new_state  => 'fixed',
        }
    );

    unless ( $questionnaire->in_storage ) {
        $questionnaire->ever_reported( $c->stash->{reported} eq 'Yes' );
        $questionnaire->whensent( \'ms_current_timestamp()' );
        $questionnaire->whenanswered( \'ms_current_timestamp()' );
        $questionnaire->insert;
    }

    $c->stash->{creator_fixed} = 1;
    $c->stash->{template} = 'tokens/confirm_update.html';

    return 1;
}

sub submit_standard : Private {
    my ( $self, $c ) = @_;

    $c->forward( '/tokens/load_questionnaire_id', [ $c->req->params->{token} ] );
    $c->forward( 'load_questionnaire' );

    my $problem = $c->stash->{problem};

    # 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 );<commit_msg>Perl boolean false is '', which the DB doesn't like.<commit_after>package FixMyStreet::App::Controller::Questionnaire;

use Moose;
use namespace::autoclean;
use Utils;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Questionnaire - Catalyst Controller

=head1 DESCRIPTION

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 ) = @_;

    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;
    }

    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->{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 ) = @_;

    if ( $c->req->params->{token} ) {
        $c->forward('submit_standard');
    } elsif ( $c->req->params->{problem} ) {
        $c->forward('submit_creator_fixed');
    } else {
        return;
    }

    return 1;
}

sub submit_creator_fixed : Private {
    my ( $self, $c ) = @_;

    my @errors;

    map { $c->stash->{$_} = $c->req->params->{$_} || '' } qw(reported problem);

    push @errors, _('Please say whether you\'ve ever reported a problem to your council before') unless $c->stash->{reported};

    $c->stash->{problem_id} = $c->stash->{problem};
    $c->stash->{errors} = \@errors;
    $c->detach( 'creator_fixed' ) if scalar @errors;

    my $questionnaire = $c->model( 'DB::Questionnaire' )->find_or_new(
        {
            problem_id => $c->stash->{problem},
            old_state  => 'confirmed',
            new_state  => 'fixed',
        }
    );

    unless ( $questionnaire->in_storage ) {
        $questionnaire->ever_reported( $c->stash->{reported} eq 'Yes' ? 1 : 0 );
        $questionnaire->whensent( \'ms_current_timestamp()' );
        $questionnaire->whenanswered( \'ms_current_timestamp()' );
        $questionnaire->insert;
    }

    $c->stash->{creator_fixed} = 1;
    $c->stash->{template} = 'tokens/confirm_update.html';

    return 1;
}

sub submit_standard : Private {
    my ( $self, $c ) = @_;

    $c->forward( '/tokens/load_questionnaire_id', [ $c->req->params->{token} ] );
    $c->forward( 'load_questionnaire' );

    my $problem = $c->stash->{problem};

    # 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 = 1 if $c->stash->{reported} eq 'Yes';
    $reported = 0 if $c->stash->{reported} eq 'No';

    my $q = $c->stash->{questionnaire};
    $q->whenanswered( \'ms_current_timestamp()' );
    $q->ever_reported( $reported );
{'fraction_non_alphanumeric': 0.1765812859383168, 'fraction_numerical': 0.002300052273915316, 'mean_word_length': 3.117950925527335, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 239, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6075088', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3147, 'n_words': 740}