# Type: Input and output (event based) # Category: Task # Description: Handles night tasks (for buggy Jabber clients) # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; if ($user->GetTask) { my $date = $context->GetField('date'); my $lastmessage = $user->GetLastMessageTime; if ((!defined($lastmessage) || (($date->Date - $lastmessage) >= 1800)) && ($date->Elapsed < 7200)) { my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $context->UnsetField('break'); $yesno->InsertForeignHandler($context, 1800); return Orangutan::Context::Random( 'Are you still there?', 'Are you there?', 'You there?' ); } } } return undef; }, handler => sub { my ($context, $user, $item, $answer) = @_; if ($answer eq 'Y') { $user->SendMessage([ 'Ok... ', 'Got it!.. ', 'I see... ' ]); } else { my $message = Orangutan::Context::Random('Kidding? :)', ':)', 'Ok... :)'); if ($user->GetTask) { $message .= ' '.Orangutan::Context::Random( 'Be sure to stop the task before leaving...', "Don't forget to stop the task!" ); } $user->SendMessage($message); } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return undef; }, ontimeout => sub { my ($context, $user) = @_; my $task = $user->GetTask; if (defined($task)) { my $hour = $user->GetWorkTimeEnd + 1; if ($hour == 24) { $hour = 0; } my $date = new Orangutan::Date(sprintf('%02d:00:00', $hour), $task->GetStart->Date); my $lastmessage = $user->GetLastMessageTime; if (!defined($lastmessage) || ($lastmessage <= $date->Date)) { if ($context->GetField('break')) { $user->RemoveTask; $user->FireEvent('task', 'cancel', $task); } elsif ($main::query->GetEntries($user, $task->GetStart, $date) == 0) { $task->SetEnd($date); my $message = Orangutan::Context::Random('Ok...', 'Well...', 'I see...'); my $format = Orangutan::Context::Random( 'So I stop the task using %02d:%02d.', 'Stopping the task using %02d:%02d.' ); $message .= ' '.sprintf($format, $date->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( 'You did not specify the activity so I use %s...', 'Using activity "%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); } } } } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return undef; }, ontask => sub { my ($context, $user, $type, $status) = @_; if ($status eq 'break') { my $nighttask = $user->GetResponse($context->GetID); if ($nighttask) { $nighttask->SetField('break', 1); } } elsif (($status ne 'add') && ($status ne 'change') && ($status ne 'remove')) { my $nighttask = $user->GetResponse($context->GetID); if ($nighttask) { my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($nighttask); } $user->RemoveRequestResponse($nighttask); } } }, schedule => sub { my ($context, $scheduler, $arg) = @_; my $date = new Orangutan::Date; my $hour = ($date->GetTime)[0]; if ($hour < 21) { $hour = 21; if (!$date->IsWorkDay) { $date = $date->NextWorkDay; } } else { my $next; foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); my $end = $user->GetWorkTimeEnd + 1; if (($end <= 24) && ($end > $hour) && (!defined($next) || ($next > $end))) { $next = $end; } } if (!$date->IsWorkDay || !defined($next)) { $date = $date->NextWorkDay; $hour = 21; } else { $hour = $next; } } my ($day, $month, $year) = (localtime($date->Date))[3..5]; my $time = POSIX::mktime(0, 0, $hour, $day, $month, $year); $scheduler->Schedule($time, $context, 'ask'); if ($arg eq 'ask') { my $time = new Orangutan::Date; $hour = ($time->GetTime)[0]; foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); if ($user->GetTask) { my $entry = $user->GetHistoryEntry; if ($user->IsOnline || !defined($entry) || (($time->Date - $entry->{'date'}->Date) <= 300)) { my $end = $user->GetWorkTimeEnd + 1; if (($end == $hour) || (($end == 24) && ($hour == 0)) || (($end < 21) && ($hour == 21))) { my $lastmessage = $user->GetLastMessageTime; if (!defined($lastmessage) || (($time->Date - $lastmessage) >= 1800)) { if (!$user->GetResponse($context->GetID)) { my $nighttask = $user->AddRequestResponse($context, 90); $nighttask->SetField('date', $time); } } } } } } } }, timeout => 1800, weight => undef ); # kate: syntax perl