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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttConfigHelper');
30 import('ttGroupHelper');
34 var $login = null; // User login.
35 var $name = null; // User name.
36 var $id = null; // User id.
37 var $org_id = null; // Organization id.
38 var $group_id = null; // Group id.
39 var $role_id = null; // Role id.
40 var $role_name = null; // Role name.
41 var $rank = null; // User role rank.
42 var $client_id = null; // Client id for client user role.
43 var $behalf_id = null; // User id, on behalf of whom we are working.
44 var $behalf_group_id = null; // Group id, on behalf of which we are working.
45 var $behalf_name = null; // User name, on behalf of whom we are working.
46 var $group_name = null; // Group name.
47 var $behalf_group_name = null;// Group name, on behalf of which we are working.
48 var $email = null; // User email.
49 var $lang = null; // Language.
50 var $decimal_mark = null; // Decimal separator.
51 var $date_format = null; // Date format.
52 var $time_format = null; // Time format.
53 var $week_start = 0; // Week start day.
54 var $show_holidays = 0; // Whether to show holidays in calendar.
55 var $tracking_mode = 0; // Tracking mode.
56 var $project_required = 0; // Whether project selection is required on time entires.
57 var $task_required = 0; // Whether task selection is required on time entires.
58 var $record_type = 0; // Record type (duration vs start and finish, or both).
59 var $punch_mode = 0; // Whether punch mode is enabled for user.
60 var $allow_overlap = 0; // Whether to allow overlapping time entries.
61 var $future_entries = 0; // Whether to allow creating future entries.
62 var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
63 var $bcc_email = null; // Bcc email.
64 var $allow_ip = null; // Specification from where user is allowed access.
65 var $password_complexity = null; // Password complexity example.
66 var $currency = null; // Currency.
67 var $plugins = null; // Comma-separated list of enabled plugins.
68 var $config = null; // Comma-separated list of miscellaneous config options.
69 var $custom_logo = 0; // Whether to use a custom logo for group.
70 var $lock_spec = null; // Cron specification for record locking.
71 var $workday_minutes = 480; // Number of work minutes in a regular day.
72 var $rights = array(); // An array of user rights such as 'track_own_time', etc.
73 var $is_client = false; // Whether user is a client as determined by missing 'track_own_time' right.
74 var $minutes_in_unit = 15; // Number of minutes in unit for Work units plugin.
75 var $first_unit_threshold = 0;// Threshold for 1st unit for Work units plugin.
76 var $unit_totals_only = 0; // Totals only option for the Work units plugin.
78 var $behalfGroup = null; // A ttGroup instance with on behalf group attributes.
81 function __construct($login, $id = null) {
82 if (!$login && !$id) {
83 // nothing to initialize
87 $mdb2 = getConnection();
89 $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id, u.email,
90 g.org_id, g.name as group_name, g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
91 g.tracking_mode, g.project_required, g.task_required, g.record_type,
92 g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
93 FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
97 $sql .= "u.login = ".$mdb2->quote($login);
98 $sql .= " AND u.status = 1";
100 $res = $mdb2->query($sql);
101 if (is_a($res, 'PEAR_Error')) {
105 $val = $res->fetchRow();
106 if ($val['id'] > 0) {
107 $this->login = $val['login'];
108 $this->name = $val['name'];
109 $this->id = $val['id'];
110 $this->org_id = $val['org_id'];
111 $this->group_id = $val['group_id'];
112 $this->role_id = $val['role_id'];
113 $this->role_name = $val['role_name'];
114 $this->rights = explode(',', $val['rights']);
115 $this->is_client = !in_array('track_own_time', $this->rights);
116 $this->rank = $val['rank'];
117 $this->client_id = $val['client_id'];
118 $this->email = $val['email'];
119 $this->lang = $val['lang'];
120 $this->decimal_mark = $val['decimal_mark'];
121 $this->date_format = $val['date_format'];
122 $this->time_format = $val['time_format'];
123 $this->week_start = $val['week_start'];
124 $this->tracking_mode = $val['tracking_mode'];
125 $this->project_required = $val['project_required'];
126 $this->task_required = $val['task_required'];
127 $this->record_type = $val['record_type'];
128 $this->bcc_email = $val['bcc_email'];
129 $this->allow_ip = $val['allow_ip'];
130 $this->password_complexity = $val['password_complexity'];
131 $this->group_name = $val['group_name'];
132 $this->currency = $val['currency'];
133 $this->plugins = $val['plugins'];
134 $this->lock_spec = $val['lock_spec'];
135 $this->workday_minutes = $val['workday_minutes'];
136 $this->custom_logo = $val['custom_logo'];
138 $this->config = $val['config'];
139 $config = new ttConfigHelper($this->config);
140 // Set user config options.
141 $this->show_holidays = $config->getDefinedValue('show_holidays');
142 $this->punch_mode = $config->getDefinedValue('punch_mode');
143 $this->allow_overlap = $config->getDefinedValue('allow_overlap');
144 $this->future_entries = $config->getDefinedValue('future_entries');
145 $this->uncompleted_indicators = $config->getDefinedValue('uncompleted_indicators');
146 if ($this->isPluginEnabled('wu')) {
147 $minutes_in_unit = $config->getIntValue('minutes_in_unit');
148 if ($minutes_in_unit) $this->minutes_in_unit = $minutes_in_unit;
149 $first_unit_threshold = $config->getIntValue('1st_unit_threshold');
150 if ($first_unit_threshold) $this->first_unit_threshold = $first_unit_threshold;
151 $this->unit_totals_only = $config->getDefinedValue('unit_totals_only');
154 // Set "on behalf" id and name (user).
155 if (isset($_SESSION['behalf_id'])) {
156 $this->behalf_id = $_SESSION['behalf_id'];
157 $this->behalf_name = $_SESSION['behalf_name'];
159 // Set "on behalf" id and name (group).
160 if (isset($_SESSION['behalf_group_id'])) {
161 $this->behalf_group_id = $_SESSION['behalf_group_id'];
162 $this->behalf_group_name = $_SESSION['behalf_group_name'];
164 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
169 // The getGroup returns group id on behalf of which the current user is operating.
170 function getGroup() {
171 return ($this->behalfGroup ? $this->behalfGroup->id : $this->group_id);
174 // getDecimalMark returns decimal mark for active group.
175 function getDecimalMark() {
176 return ($this->behalfGroup ? $this->behalfGroup->decimal_mark : $this->decimal_mark);
179 // getTrackingMode returns tracking mode for active group.
180 function getTrackingMode() {
181 return ($this->behalfGroup ? $this->behalfGroup->tracking_mode : $this->tracking_mode);
184 // getRecordType returns record type for active group.
185 function getRecordType() {
186 return ($this->behalfGroup ? $this->behalfGroup->record_type : $this->record_type);
189 // getPlugins returns plugins string for active group.
190 function getPlugins() {
191 return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins);
194 // getConfig returns config string for active group.
195 function getConfig() {
196 return ($this->behalfGroup ? $this->behalfGroup->config : $this->config);
199 // The getActiveUser returns user id on behalf of whom the current user is operating.
200 function getActiveUser() {
201 return ($this->behalf_id ? $this->behalf_id : $this->id);
204 // can - determines whether user has a right to do something.
205 function can($do_something) {
206 return in_array($do_something, $this->rights);
209 // isClient - determines whether current user is a client.
210 function isClient() {
211 return $this->is_client;
214 // isPluginEnabled checks whether a plugin is enabled for user.
215 function isPluginEnabled($plugin)
217 return in_array($plugin, explode(',', $this->getPlugins()));
220 // getAssignedProjects - returns an array of assigned projects.
221 function getAssignedProjects()
224 $mdb2 = getConnection();
226 $group_id = $this->behalf_group_id ? $this->behalf_group_id : $this->group_id;
227 // Do a query with inner join to get assigned projects.
228 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
229 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
230 where p.group_id = $group_id and p.status = 1 order by p.name";
231 $res = $mdb2->query($sql);
232 if (!is_a($res, 'PEAR_Error')) {
233 while ($val = $res->fetchRow()) {
240 // getAssignedTasks - returns an array of assigned tasks.
241 function getAssignedTasks()
243 // Start with projects;
244 $projects = $this->getAssignedProjects();
245 if (!$projects) return false;
247 // Build an array of task ids.
249 foreach($projects as $project) {
250 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
251 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
253 if (!$task_ids) return false;
255 // Get task descriptions.
257 $mdb2 = getConnection();
258 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
260 $sql = "select id, name, description from tt_tasks".
261 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
262 $res = $mdb2->query($sql);
263 if (!is_a($res, 'PEAR_Error')) {
264 while ($val = $res->fetchRow()) {
271 // getAssignedClients - returns an array of clients assigned to own projects.
272 function getAssignedClients()
274 // Start with projects;
275 $projects = $this->getAssignedProjects();
276 if (!$projects) return false;
277 $assigned_project_ids = array();
278 foreach($projects as $project) {
279 $assigned_project_ids[] = $project['id'];
282 $mdb2 = getConnection();
284 // Get active clients for group.
286 $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
287 $res = $mdb2->query($sql);
288 if (!is_a($res, 'PEAR_Error')) {
289 while ($val = $res->fetchRow()) {
290 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
291 if (array_intersect($assigned_project_ids, $client_project_ids))
292 $clients[] = $val; // Add client if one of user projects is a client project, too.
298 // isDateLocked checks whether a specifc date is locked for modifications.
299 function isDateLocked($date)
301 if (!$this->isPluginEnabled('lk'))
302 return false; // Locking feature is disabled.
304 if (!$this->lock_spec)
305 return false; // There is no lock specification.
307 if (!$this->behalf_id && $this->can('override_own_date_lock'))
308 return false; // User is working as self and can override own date lock.
310 if ($this->behalf_id && $this->can('override_date_lock'))
311 return false; // User is working on behalf of someone else and can override date lock.
313 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
314 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
316 // Calculate the last occurrence of a lock.
317 $last = tdCron::getLastOccurrence($this->lock_spec, time());
318 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
319 if ($date->before($lockdate))
325 // canOverridePunchMode checks whether a user can override punch mode in a situation.
326 function canOverridePunchMode()
328 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
329 return false; // User is working as self and cannot override for self.
331 if ($this->behalf_id && !$this->can('override_punch_mode'))
332 return false; // User is working on behalf of someone else and cannot override.
337 // getUsers obtains users in a group, as specififed by options.
338 function getUsers($options) {
339 $mdb2 = getConnection();
341 $group_id = $this->getGroup();
342 $org_id = $this->org_id;
344 $skipClients = !isset($options['include_clients']);
345 $includeSelf = isset($options['include_self']);
347 $select_part = 'select u.id, u.group_id, u.name';
348 if (isset($options['include_login'])) $select_part .= ', u.login';
349 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
350 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
352 $from_part = ' from tt_users u';
355 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
356 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
358 $where_part = " where u.org_id = $org_id and u.group_id = $group_id";
359 if (isset($options['status']))
360 $where_part .= ' and u.status = '.(int)$options['status'];
362 $where_part .= ' and u.status is not null';
364 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
366 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
369 $order_part = " order by upper(u.name)";
371 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
372 $res = $mdb2->query($sql);
373 $user_list = array();
374 if (is_a($res, 'PEAR_Error'))
377 while ($val = $res->fetchRow()) {
379 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
381 continue; // Skip adding clients.
386 if (isset($options['self_first'])) {
387 // Put own entry at the front.
388 $cnt = count($user_list);
389 for($i = 0; $i < $cnt; $i++) {
390 if ($user_list[$i]['id'] == $this->id) {
391 $self = $user_list[$i]; // Found self.
392 array_unshift($user_list, $self); // Put own entry at the front.
393 array_splice($user_list, $i+1, 1); // Remove duplicate.
400 // getGroupsForDropdown obtains an array of groups to populate "Group" dropdown.
402 // - User home group.
403 // - The entire stack of groups all the way down to current on behalf group.
404 // - All immediate children of the current on behalf group.
405 // This allows user to navigate easily to home group, anything in between, and 1 level below.
407 // Note 1: group dropdown is, by design, to be placed on all pages where "relevant",
408 // such as users.php, projects.php, tasks.php, etc. But some features may be disabled
409 // in some groups. We should check for feature availability on group change
410 // in post and redirect to feature_disabled.php when this happens.
411 // This will allow us to keep dropdown content consistent on all pages.
412 // Filtering content of the dropdown does not seem right.
414 // Note 2: Menu should display according to $user home group settings.
415 // Pages, should look according to $user->behalfGroup settings (if set).
416 // For example, if home group allows tasks, menu should display Tasks,
417 // even when we are on behalf of a subgroup without tasks.
419 // Note 3: Language of all pages should be as in $user home group even when
420 // subgroups have a different language.
421 function getGroupsForDropdown() {
422 $mdb2 = getConnection();
424 // Start with subgroups.
426 $group_id = $this->getGroup();
427 $sql = "select id, name from tt_groups where org_id = $this->org_id and parent_id = $group_id and status = 1";
428 $res = $mdb2->query($sql);
429 if (!is_a($res, 'PEAR_Error')) {
430 while ($val = $res->fetchRow()) {
435 // Add current on behalf group to the beginning of array.
436 $selected_group_id = ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
437 $selected_group_name = ($this->behalf_group_id ? $this->behalf_group_name : $this->group_name);
438 array_unshift($groups, array('id'=>$selected_group_id,'name'=>$selected_group_name));
440 // Iterate all the way to the home group, starting with selected ("on behalf") group.
441 $current_group_id = $selected_group_id;
442 while ($current_group_id != $this->group_id) {
443 $sql = "select parent_id from tt_groups where org_id = $this->org_id and id = $current_group_id and status = 1";
444 $res = $mdb2->query($sql);
445 if (is_a($res, 'PEAR_Error')) return false;
447 $val = $res->fetchRow();
448 $parent_id = $val['parent_id'];
450 // Get parent group name.
451 $sql = "select name from tt_groups where org_id = $this->org_id and id = $parent_id and status = 1";
452 $res = $mdb2->query($sql);
453 if (is_a($res, 'PEAR_Error')) return false;
454 $val = $res->fetchRow();
455 if (!$val) return false;
456 array_unshift($groups, array('id'=>$parent_id,'name'=>$val['name']));
457 $current_group_id = $parent_id;
465 // getSubgroups obtains a list of immediate subgroups.
466 function getSubgroups($group_id = null) {
467 $mdb2 = getConnection();
469 if (!$group_id) $group_id = $this->getGroup();
471 $sql = "select id, name, description from tt_groups where org_id = $this->org_id".
472 " and parent_id = $group_id and status is not null order by upper(name)";
473 $res = $mdb2->query($sql);
474 if (!is_a($res, 'PEAR_Error')) {
475 while ($val = $res->fetchRow()) {
482 // getUserDetails function is used to manage users in group and returns user details.
483 // At the moment, the function is used for user edits and deletes.
484 function getUserDetails($user_id) {
485 if (!$this->can('manage_users')) return false;
487 $mdb2 = getConnection();
488 $group_id = $this->getGroup();
489 $org_id = $this->org_id;
491 // Determine max rank. If we are searching in on behalf group
492 // then rank restriction does not apply.
493 $max_rank = $this->behalfGroup ? MAX_RANK : $this->rank;
495 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
496 " left join tt_roles r on (u.role_id = r.id)".
497 " where u.id = $user_id and u.group_id = $group_id and u.org_id = $org_id and u.status is not null".
498 " and (r.rank < $max_rank or (r.rank = $max_rank and u.id = $this->id))"; // Users with lesser roles or self.
499 $res = $mdb2->query($sql);
500 if (!is_a($res, 'PEAR_Error')) {
501 $val = $res->fetchRow();
507 // checkBehalfId checks whether behalf_id is appropriate.
508 // On behalf user must be active and have lower rank if the user is from home group,
510 // - subgroup must ve valid;
511 // - user should be a member of it.
512 function checkBehalfId() {
513 if (!$this->behalf_group_id) {
514 // Checking user from home group.
515 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
516 $users = $this->getUsers($options);
517 foreach($users as $one_user) {
518 if ($one_user['id'] == $this->behalf_id)
522 // Checking user from a subgroup.
523 $group_id = $this->behalf_group_id;
524 if (!$this->isSubgroupValid($group_id))
527 // So far, so good. Check user now.
528 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>MAX_RANK);
529 $users = $this->getUsers($options);
530 foreach($users as $one_user) {
531 if ($one_user['id'] == $this->behalf_id)
538 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
541 // Needed for situations when user does not have do_own_something right.
542 // Example: has view_charts but does not have view_own_charts.
543 // In this case we still allow access to charts, but set behalf_id to someone else.
544 // Another example: working in a subgroup on behalf of someone else.
545 function adjustBehalfId() {
546 $group_id = $this->behalf_group_id ? $this->behalf_group_id : $this->group_id;
547 $rank = $this->getMaxRankForGroup($group_id);
549 // Adjust to first found user in group.
550 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank);
551 $users = $this->getUsers($options);
552 foreach($users as $one_user) {
553 // Fake loop to access first element.
554 $this->behalf_id = $one_user['id'];
555 $this->behalf_name = $one_user['name'];
556 $_SESSION['behalf_id'] = $this->behalf_id;
557 $_SESSION['behalf_name'] = $this->behalf_name;
563 // updateGroup updates group information with new data.
564 function updateGroup($fields) {
565 if (!($this->can('manage_basic_settings') ||
566 $this->can('manage_advanced_settings') ||
567 $this->can('manage_features'))) return false;
568 // TODO: update the above for subgroup updates.
570 $group_id = $fields['group_id'];
571 if ($group_id && !$this->isGroupValid($group_id)) return false;
573 $mdb2 = getConnection();
574 if (!$group_id) $group_id = $this->getGroup();
576 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
577 if (isset($fields['description'])) $description_part = ', description = '.$mdb2->quote($fields['description']);
578 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
579 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
580 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
581 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
582 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
583 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
584 if (isset($fields['tracking_mode'])) {
585 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
586 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
587 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
589 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
590 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
591 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
592 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
593 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
594 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
595 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
596 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
598 $parts = trim($name_part.$description_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
599 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
600 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
602 $sql = "update tt_groups set $parts where id = $group_id and org_id = $this->org_id";
603 $affected = $mdb2->exec($sql);
604 if (is_a($affected, 'PEAR_Error')) return false;
609 // markUserDeleted marks a user in group as deleted.
610 function markUserDeleted($user_id) {
611 if (!$this->can('manage_users') || $this->id == $user_id)
614 // Make sure we operate on a legit user.
615 $user_details = $this->getUserDetails($user_id);
616 if (!$user_details) return false;
618 $mdb2 = getConnection();
619 $group_id = $this->getGroup();
620 $org_id = $this->org_id;
622 // Mark user to project binds as deleted.
623 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id".
624 " and group_id = $group_id and org_id = $org_id";
625 $affected = $mdb2->exec($sql);
626 if (is_a($affected, 'PEAR_Error'))
629 // Mark user favorite reports as deleted.
630 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id".
631 " and group_id = $group_id and org_id = $org_id";
632 $affected = $mdb2->exec($sql);
633 if (is_a($affected, 'PEAR_Error'))
636 // Mark user as deleted.
637 $sql = "update tt_users set status = NULL where id = $user_id".
638 " and group_id = $group_id and org_id = $org_id";
639 $affected = $mdb2->exec($sql);
640 if (is_a($affected, 'PEAR_Error'))
646 // enablePlugin either enables or disables a specific plugin for group.
647 function enablePlugin($plugin, $enable = true)
649 if (!$this->can('manage_advanced_settings'))
650 return false; // Note: enablePlugin is currently only used on week_view.php.
651 // So, it's not really a plugin we are enabling, but rather week view display options.
652 // Therefore, a check for manage_advanced_settings, not manage_features.
654 $plugin_array = explode(',', $this->plugins);
655 if ($enable && !in_array($plugin, $plugin_array))
656 $plugin_array[] = $plugin; // Add plugin to array.
658 if (!$enable && in_array($plugin, $plugin_array)) {
659 $key = array_search($plugin, $plugin_array);
661 unset($plugin_array[$key]); // Remove plugin from array.
664 $plugins = implode(',', $plugin_array);
665 if ($plugins != $this->plugins) {
666 if (!$this->updateGroup(array('plugins' => $plugins)))
668 $this->plugins = $plugins;
674 // isUserValid determines if a user is valid for on behalf work.
675 function isUserValid($user_id) {
676 if ($user_id == $this->id)
678 return ($this->getUserDetails($user_id) != null);
681 // isGroupValid determines if a group is valid for user.
682 function isGroupValid($group_id) {
683 if ($group_id == $this->group_id)
686 return $this->isSubgroupValid($group_id);
689 // isSubgroupValid determines if a subgroup is valid for user.
690 // A subgroup is valid if:
691 // - user can manage_subgroups;
692 // - subgroup is either a direct child of user group, or "on the path"
693 // to it (grand-child, etc.).
694 function isSubgroupValid($subgroup_id) {
695 if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
697 $current_group_id = $subgroup_id;
698 while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
699 if ($parent_group_id == $this->group_id) {
700 return true; // Found it.
702 $current_group_id = $parent_group_id;
707 // getMaxRankForGroup determines effective user rank for a user in a given group.
708 // For home group it is the existing user rank (as per role) minus 1.
709 // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
710 function getMaxRankForGroup($group_id) {
712 $max_rank = 0; // Start safely.
713 if ($this->group_id == $group_id) {
714 $max_rank = $this->rank - 1;
718 if ($this->isSubgroupValid($group_id))
719 $max_rank = MAX_RANK;
724 // getUserPartForHeader constructs a string for user to display on pages header.
725 // It changes with "on behalf" attributes for both user and group.
726 function getUserPartForHeader() {
728 if (!$this->id) return null;
730 $user_part = htmlspecialchars($this->name);
731 $user_part .= ' - '.htmlspecialchars($this->role_name);
732 if ($this->behalf_id) {
733 $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
735 if ($this->behalf_group_id) {
736 $user_part .= ', <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_group_name).'</span>';
738 if ($this->group_name) // Note: we did not require group names in the past.
739 $user_part .= ', '.$this->group_name;
744 // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
745 function setOnBehalfGroup($group_id) {
747 // Unset things first.
748 $this->behalf_group_id = null;
749 $this->behalf_group_name = null;
750 $this->behalf_id = null;
751 $this->behalf_name = null;
752 unset($this->behalfGroup);
753 unset($_SESSION['behalf_group_id']);
754 unset($_SESSION['behalf_group_name']);
755 unset($_SESSION['behalf_id']);
756 unset($_SESSION['behalf_name']);
758 // Do not do anything if we don't have rights.
759 if (!$this->can('manage_subgroups')) return;
761 // No need to set if group is our home group.
762 if ($group_id == $this->group_id) return;
764 // No need to set if subgroup is not valid.
765 if (!$this->isSubgroupValid($group_id)) return;
767 // We are good to set on behalf group.
768 $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
769 $_SESSION['behalf_group_id'] = $group_id;
770 $_SESSION['behalf_group_name'] = $onBehalfGroupName;
771 $this->behalf_group_id = $group_id;
772 $this->behalf_group_name = $onBehalfGroupName;
774 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
776 // Adjust on behalf user.
777 $this->adjustBehalfId();
781 // setOnBehalfUser sets on behalf user both the object and the session.
782 function setOnBehalfUser($user_id) {
784 // Unset things first.
785 $this->behalf_id = null;
786 $this->behalf_name = null;
787 unset($_SESSION['behalf_id']);
788 unset($_SESSION['behalf_name']);
790 // No need to set if user is us.
791 if ($user_id == $this->id) return;
793 // No need to set if user id is not valid.
794 if (!$this->isUserValid($user_id)) return;
796 // We are good to set on behalf user.
797 $onBehalfUserName = ttUserHelper::getUserName($user_id);
798 $_SESSION['behalf_id'] = $user_id;
799 $_SESSION['behalf_name'] = $onBehalfUserName;
800 $this->behalf_id = $user_id;
801 $this->behalf_name = $onBehalfUserName;