# Type: Input # Category: Report # Description: Show tasks report - how much time was spent for each task # Author: Andriy Lesyuk sub TasksReport_DefaultPeriod($$) { my ($message, $result) = @_; # Base on work day my $now = time; if ((localtime($now))[2] < 6) { $now -= 21600; } # Previous month my $time = new Orangutan::Date($now); my $prev = $time->MonthStart->Previous; my $start = $prev->MonthStart->Date; my $end = $prev->MonthEnd->Date; push(@{$result}, new Orangutan::Date($start + 21600)); push(@{$result}, new Orangutan::Date($end + 21600)); } new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); # Remove ending $message =~ s/\?*!*\.*$//; my @regexps = ( '(?:(?:Show|give)(?: me)? )?(?:tasks )?report (?:of|for) ([^ ]+)', '(?:(?:Show|give)(?: me)? )?(?:(?:my|([^ ]+?)(?:\'s)?) )?(?:tasks )?report' ); # Check base query for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /^$regexps[$i] */i) { if (defined($1)) { my $id = $main::query->UserExists($1); if ($id) { push(@result, 1); push(@result, $id); $message =~ s/^$regexps[$i] *//i; last; } } else { push(@result, 1); push(@result, undef); $message =~ s/^$regexps[$i] *//i; last; } } } if (scalar @result > 0) { ShowTasks_ParsePeriod(\$message, \@result); if (scalar @result == 2) { TasksReport_DefaultPeriod(\$message, \@result); } ShowTasks_ParseFilter(\$message, \@result); if ($message !~ /^ *$/) { @result = ( ); } } return @result; }, handler => sub { my ($context, $manager, $item, $userid, $start, $end, $project, $issue, $activity) = @_; my $user; if (!defined($userid) || ($userid == $manager->GetUserID)) { $userid = $manager->GetUserID; $user = $manager; } elsif (!$manager->IsCreator && !$manager->IsManager) { $manager->SendMessage([ "Why do you care? Are you manager of this user? Anyway I won't show you this report!", "I'm not going to show you report for this user...", 'Ask the user!' ]); return; } else { $user = $main::users->GetUserID($userid); } if (!defined($start) || !defined($end)) { $manager->SendMessage([ 'You made a mistake in the request...', 'There is an error in the request...', 'Invalid request!' ]); return; } if (defined($issue) && defined($activity)) { $manager->SendMessage([ 'All issues are shown and activity is ignored in this report...', 'I show all issues or nothing! And I always ignore activity...', 'Ignoring the issue ID and the activity...' ]); } elsif (defined($issue)) { $manager->SendMessage([ 'This report was designed to show tasks for all available issues!', "Can't filter data by issue - maybe you need another report...", 'Ignoring the issue...' ]); } elsif (defined($activity)) { $manager->SendMessage([ 'The activity is not taken into account in this report!', 'This report ignores activity...', 'Ignoring the activity...' ]); } $start->Set($start->Date - 21600); $end->Set($end->Date - 21600); my $projectid = defined($project) ? $project->{'id'} : undef; my %report = Orangutan::User::GetTasksReport(defined($user) ? $user : $userid, $start, $end, $projectid); if (scalar keys %report > 0) { my $message = Orangutan::Context::Random( 'Here is the report', 'Showing report' ).' '; if (defined($project) || (scalar keys %report == 1)) { my $projectid = (keys %report)[0]; $message .= 'for '.$report{$projectid}->{'name'}.' '; } my ($sday, $smon, $syear, $swday) = (localtime($start->Date))[3..6]; my ($eday, $emon, $eyear, $ewday) = (localtime($end->Date))[3..6]; $message .= sprintf('from %d %s %d (%s) to %d %s %d (%s)', $sday, $Orangutan::Date::months[$smon], $syear + 1900, $Orangutan::Date::weekdays_short[$swday], $eday, $Orangutan::Date::months[$emon], $eyear + 1900, $Orangutan::Date::weekdays_short[$ewday]); $message .= ":\n"; foreach my $projectid (sort { $report{$a}->{'spent_on'} <=> $report{$b}->{'spent_on'} } keys %report) { if (!defined($project) && (scalar keys %report > 1)) { $message .= "\n"; $message .= 'Project "'; $message .= $report{$projectid}->{'name'}; $message .= '" - '; $message .= sprintf('%d:%02d', int($report{$projectid}->{'duration'} / 3600), int(($report{$projectid}->{'duration'} % 3600) / 60)); $message .= " (100%):\n"; } foreach my $issueid (sort { ($b == 0) <=> ($a == 0) || $report{$projectid}->{'issues'}->{$a}->{'spent_on'} <=> $report{$projectid}->{'issues'}->{$b}->{'spent_on'} } keys %{$report{$projectid}->{'issues'}}) { if ($issueid > 0) { $message .= $report{$projectid}->{'issues'}->{$issueid}->{'tracker'}; $message .= sprintf(" #%d: ", $issueid); $message .= $report{$projectid}->{'issues'}->{$issueid}->{'subject'}; $message .= ' - '; $message .= sprintf('%d:%02d', int($report{$projectid}->{'issues'}->{$issueid}->{'duration'} / 3600), int(($report{$projectid}->{'issues'}->{$issueid}->{'duration'} % 3600) / 60)); $message .= sprintf(' (%.4g%%)', ($report{$projectid}->{'issues'}->{$issueid}->{'duration'} * 100) / $report{$projectid}->{'duration'}); $message .= ":\n"; } else { $message .= 'Without issue - '; $message .= sprintf('%d:%02d', int($report{$projectid}->{'issues'}->{$issueid}->{'duration'} / 3600), int(($report{$projectid}->{'issues'}->{$issueid}->{'duration'} % 3600) / 60)); $message .= sprintf(' (%.4g%%)', ($report{$projectid}->{'issues'}->{$issueid}->{'duration'} * 100) / $report{$projectid}->{'duration'}); $message .= ":\n"; } foreach my $task (@{$report{$projectid}->{'issues'}->{$issueid}->{'tasks'}}) { $message .= ' o '; $message .= $task->{'description'}; $message .= ' - '; $message .= sprintf('%d:%02d', int($task->{'duration'} / 3600), int(($task->{'duration'} % 3600) / 60)); $message .= sprintf(' (%.4g%%)', ($task->{'duration'} * 100) / $report{$projectid}->{'duration'}); $message .= "\n"; } } } $manager->SendMessage($message); } else { $manager->SendMessage([ 'Cannot find any tasks...', 'Nothing to show.', 'No tasks.' ]); } }, help => { title => 'How can I list task names?', question => '^(?:How )?(?:(?:can|do) I |to )(?:list|check) (?:(?:my|the) )?tasks? names'. '(?: (?:during|for|at) some period)?\?*!*\.*$', answer => "For some reports it can be useful to list all task names you worked on. ". "Orangutan can show you such report for today, yesterday, current week, last month or any other period.\n". "Samples:\n". " o Show report for previous week\n". " o My tasks report for August\n". " o Report for this month\n". " o Show me tasks report\n". " o Report\n". "You can optionally specify project name:\n". " o Report for Orangutan\n". "If no period is specified Orangutan shows report for the previous month.", weight => 750 } ); # kate: syntax perl