+ " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
+ $affected = $mdb2->exec($sql);
+ return (!is_a($affected, 'PEAR_Error'));
+ }
+
+ // getReportOptions prepares $options array to be used with ttReportHelper
+ // to obtain items for timesheet view.
+ static function getReportOptions($timesheet) {
+ global $user;
+ $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
+ $trackingMode = $user->getTrackingMode();
+ $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
+
+ $options['timesheet_id'] = $timesheet['id'];
+ $options['group_by1'] = 'date';
+ if ($group_by_client || $group_by_project) {
+ $options['group_by2'] = $group_by_client ? 'client' : 'project';
+ }
+ if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
+ $options['group_by3'] = 'project';
+ }
+ return $options;
+ }
+
+ // getApprovers obtains a list of users who can approve a timesheet for a given user
+ // and also have an email to receive a notification about it.
+ static function getApprovers() {
+ global $user;
+ $mdb2 = getConnection();
+
+ $user_id = $user->getUser();
+ $group_id = $user->getGroup();
+ $org_id = $user->org_id;
+
+ $approvers = array();
+ $rank = ttUserHelper::getUserRank($user_id);
+ $sql = "select u.id, u.name, u.email".
+ " from tt_users u".
+ " left join tt_roles r on (r.id = u.role_id)".
+ " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
+ " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
+ $res = $mdb2->query($sql);
+ if (!is_a($res, 'PEAR_Error')) {
+ while ($val = $res->fetchRow()) {
+ $approvers[] = $val;
+ }
+ }
+ return $approvers;
+ }
+
+ // getApprover obtains approver properties such as name and email.
+ static function getApprover($user_id) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $group_id = $user->getGroup();
+ $org_id = $user->org_id;
+
+ $rank = ttUserHelper::getUserRank($user->getUser());
+ $sql = "select u.name, u.email".
+ " from tt_users u".
+ " left join tt_roles r on (r.id = u.role_id)".
+ " where u.id = $user_id and u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
+ " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
+ $res = $mdb2->query($sql);
+ if (!is_a($res, 'PEAR_Error')) {
+ if ($val = $res->fetchRow()) {
+ return $val;
+ }
+ }
+ return false;
+ }
+
+ // markSubmitted marks a timesheet as submitted.
+ static function markSubmitted($fields) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $user_id = $user->getUser();
+ $group_id = $user->getGroup();
+ $org_id = $user->org_id;
+
+ $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
+ $timesheet_id = $fields['timesheet_id'];
+ $sql = "update tt_timesheets set submit_status = 1".$modified_part.
+ " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
+ $affected = $mdb2->exec($sql);
+ return (!is_a($affected, 'PEAR_Error'));
+ }
+
+ // sendSubmitEmail sends a notification to an approver about a timesheet submit.
+ static function sendSubmitEmail($fields) {
+ global $i18n;
+ global $user;
+
+ // Send email to a selected approver.
+ if (!$fields['approver_id']) return true; // No approver, nothing to do.
+
+ $approver = ttTimesheetHelper::getApprover($fields['approver_id']);
+ if (!$approver) return false; // Invalid approver id.
+
+ $fields['to'] = $approver['email'];
+ $fields['subject'] = $i18n->get('form.timesheet_view.submit_subject');
+ $fields['body'] = sprintf($i18n->get('form.timesheet_view.submit_body'), $user->getName());
+
+ return ttTimesheetHelper::sendEmail($fields);
+ }
+
+ // sendApprovedEmail sends a notification to user about a timesheet approval.
+ static function sendApprovedEmail($fields) {
+ global $i18n;
+ global $user;
+
+ // Obtain user email.
+ $user_details = $user->getUserDetails($fields['user_id']);
+ $email = $user_details['email'];
+ if (!$email) return true; // No email to send to, nothing to do.
+
+ $fields['to'] = $email;
+ $fields['subject'] = $i18n->get('form.timesheet_view.approve_subject');
+ $fields['body'] = sprintf($i18n->get('form.timesheet_view.approve_body'), htmlspecialchars($fields['name']), htmlspecialchars($fields['comment']));
+
+ return ttTimesheetHelper::sendEmail($fields);
+ }
+
+ // sendDisapprovedEmail sends a notification to user about a timesheet disapproval.
+ static function sendDisapprovedEmail($fields) {
+ global $i18n;
+ global $user;
+
+ // Obtain user email.
+ $user_details = $user->getUserDetails($fields['user_id']);
+ $email = $user_details['email'];
+ if (!$email) return true; // No email to send to, nothing to do.
+
+ $fields['to'] = $email;
+ $fields['subject'] = $i18n->get('form.timesheet_view.disapprove_subject');
+ $fields['body'] = sprintf($i18n->get('form.timesheet_view.disapprove_body'), htmlspecialchars($fields['name']), htmlspecialchars($fields['comment']));
+
+ return ttTimesheetHelper::sendEmail($fields);
+ }
+
+ // sendEmail is a generic finction that sends a timesheet related email.
+ // TODO: perhaps make it even more generic for the entire application.
+ static function sendEmail($fields, $html = true) {
+ global $i18n;
+ global $user;
+
+ // Send email.
+ import('mail.Mailer');
+ $mailer = new Mailer();
+ $mailer->setCharSet(CHARSET);
+ if ($html)
+ $mailer->setContentType('text/html');
+ $mailer->setSender(SENDER);
+ $mailer->setReceiver($fields['to']);
+ if (!empty($user->bcc_email))
+ $mailer->setReceiverBCC($user->bcc_email);
+ $mailer->setMailMode(MAIL_MODE);
+ if (!$mailer->send($fields['subject'], $fields['body']))
+ return false;
+
+ return true;
+ }
+
+ // markApproved marks a timesheet as approved.
+ static function markApproved($fields) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $user_id = $user->getUser();
+ $group_id = $user->getGroup();
+ $org_id = $user->org_id;
+
+ $timesheet_id = $fields['timesheet_id'];
+ $comment = $fields['comment'];
+
+ $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
+ $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).$modified_part.
+ " where id = $timesheet_id and submit_status = 1 and user_id = $user_id and group_id = $group_id and org_id = $org_id";