diff options
Diffstat (limited to 'perllib/FixMyStreet/Script')
-rw-r--r-- | perllib/FixMyStreet/Script/Alerts.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm | 53 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Questionnaires.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Reports.pm | 31 | ||||
-rwxr-xr-x | perllib/FixMyStreet/Script/UpdateAllReports.pm | 38 |
5 files changed, 96 insertions, 46 deletions
diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index 4b5641f9e..55f4b3db5 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -8,7 +8,7 @@ use IO::String; use FixMyStreet::Gaze; use mySociety::Locale; -use mySociety::MaPit; +use FixMyStreet::MapIt; use RABX; use FixMyStreet::Cobrand; @@ -185,12 +185,15 @@ sub send() { # Get a report object for its photo and static map $data{report} = $schema->resultset('Problem')->find({ id => $row->{id} }); } - if ($ref eq 'area_problems' || $ref eq 'council_problems' || $ref eq 'ward_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter}); + if ($ref eq 'area_problems') { + my $va_info = FixMyStreet::MapIt::call('area', $row->{alert_parameter}); $data{area_name} = $va_info->{name}; + } elsif ($ref eq 'council_problems' || $ref eq 'ward_problems') { + my $body = FixMyStreet::DB->resultset('Body')->find({ id => $row->{alert_parameter} }); + $data{area_name} = $body->name; } if ($ref eq 'ward_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter2}); + my $va_info = FixMyStreet::MapIt::call('area', $row->{alert_parameter2}); $data{ward_name} = $va_info->{name}; } } diff --git a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm index 03bc511a0..0c938682d 100644 --- a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm +++ b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm @@ -1,11 +1,8 @@ package FixMyStreet::Script::ArchiveOldEnquiries; -use strict; +use v5.14; use warnings; -require 5.8.0; -use FixMyStreet; -use FixMyStreet::App; use FixMyStreet::DB; use FixMyStreet::Cobrand; use FixMyStreet::Map; @@ -17,17 +14,17 @@ my $opts = { }; sub query { - return { - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, - -and => [ + my $rs = shift; + return $rs->to_body($opts->{body})->search({ + -and => [ lastupdate => { '<', $opts->{email_cutoff} }, lastupdate => { '>', $opts->{closure_cutoff} }, ], - state => [ FixMyStreet::DB::Result::Problem->open_states() ], - }; + state => [ FixMyStreet::DB::Result::Problem->open_states() ], + }); } -sub archive { +sub update_options { my $params = shift; if ( $params ) { $opts = { @@ -35,13 +32,19 @@ sub archive { %$params, }; } +} + +sub archive { + my $params = shift; + update_options($params); unless ( $opts->{commit} ) { printf "Doing a dry run; emails won't be sent and reports won't be closed.\n"; printf "Re-run with --commit to actually archive reports.\n\n"; } - my @user_ids = FixMyStreet::DB->resultset('Problem')->search(query(), + my $rs = FixMyStreet::DB->resultset('Problem'); + my @user_ids = query($rs)->search(undef, { distinct => 1, columns => ['user_id'], @@ -55,7 +58,7 @@ sub archive { }); my $user_count = $users->count; - my $problem_count = FixMyStreet::DB->resultset('Problem')->search(query(), + my $problem_count = query($rs)->search(undef, { columns => ['id'], rows => $opts->{limit}, @@ -71,8 +74,7 @@ sub archive { } } - my $problems_to_close = FixMyStreet::DB->resultset('Problem')->search({ - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, + my $problems_to_close = $rs->to_body($opts->{body})->search({ lastupdate => { '<', $opts->{closure_cutoff} }, state => [ FixMyStreet::DB::Result::Problem->open_states() ], }, { @@ -87,7 +89,8 @@ sub archive { sub send_email_and_close { my ($user) = @_; - my $problems = $user->problems->search(query(), { + my $problems = $user->problems; + $problems = query($problems)->search(undef, { order_by => { -desc => 'confirmed' }, }); @@ -135,22 +138,36 @@ sub close_problems { return unless $opts->{commit}; my $problems = shift; + + my $extra = { auto_closed_by_script => 1 }; + $extra->{is_superuser} = 1 if !$opts->{user_name}; + + my $cobrand; while (my $problem = $problems->next) { + # need to do this in case no reports were closed with an + # email in which case we won't have set the lang and domain + if ($opts->{cobrand} && !$cobrand) { + $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($opts->{cobrand})->new(); + $cobrand->set_lang_and_domain($problem->lang, 1); + } + my $timestamp = \'current_timestamp'; my $comment = $problem->add_to_comments( { - text => '', + text => $opts->{closure_text} || '', created => $timestamp, confirmed => $timestamp, user_id => $opts->{user}, - name => _('an administrator'), + name => $opts->{user_name} || _('an administrator'), mark_fixed => 0, anonymous => 0, state => 'confirmed', problem_state => 'closed', - extra => { is_superuser => 1 }, + extra => $extra, } ); $problem->update({ state => 'closed', send_questionnaire => 0 }); + next if $opts->{retain_alerts}; + # Stop any alerts being sent out about this closure. my @alerts = FixMyStreet::DB->resultset('Alert')->search( { alert_type => 'new_updates', diff --git a/perllib/FixMyStreet/Script/Questionnaires.pm b/perllib/FixMyStreet/Script/Questionnaires.pm index 5fc01512d..aab4b9b75 100644 --- a/perllib/FixMyStreet/Script/Questionnaires.pm +++ b/perllib/FixMyStreet/Script/Questionnaires.pm @@ -43,14 +43,19 @@ sub send_questionnaires_period { while (my $row = $unsent->next) { - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); + my $cobrand = $row->get_cobrand_logged; $cobrand->set_lang_and_domain($row->lang, 1); FixMyStreet::Map::set_map_class($cobrand->map_type); # Not all cobrands send questionnaires next unless $cobrand->send_questionnaires; - if ($row->is_from_abuser || !$row->user->email_verified) { + # Cobrands can also override sending per row if they wish + my $cobrand_send = $cobrand->call_hook('send_questionnaire', $row) // 1; + + if ($row->is_from_abuser || !$row->user->email_verified || + !$cobrand_send || $row->is_closed + ) { $row->update( { send_questionnaire => 0 } ); next; } diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index 578d966d6..ecd461cd9 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -8,7 +8,6 @@ use DateTime::Format::Pg; use Utils; use Utils::OpenStreetMap; -use mySociety::MaPit; use FixMyStreet; use FixMyStreet::Cobrand; @@ -44,7 +43,7 @@ sub send(;$) { debug_print("starting to loop through unsent problem reports...") if $debug_mode; while (my $row = $unsent->next) { - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); + my $cobrand = $row->get_cobrand_logged; FixMyStreet::DB->schema->cobrand($cobrand); if ($debug_mode) { @@ -127,10 +126,19 @@ sub send(;$) { $missing = join(' / ', @missing) if @missing; } + my $send_confirmation_email = $cobrand->report_sent_confirmation_email; + my @dear; my %reporters = (); my $skip = 0; while (my $body = $bodies->next) { + # See if this body wants confirmation email (in case report made on national site, for example) + if (my $cobrand_body = $body->get_cobrand_handler) { + if (my $id_ref = $cobrand_body->report_sent_confirmation_email) { + $send_confirmation_email = $id_ref; + } + } + my $sender_info = $cobrand->get_body_sender( $body, $row->category ); my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; @@ -140,7 +148,9 @@ sub send(;$) { } $reporters{ $sender } ||= $sender->new(); - my $inspection_required = $sender_info->{contact}->get_extra_metadata('inspection_required') if $sender_info->{contact}; + my $inspection_required = $sender_info->{contact} + ? $sender_info->{contact}->get_extra_metadata('inspection_required') + : undef; if ( $inspection_required ) { my $reputation_threshold = $sender_info->{contact}->get_extra_metadata('reputation_threshold') || 0; my $reputation_threshold_met = 0; @@ -211,12 +221,13 @@ sub send(;$) { # Multiply results together, so one success counts as a success. my $result = -1; + my @methods; for my $sender ( keys %reporters ) { debug_print("sending using " . $sender, $row->id) if $debug_mode; $sender = $reporters{$sender}; my $res = $sender->send( $row, \%h ); $result *= $res; - $row->add_send_method($sender) if !$res; + push @methods, $sender if !$res; if ( $sender->unconfirmed_counts) { foreach my $e (keys %{ $sender->unconfirmed_counts } ) { foreach my $c (keys %{ $sender->unconfirmed_counts->{$e} }) { @@ -229,12 +240,19 @@ sub send(;$) { if FixMyStreet->test_mode && $sender->can('open311_test_req_used'); } + # Add the send methods now because e.g. Open311 + # send() calls $row->discard_changes + foreach (@methods) { + $row->add_send_method($_); + } + unless ($result) { $row->update( { whensent => \'current_timestamp', lastupdate => \'current_timestamp', } ); - if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { + if ($send_confirmation_email && !$h{anonymous_report}) { + $h{sent_confirm_id_ref} = $row->$send_confirmation_email; _send_report_sent_email( $row, \%h, $nomail, $cobrand ); } debug_print("send successful: OK", $row->id) if $debug_mode; @@ -274,7 +292,7 @@ sub send(;$) { } ); while (my $row = $unsent->next) { my $base_url = FixMyStreet->config('BASE_URL'); - $sending_errors .= "* " . $base_url . "/report/" . $row->id . ", failed " + $sending_errors .= "\n" . '=' x 80 . "\n\n" . "* " . $base_url . "/report/" . $row->id . ", failed " . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp . ", reason " . $row->send_fail_reason . "\n"; } @@ -304,7 +322,6 @@ sub _send_report_sent_email { $h, { To => $row->user->email, - From => [ $cobrand->contact_email, $cobrand->contact_name ], }, undef, $nomail, diff --git a/perllib/FixMyStreet/Script/UpdateAllReports.pm b/perllib/FixMyStreet/Script/UpdateAllReports.pm index 21d8d28a0..33665b9da 100755 --- a/perllib/FixMyStreet/Script/UpdateAllReports.pm +++ b/perllib/FixMyStreet/Script/UpdateAllReports.pm @@ -99,13 +99,14 @@ sub generate { } sub end_period { - my $period = shift; - FixMyStreet->set_time_zone(DateTime->now)->truncate(to => $period)->add($period . 's' => 1)->subtract(seconds => 1); + my ($period, $end) = @_; + $end ||= DateTime->now; + FixMyStreet->set_time_zone($end)->truncate(to => $period)->add($period . 's' => 1)->subtract(seconds => 1); } sub loop_period { - my ($date, $period, $extra) = @_; - my $end = end_period($period); + my ($date, $extra, $period, $end) = @_; + $end = end_period($period, $end); my @out; while ($date <= $end) { push @out, { n => $date->$period, $extra ? (d => $date->$extra) : () }; @@ -114,6 +115,21 @@ sub loop_period { return @out; } +sub get_period_group { + my ($start, $end) = @_; + my ($group_by, $extra); + if (DateTime::Duration->compare($end - $start, DateTime::Duration->new(months => 1)) < 0) { + $group_by = 'day'; + } elsif (DateTime::Duration->compare($end - $start, DateTime::Duration->new(years => 1)) < 0) { + $group_by = 'month'; + $extra = 'month_abbr'; + } else { + $group_by = 'year'; + } + + return ($group_by, $extra); +} + sub generate_dashboard { my $body = shift; @@ -138,16 +154,8 @@ sub generate_dashboard { $min_confirmed = FixMyStreet->set_time_zone(DateTime->now)->truncate(to => 'day'); } - my ($group_by, $extra); - if (DateTime::Duration->compare($end_today - $min_confirmed, DateTime::Duration->new(months => 1)) < 0) { - $group_by = 'day'; - } elsif (DateTime::Duration->compare($end_today - $min_confirmed, DateTime::Duration->new(years => 1)) < 0) { - $group_by = 'month'; - $extra = 'month_abbr'; - } else { - $group_by = 'year'; - } - my @problem_periods = loop_period($min_confirmed, $group_by, $extra); + my ($group_by, $extra) = get_period_group($min_confirmed, $end_today); + my @problem_periods = loop_period($min_confirmed, $extra, $group_by); my %problems_reported_by_period = stuff_by_day_or_year( $group_by, $rs, @@ -261,7 +269,7 @@ sub calculate_top_five_bodies { my $bodies = FixMyStreet::DB->resultset('Body')->search; while (my $body = $bodies->next) { - my $avg = $body->calculate_average; + my $avg = $body->calculate_average($cobrand_cls->call_hook("body_responsiveness_threshold")); push @top_five_bodies, { name => $body->name, days => int($avg / 60 / 60 / 24 + 0.5) } if defined $avg; } |