# Type: Input and output # Category: Task # Description: Sets project for the task # 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) { $context->SetField('asked', 1); return Orangutan::Context::Random( 'What is the project?', 'What project is this?', 'On what project is this task?' ); } return undef; }, response => sub { my ($context, $user, $project) = @_; my @result = ( ); my @regexps = ( '(?:(?:My|The|This) )?project is ["\']?(.+?)["\']?!*\.*', '(?:(?:This|The) )?task is on ["\']?(.+?)["\']?!*\.*', 'Change(?: (?:the|my))? project to ["\']?(.+?)["\']?!*\.*' ); if ($context->GetField('asked')) { push(@regexps, '(?:This|It) is ["\']?(.+?)["\']?!*\.*'); } my $matched = 0; foreach my $regexp (@regexps) { if ($project =~ /^$regexp$/i) { $project = $1; $matched++; last; } } if (($matched || $context->GetField('asked')) && ($project =~ /[a-z]+/i)) { push(@result, 1); push(@result, $project); } return @result; }, handler => sub { my ($context, $user, $item, $project) = @_; my $task = $user->GetTask; if (!$task) { my $last = $user->GetLast; if ($last) { if ($last->GetEnd->Elapsed <= 3600) { $task = $last; } } } if ($task) { # Check if project exists my $projects = $main::query->ProjectExists($project); if (defined($projects)) { if (ref $projects eq 'ARRAY') { $user->SendMessage( 'Oops!.. Redmine has several projects with this name... :( '. 'I have no idea what to do in this case - will ask my creator...' ); $main::users->TellCreator( "Congratulations! :) You have a little more work now... ". "I found several projects with name \"$project\"... ;)" ); } elsif (ref $projects eq 'HASH') { my $projectid = $task->GetProject('id'); if (!defined($projectid) || ($projectid != $projects->{'id'})) { $task->SetIssue; $task->SetProject( id => $projects->{'id'}, name => $projects->{'name'} ); my $message = Orangutan::Context::Random('Ok...', 'Got it!..', 'I see...'); if (defined($task->GetActivity)) { my $activityid = $main::query->ActivityExists($task->GetActivity, $task->GetProject('id')); if ($activityid) { $task->SetActivity( id => $activityid, name => $task->GetActivity ); } else { $task->SetActivity; # clear activity information } } if (!defined($task->GetActivity) && defined($user->GetActivity)) { my $activityid = $main::query->ActivityExists($user->GetActivity, $task->GetProject('id')); if ($activityid) { $task->SetActivity( id => $activityid, name => $user->GetActivity ); } } if (!defined($task->GetActivity)) { my $default_activity = $main::query->GetDefaultActivity($task->GetProject('id')); if (defined($default_activity)) { $task->SetActivity( id => $default_activity->{'id'}, name => $default_activity->{'name'} ); } } if ($task->GetID) { $user->ResubmitLastTask; } if (defined($task->GetActivity)) { $message .= ' Using activity "'.$task->GetActivity.'"...'; } $user->SendMessage($message); my $issue = $user->GetResponse('Issue'); if ($issue) { $user->AddRequest($issue); } } else { $user->SendMessage([ "That's what is used currently!", 'I already use this project...', 'I know...' ]); } if ($user->GetRequest($context->GetID)) { $user->RemoveRequest($context); } $context->UnsetField('asked'); $context->SetTimeout; } else { $user->SendMessage([ 'I cannot find project with such name - use the correct name or create the project.', 'Seems no such project exists... I guess you need to create it or use the correct name.', 'Hm... Redmine does not have project with such name... '. 'Please check projects list for correct name or create this project.' ]); } } else { $main::config->Context('NoDatabase')->Tell($user); } } else { $user->SendMessage([ 'First you tell me your task then you tell me the project this task is on - not vice versa.', "And? I don't have any information regarding the task you are working on!..", "Let's begin from what are you working on..." ]); $context->SetTimeout; } return 1; }, ontimeout => sub { my ($context, $user) = @_; $context->UnsetField('asked'); $context->SetTimeout; return 1; }, ontask => sub { my ($context, $user, $type, $status) = @_; if ($status eq 'cancel') { my $project = $user->GetResponse($context->GetID); if ($project) { if ($user->GetRequest($context->GetID)) { $user->RemoveRequest($project); } elsif ($project->GetField('asked')) { $user->SendMessage([ 'No need to tell the project name then...', "Then I don't need the project name...", 'Then forget about the project name...' ]); $project->UnsetField('asked'); $project->SetTimeout; } } } }, timeout => 1800, weight => [ undef, 90, undef ] ); # kate: syntax perl