# Type: Input and output # Category: Configuration # Description: Sets activity type for the main project # Dialog: Hi -> OtherQuestions -> MainProject -> MainIssue -> # Timeout: 5 mins # Fields: asked, initial # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; my $message = Orangutan::Context::Random( 'Tell me your primary activity', 'What is your primary activity', 'What is your main activity' ); if ($main::query->GetActivities($user->GetProjectID)) { $message .= ':'; my $number = 1; while (defined(my $activity = $main::query->Next)) { $message .= " ".$number.") ".$activity->{'name'}; $number++; } } $message .= '?.. '; if (defined($user->GetActivity)) { my $format .= Orangutan::Context::Random( 'If you do not answer I will use %s...', 'You can ignore this question - in this case I will use %s...' ); $message .= sprintf($format, $user->GetActivity); } else { $message .= Orangutan::Context::Random( 'If you do not answer I will use the default one...', 'You can ignore this question - in this case I will use the default activity...' ); } $context->SetField('asked', 1); my $number = $user->GetResponse('Number'); if ($number) { $number->InsertForeignHandler($context, 300); } return $message; }, response => sub { my ($context, $user, $activity) = @_; my @result = ( ); my @regexps = ( '(?:(?:(?:From )?now|currently) )?(?:(?:the|my) )?(?:main|primary|default) activity is ["\']?(.+?)["\']?!*\.*', '(?:Change|set)(?: (?:the|my))? (?:main|primary|default) 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, $value) = @_; if ($main::query->GetActivities($user->GetProjectID)) { my $activity; if (($item == 1) || ($item == -2)) { $activity = $main::query->GetRow($value - 1); if (!defined($activity)) { $user->SendMessage([ 'Wrong activity number!', 'No activity under this number for this project!', 'There is no activity with such number... Please select the correct number!' ]); } } else { while (defined($activity = $main::query->Next)) { if (lc($activity->{'name'}) eq lc($value)) { last; } } if (!defined($activity)) { $user->SendMessage( 'Perhaps you made a mistake in activity name...', 'Hm... This project does not have such activity!.. '. 'Please tell me the correcy activity or just its number.' ); } } if (defined($activity)) { $user->SetActivityID($activity->{'id'}); $user->SetActivity($activity->{'name'}); $main::config->Context('Ok')->Tell($user); if ($context->GetField('initial')) { my $message = Orangutan::Context::Random("That's it! ", "That's all! ", 'No more questions! '); $message .= Orangutan::Context::Random('Thanks.', 'Thank you.'); $user->SendMessage($message); $context->UnsetField('initial'); } my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; } } else { $main::config->Context('NoDatabase')->Tell($user); } $context->UnsetField('asked'); return 1; }, ontimeout => sub { my ($context, $user) = @_; if (!defined($user->GetActivity)) { my $default_activity = $main::query->GetDefaultActivity($user->GetProjectID); if (defined($default_activity)) { $user->SetActivityID($default_activity->{'id'}); $user->SetActivity($default_activity->{'name'}); } } if (defined($user->GetActivity)) { my $message = 'Ok... '; my $format .= Orangutan::Context::Random( 'Will use "%s"...', 'Using "%s"...' ); $message .= sprintf($format, $user->GetActivity); $user->SendMessage($message); if ($context->GetField('initial')) { $message = Orangutan::Context::Random("That's it! ", "That's all! ", 'No more questions! '); $message .= Orangutan::Context::Random('Thanks.', 'Thank you.'); $user->SendMessage($message); } } $context->UnsetField('initial'); $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; return 1; }, help => { title => 'How can I change default activity?', question => '^(?:How )?(?:(?:can|do) I |to )(?:change|set|choose|select) '. '(?:(?:the|my) )?(?:default|main|primary) activity\?*!*\.*$', answer => "You can choose the activity which will be used if no activity name ". "was specified on the time of creating new task. ". "Of course, this activity should be a valid activity of your main project.\n". "Samples:\n". " o The default activity is \n". " o My primary activity is \n". " o Set main activity to \n". "Orangutan asks user to select the default activity in the initial configuration dialog...", weight => 530 }, timeout => 300, weight => [ undef, 90, undef ] ); # kate: syntax perl