# Type: Input # Category: Report # Description: Show activities report - how much time was spent for each activity # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); # Prepare tracker regexp my $regexp = 'issue'; my $trackers = $main::config->Get('redmine', '_trackers_regexp'); if ($trackers) { $regexp = "(?:issue|$trackers)"; } my @regexps = ( 'How (?:much time|many hours) (?:did )?(?:I|([^ ]+)) spen[td] for (?:each )?activit(?:y|ies) on '. '(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)\?*!*\.*', '(?:(?:Show|give|generate)(?: me)? )?(?:(?:my|([^ ]+?)(?:\'s)?) )?activities report for '. '(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*', '(?:(?:Show|give|generate)(?: me)? )?(?:(?:my|([^ ]+?)(?:\'s)?) )?report for '. '(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*' ); # Extract issue ID foreach my $regexp (@regexps) { if ($message =~ /^$regexp$/i) { push(@result, 1); push(@result, $1); push(@result, $2); last; } } return @result; }, handler => sub { my ($context, $user, $item, $userid, $issueid) = @_; if (defined($userid)) { if (!$user->IsCreator && !$user->IsManager && ($userid ne $user->GetJID) && ($userid ne $user->GetJabberUserID)) { if (defined($userid = $main::query->UserExists($userid))) { $user->SendMessage([ "Can't give you this report without user's approval... And not able to get user's approval! ;)", "Well... Use Redmine! Maybe Redmine will show you this data - I won't!", 'Why do you care?!' ]); } else { $user->SendMessage([ "Can't understand what do you want from me...", 'Sorry, I did not understand the request...', 'I do not get it! Please check the request.' ]); } return; } elsif (!($userid = $main::query->UserExists($userid))) { $user->SendMessage([ 'Check username! I cannot find such user...', 'I guess you made a mistake in username...', 'There is no such user!' ]); return; } } else { $userid = $user->GetUserID; } my $issue = $main::query->GetIssue($issueid); if (!defined($issue)) { $user->SendMessage([ 'Issue with this number does not exist!', 'You made a mistake in issue number...', 'There is no such issue!' ]); return; } my @activities = ( ); if ($main::query->GetActivitiesReport($userid, $issueid) > 0) { while (defined(my $activity = $main::query->Next)) { push(@activities, $activity); } } if (scalar @activities > 0) { my $message = Orangutan::Context::Random( 'The report is', 'Here it goes', 'Catch it' ); $message .= ":\n"; for (my $i = 0; $i < scalar @activities; $i++) { if ($i == $#activities) { if ($i > 1) { $message .= 'Total: '; } else { last; } } else { $message .= ' o '; if (defined($activities[$i]->{'name'})) { $message .= $activities[$i]->{'name'}; } else { $message .= 'No activity'; } $message .= ' - '; } $message .= sprintf('%d:%02d', int($activities[$i]->{'hours'} / 3600), int(($activities[$i]->{'hours'} % 3600) / 60)); $message .= ' / '; my $days = 0; my $employee = $main::users->GetUserID($userid); if ($employee) { $days = scalar $employee->GetWorkDays(new Orangutan::Date($activities[$i]->{'start_date'}), new Orangutan::Date($activities[$i]->{'end_date'})); } else { $days = scalar Orangutan::Date::GetWorkDays(new Orangutan::Date($activities[$i]->{'start_date'}), new Orangutan::Date($activities[$i]->{'end_date'})); } if ($days > 1) { $message .= sprintf("%d days", $days); } else { $message .= '1 day'; } $message .= ' ('; if ($activities[$i]->{'start_date'} == $activities[$i]->{'end_date'}) { my ($day, $mon) = (localtime($activities[$i]->{'start_date'}))[3..4]; $message .= sprintf('%s %d', $Orangutan::Date::months_short[$mon], $day); } else { my ($sday, $smon, $syear) = (localtime($activities[$i]->{'start_date'}))[3..5]; my ($eday, $emon, $eyear) = (localtime($activities[$i]->{'end_date'}))[3..5]; if ($syear == $eyear) { $message .= sprintf('%s %d - %s %d', $Orangutan::Date::months_short[$smon], $sday, $Orangutan::Date::months_short[$emon], $eday); } else { $message .= sprintf('%s %d, %04d - %s %d, %04d', $Orangutan::Date::months_short[$smon], $sday, $syear + 1900, $Orangutan::Date::months_short[$emon], $eday, $eyear + 1900); } } $message .= ")\n"; } $user->SendMessage($message); } else { $user->SendMessage([ 'Try logging to this issue first! ;)', 'No activities on this issue...', 'Nothing to show...' ]); } }, help => { title => 'How can I check how much time did I spend on activities of issue?', question => [ '^(?:(?:How )?(?:(?:can|do) I |to )(?:check|see) )?how (?:much|many) (?:time|hours) '. '(?:did )?I spen[td] (?:on|for) (?:each )?activit(?:y|ies)(?: of (?:(?:some|a) )?issue)?\?*!*\.*$', '^(?:How )?(?:(?:can|do) I |to )(?:check|get|see) (?:(?:my|the) )?activit(?:y|ies) '. 'report(?: for (?:(?:some|a) )?issue)?\?*!*\.*$' ], answer => "Usually activities reflect different phases of research and development process. ". "It may be needed to generate a report containing details about time spent for each activity. ". "Orangutan can be used to generate such report.\n". "Here are samples:\n". " o How many hours did I spend for each activity on #256?\n". " o Generate activities report for bug #63\n". " o Show me report for #148", weight => 730 } ); # kate: syntax perl