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.
75 var $minutes_in_unit = 15; // Number of minutes in unit for Work units plugin.
76 var $first_unit_threshold = 0;// Threshold for 1st unit for Work units plugin.
77 var $unit_totals_only = 0; // Totals only option for the Work units plugin.
79 var $behalfGroup = null; // A ttGroup instance with on behalf group attributes.
82 function __construct($login, $id = null) {
83 if (!$login && !$id) {
84 // nothing to initialize
88 $mdb2 = getConnection();
90 $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,
91 g.org_id, g.name as group_name, g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
92 g.tracking_mode, g.project_required, g.task_required, g.record_type,
93 g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
94 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 ";
98 $sql .= "u.login = ".$mdb2->quote($login);
99 $sql .= " AND u.status = 1";
101 $res = $mdb2->query($sql);
102 if (is_a($res, 'PEAR_Error')) {
106 $val = $res->fetchRow();
107 if ($val['id'] > 0) {
108 $this->login = $val['login'];
109 $this->name = $val['name'];
110 $this->id = $val['id'];
111 $this->org_id = $val['org_id'];
112 $this->group_id = $val['group_id'];
113 $this->role_id = $val['role_id'];
114 $this->role_name = $val['role_name'];
115 $this->rights = explode(',', $val['rights']);
116 $this->rank = $val['rank'];
117 $this->client_id = $val['client_id'];
118 $this->is_client = $this->client_id && !in_array('track_own_time', $this->rights);
119 $this->email = $val['email'];
120 $this->lang = $val['lang'];
121 $this->decimal_mark = $val['decimal_mark'];
122 $this->date_format = $val['date_format'];
123 $this->time_format = $val['time_format'];
124 $this->week_start = $val['week_start'];
125 $this->tracking_mode = $val['tracking_mode'];
126 $this->project_required = $val['project_required'];
127 $this->task_required = $val['task_required'];
128 $this->record_type = $val['record_type'];
129 $this->bcc_email = $val['bcc_email'];
130 $this->allow_ip = $val['allow_ip'];
131 $this->password_complexity = $val['password_complexity'];
132 $this->group_name = $val['group_name'];
133 $this->currency = $val['currency'];
134 $this->plugins = $val['plugins'];
135 $this->lock_spec = $val['lock_spec'];
136 $this->workday_minutes = $val['workday_minutes'];
137 $this->custom_logo = $val['custom_logo'];
139 $this->config = $val['config'];
140 $config = new ttConfigHelper($this->config);
141 // Set user config options.
142 $this->show_holidays = $config->getDefinedValue('show_holidays');
143 $this->punch_mode = $config->getDefinedValue('punch_mode');
144 $this->allow_overlap = $config->getDefinedValue('allow_overlap');
145 $this->future_entries = $config->getDefinedValue('future_entries');
146 if ($this->isPluginEnabled('wu')) {
147 $minutes_in_unit = $config->getIntValue('minutes_in_unit', 15);
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 getUser returns user id on behalf of whom the current user is operating.
171 return ($this->behalf_id ? $this->behalf_id : $this->id);
174 // The getGroup returns group id on behalf of which the current user is operating.
175 function getGroup() {
176 return ($this->behalfGroup ? $this->behalfGroup->id : $this->group_id);
179 // getDecimalMark returns decimal mark for active group.
180 function getDecimalMark() {
181 return ($this->behalfGroup ? $this->behalfGroup->decimal_mark : $this->decimal_mark);
184 // getDateFormat returns date format for active group.
185 function getDateFormat() {
186 return ($this->behalfGroup ? $this->behalfGroup->date_format : $this->date_format);
189 // getTimeFormat returns time format for active group.
190 function getTimeFormat() {
191 return ($this->behalfGroup ? $this->behalfGroup->time_format : $this->time_format);
194 // getTrackingMode returns tracking mode for active group.
195 function getTrackingMode() {
196 return ($this->behalfGroup ? $this->behalfGroup->tracking_mode : $this->tracking_mode);
199 // getRecordType returns record type for active group.
200 function getRecordType() {
201 return ($this->behalfGroup ? $this->behalfGroup->record_type : $this->record_type);
204 // getCurrency returns currency string for active group.
205 function getCurrency() {
206 return ($this->behalfGroup ? $this->behalfGroup->currency : $this->currency);
209 // getPlugins returns plugins string for active group.
210 function getPlugins() {
211 return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins);
214 // getLockSpec returns lock specification for active group.
215 function getLockSpec() {
216 return ($this->behalfGroup ? $this->behalfGroup->lock_spec : $this->lock_spec);
219 // getWorkdayMinutes returns workday_minutes for active group.
220 function getWorkdayMinutes() {
221 return ($this->behalfGroup ? $this->behalfGroup->workday_minutes : $this->workday_minutes);
224 // getConfig returns config string for active group.
225 function getConfig() {
226 return ($this->behalfGroup ? $this->behalfGroup->config : $this->config);
229 // getConfigOption returns true if an option is defined for group.
230 // This helps us keeping a set of user attributes smaller.
231 // We determine whether the option is set only on pages that need to know.
232 // For example: confirm_save is used only on time and expense edit pages.
233 function getConfigOption($name) {
234 $config = new ttConfigHelper($this->getConfig());
235 return $config->getDefinedValue($name);
238 // getConfigInt retruns an integer value defined in a group, or false.
239 function getConfigInt($name, $defaultVal) {
240 $config = new ttConfigHelper($this->getConfig());
241 return $config->getIntValue($name, $defaultVal);
244 // can - determines whether user has a right to do something.
245 function can($do_something) {
246 return in_array($do_something, $this->rights);
249 // isClient - determines whether current user is a client.
250 function isClient() {
251 return $this->is_client;
254 // isPluginEnabled checks whether a plugin is enabled for user.
255 function isPluginEnabled($plugin)
257 return in_array($plugin, explode(',', $this->getPlugins()));
260 // getAssignedProjects - returns an array of assigned projects.
261 function getAssignedProjects()
264 $mdb2 = getConnection();
266 $user_id = $this->getUser();
267 $group_id = $this->getGroup();
268 $org_id = $this->org_id;
270 // Do a query with inner join to get assigned projects.
271 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p".
272 " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
273 " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by p.name";
274 $res = $mdb2->query($sql);
275 if (!is_a($res, 'PEAR_Error')) {
276 while ($val = $res->fetchRow()) {
283 // getAssignedTasks - returns an array of assigned tasks.
284 function getAssignedTasks()
286 // Start with projects;
287 $projects = $this->getAssignedProjects();
288 if (!$projects) return false;
290 // Build an array of task ids.
292 foreach($projects as $project) {
293 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
294 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
296 if (!$task_ids) return false;
298 // Get task descriptions.
300 $mdb2 = getConnection();
301 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
303 $group_id = $this->getGroup();
304 $org_id = $this->org_id;
306 $sql = "select id, name, description from tt_tasks".
307 " where group_id = $group_id and org_id = $org_id and status = 1 and id in ($tasks) order by name";
308 $res = $mdb2->query($sql);
309 if (!is_a($res, 'PEAR_Error')) {
310 while ($val = $res->fetchRow()) {
317 // getAssignedClients - returns an array of clients assigned to own projects.
318 function getAssignedClients()
320 // Start with projects;
321 $projects = $this->getAssignedProjects();
322 if (!$projects) return false;
323 $assigned_project_ids = array();
324 foreach($projects as $project) {
325 $assigned_project_ids[] = $project['id'];
328 $mdb2 = getConnection();
330 $group_id = $this->getGroup();
331 $org_id = $this->org_id;
333 // Get active clients for group.
335 $sql = "select id, name, address, projects from tt_clients where group_id = $group_id and org_id = $org_id and status = 1";
336 $res = $mdb2->query($sql);
337 if (!is_a($res, 'PEAR_Error')) {
338 while ($val = $res->fetchRow()) {
339 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
340 if (array_intersect($assigned_project_ids, $client_project_ids))
341 $clients[] = $val; // Add client if one of user projects is a client project, too.
347 // isDateLocked checks whether a specifc date is locked for modifications.
348 function isDateLocked($date)
350 if (!$this->isPluginEnabled('lk'))
351 return false; // Locking feature is disabled.
353 if (!$this->getLockSpec())
354 return false; // There is no lock specification.
356 if (!$this->behalf_id && $this->can('override_own_date_lock'))
357 return false; // User is working as self and can override own date lock.
359 if ($this->behalf_id && $this->can('override_date_lock'))
360 return false; // User is working on behalf of someone else and can override date lock.
362 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
363 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
365 // Calculate the last occurrence of a lock.
366 $last = tdCron::getLastOccurrence($this->getLockSpec(), time());
367 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
368 if ($date->before($lockdate))
374 // canOverridePunchMode checks whether a user can override punch mode in a situation.
375 function canOverridePunchMode()
377 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
378 return false; // User is working as self and cannot override for self.
380 if ($this->behalf_id && !$this->can('override_punch_mode'))
381 return false; // User is working on behalf of someone else and cannot override.
386 // getUsers obtains users in a group, as specififed by options.
387 function getUsers($options) {
388 $mdb2 = getConnection();
390 $group_id = $this->getGroup();
391 $org_id = $this->org_id;
393 $skipClients = !isset($options['include_clients']);
394 $includeSelf = isset($options['include_self']);
396 $select_part = 'select u.id, u.group_id, u.name';
397 if (isset($options['include_login'])) $select_part .= ', u.login';
398 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
399 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
401 $from_part = ' from tt_users u';
404 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
405 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
407 $where_part = " where u.org_id = $org_id and u.group_id = $group_id";
408 if (isset($options['status']))
409 $where_part .= ' and u.status = '.(int)$options['status'];
411 $where_part .= ' and u.status is not null';
413 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
415 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
418 $order_part = " order by upper(u.name)";
420 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
421 $res = $mdb2->query($sql);
422 $user_list = array();
423 if (is_a($res, 'PEAR_Error'))
426 while ($val = $res->fetchRow()) {
428 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
430 continue; // Skip adding clients.
435 if (isset($options['self_first'])) {
436 // Put own entry at the front.
437 $cnt = count($user_list);
438 for($i = 0; $i < $cnt; $i++) {
439 if ($user_list[$i]['id'] == $this->id) {
440 $self = $user_list[$i]; // Found self.
441 array_unshift($user_list, $self); // Put own entry at the front.
442 array_splice($user_list, $i+1, 1); // Remove duplicate.
449 // getGroupsForDropdown obtains an array of groups to populate "Group" dropdown.
451 // - User home group.
452 // - The entire stack of groups all the way down to current on behalf group.
453 // - All immediate children of the current on behalf group.
454 // This allows user to navigate easily to home group, anything in between, and 1 level below.
456 // Note 1: group dropdown is, by design, to be placed on all pages where "relevant",
457 // such as users.php, projects.php, tasks.php, etc. But some features may be disabled
458 // in some groups. We should check for feature availability on group change
459 // in post and redirect to feature_disabled.php when this happens.
460 // This will allow us to keep dropdown content consistent on all pages.
461 // Filtering content of the dropdown does not seem right.
463 // Note 2: Menu should display according to $user home group settings.
464 // Pages, should look according to $user->behalfGroup settings (if set).
465 // For example, if home group allows tasks, menu should display Tasks,
466 // even when we are on behalf of a subgroup without tasks.
468 // Note 3: Language of all pages should be as in $user home group even when
469 // subgroups have a different language.
470 function getGroupsForDropdown() {
471 $mdb2 = getConnection();
473 // Start with subgroups.
475 $group_id = $this->getGroup();
476 $sql = "select id, name from tt_groups where org_id = $this->org_id and parent_id = $group_id and status = 1";
477 $res = $mdb2->query($sql);
478 if (!is_a($res, 'PEAR_Error')) {
479 while ($val = $res->fetchRow()) {
484 // Add current on behalf group to the beginning of array.
485 $selected_group_id = ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
486 $selected_group_name = ($this->behalf_group_id ? $this->behalf_group_name : $this->group_name);
487 array_unshift($groups, array('id'=>$selected_group_id,'name'=>$selected_group_name));
489 // Iterate all the way to the home group, starting with selected ("on behalf") group.
490 $current_group_id = $selected_group_id;
491 while ($current_group_id != $this->group_id) {
492 $sql = "select parent_id from tt_groups where org_id = $this->org_id and id = $current_group_id and status = 1";
493 $res = $mdb2->query($sql);
494 if (is_a($res, 'PEAR_Error')) return false;
496 $val = $res->fetchRow();
497 $parent_id = $val['parent_id'];
499 // Get parent group name.
500 $sql = "select name from tt_groups where org_id = $this->org_id and id = $parent_id and status = 1";
501 $res = $mdb2->query($sql);
502 if (is_a($res, 'PEAR_Error')) return false;
503 $val = $res->fetchRow();
504 if (!$val) return false;
505 array_unshift($groups, array('id'=>$parent_id,'name'=>$val['name']));
506 $current_group_id = $parent_id;
514 // getSubgroups obtains a list of immediate subgroups.
515 function getSubgroups($group_id = null) {
516 $mdb2 = getConnection();
518 if (!$group_id) $group_id = $this->getGroup();
520 $sql = "select id, name, description from tt_groups where org_id = $this->org_id".
521 " and parent_id = $group_id and status is not null order by upper(name)";
522 $res = $mdb2->query($sql);
523 if (!is_a($res, 'PEAR_Error')) {
524 while ($val = $res->fetchRow()) {
531 // getUserDetails function is used to manage users in group and returns user details.
532 // At the moment, the function is used for user edits and deletes.
533 function getUserDetails($user_id) {
534 if (!$this->can('manage_users')) return false;
536 $mdb2 = getConnection();
537 $group_id = $this->getGroup();
538 $org_id = $this->org_id;
540 // Determine max rank. If we are searching in on behalf group
541 // then rank restriction does not apply.
542 $max_rank = $this->behalfGroup ? MAX_RANK : $this->rank;
544 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
545 " left join tt_roles r on (u.role_id = r.id)".
546 " where u.id = $user_id and u.group_id = $group_id and u.org_id = $org_id and u.status is not null".
547 " and (r.rank < $max_rank or (r.rank = $max_rank and u.id = $this->id))"; // Users with lesser roles or self.
548 $res = $mdb2->query($sql);
549 if (!is_a($res, 'PEAR_Error')) {
550 $val = $res->fetchRow();
556 // checkBehalfId checks whether behalf_id is appropriate.
557 // On behalf user must be active and have lower rank if the user is from home group,
559 // - subgroup must ve valid;
560 // - user should be a member of it.
561 function checkBehalfId() {
562 if (!$this->behalfGroup) {
563 // Checking user from home group.
564 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
565 $users = $this->getUsers($options);
566 foreach($users as $one_user) {
567 if ($one_user['id'] == $this->behalf_id)
571 // Checking user from a subgroup.
572 $group_id = $this->behalfGroup->id;
573 if (!$this->isSubgroupValid($group_id))
576 // So far, so good. Check user now.
577 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>MAX_RANK);
578 $users = $this->getUsers($options);
579 foreach($users as $one_user) {
580 if ($one_user['id'] == $this->behalf_id)
587 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
590 // Needed for situations when user does not have do_own_something right.
591 // Example: has view_charts but does not have view_own_charts.
592 // In this case we still allow access to charts, but set behalf_id to someone else.
593 // Another example: working in a subgroup on behalf of someone else.
594 function adjustBehalfId() {
595 $rank = $this->getMaxRankForGroup($this->getGroup());
597 // Adjust to first found user in group.
598 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
599 $users = $this->getUsers($options);
600 foreach($users as $one_user) {
601 // Fake loop to access first element.
602 $this->behalf_id = $one_user['id'];
603 $this->behalf_name = $one_user['name'];
604 $_SESSION['behalf_id'] = $this->behalf_id;
605 $_SESSION['behalf_name'] = $this->behalf_name;
611 // updateGroup updates group information with new data.
612 function updateGroup($fields) {
613 $mdb2 = getConnection();
615 $group_id = $fields['group_id'];
616 if ($group_id && !$this->isGroupValid($group_id)) return false;
617 if (!$group_id) $group_id = $this->getGroup();
619 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
620 if (isset($fields['description'])) $description_part = ', description = '.$mdb2->quote($fields['description']);
621 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
622 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
623 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
624 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
625 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
626 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
627 if (isset($fields['tracking_mode'])) {
628 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
629 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
630 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
632 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
633 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
634 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
635 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
636 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
637 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
638 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
639 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
641 $parts = trim($name_part.$description_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
642 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
643 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
645 $sql = "update tt_groups set $parts where id = $group_id and org_id = $this->org_id";
646 $affected = $mdb2->exec($sql);
647 if (is_a($affected, 'PEAR_Error')) return false;
652 // markUserDeleted marks a user in group as deleted.
653 function markUserDeleted($user_id) {
654 if (!$this->can('manage_users') || $this->id == $user_id)
657 // Make sure we operate on a legit user.
658 $user_details = $this->getUserDetails($user_id);
659 if (!$user_details) return false;
661 $mdb2 = getConnection();
662 $group_id = $this->getGroup();
663 $org_id = $this->org_id;
665 // Mark user to project binds as deleted.
666 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id".
667 " and group_id = $group_id and org_id = $org_id";
668 $affected = $mdb2->exec($sql);
669 if (is_a($affected, 'PEAR_Error'))
672 // Mark user favorite reports as deleted.
673 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id".
674 " and group_id = $group_id and org_id = $org_id";
675 $affected = $mdb2->exec($sql);
676 if (is_a($affected, 'PEAR_Error'))
679 // Mark user as deleted.
680 $sql = "update tt_users set status = NULL where id = $user_id".
681 " and group_id = $group_id and org_id = $org_id";
682 $affected = $mdb2->exec($sql);
683 if (is_a($affected, 'PEAR_Error'))
689 // enablePlugin either enables or disables a specific plugin for group.
690 function enablePlugin($plugin, $enable = true)
692 if (!$this->can('manage_advanced_settings'))
693 return false; // Note: enablePlugin is currently only used on week_view.php.
694 // So, it's not really a plugin we are enabling, but rather week view display options.
695 // Therefore, a check for manage_advanced_settings, not manage_features.
697 $plugin_array = explode(',', $this->plugins);
698 if ($enable && !in_array($plugin, $plugin_array))
699 $plugin_array[] = $plugin; // Add plugin to array.
701 if (!$enable && in_array($plugin, $plugin_array)) {
702 $key = array_search($plugin, $plugin_array);
704 unset($plugin_array[$key]); // Remove plugin from array.
707 $plugins = implode(',', $plugin_array);
708 if ($plugins != $this->plugins) {
709 if (!$this->updateGroup(array('plugins' => $plugins)))
711 $this->plugins = $plugins;
717 // isUserValid determines if a user is valid for on behalf work.
718 function isUserValid($user_id) {
719 if ($user_id == $this->id)
721 return ($this->getUserDetails($user_id) != null);
724 // isGroupValid determines if a group is valid for user.
725 function isGroupValid($group_id) {
726 if ($group_id == $this->group_id)
729 return $this->isSubgroupValid($group_id);
732 // isSubgroupValid determines if a subgroup is valid for user.
733 // A subgroup is valid if:
734 // - user can manage_subgroups;
735 // - subgroup is either a direct child of user group, or "on the path"
736 // to it (grand-child, etc.).
737 function isSubgroupValid($subgroup_id) {
738 if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
740 $current_group_id = $subgroup_id;
741 while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
742 if ($parent_group_id == $this->group_id) {
743 return true; // Found it.
745 $current_group_id = $parent_group_id;
750 // getMaxRankForGroup determines effective user rank for a user in a given group.
751 // For home group it is the existing user rank (as per role) minus 1.
752 // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
753 function getMaxRankForGroup($group_id) {
755 $max_rank = 0; // Start safely.
756 if ($this->group_id == $group_id) {
757 $max_rank = $this->rank - 1;
761 if ($this->isSubgroupValid($group_id))
762 $max_rank = MAX_RANK;
767 // getUserPartForHeader constructs a string for user to display on pages header.
768 // It changes with "on behalf" attributes for both user and group.
769 function getUserPartForHeader() {
771 if (!$this->id) return null;
773 $user_part = htmlspecialchars($this->name);
774 $user_part .= ' - '.htmlspecialchars($this->role_name);
775 if ($this->behalf_id) {
776 $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
778 if ($this->behalf_group_id) {
779 $user_part .= ', <span class="onBehalf">'.htmlspecialchars($this->behalf_group_name).'</span>';
781 if ($this->group_name) // Note: we did not require group names in the past.
782 $user_part .= ', '.$this->group_name;
787 // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
788 function setOnBehalfGroup($group_id) {
790 // Unset things first.
791 $this->behalf_group_id = null;
792 $this->behalf_group_name = null;
793 $this->behalf_id = null;
794 $this->behalf_name = null;
795 unset($this->behalfGroup);
796 unset($_SESSION['behalf_group_id']);
797 unset($_SESSION['behalf_group_name']);
798 unset($_SESSION['behalf_id']);
799 unset($_SESSION['behalf_name']);
801 // Destroy report bean if it was set in session.
802 $form = new Form('dummyForm');
803 $bean = new ActionForm('reportBean', $form, $request);
804 if ($bean->isSaved()) {
805 $bean->destroyBean();
808 // Do not do anything if we don't have rights.
809 if (!$this->can('manage_subgroups')) return;
811 // No need to set if group is our home group.
812 if ($group_id == $this->group_id) return;
814 // No need to set if subgroup is not valid.
815 if (!$this->isSubgroupValid($group_id)) return;
817 // We are good to set on behalf group.
818 $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
819 $_SESSION['behalf_group_id'] = $group_id;
820 $_SESSION['behalf_group_name'] = $onBehalfGroupName;
821 $this->behalf_group_id = $group_id;
822 $this->behalf_group_name = $onBehalfGroupName;
824 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
826 // Adjust on behalf user to first found user in subgroup.
827 $this->adjustBehalfId();
831 // setOnBehalfUser sets on behalf user both the object and the session.
832 function setOnBehalfUser($user_id) {
834 // Unset things first.
835 $this->behalf_id = null;
836 $this->behalf_name = null;
837 unset($_SESSION['behalf_id']);
838 unset($_SESSION['behalf_name']);
840 // No need to set if user is us.
841 if ($user_id == $this->id) return;
843 // No need to set if user id is not valid.
844 if (!$this->isUserValid($user_id)) return;
846 // We are good to set on behalf user.
847 $onBehalfUserName = ttUserHelper::getUserName($user_id);
848 $_SESSION['behalf_id'] = $user_id;
849 $_SESSION['behalf_name'] = $onBehalfUserName;
850 $this->behalf_id = $user_id;
851 $this->behalf_name = $onBehalfUserName;
855 // The exists() function determines if an active user exists in context of a page.
856 // If we are working as self, true.
857 // If we are working in a subgroup with active users, true.
858 // If we are working in a subgroup without active users, false.
860 if (!$this->behalfGroup)
861 return true; // Working as self.
862 else if ($this->behalfGroup->active_users)
863 return true; // Subgroup has users.