Work in progress on task required option.
[timetracker.git] / WEB-INF / lib / ttUser.class.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 class ttUser {
30   var $login = null;            // User login.
31   var $name = null;             // User name.
32   var $id = null;               // User id.
33   var $team_id = null;          // Team id.
34   var $role = null;             // User role (user, client, comanager, manager, admin).
35   var $client_id = null;        // Client id for client user role.
36   var $behalf_id = null;        // User id, on behalf of whom we are working.
37   var $behalf_name = null;      // User name, on behalf of whom we are working.
38   var $email = null;            // User email.
39   var $lang = null;             // Language.
40   var $decimal_mark = null;     // Decimal separator.
41   var $date_format = null;      // Date format.
42   var $time_format = null;      // Time format.
43   var $week_start = 0;          // Week start day.
44   var $tracking_mode = 0;       // Tracking mode.
45   var $task_required = null;    // Whether task selection is required on time entires.
46   var $record_type = 0;         // Record type (duration vs start and finish, or both).
47   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
48   var $currency = null;         // Currency.
49   var $plugins = null;          // Comma-separated list of enabled plugins.
50   var $team = null;             // Team name.
51   var $custom_logo = 0;         // Whether to use a custom logo for team.
52   var $address = null;          // Address for invoices.
53   var $lock_spec = null;        // Cron specification for record locking.
54   var $workday_hours = 8;       // Number of work hours in a regular day.
55   var $rights = 0;              // A mask of user rights.
56
57   // Constructor.
58   function __construct($login, $id = null) {
59     if (!$login && !$id) {
60       // nothing to initialize
61       return;
62     }
63
64     $mdb2 = getConnection();
65
66     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.client_id, u.email, t.name as team_name, 
67       t.address, t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
68       t.tracking_mode, t.task_required, t.record_type, t.uncompleted_indicators, t.plugins, t.lock_spec, t.workday_hours, t.custom_logo
69       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) WHERE ";
70     if ($id)
71       $sql .= "u.id = $id";
72     else
73       $sql .= "u.login = ".$mdb2->quote($login);
74     $sql .= " AND u.status = 1";
75
76     $res = $mdb2->query($sql);
77     if (is_a($res, 'PEAR_Error')) {
78       return;
79     }
80
81     $val = $res->fetchRow();
82     if ($val['id'] > 0) {
83       $this->login = $val['login'];
84       $this->name = $val['name'];
85       $this->id = $val['id'];
86       $this->team_id = $val['team_id'];
87       $this->role = $val['role'];
88       $this->client_id = $val['client_id'];
89       $this->email = $val['email'];
90       $this->lang = $val['lang'];
91       $this->decimal_mark = $val['decimal_mark'];
92       $this->date_format = $val['date_format'];
93       $this->time_format = $val['time_format'];
94       $this->week_start = $val['week_start'];
95       $this->tracking_mode = $val['tracking_mode'];
96       $this->task_required = $val['task_required'];
97       $this->record_type = $val['record_type'];
98       $this->uncompleted_indicators = $val['uncompleted_indicators'];
99       $this->team = $val['team_name'];
100       $this->address = $val['address'];
101       $this->currency = $val['currency'];
102       $this->plugins = $val['plugins'];
103       $this->lock_spec = $val['lock_spec'];
104       $this->workday_hours = $val['workday_hours'];
105       $this->custom_logo = $val['custom_logo'];
106
107       // Set "on behalf" id and name.
108       if (isset($_SESSION['behalf_id'])) {
109           $this->behalf_id = $_SESSION['behalf_id'];
110           $this->behalf_name = $_SESSION['behalf_name'];
111       }
112
113       // Set user rights.
114       if ($this->role == ROLE_USER) {
115         $this->rights = right_data_entry|right_view_charts|right_view_reports;
116       } elseif ($this->role == ROLE_CLIENT) {
117         $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
118       } elseif ($this->role == ROLE_COMANAGER) {
119         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
120       } elseif ($this->role == ROLE_MANAGER) {
121         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
122       } elseif ($this->role == ROLE_SITE_ADMIN) {
123         $this->rights = right_administer_site;
124       }
125     }
126   }
127
128   // The getActiveUser returns user id on behalf of whom current user is operating.
129   function getActiveUser() {
130     return ($this->behalf_id ? $this->behalf_id : $this->id);
131   }
132
133   // isAdmin - determines whether current user is admin (has right_administer_site).
134   function isAdmin() {
135     return (right_administer_site & $this->role);
136   }
137
138   // isManager - determines whether current user is team manager.
139   function isManager() {
140     return (ROLE_MANAGER == $this->role);
141   }
142
143   // isCoManager - determines whether current user is team comanager.
144   function isCoManager() {
145     return (ROLE_COMANAGER == $this->role);
146   }
147
148   // isClient - determines whether current user is a client.
149   function isClient() {
150     return (ROLE_CLIENT == $this->role);
151   }
152
153   // canManageTeam - determines whether current user is manager or co-manager.
154   function canManageTeam() {
155     return (right_manage_team & $this->role);
156   }
157
158   // isPluginEnabled checks whether a plugin is enabled for user.
159   function isPluginEnabled($plugin)
160   {
161     return in_array($plugin, explode(',', $this->plugins));
162   }
163
164   // getAssignedProjects - returns an array of assigned projects.
165   function getAssignedProjects()
166   {
167     $result = array();
168     $mdb2 = getConnection();
169
170     // Do a query with inner join to get assigned projects.
171     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
172       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
173       where p.team_id = $this->team_id and p.status = 1 order by p.name";
174     $res = $mdb2->query($sql);
175     if (!is_a($res, 'PEAR_Error')) {
176       while ($val = $res->fetchRow()) {
177         $result[] = $val;
178       }
179     }
180     return $result;
181   }
182
183   // isDateLocked checks whether a specifc date is locked for modifications.
184   function isDateLocked($date)
185   {
186     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
187       // Override for managers.
188       if ($this->canManageTeam()) return false;
189
190       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
191       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
192
193       // Calculate the last occurrence of a lock.
194       $last = tdCron::getLastOccurrence($this->lock_spec, mktime());
195       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
196       if ($date->before($lockdate)) {
197         return true;
198       }
199     }
200     return false;
201   }
202 }