# Type: Input and output # Category: Configuration # Description: Sets the main issue of the user # Dialog: Hi -> OtherQuestions -> MainProject -> -> MainActivity # Timeout: 5 mins # Fields: asked # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; $context->SetField('asked', 1); my $number = $user->GetResponse('Number'); if ($number) { $number->InsertForeignHandler($context, 300); } if ($main::query->GetBestIssues($user, $user->GetProjectID)) { my $message = Orangutan::Context::Random( 'You can specify the default issue.', 'Please specify your primary issue.', 'What is your primary issue?' ).' '; if ($main::query->Rows == 1) { $message .= Orangutan::Context::Random( 'This issue looks to be the one', 'Maybe this one', 'It might be' ); $context->SetField('issue', $main::query->GetRow(0)->{'id'}); my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->InsertForeignHandler($context, 300); } } else { $message .= Orangutan::Context::Random( 'Some issues which might be the ones', 'I guess you work on one of these', 'Some of the issues' ); } $message .= ":\n"; while (defined(my $issue = $main::query->Next)) { utf8::decode($issue->{'subject'}); $issue->{'status_name'} =~ tr/[A-Z]/[a-z]/; $issue->{'priority_name'} =~ tr/[A-Z]/[a-z]/; $message .= sprintf("#%d) ", $issue->{'id'}); $message .= $issue->{'tracker_name'}; $message .= ' ('.$issue->{'status_name'}.'): '; $message .= $issue->{'subject'}; $message .= ' ['.$issue->{'priority_name'}.']'; if (!defined($user->GetProjectID)) { $message .= ' ('.$issue->{'project_name'}.')'; } $message .= "\n"; } $message .= Orangutan::Context::Random( 'Asnwer "Skip" here if you have too many small issues to select the default one...', 'You can answer "Skip" if you are not sure...', 'To skip this question just type "Skip"...' ); return $message; } else { return Orangutan::Context::Random( 'Tell me your primary issue number.', 'What is your primary issue?', 'What is your main issue?' ); } }, response => sub { my ($context, $user, $issue) = @_; my @result = ( ); my @regexps = ( '(?:Change|set) (?:(?:the|my) )?(?:main|primary|default) issue(?: (?:number|ID))? to (?:#|No\.? ?)?([0-9]+)!*\.*', '(?:The )?(?:number|ID) of (?:(?:the|my) )?(?:main|primary|default) issue is (?:#|No\.? ?)?([0-9]+)!*\.*', '(?:(?:The|my) )?(?:main|primary|default) issue(?: (?:number|ID))? is (?:#|No\.? ?)?([0-9]+)!*\.*', 'Now I(?:\'m| (?:am(?: working)?|work)) on (?:(?:the )?issue )?(?:#|No\.? ?)?([0-9]+)!*\.*' ); if ($context->GetField('asked')) { push(@regexps, '(?:This|it) is (?:#|No\.? ?)?([0-9]+)!*\.*'); push(@regexps, '(?:(?:The )?issue )?(?:#|No\.? ?)?([0-9]+)!*\.*'); } foreach my $regexp (@regexps) { if ($issue =~ /^$regexp$/i) { push(@result, 1); push(@result, $1); last; } } if (scalar @result == 0) { @regexps = ( '(?:Clear|unset|remove) (?:(?:the|my) )?(?:main|primary|default) issue(?: (?:number|ID))?!*\.*' ); if ($context->GetField('asked')) { push(@regexps, '(?:Please )?skip(?: (?:this )?question)?!*\.*'); push(@regexps, '(?:I )?do(?:n\'t| not) know!*\.*'); push(@regexps, '(?:I(?:\'m| am) )?not sure!*\.*'); push(@regexps, 'None!*\.*'); } foreach my $regexp (@regexps) { if ($issue =~ /^$regexp$/i) { push(@result, 2); push(@result, undef); last; } } } return @result; }, handler => sub { my ($context, $user, $item, $issueid) = @_; if (($item != -1) || ($issueid eq 'Y')) { my $issue; if (($item == 1) || ($item == -2)) { $issue = $main::query->GetIssue($issueid); if (defined($issue)) { if ($main::query->Can('edit_time_entries', $user, $issue->{'project_id'})) { if ($issue->{'is_closed'}) { $user->SendMessage([ 'I hope you know that this issue is already closed...', 'Weird. This issue seems to be closed...', 'Hm... This issue looks to be closed...' ]); } elsif ($issue->{'done_ratio'} == 100) { $user->SendMessage([ 'Hm... Someone believes that this issue has been completed...', 'Weird... This issue is marked to be completed...', 'Hm... I thought it was done...' ]); } } else { my $format = Orangutan::Context::Random( 'To be able to log time for this issue you must be a member of %s.', "Sorry. I can't use this issue - you are not a member of %s!", "You can't log time for this issue!" ); $user->SendMessage(sprintf($format, $issue->{'project_name'})); my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return 1; } } } elsif ($item == 2) { $user->SetIssue; $main::config->Context('Ok')->Tell($user); $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } $context->SetTimeout; if ($context->GetField('initial')) { if ($main::query->GetActivities($user->GetProjectID) && ($main::query->Rows > 1)) { my $activity = $user->GetResponse('MainActivity'); if ($activity) { $activity->SetField('initial', 1); $user->AddRequest($activity); } } else { 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'); } return 1; } else { $issueid = $context->GetField('issue'); $issue = $main::query->GetIssue($issueid); } if (defined($issue)) { $user->SetIssue($issue->{'id'}); if (!defined($user->GetProjectID) || ($user->GetProjectID != $issue->{'project_id'})) { if ($context->GetField('asked')) { my $format = Orangutan::Context::Random( 'Hm... You told that the main project is %s but this issue is in %s...', 'Hm... I hope you know that this issue is not in %s...' ); $user->SendMessage(sprintf($format, $user->GetProject), $issue->{'project_name'}); } $user->SetProjectID($issue->{'project_id'}); $user->SetProject($issue->{'project_name'}); if (defined($user->GetActivity)) { my $activityid = $main::query->ActivityExists($user->GetActivity, $user->GetProjectID); if ($activityid) { $user->SetActivityID($activityid); } else { $user->SetActivity; $user->SetActivityID; } } 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 ($user->GetRequest($context->GetID)) { $user->RemoveRequest($context); } $main::config->Context('Ok')->Tell($user); $context->UnsetField('asked'); my $number = $user->GetResponse('Number'); if ($number) { $number->RemoveForeignHandler($context); } $context->SetTimeout; if ($context->GetField('initial')) { if ($main::query->GetActivities($user->GetProjectID) && ($main::query->Rows > 1)) { my $activity = $user->GetResponse('MainActivity'); if ($activity) { $activity->SetField('initial', 1); $user->AddRequest($activity); } } else { 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'); } } else { if ($item == -1) { $user->SendMessage('Weird... The issue has gone... :S'); } else { $user->SendMessage([ 'Such issue does not exist.', "I can't find such issue.", 'No such issue.' ]); } } } else { $user->SendMessage([ 'Then just tell me the correct issue.', 'Ok... Tell me the correct issue ID.', 'So what is the correct issue?' ]); } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } return 1; }, ontimeout => sub { my ($context, $user) = @_; if ($context->GetField('asked')) { $user->SendMessage([ 'Ok... But note that you will need to specify issue ID each time you create a task.', "Well... Don't forget to specify the issue when creating a task." ]); $context->UnsetField('asked'); } if ($context->GetField('initial')) { if ($main::query->GetActivities($user->GetProjectID) && ($main::query->Rows > 1)) { my $activity = $user->GetResponse('MainActivity'); if ($activity) { $activity->SetField('initial', 1); $user->AddRequest($activity); } } else { 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); } my $yesno = $user->GetResponse('YesNo'); if ($yesno) { $yesno->RemoveForeignHandler($context); } $context->SetTimeout; return 1; }, help => { title => 'How can I change default issue?', question => [ '^(?:How )?(?:(?:can|do) I |to )(?:change|set|choose|select) (?:(?:the|my) )?'. '(?:default|main|primary) (?:number|ID) of (?:the )?issue\?*!*\.*$', '^(?:How )?(?:(?:can|do) I |to )(?:change|set|choose|select) (?:(?:the|my) )?'. '(?:default|main|primary) issue(?: (?:number|ID))?\?*!*\.*$' ], answer => "It can be useful to specify the default issue ". "which will be used if no issue ID is specified when creating a task. ". "Samples how to set the default issue:\n". " o Set the primary issue number to #87\n". " o The ID of my default issue is #63\n". " o Change my default issue to #50\n". " o Now I work on issue #54\n". " o My main issue is #32\n". "The default issue is asked in the initial configuration dialog...", weight => 520 }, timeout => 300, weight => [ undef, 0, undef ] ); # kate: syntax perl