diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Dashboard.pm | 67 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/BathNES.pm | 28 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 23 | ||||
-rw-r--r-- | t/app/controller/dashboard.t | 32 | ||||
-rw-r--r-- | t/cobrand/bathnes.t | 19 | ||||
-rw-r--r-- | templates/web/base/dashboard/index.html | 6 |
8 files changed, 160 insertions, 20 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 602bae632..0caa88003 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## Releases * Unreleased + - New features: + - Dashboard now has update CSV export. #2249 + - Front end improvements: - Clearer relocation options while you’re reporting a problem #2238 * v2.4.1 (2nd October 2018) diff --git a/perllib/FixMyStreet/App/Controller/Dashboard.pm b/perllib/FixMyStreet/App/Controller/Dashboard.pm index 65f0452c5..14042732e 100644 --- a/perllib/FixMyStreet/App/Controller/Dashboard.pm +++ b/perllib/FixMyStreet/App/Controller/Dashboard.pm @@ -112,10 +112,14 @@ sub index : Path : Args(0) { $c->stash->{end_date} = $c->get_param('end_date'); $c->stash->{q_state} = $c->get_param('state') || ''; - $c->forward('construct_rs_filter'); + $c->forward('construct_rs_filter', [ $c->get_param('updates') ]); if ( $c->get_param('export') ) { - $c->forward('export_as_csv'); + if ($c->get_param('updates')) { + $c->forward('export_as_csv_updates'); + } else { + $c->forward('export_as_csv'); + } } else { $c->forward('generate_grouped_data'); $self->generate_summary_figures($c); @@ -123,7 +127,7 @@ sub index : Path : Args(0) { } sub construct_rs_filter : Private { - my ($self, $c) = @_; + my ($self, $c, $updates) = @_; my %where; $where{areas} = { 'like', '%,' . $c->stash->{ward} . ',%' } @@ -131,28 +135,31 @@ sub construct_rs_filter : Private { $where{category} = $c->stash->{category} if $c->stash->{category}; + my $table_name = $updates ? 'problem' : 'me'; + my $state = $c->stash->{q_state}; if ( FixMyStreet::DB::Result::Problem->fixed_states->{$state} ) { # Probably fixed - council - $where{'me.state'} = [ FixMyStreet::DB::Result::Problem->fixed_states() ]; + $where{"$table_name.state"} = [ FixMyStreet::DB::Result::Problem->fixed_states() ]; } elsif ( $state ) { - $where{'me.state'} = $state; + $where{"$table_name.state"} = $state; } else { - $where{'me.state'} = [ FixMyStreet::DB::Result::Problem->visible_states() ]; + $where{"$table_name.state"} = [ FixMyStreet::DB::Result::Problem->visible_states() ]; } my $dtf = $c->model('DB')->storage->datetime_parser; my $start_date = $dtf->parse_datetime($c->stash->{start_date}); - $where{'me.confirmed'} = { '>=', $dtf->format_datetime($start_date) }; + $where{"$table_name.confirmed"} = { '>=', $dtf->format_datetime($start_date) }; if (my $end_date = $c->stash->{end_date}) { my $one_day = DateTime::Duration->new( days => 1 ); $end_date = $dtf->parse_datetime($end_date) + $one_day; - $where{'me.confirmed'} = [ -and => $where{'me.confirmed'}, { '<', $dtf->format_datetime($end_date) } ]; + $where{"$table_name.confirmed"} = [ -and => $where{"$table_name.confirmed"}, { '<', $dtf->format_datetime($end_date) } ]; } $c->stash->{params} = \%where; - $c->stash->{objects_rs} = $c->cobrand->problems->to_body($c->stash->{body})->search( \%where ); + my $rs = $updates ? $c->cobrand->updates : $c->cobrand->problems; + $c->stash->{objects_rs} = $rs->to_body($c->stash->{body})->search( \%where ); } sub generate_grouped_data : Private { @@ -275,7 +282,7 @@ sub generate_body_response_time : Private { } sub csv_filename { - my ($self, $c) = @_; + my ($self, $c, $updates) = @_; my %where = ( category => $c->stash->{category}, state => $c->stash->{q_state}, @@ -284,12 +291,34 @@ sub csv_filename { $where{body} = $c->stash->{body}->id if $c->stash->{body}; join '-', $c->req->uri->host, + $updates ? ('updates') : (), map { my $value = $where{$_}; (defined $value and length $value) ? ($_, $value) : () } sort keys %where }; +sub export_as_csv_updates : Private { + my ($self, $c) = @_; + + my $csv = $c->stash->{csv} = { + objects => $c->stash->{objects_rs}->search_rs({}, { + order_by => ['me.confirmed', 'me.id'], + }), + headers => [ + 'Report ID', 'Update ID', 'Date', 'Status', 'Problem state', + 'Text', 'User Name', 'Reported As', + ], + columns => [ + 'problem_id', 'id', 'confirmed', 'state', 'problem_state', + 'text', 'user_name_display', 'reported_as', + ], + filename => $self->csv_filename($c, 1), + }; + $c->cobrand->call_hook("dashboard_export_updates_add_columns"); + $c->forward('generate_csv'); +} + sub export_as_csv : Private { my ($self, $c) = @_; @@ -340,9 +369,9 @@ sub export_as_csv : Private { 'site_used', 'reported_as', ], - filename => $self->csv_filename($c), + filename => $self->csv_filename($c, 0), }; - $c->cobrand->call_hook("dashboard_export_add_columns"); + $c->cobrand->call_hook("dashboard_export_problems_add_columns"); $c->forward('generate_csv'); } @@ -404,11 +433,17 @@ sub generate_csv : Private { split ',', $hashref->{areas}; } - ($hashref->{local_coords_x}, $hashref->{local_coords_y}) = - $obj->local_coords; - $hashref->{url} = join '', $c->cobrand->base_url_for_report($obj), $obj->url; + if ($obj->can('local_coords')) { + ($hashref->{local_coords_x}, $hashref->{local_coords_y}) = + $obj->local_coords; + } + if ($obj->can('url')) { + my $base = $c->cobrand->base_url_for_report($obj->can('problem') ? $obj->problem : $obj); + $hashref->{url} = join '', $base, $obj->url; + } + + $hashref->{site_used} = $obj->can('service') ? ($obj->service || $obj->cobrand) : $obj->cobrand; - $hashref->{site_used} = $obj->service || $obj->cobrand; $hashref->{reported_as} = $obj->get_extra_metadata('contributed_as') || ''; if (my $fn = $c->stash->{csv}->{extra_data}) { diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index ffc89c8b0..1ca4cbb09 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -461,7 +461,7 @@ sub summary : Private { $c->forward('/admin/fetch_contacts'); $c->stash->{contacts} = [ $c->stash->{contacts}->all ]; - $c->forward('/dashboard/construct_rs_filter'); + $c->forward('/dashboard/construct_rs_filter', []); if ( $c->get_param('csv') ) { $c->detach('export_summary_csv'); diff --git a/perllib/FixMyStreet/Cobrand/BathNES.pm b/perllib/FixMyStreet/Cobrand/BathNES.pm index d69853881..b341f7c5b 100644 --- a/perllib/FixMyStreet/Cobrand/BathNES.pm +++ b/perllib/FixMyStreet/Cobrand/BathNES.pm @@ -206,7 +206,33 @@ sub categories_restriction { ] } ); } -sub dashboard_export_add_columns { +sub dashboard_export_updates_add_columns { + my $self = shift; + my $c = $self->{c}; + + return unless $c->user->has_body_permission_to('export_extra_columns'); + + push @{$c->stash->{csv}->{headers}}, "Staff User"; + push @{$c->stash->{csv}->{headers}}, "User Email"; + push @{$c->stash->{csv}->{columns}}, "staff_user"; + push @{$c->stash->{csv}->{columns}}, "user_email"; + + $c->stash->{csv}->{extra_data} = sub { + my $report = shift; + + my $staff_user = ''; + if ( my $contributed_by = $report->get_extra_metadata('contributed_by') ) { + $staff_user = $c->model('DB::User')->find({ id => $contributed_by })->email; + } + + return { + user_email => $report->user->email || '', + staff_user => $staff_user, + }; + }; +} + +sub dashboard_export_problems_add_columns { my $self = shift; my $c = $self->{c}; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index b35c5fd0c..b4e42b456 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -296,4 +296,27 @@ sub hide { return $ret; } +sub as_hashref { + my ($self, $c, $cols) = @_; + + my $out = { + id => $self->id, + problem_id => $self->problem_id, + text => $self->text, + state => $self->state, + created => $self->created, + }; + + $out->{problem_state} = $self->problem_state_processed; + + $out->{photos} = [ map { $_->{url} } @{$self->photos} ] if !$cols || $cols->{photos}; + + if ($self->confirmed) { + $out->{confirmed} = $self->confirmed if !$cols || $cols->{confirmed}; + $out->{confirmed_pp} = $c->cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp}; + } + + return $out; +} + 1; diff --git a/t/app/controller/dashboard.t b/t/app/controller/dashboard.t index 598898e9f..3a031bec3 100644 --- a/t/app/controller/dashboard.t +++ b/t/app/controller/dashboard.t @@ -43,9 +43,13 @@ foreach my $problem (@fixed_problems) { $mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'fixed'); } +my $first_problem_id; +my $first_update_id; foreach my $problem (@closed_problems) { $problem->update({ state => 'closed' }); - $mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'closed', { confirmed => \'current_timestamp' }); + my ($update) = $mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'closed', { confirmed => \'current_timestamp' }); + $first_problem_id = $problem->id unless $first_problem_id; + $first_update_id = $update->id unless $first_update_id; } my $categories = scraper { @@ -186,6 +190,32 @@ FixMyStreet::override_config { is $rows[5]->[16], '179716', 'Correct Northing conversion'; }; + subtest 'export updates as csv' => sub { + $mech->get_ok('/dashboard?updates=1&export=1'); + open my $data_handle, '<', \$mech->content; + my $csv = Text::CSV->new( { binary => 1 } ); + my @rows; + while ( my $row = $csv->getline( $data_handle ) ) { + push @rows, $row; + } + is scalar @rows, 15, '1 (header) + 14 (updates) = 15 lines'; + is scalar @{$rows[0]}, 8, '8 columns present'; + + is_deeply $rows[0], + [ + 'Report ID', 'Update ID', 'Date', 'Status', 'Problem state', + 'Text', 'User Name', 'Reported As', + ], + 'Column headers look correct'; + + is $rows[1]->[0], $first_problem_id, 'Correct report ID'; + is $rows[1]->[1], $first_update_id, 'Correct update ID'; + is $rows[1]->[3], 'confirmed', 'Correct state'; + is $rows[1]->[4], 'closed', 'Correct problem state'; + is $rows[1]->[5], 'text', 'Correct text'; + is $rows[1]->[6], 'Title', 'Correct name'; + }; + subtest 'export as csv using token' => sub { $mech->log_out_ok; diff --git a/t/cobrand/bathnes.t b/t/cobrand/bathnes.t index 0a16fc96e..59e0d5246 100644 --- a/t/cobrand/bathnes.t +++ b/t/cobrand/bathnes.t @@ -191,6 +191,25 @@ subtest 'extra CSV columns are present if permission granted' => sub { is $rows[4]->[21], '', 'User phone number is correct'; is $rows[4]->[22], '', 'Staff User is empty if not made on behalf of another user'; is $rows[4]->[23], '', 'Attribute Data is correct'; + + $mech->get_ok('/dashboard?export=1&updates=1'); + + open $data_handle, '<', \$mech->content; + $csv = Text::CSV->new( { binary => 1 } ); + @rows = (); + while ( my $row = $csv->getline( $data_handle ) ) { + push @rows, $row; + } + + is scalar @rows, 1, '1 (header) + 0 (updates)'; + is scalar @{$rows[0]}, 10, '10 columns present'; + is_deeply $rows[0], + [ + 'Report ID', 'Update ID', 'Date', 'Status', 'Problem state', + 'Text', 'User Name', 'Reported As', 'Staff User', + 'User Email', + ], + 'Column headers look correct'; }; diff --git a/templates/web/base/dashboard/index.html b/templates/web/base/dashboard/index.html index 5377fa938..7c3966b04 100644 --- a/templates/web/base/dashboard/index.html +++ b/templates/web/base/dashboard/index.html @@ -102,7 +102,11 @@ <li>[% INCLUDE gb new_gb='month' text=loc('Month') %]</li> <li>[% INCLUDE gb new_gb='category+state' text=loc('Category and State') %]</li> <li>[% INCLUDE gb new_gb='device+site' text=loc('Device and Site') %]</li> - <li class="pull-right"><a href="[% c.uri_with({ export => 1 }) %]">[% loc('Export as CSV') %]</a></li> + <li class="pull-right"> + <span>[% loc('Export as CSV') %]:</span> + <a href="[% c.uri_with({ export => 1 }) %]">[% loc('Reports') %]</a> + <a href="[% c.uri_with({ export => 1, updates => 1 }) %]">[% loc('Updates') %]</a> + </li> </ul> <table width="100%" id="overview"> |