# Type: Silent # Category: Extra # Description: Implements work day start and end events # Author: Andriy Lesyuk new Orangutan::Context( schedule => sub { my ($context, $scheduler, $arg) = @_; my $date = new Orangutan::Date; my $hour = ($date->GetTime)[0]; my ($first, $next); foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); my $start = $user->GetWorkTimeStart; if (!defined($first) || ($first > $start)) { $first = $start; } if (($start > $hour) && (!defined($next) || ($next > $start))) { $next = $start; } my $end = $user->GetWorkTimeEnd; if (($end > $hour) && (!defined($next) || ($next > $end))) { $next = $end; } } if (defined($first)) { if (!$date->IsWorkDay || !defined($next)) { $date = $date->NextWorkDay; $next = $first; } my ($day, $month, $year) = (localtime($date->Date))[3..5]; my $time = POSIX::mktime(0, 0, $next, $day, $month, $year); $scheduler->Schedule($time, $context, 'notify'); if ($arg eq 'notify') { foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); if ($hour == $user->GetWorkTimeStart) { $user->FireEvent('workday', 'start'); } elsif ($hour == $user->GetWorkTimeEnd) { $user->FireEvent('workday', 'end'); } } } } } ); # kate: syntax perl