# Type: Input and output # Category: Task # Description: Sets activity type for the project. # Dialog: Task -> Project -> # Fields: asked # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; my $task = $user->GetTask; if (!$task) { my $last = $user->GetLast; if ($last) { if ($last->GetEnd->Elapsed <= 3600) { $task = $last; } } } if ($task) { if ($main::query->GetActivities($task->GetProject('id'))) { my $message = Orangutan::Context::Random( 'What is the activity', 'What activity is this' ); $message .= ':'; my $number = 1; while (defined(my $activity = $main::query->Next)) { $message .= " ".$number.") ".$activity->{'name'}; $number++; } $message .= '?'; $context->SetField('asked', 1); my $ncontext = $user->GetResponse('Number'); if ($ncontext) { $ncontext->InsertForeignHandler($context); } return $message; } } return undef; }, response => sub { my ($context, $user, $activity) = @_; my @result = ( ); my @regexps = ( '(?:(?:The|my) )?activity is ["\']?(.+?)["\']?!*\.*', '(?:Change|set)(?: (?:the|my))? activity to ["\']?(.+?)["\']?!*\.*' ); if ($context->GetField('asked')) { push(@regexps, '(?:This|It) is ["\']?(.+?)["\']?!*\.*'); } my $matched = 0; foreach my $regexp (@regexps) { if ($activity =~ /^$regexp$/i) { $activity = $1; $matched++; last; } } if ($matched || $context->GetField('asked')) { if ($activity =~ /^([0-9]+)\)?\.*/) { push(@result, 1); push(@result, $1); } elsif ($activity =~ /[a-z]+/i) { push(@result, 2); push(@result, $activity); } } return @result; }, handler => sub { my ($context, $user, $item, $activity) = @_; my $task = $user->GetTask; if (!$task) { my $last = $user->GetLast; if ($last) { if ($last->GetEnd->Elapsed <= 3600) { $task = $last; } } } if ($task) { if ($main::query->GetActivities($task->GetProject('id'))) { if (($item == 1) || ($item == -2)) { my $row = $main::query->GetRow($activity - 1); if (defined($row)) { $task->SetActivity( id => $row->{'id'}, name => $row->{'name'} ); if ($task->GetID) { $user->ResubmitLastTask; } $main::config->Context('Ok')->Tell($user); if ($user->GetRequest($context->GetID)) { $user->RemoveRequest($context); } $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; } else { $user->SendMessage([ 'This project does not have an activity with this number.', 'The number you specified is wrong!', 'No activity with this number.' ]); } } else { while (defined(my $item = $main::query->Next)) { if (lc($item->{'name'}) eq lc($activity)) { $task->SetActivity( id => $item->{'id'}, name => $item->{'name'} ); if ($task->GetID) { $user->ResubmitLastTask; } $main::config->Context('Ok')->Tell($user); if ($user->GetRequest($context->GetID)) { $user->RemoveRequest($context); } $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; return 1; } } $user->SendMessage([ 'The project does not have such activity.', 'Use the correct activity name.' ]); } } else { $main::config->Context('NoDatabase')->Tell($user); } } else { $user->SendMessage([ 'I need to know at least a task name to use the activity!', "Activity is useless without task name and project name... So let's begin with task name..." ]); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; } return 1; }, ontimeout => sub { my ($context, $user) = @_; $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; return 1; }, ontask => sub { my ($context, $user, $type, $status) = @_; if ($status eq 'cancel') { my $activity = $user->GetResponse($context->GetID); if ($activity) { if ($user->GetRequest($context->GetID)) { $user->RemoveRequest($activity); } elsif ($activity->GetField('asked')) { $user->SendMessage([ 'So I do not need the activity anymore...', 'No task - no activity!.. :)' ]); $activity->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $activity->SetTimeout; } } } }, help => { title => 'How can I change the activity of the task?', question => '^(?:How )?(?:(?:can|do) I |to )(?:change|set) (?:the )?activity(?: of (?:the )?(?:current )?task)?\?*!*\.*$', answer => "Just tell me to change it.\n". "Some examples:\n". " o Change activity to \n". " o Activity is \n". "Alternatively you can specify the activity when creating a task:\n". " o Working on the issue ()", weight => 630 }, timeout => 1800, weight => [ undef, 90, undef ] ); # kate: syntax perl