aboutsummaryrefslogtreecommitdiffstats
path: root/perllib
diff options
context:
space:
mode:
Diffstat (limited to 'perllib')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin.pm13
-rw-r--r--perllib/FixMyStreet/Cobrand/Zurich.pm54
-rw-r--r--perllib/FixMyStreet/DB/Result/AdminLog.pm2
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm12
-rw-r--r--perllib/FixMyStreet/TestMech.pm1
5 files changed, 77 insertions, 5 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm
index be705110b..5ebeffc11 100644
--- a/perllib/FixMyStreet/App/Controller/Admin.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin.pm
@@ -1339,13 +1339,24 @@ Adds an entry into the admin_log table using the current user.
=cut
sub log_edit : Private {
- my ( $self, $c, $id, $object_type, $action ) = @_;
+ my ( $self, $c, $id, $object_type, $action, $time_spent ) = @_;
+
+ $time_spent //= 0;
+ $time_spent = 0 if $time_spent < 0;
+
+ my $user_object = do {
+ my $auth_user = $c->user;
+ $auth_user ? $auth_user->get_object : undef;
+ };
+
$c->model('DB::AdminLog')->create(
{
admin_user => $c->forward('get_user'),
+ $user_object ? ( user => $user_object ) : (), # as (rel => undef) doesn't work
object_type => $object_type,
action => $action,
object_id => $id,
+ time_spent => $time_spent,
}
)->insert();
}
diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm
index 46d57158b..c039deb0d 100644
--- a/perllib/FixMyStreet/Cobrand/Zurich.pm
+++ b/perllib/FixMyStreet/Cobrand/Zurich.pm
@@ -5,6 +5,7 @@ use DateTime;
use POSIX qw(strcoll);
use RABX;
use Scalar::Util 'blessed';
+use DateTime::Format::Pg;
use strict;
use warnings;
@@ -277,13 +278,46 @@ sub get_or_check_overdue {
return $self->overdue($problem);
}
+=head1 C<set_problem_state>
+
+If the state has changed, sets the state and calls C::Admin's C<log_edit> action.
+If the state hasn't changed, defers to update_admin_log (to update time_spent if any).
+
+Returns either undef or the AdminLog entry created.
+
+=cut
+
sub set_problem_state {
my ($self, $c, $problem, $new_state) = @_;
- return if $new_state eq $problem->state;
+ return $self->update_admin_log($c, $problem) if $new_state eq $problem->state;
$problem->state( $new_state );
$c->forward( 'log_edit', [ $problem->id, 'problem', "state change to $new_state" ] );
}
+=head1 C<update_admin_log>
+
+Calls C::Admin's C<log_edit> if either a) text is provided, or b) there has
+been time_spent on the task. As set_problem_state will already call log_edit
+if required, don't call this as well.
+
+Returns either undef or the AdminLog entry created.
+
+=cut
+
+sub update_admin_log {
+ my ($self, $c, $problem, $text) = @_;
+
+ my $time_spent = ( ($c->req->param('time_spent') // 0) + 0 );
+ $c->req->param('time_spent' => 0); # explicitly zero this to avoid duplicates
+
+ if (!$text) {
+ return unless $time_spent;
+ $text = "Logging time_spent";
+ }
+
+ $c->forward( 'log_edit', [ $problem->id, 'problem', $text, $time_spent ] );
+}
+
# Specific administrative displays
sub admin_pages {
@@ -503,6 +537,7 @@ sub admin_report_edit {
$problem->whensent( undef );
$problem->set_extra_metadata(changed_category => 1);
$internal_note_text = "Weitergeleitet von $old_cat an $new_cat";
+ $self->update_admin_log($c, $problem, "Changed category from $old_cat to $new_cat");
$redirect = 1 if $cat->body_id ne $body->id;
} elsif ( my $subdiv = $c->get_param('body_subdivision') ) {
$problem->set_extra_metadata_if_undefined( moderated_overdue => $self->overdue( $problem ) );
@@ -576,6 +611,10 @@ sub admin_report_edit {
} );
}
+ # Just update if time_spent still hasn't been logged
+ # (this will only happen if no other update_admin_log has already been called)
+ $self->update_admin_log($c, $problem);
+
if ( $redirect ) {
$c->detach('index');
}
@@ -748,7 +787,10 @@ sub admin_stats {
if ($y && $m) {
$c->stash->{start_date} = DateTime->new( year => $y, month => $m, day => 1 );
$c->stash->{end_date} = $c->stash->{start_date} + DateTime::Duration->new( months => 1 );
- $date_params{created} = { '>=', $c->stash->{start_date}, '<', $c->stash->{end_date} };
+ $date_params{created} = {
+ '>=', DateTime::Format::Pg->format_datetime($c->stash->{start_date}),
+ '<', DateTime::Format::Pg->format_datetime($c->stash->{end_date}),
+ };
}
my %params = (
@@ -760,6 +802,8 @@ sub admin_stats {
my $problems = $c->model('DB::Problem')->search(
{%date_params},
{
+ join => 'admin_log_entries',
+ distinct => 1,
columns => [
'id', 'created',
'latitude', 'longitude',
@@ -771,10 +815,11 @@ sub admin_stats {
'whensent', 'lastupdate',
'service',
'extra',
- ],
+ { sum_time_spent => { sum => 'admin_log_entries.time_spent' } },
+ ]
}
);
- my $body = "Report ID,Created,Sent to Agency,Last Updated,E,N,Category,Status,UserID,External Body,Title,Detail,Media URL,Interface Used,Council Response\n";
+ my $body = "Report ID,Created,Sent to Agency,Last Updated,E,N,Category,Status,UserID,External Body,Time Spent,Title,Detail,Media URL,Interface Used,Council Response\n";
require Text::CSV;
my $csv = Text::CSV->new({ binary => 1 });
while ( my $report = $problems->next ) {
@@ -802,6 +847,7 @@ sub admin_stats {
$report->local_coords, $report->category,
$report->state, $report->user_id,
$body_name,
+ $report->get_column('sum_time_spent') || 0,
$report->title,
$detail,
$media_url,
diff --git a/perllib/FixMyStreet/DB/Result/AdminLog.pm b/perllib/FixMyStreet/DB/Result/AdminLog.pm
index fcf909692..d60915cfc 100644
--- a/perllib/FixMyStreet/DB/Result/AdminLog.pm
+++ b/perllib/FixMyStreet/DB/Result/AdminLog.pm
@@ -37,6 +37,8 @@ __PACKAGE__->add_columns(
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
"reason",
{ data_type => "text", default_value => "", is_nullable => 0 },
+ "time_spent",
+ { data_type => "integer", default_value => "0", is_nullable => 0 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to(
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index d3a30db4e..637f4acbf 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -886,6 +886,18 @@ __PACKAGE__->has_many(
}
);
+sub get_time_spent {
+ my $self = shift;
+ my $admin_logs = $self->admin_log_entries->search({},
+ {
+ group_by => 'object_id',
+ columns => [
+ { sum_time_spent => { sum => 'time_spent' } },
+ ]
+ })->single;
+ return $admin_logs ? $admin_logs->get_column('sum_time_spent') : 0;
+}
+
# we need the inline_constructor bit as we don't inherit from Moose
__PACKAGE__->meta->make_immutable( inline_constructor => 0 );
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index 8325b07a8..1035a47ba 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -165,6 +165,7 @@ sub delete_user {
$a->delete;
}
$_->delete for $user->comments;
+ $_->delete for $user->admin_logs;
$user->delete;
return 1;