# Type: Input and output (event based) # Category: Task # Description: Handles very long task (longer than 24 hours) # Events: status # Uses: Done # Timeout: 30 min # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; $message = Orangutan::Context::Random( 'Your current task lasts for more than 24 hours! ', 'The task exceeds time limit which is 24 hours... ', "It is more than 24 hours from the task's start time! " ); $message .= Orangutan::Context::Random( 'Please tell me its end time.', 'At what time did you finish the task?', 'Please specify the end time for the task.' ); return $message; }, response => '^([0-9]{1,2}(?:[:-][0-9]{2})? *(?:(?:a\.?|p\.?)m\.?)?)!*\.*$', handler => sub { my ($context, $user, $item, $time) = @_; my $done = $user->GetResponse('Done'); if ($done) { $done->CallHandler($user, $item, $time); } return undef; }, ontimeout => sub { my ($context, $user) = @_; my $task = $user->GetTask; if ($task) { $task->SetEnd(new Orangutan::Date($task->GetStart->Date + (86400 - 1))); if ($main::query->GetEntries($user, $task->GetStart, $task->GetEnd) == 0) { my $message = Orangutan::Context::Random('Ok...', 'Well...', 'Okay...'); my $format = Orangutan::Context::Random( 'Stopping it at %02d:%02d that is in 24 hours...', 'Will use %02d:%02d (24 hours after the beginning)...', 'Taking the start time plus 24 hours as the end time of the task (that is %02d:%02d)...' ); $message .= ' '.sprintf($format, $task->GetEnd->GetTime); if (!defined($task->GetActivity('id'))) { my $default_activity = $main::query->GetDefaultActivity($task->GetProject('id')); if (defined($default_activity)) { $task->SetActivity( id => $default_activity->{'id'}, name => $default_activity->{'name'} ); my $format = Orangutan::Context::Random( 'Using activity "%s"...', 'The activity is "%s"...' ); $message .= ' '.sprintf($format, $default_activity->{'name'}); } } if (defined($task->GetActivity('id'))) { if ($task->GetStart->WorkDayStart->Date != $task->GetEnd->WorkDayStart->Date) { my $copy = new Orangutan::Task($task); $task->SetEnd($task->GetStart->WorkDayEnd); $user->SubmitTask; $user->SetTask($copy); $copy->SetStart($copy->GetEnd->WorkDayStart); $user->SubmitTask; } else { $user->SubmitTask; } $user->FireEvent('task', 'end', $task); $user->SendMessage($message); } else { $user->SendMessage([ 'Just note that something is wrong with the activities in this task...', 'Hm... There going to be troubles with stopping this task...' ]); } } else { $user->SendMessage([ 'In such cases I stop the task automatically... But not this time... '. 'There are other tasks in Redmine which conflict with this one! So please stop it!', "Was going to stop it... But found that the task conflicts with some other tasks... ". "So it's up to you to stop it and specify correct time!" ]); } } return undef; }, onstatus => sub { my ($context, $user, $type, $status) = @_; my $task = $user->GetTask; if (defined($task)) { my $previous = $user->GetStatus; if (!defined($previous) || (defined($status) && ($previous eq 'away') && ($status eq 'online'))) { my $time = new Orangutan::Date; my $entry = defined($previous) ? $user->GetHistoryEntry : $user->GetHistoryEntry('away'); if ((defined($entry) && ($entry->{'date'}->Date - $task->GetStart->Date) >= 86400) || (!defined($entry) && ($time->Date - $task->GetStart->Date) >= 86400)) { if (!$user->GetResponse($context->GetID)) { $user->AddRequestResponse($context); } } } } }, ontask => sub { my ($context, $user, $type, $status) = @_; if (($status ne 'add') && ($status ne 'change') && ($status ne 'remove') && (($status ne 'break') || ($user->GetTask->GetStart->Elapsed < 86400))) { if ($user->GetResponse($context->GetID)) { $user->RemoveRequestResponse($context); } } }, schedule => sub { my ($context, $scheduler, $arg) = @_; if ($arg eq 'check') { foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); my $task = $user->GetTask; if (defined($task) && ($task->GetStart->Elapsed >= 86400)) { if (!$user->GetResponse($context->GetID)) { $user->AddRequestResponse($context); } } } } $scheduler->Schedule(time + 28800, $context, 'check'); # every 8 hours }, timeout => 1800, weight => undef ); # kate: syntax perl