aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm
blob: 6a1ce0d782978272f6835d7a84bb2cfd838e097a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package FixMyStreet::DB::ResultSet::Questionnaire;
use base 'DBIx::Class::ResultSet';

use strict;
use warnings;

sub send_questionnaires {
    my ( $rs, $params ) = @_;
    require FixMyStreet::Script::Questionnaires;
    FixMyStreet::Script::Questionnaires::send($params);
}

sub timeline {
    my ( $rs, $restriction ) = @_;

    my $attrs;
    if (%$restriction) {
        $attrs = {
            -select => [qw/me.*/],
            prefetch => [qw/problem/],
        }
    }
    return $rs->search(
        {
            -or => {
                whenanswered => { '>=', \"current_timestamp-'7 days'::interval" },
                'me.whensent'  => { '>=', \"current_timestamp-'7 days'::interval" },
            },
            %{ $restriction },
        },
        $attrs
    );
}

sub summary_count {
    my ( $rs, $restriction ) = @_;

    my $params = {
        group_by => [ \'whenanswered is not null' ],
        select => [ \'(whenanswered is not null)', { count => 'me.id' } ],
        as => [qw/answered questionnaire_count/],
    };
    if (%$restriction) {
        $params->{join} = 'problem';
    }
    return $rs->search($restriction, $params);
}
1;