Resuming refactoring report related functions.
[timetracker.git] / cron.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 /*
30  * cron.php - this file is an entry point to execute scheduled jobs in Time Tracker.
31  * It must be called externally (for example, from the system cron or task scheduler).
32  * 
33  * Internally, we store scheduled jobs in tt_cron table in db. The cron_spec field is in cron format.
34  * Along with it, we store last and next timestamps for jobs, we use them as an execute condition.
35  * 
36  * Although cron_spec follows 5-field cron specification precisely, actual job timing depends on
37  * how often cron.php is called. For example, an hourly ping will execute jobs no more than once
38  * each hour, even if they are due more often. Configure whatever calls this file accordingly.
39  */
40
41 require_once('initialize.php');
42 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
43 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
44 import('ttFavReportHelper');
45 import('ttReportHelper');
46
47 $mdb2 = getConnection();
48 $now = time();
49
50  $sql = "select c.id, c.cron_spec, c.report_id, c.email, c.cc, c.subject, c.report_condition from tt_cron c
51    left join tt_fav_reports fr on (c.report_id = fr.id)
52    where $now >= c.next and fr.status = 1
53    and c.status = 1 and c.report_id is not null and c.email is not null";
54 $res = $mdb2->query($sql);
55 if (is_a($res, 'PEAR_Error'))
56   exit();
57
58 while ($val = $res->fetchRow()) {
59   // We have jobs to execute in user language.
60
61   // Get favorite report details.
62   $options = ttFavReportHelper::getReportOptions($val['report_id']);
63   if (!$options) continue; // Skip not found report.
64
65   // Recycle global $user object, as user settings are specific for each report.
66   $user = new ttUser(null, $options['user_id']);
67   if (!$user->id) continue; // Skip not found user.
68
69   // TODO: write a new function ttFavReportHelper::adjustOptions that will use
70   // a $user objecte recycled above. Put user handling below into it.
71   // Also adjust remaining options for potentially changed user access rights and group properties.
72   // For example, tracking mode may have changed, but fav report options are still old...
73   // This needs to be fixed.
74
75   // Special handling of the NULL $options['users'] field (this used to mean "all users").
76   if (!$options['users']) {
77     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
78       if ($user->can('view_reports') || $user->can('view_all_reports')) {
79         $max_rank = $user->rank-1;
80         if ($user->can('view_all_reports')) $max_rank = 512;
81         if ($user->can('view_own_reports'))
82           $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
83         else
84           $user_options = array('max_rank'=>$max_rank);
85         $users = $user->getUsers($user_options); // Active and inactive users.
86       } elseif ($user->isClient()) {
87         $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
88       }
89       foreach ($users as $single_user) {
90         $user_ids[] = $single_user['id'];
91       }
92       $options['users'] = implode(',', $user_ids);
93     }
94   } else {
95     // TODO: add checking the existing user list for potentially changed access rights for user.
96   }
97
98   // Skip users with disabled Notifications plugin.
99   if (!$user->isPluginEnabled('no')) continue;
100
101   // Recycle $i18n object because language is user-specific.
102   $i18n->load($user->lang);
103
104   // Check condition on a report.
105   $condition_ok = true;
106   if ($val['report_condition'])
107     $condition_ok = ttReportHelper::checkFavReportCondition($options, $val['report_condition']);
108
109   // Email report if condition is okay.
110   if ($condition_ok) {
111     if (ttReportHelper::sendFavReport($options, $val['subject'], $val['email'], $val['cc']))
112       echo "Report ".$val['report_id']. " sent.<br>";
113     else
114       echo "Error while emailing report...<br>";
115   }
116
117   // Calculate next execution time.
118   $next = tdCron::getNextOccurrence($val['cron_spec'], $now + 60); // +60 sec is here to get us correct $next when $now is close to existing "next".
119                                                                    // This is because the accuracy of tdcron class appears to be 1 minute.
120
121   // Update last and next values in tt_cron.
122   $sql = "update tt_cron set last = $now, next = $next where id = ".$val['id'];
123   $affected = $mdb2->exec($sql);
124   if (is_a($affected, 'PEAR_Error')) continue;
125 }
126
127 echo "Done!";