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