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');
33 import('form.ActionForm');
36 var $login = null; // User login.
37 var $name = null; // User name.
38 var $id = null; // User id.
39 var $org_id = null; // Organization id.
40 var $group_id = null; // Group id.
41 var $role_id = null; // Role id.
42 var $role_name = null; // Role name.
43 var $rank = null; // User role rank.
44 var $client_id = null; // Client id for client user role.
45 var $behalf_id = null; // User id, on behalf of whom we are working.
46 var $behalf_group_id = null; // Group id, on behalf of which we are working.
47 var $behalf_name = null; // User name, on behalf of whom we are working.
48 var $group_name = null; // Group name.
49 var $behalf_group_name = null;// Group name, on behalf of which we are working.
50 var $email = null; // User email.
51 var $lang = null; // Language.
52 var $decimal_mark = null; // Decimal separator.
53 var $date_format = null; // Date format.
54 var $time_format = null; // Time format.
55 var $week_start = 0; // Week start day.
56 var $show_holidays = 0; // Whether to show holidays in calendar.
57 var $tracking_mode = 0; // Tracking mode.
58 var $project_required = 0; // Whether project selection is required on time entires.
59 var $task_required = 0; // Whether task selection is required on time entires.
60 var $record_type = 0; // Record type (duration vs start and finish, or both).
61 var $punch_mode = 0; // Whether punch mode is enabled for user.
62 var $allow_overlap = 0; // Whether to allow overlapping time entries.
63 var $future_entries = 0; // Whether to allow creating future entries.
64 var $bcc_email = null; // Bcc email.
65 var $allow_ip = null; // Specification from where user is allowed access.
66 var $password_complexity = null; // Password complexity example.
67 var $currency = null; // Currency.
68 var $plugins = null; // Comma-separated list of enabled plugins.
69 var $config = null; // Comma-separated list of miscellaneous config options.
70 var $custom_logo = 0; // Whether to use a custom logo for group.
71 var $lock_spec = null; // Cron specification for record locking.
72 var $workday_minutes = 480; // Number of work minutes in a regular day.
73 var $rights = array(); // An array of user rights such as 'track_own_time', etc.
74 var $is_client = false; // Whether user is a client as determined by missing 'track_own_time' right.
76 var $behalfGroup = null; // A ttGroup instance with on behalf group attributes.
79 function __construct($login, $id = null) {
80 if (!$login && !$id) {
81 // nothing to initialize
85 $mdb2 = getConnection();
87 $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,
88 g.org_id, g.name as group_name, g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
89 g.tracking_mode, g.project_required, g.task_required, g.record_type,
90 g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
91 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 ";
95 $sql .= "u.login = ".$mdb2->quote($login);
96 $sql .= " AND u.status = 1";
98 $res = $mdb2->query($sql);
99 if (is_a($res, 'PEAR_Error')) {
103 $val = $res->fetchRow();
104 if ($val['id'] > 0) {
105 $this->login = $val['login'];
106 $this->name = $val['name'];
107 $this->id = $val['id'];
108 $this->org_id = $val['org_id'];
109 $this->group_id = $val['group_id'];
110 $this->role_id = $val['role_id'];
111 $this->role_name = $val['role_name'];
112 $this->rights = explode(',', $val['rights']);
113 $this->rank = $val['rank'];
114 $this->client_id = $val['client_id'];
115 $this->is_client = $this->client_id && !in_array('track_own_time', $this->rights);
116 $this->email = $val['email'];
117 $this->lang = $val['lang'];
118 $this->decimal_mark = $val['decimal_mark'];
119 $this->date_format = $val['date_format'];
120 $this->time_format = $val['time_format'];
121 $this->week_start = $val['week_start'];
122 $this->tracking_mode = $val['tracking_mode'];
123 $this->project_required = $val['project_required'];
124 $this->task_required = $val['task_required'];
125 $this->record_type = $val['record_type'];
126 $this->bcc_email = $val['bcc_email'];
127 $this->allow_ip = $val['allow_ip'];
128 $this->password_complexity = $val['password_complexity'];
129 $this->group_name = $val['group_name'];
130 $this->currency = $val['currency'];
131 $this->plugins = $val['plugins'];
132 $this->lock_spec = $val['lock_spec'];
133 $this->workday_minutes = $val['workday_minutes'];
134 $this->custom_logo = $val['custom_logo'];
136 $this->config = $val['config'];
137 $config = new ttConfigHelper($this->config);
138 // Set user config options.
139 $this->show_holidays = $config->getDefinedValue('show_holidays');
140 $this->punch_mode = $config->getDefinedValue('punch_mode');
141 $this->allow_overlap = $config->getDefinedValue('allow_overlap');
142 $this->future_entries = $config->getDefinedValue('future_entries');
144 // Set "on behalf" id and name (user).
145 if (isset($_SESSION['behalf_id'])) {
146 $this->behalf_id = $_SESSION['behalf_id'];
147 $this->behalf_name = $_SESSION['behalf_name'];
149 // Set "on behalf" id and name (group).
150 if (isset($_SESSION['behalf_group_id'])) {
151 $this->behalf_group_id = $_SESSION['behalf_group_id'];
152 $this->behalf_group_name = $_SESSION['behalf_group_name'];
154 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
159 // The getUser returns user id on behalf of whom the current user is operating.
161 return ($this->behalf_id ? $this->behalf_id : $this->id);
164 // The getGroup returns group id on behalf of which the current user is operating.
165 function getGroup() {
166 return ($this->behalfGroup ? $this->behalfGroup->id : $this->group_id);
169 // getDecimalMark returns decimal mark for active group.
170 function getDecimalMark() {
171 return ($this->behalfGroup ? $this->behalfGroup->decimal_mark : $this->decimal_mark);
174 // getDateFormat returns date format for active group.
175 function getDateFormat() {
176 return ($this->behalfGroup ? $this->behalfGroup->date_format : $this->date_format);
179 // getTimeFormat returns time format for active group.
180 function getTimeFormat() {
181 return ($this->behalfGroup ? $this->behalfGroup->time_format : $this->time_format);
184 // getTrackingMode returns tracking mode for active group.
185 function getTrackingMode() {
186 return ($this->behalfGroup ? $this->behalfGroup->tracking_mode : $this->tracking_mode);
189 // getRecordType returns record type for active group.
190 function getRecordType() {
191 return ($this->behalfGroup ? $this->behalfGroup->record_type : $this->record_type);
194 // getCurrency returns currency string for active group.
195 function getCurrency() {
196 return ($this->behalfGroup ? $this->behalfGroup->currency : $this->currency);
199 // getPlugins returns plugins string for active group.
200 function getPlugins() {
201 return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins);
204 // getLockSpec returns lock specification for active group.
205 function getLockSpec() {
206 return ($this->behalfGroup ? $this->behalfGroup->lock_spec : $this->lock_spec);
209 // getWorkdayMinutes returns workday_minutes for active group.
210 function getWorkdayMinutes() {
211 return ($this->behalfGroup ? $this->behalfGroup->workday_minutes : $this->workday_minutes);
214 // getConfig returns config string for active group.
215 function getConfig() {
216 return ($this->behalfGroup ? $this->behalfGroup->config : $this->config);
219 // getConfigOption returns true if an option is defined for group.
220 // This helps us keeping a set of user attributes smaller.
221 // We determine whether the option is set only on pages that need to know.
222 // For example: confirm_save is used only on time and expense edit pages.
223 function getConfigOption($name) {
224 $config = new ttConfigHelper($this->getConfig());
225 return $config->getDefinedValue($name);
228 // getConfigInt retruns an integer value defined in a group, or false.
229 function getConfigInt($name, $defaultVal) {
230 $config = new ttConfigHelper($this->getConfig());
231 return $config->getIntValue($name, $defaultVal);
234 // can - determines whether user has a right to do something.
235 function can($do_something) {
236 return in_array($do_something, $this->rights);
239 // isClient - determines whether current user is a client.
240 function isClient() {
241 return $this->is_client;
244 // isPluginEnabled checks whether a plugin is enabled for user.
245 function isPluginEnabled($plugin)
247 return in_array($plugin, explode(',', $this->getPlugins()));
250 // getAssignedProjects - returns an array of assigned projects.
251 function getAssignedProjects()
254 $mdb2 = getConnection();
256 $user_id = $this->getUser();
257 $group_id = $this->getGroup();
258 $org_id = $this->org_id;
260 // Do a query with inner join to get assigned projects.
261 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p".
262 " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
263 " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by p.name";
264 $res = $mdb2->query($sql);
265 if (!is_a($res, 'PEAR_Error')) {
266 while ($val = $res->fetchRow()) {
273 // getAssignedTasks - returns an array of assigned tasks.
274 function getAssignedTasks()
276 // Start with projects;
277 $projects = $this->getAssignedProjects();
278 if (!$projects) return false;
280 // Build an array of task ids.
282 foreach($projects as $project) {
283 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
284 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
286 if (!$task_ids) return false;
288 // Get task descriptions.
290 $mdb2 = getConnection();
291 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
293 $group_id = $this->getGroup();
294 $org_id = $this->org_id;
296 $sql = "select id, name, description from tt_tasks".
297 " where group_id = $group_id and org_id = $org_id and status = 1 and id in ($tasks) order by name";
298 $res = $mdb2->query($sql);
299 if (!is_a($res, 'PEAR_Error')) {
300 while ($val = $res->fetchRow()) {
307 // getAssignedClients - returns an array of clients assigned to own projects.
308 function getAssignedClients()
310 // Start with projects;
311 $projects = $this->getAssignedProjects();
312 if (!$projects) return false;
313 $assigned_project_ids = array();
314 foreach($projects as $project) {
315 $assigned_project_ids[] = $project['id'];
318 $mdb2 = getConnection();
320 $group_id = $this->getGroup();
321 $org_id = $this->org_id;
323 // Get active clients for group.
325 $sql = "select id, name, address, projects from tt_clients where group_id = $group_id and org_id = $org_id and status = 1";
326 $res = $mdb2->query($sql);
327 if (!is_a($res, 'PEAR_Error')) {
328 while ($val = $res->fetchRow()) {
329 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
330 if (array_intersect($assigned_project_ids, $client_project_ids))
331 $clients[] = $val; // Add client if one of user projects is a client project, too.
337 // isDateLocked checks whether a specifc date is locked for modifications.
338 function isDateLocked($date)
340 if (!$this->isPluginEnabled('lk'))
341 return false; // Locking feature is disabled.
343 if (!$this->getLockSpec())
344 return false; // There is no lock specification.
346 if (!$this->behalf_id && $this->can('override_own_date_lock'))
347 return false; // User is working as self and can override own date lock.
349 if ($this->behalf_id && $this->can('override_date_lock'))
350 return false; // User is working on behalf of someone else and can override date lock.
352 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
353 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
355 // Calculate the last occurrence of a lock.
356 $last = tdCron::getLastOccurrence($this->getLockSpec(), time());
357 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
358 if ($date->before($lockdate))
364 // canOverridePunchMode checks whether a user can override punch mode in a situation.
365 function canOverridePunchMode()
367 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
368 return false; // User is working as self and cannot override for self.
370 if ($this->behalf_id && !$this->can('override_punch_mode'))
371 return false; // User is working on behalf of someone else and cannot override.
376 // getUsers obtains users in a group, as specififed by options.
377 function getUsers($options) {
378 $mdb2 = getConnection();
380 $group_id = $this->getGroup();
381 $org_id = $this->org_id;
383 $skipClients = !isset($options['include_clients']);
384 $includeSelf = isset($options['include_self']);
386 $select_part = 'select u.id, u.group_id, u.name';
387 if (isset($options['include_login'])) $select_part .= ', u.login';
388 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
389 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
391 $from_part = ' from tt_users u';
394 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
395 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
397 $where_part = " where u.org_id = $org_id and u.group_id = $group_id";
398 if (isset($options['status']))
399 $where_part .= ' and u.status = '.(int)$options['status'];
401 $where_part .= ' and u.status is not null';
403 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
405 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
408 $order_part = " order by upper(u.name)";
410 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
411 $res = $mdb2->query($sql);
412 $user_list = array();
413 if (is_a($res, 'PEAR_Error'))
416 while ($val = $res->fetchRow()) {
418 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
420 continue; // Skip adding clients.
425 if (isset($options['self_first'])) {
426 // Put own entry at the front.
427 $cnt = count($user_list);
428 for($i = 0; $i < $cnt; $i++) {
429 if ($user_list[$i]['id'] == $this->id) {
430 $self = $user_list[$i]; // Found self.
431 array_unshift($user_list, $self); // Put own entry at the front.
432 array_splice($user_list, $i+1, 1); // Remove duplicate.
439 // getGroupsForDropdown obtains an array of groups to populate "Group" dropdown.
441 // - User home group.
442 // - The entire stack of groups all the way down to current on behalf group.
443 // - All immediate children of the current on behalf group.
444 // This allows user to navigate easily to home group, anything in between, and 1 level below.
446 // Note 1: group dropdown is, by design, to be placed on all pages where "relevant",
447 // such as users.php, projects.php, tasks.php, etc. But some features may be disabled
448 // in some groups. We should check for feature availability on group change
449 // in post and redirect to feature_disabled.php when this happens.
450 // This will allow us to keep dropdown content consistent on all pages.
451 // Filtering content of the dropdown does not seem right.
453 // Note 2: Menu should display according to $user home group settings.
454 // Pages, should look according to $user->behalfGroup settings (if set).
455 // For example, if home group allows tasks, menu should display Tasks,
456 // even when we are on behalf of a subgroup without tasks.
458 // Note 3: Language of all pages should be as in $user home group even when
459 // subgroups have a different language.
460 function getGroupsForDropdown() {
461 $mdb2 = getConnection();
463 // Start with subgroups.
465 $group_id = $this->getGroup();
466 $sql = "select id, name from tt_groups where org_id = $this->org_id and parent_id = $group_id and status = 1";
467 $res = $mdb2->query($sql);
468 if (!is_a($res, 'PEAR_Error')) {
469 while ($val = $res->fetchRow()) {
474 // Add current on behalf group to the beginning of array.
475 $selected_group_id = ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
476 $selected_group_name = ($this->behalf_group_id ? $this->behalf_group_name : $this->group_name);
477 array_unshift($groups, array('id'=>$selected_group_id,'name'=>$selected_group_name));
479 // Iterate all the way to the home group, starting with selected ("on behalf") group.
480 $current_group_id = $selected_group_id;
481 while ($current_group_id != $this->group_id) {
482 $sql = "select parent_id from tt_groups where org_id = $this->org_id and id = $current_group_id and status = 1";
483 $res = $mdb2->query($sql);
484 if (is_a($res, 'PEAR_Error')) return false;
486 $val = $res->fetchRow();
487 $parent_id = $val['parent_id'];
489 // Get parent group name.
490 $sql = "select name from tt_groups where org_id = $this->org_id and id = $parent_id and status = 1";
491 $res = $mdb2->query($sql);
492 if (is_a($res, 'PEAR_Error')) return false;
493 $val = $res->fetchRow();
494 if (!$val) return false;
495 array_unshift($groups, array('id'=>$parent_id,'name'=>$val['name']));
496 $current_group_id = $parent_id;
504 // getSubgroups obtains a list of immediate subgroups.
505 function getSubgroups($group_id = null) {
506 $mdb2 = getConnection();
508 if (!$group_id) $group_id = $this->getGroup();
510 $sql = "select id, name, description from tt_groups where org_id = $this->org_id".
511 " and parent_id = $group_id and status is not null order by upper(name)";
512 $res = $mdb2->query($sql);
513 if (!is_a($res, 'PEAR_Error')) {
514 while ($val = $res->fetchRow()) {
521 // getUserDetails function is used to manage users in group and returns user details.
522 // At the moment, the function is used for user edits and deletes.
523 function getUserDetails($user_id) {
524 if (!$this->can('manage_users')) return false;
526 $mdb2 = getConnection();
527 $group_id = $this->getGroup();
528 $org_id = $this->org_id;
530 // Determine max rank. If we are searching in on behalf group
531 // then rank restriction does not apply.
532 $max_rank = $this->behalfGroup ? MAX_RANK : $this->rank;
534 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
535 " left join tt_roles r on (u.role_id = r.id)".
536 " where u.id = $user_id and u.group_id = $group_id and u.org_id = $org_id and u.status is not null".
537 " and (r.rank < $max_rank or (r.rank = $max_rank and u.id = $this->id))"; // Users with lesser roles or self.
538 $res = $mdb2->query($sql);
539 if (!is_a($res, 'PEAR_Error')) {
540 $val = $res->fetchRow();
546 // checkBehalfId checks whether behalf_id is appropriate.
547 // On behalf user must be active and have lower rank if the user is from home group,
549 // - subgroup must ve valid;
550 // - user should be a member of it.
551 function checkBehalfId() {
552 if (!$this->behalfGroup) {
553 // Checking user from home group.
554 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
555 $users = $this->getUsers($options);
556 foreach($users as $one_user) {
557 if ($one_user['id'] == $this->behalf_id)
561 // Checking user from a subgroup.
562 $group_id = $this->behalfGroup->id;
563 if (!$this->isSubgroupValid($group_id))
566 // So far, so good. Check user now.
567 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>MAX_RANK);
568 $users = $this->getUsers($options);
569 foreach($users as $one_user) {
570 if ($one_user['id'] == $this->behalf_id)
577 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
580 // Needed for situations when user does not have do_own_something right.
581 // Example: has view_charts but does not have view_own_charts.
582 // In this case we still allow access to charts, but set behalf_id to someone else.
583 // Another example: working in a subgroup on behalf of someone else.
584 function adjustBehalfId() {
585 $rank = $this->getMaxRankForGroup($this->getGroup());
587 // Adjust to first found user in group.
588 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
589 $users = $this->getUsers($options);
590 foreach($users as $one_user) {
591 // Fake loop to access first element.
592 $this->behalf_id = $one_user['id'];
593 $this->behalf_name = $one_user['name'];
594 $_SESSION['behalf_id'] = $this->behalf_id;
595 $_SESSION['behalf_name'] = $this->behalf_name;
601 // updateGroup updates group information with new data.
602 function updateGroup($fields) {
603 $mdb2 = getConnection();
605 $group_id = $fields['group_id'];
606 if ($group_id && !$this->isGroupValid($group_id)) return false;
607 if (!$group_id) $group_id = $this->getGroup();
609 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
610 if (isset($fields['description'])) $description_part = ', description = '.$mdb2->quote($fields['description']);
611 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
612 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
613 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
614 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
615 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
616 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
617 if (isset($fields['tracking_mode'])) {
618 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
619 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
620 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
622 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
623 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
624 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
625 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
626 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
627 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
628 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
629 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
631 $parts = trim($name_part.$description_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
632 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
633 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
635 $sql = "update tt_groups set $parts where id = $group_id and org_id = $this->org_id";
636 $affected = $mdb2->exec($sql);
637 if (is_a($affected, 'PEAR_Error')) return false;
642 // markUserDeleted marks a user in group as deleted.
643 function markUserDeleted($user_id) {
644 if (!$this->can('manage_users') || $this->id == $user_id)
647 // Make sure we operate on a legit user.
648 $user_details = $this->getUserDetails($user_id);
649 if (!$user_details) return false;
651 $mdb2 = getConnection();
652 $group_id = $this->getGroup();
653 $org_id = $this->org_id;
655 // Mark user to project binds as deleted.
656 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id".
657 " and group_id = $group_id and org_id = $org_id";
658 $affected = $mdb2->exec($sql);
659 if (is_a($affected, 'PEAR_Error'))
662 // Mark user favorite reports as deleted.
663 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id".
664 " and group_id = $group_id and org_id = $org_id";
665 $affected = $mdb2->exec($sql);
666 if (is_a($affected, 'PEAR_Error'))
669 // Mark user as deleted.
670 $sql = "update tt_users set status = NULL where id = $user_id".
671 " and group_id = $group_id and org_id = $org_id";
672 $affected = $mdb2->exec($sql);
673 if (is_a($affected, 'PEAR_Error'))
679 // enablePlugin either enables or disables a specific plugin for group.
680 function enablePlugin($plugin, $enable = true)
682 if (!$this->can('manage_advanced_settings'))
683 return false; // Note: enablePlugin is currently only used on week_view.php.
684 // So, it's not really a plugin we are enabling, but rather week view display options.
685 // Therefore, a check for manage_advanced_settings, not manage_features.
687 $plugin_array = explode(',', $this->plugins);
688 if ($enable && !in_array($plugin, $plugin_array))
689 $plugin_array[] = $plugin; // Add plugin to array.
691 if (!$enable && in_array($plugin, $plugin_array)) {
692 $key = array_search($plugin, $plugin_array);
694 unset($plugin_array[$key]); // Remove plugin from array.
697 $plugins = implode(',', $plugin_array);
698 if ($plugins != $this->plugins) {
699 if (!$this->updateGroup(array('plugins' => $plugins)))
701 $this->plugins = $plugins;
707 // isUserValid determines if a user is valid for on behalf work.
708 function isUserValid($user_id) {
709 if ($user_id == $this->id)
711 return ($this->getUserDetails($user_id) != null);
714 // isGroupValid determines if a group is valid for user.
715 function isGroupValid($group_id) {
716 if ($group_id == $this->group_id)
719 return $this->isSubgroupValid($group_id);
722 // isSubgroupValid determines if a subgroup is valid for user.
723 // A subgroup is valid if:
724 // - user can manage_subgroups;
725 // - subgroup is either a direct child of user group, or "on the path"
726 // to it (grand-child, etc.).
727 function isSubgroupValid($subgroup_id) {
728 if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
730 $current_group_id = $subgroup_id;
731 while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
732 if ($parent_group_id == $this->group_id) {
733 return true; // Found it.
735 $current_group_id = $parent_group_id;
740 // getMaxRankForGroup determines effective user rank for a user in a given group.
741 // For home group it is the existing user rank (as per role) minus 1.
742 // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
743 function getMaxRankForGroup($group_id) {
745 $max_rank = 0; // Start safely.
746 if ($this->group_id == $group_id) {
747 $max_rank = $this->rank - 1;
751 if ($this->isSubgroupValid($group_id))
752 $max_rank = MAX_RANK;
757 // getUserPartForHeader constructs a string for user to display on pages header.
758 // It changes with "on behalf" attributes for both user and group.
759 function getUserPartForHeader() {
761 if (!$this->id) return null;
763 $user_part = htmlspecialchars($this->name);
764 $user_part .= ' - '.htmlspecialchars($this->role_name);
765 if ($this->behalf_id) {
766 $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
768 if ($this->behalf_group_id) {
769 $user_part .= ', <span class="onBehalf">'.htmlspecialchars($this->behalf_group_name).'</span>';
771 if ($this->group_name) // Note: we did not require group names in the past.
772 $user_part .= ', '.$this->group_name;
777 // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
778 function setOnBehalfGroup($group_id) {
780 // Unset things first.
781 $this->behalf_group_id = null;
782 $this->behalf_group_name = null;
783 $this->behalf_id = null;
784 $this->behalf_name = null;
785 unset($this->behalfGroup);
786 unset($_SESSION['behalf_group_id']);
787 unset($_SESSION['behalf_group_name']);
788 unset($_SESSION['behalf_id']);
789 unset($_SESSION['behalf_name']);
791 // Destroy report bean if it was set in session.
792 $form = new Form('dummyForm');
793 $bean = new ActionForm('reportBean', $form, $request);
794 if ($bean->isSaved()) {
795 $bean->destroyBean();
798 // Do not do anything if we don't have rights.
799 if (!$this->can('manage_subgroups')) return;
801 // No need to set if group is our home group.
802 if ($group_id == $this->group_id) return;
804 // No need to set if subgroup is not valid.
805 if (!$this->isSubgroupValid($group_id)) return;
807 // We are good to set on behalf group.
808 $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
809 $_SESSION['behalf_group_id'] = $group_id;
810 $_SESSION['behalf_group_name'] = $onBehalfGroupName;
811 $this->behalf_group_id = $group_id;
812 $this->behalf_group_name = $onBehalfGroupName;
814 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
816 // Adjust on behalf user to first found user in subgroup.
817 $this->adjustBehalfId();
821 // setOnBehalfUser sets on behalf user both the object and the session.
822 function setOnBehalfUser($user_id) {
824 // Unset things first.
825 $this->behalf_id = null;
826 $this->behalf_name = null;
827 unset($_SESSION['behalf_id']);
828 unset($_SESSION['behalf_name']);
830 // No need to set if user is us.
831 if ($user_id == $this->id) return;
833 // No need to set if user id is not valid.
834 if (!$this->isUserValid($user_id)) return;
836 // We are good to set on behalf user.
837 $onBehalfUserName = ttUserHelper::getUserName($user_id);
838 $_SESSION['behalf_id'] = $user_id;
839 $_SESSION['behalf_name'] = $onBehalfUserName;
840 $this->behalf_id = $user_id;
841 $this->behalf_name = $onBehalfUserName;
845 // The exists() function determines if an active user exists in context of a page.
846 // If we are working as self, true.
847 // If we are working in a subgroup with active users, true.
848 // If we are working in a subgroup without active users, false.
850 if (!$this->behalfGroup)
851 return true; // Working as self.
852 else if ($this->behalfGroup->active_users)
853 return true; // Subgroup has users.