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;
+ }
+
// submitTimesheet marks a timesheet as submitted and also sends an email
// to a selected approver.
static function submitTimesheet($fields) {
$sql = "update tt_timesheets set submit_status = 1".
" where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
$affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
+ 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.
- // TODO: send email to approver here...
- // $approver_id = $fields['approver_id'];
+ $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);
+ }
+
+ // 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;
}
return ($this->behalfUser ? $this->behalfUser->id : $this->id);
}
+ // getName returns user name on behalf of whom the current user is operating.
+ function getName() {
+ return ($this->behalfUser ? $this->behalfUser->name : $this->name);
+ }
+
// getQuotaPercent returns quota percent for active user.
function getQuotaPercent() {
return ($this->behalfUser ? $this->behalfUser->quota_percent : $this->quota_percent);
'role.admin.label' => 'Administrator',
'role.admin.low_case_label' => 'administrator',
'role.admin.description' => 'Site adminsitrator.',
+
+// Timesheet View form. See example at https://timetracker.anuko.com/timesheet_view.php.
+// TODO: improve wording, then fill in other translation files.
+'form.timesheet_view.submit_subject' => 'Timesheet approval request',
+'form.timesheet_view.submit_body' => "A new timesheet requires approval.<p>User: %s.",
+'form.timesheet_view.approve_subject' => 'Timesheet approved',
+'form.timesheet_view.approve_body' => "Dear User,\n\nYour timesheet %s was approved.",
+'form.timesheet_view.disapprove_subject' => 'Timesheet not approved',
+'form.timesheet_view.disapprove_subject' => "Dear User,\n\nYour timesheet %s was not approved.\n\n%s",
+'form.timesheet_view.success_message' => 'Notification sent by email.',
);
<br>
<table cellspacing="0" cellpadding="4" width="100%" border="0">
<tr>
- <td align="center"> Anuko Time Tracker 1.18.52.4822 | Copyright © <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
+ <td align="center"> Anuko Time Tracker 1.18.52.4823 | Copyright © <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
<a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
<a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
<a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>
if ($request->isPost()) {
$cl_comment = trim($request->getParameter('comment'));
+ $approver_id = $request->getParameter('approver');
}
$options = ttTimesheetHelper::getReportOptions($timesheet);
'name'=>'approver',
'style'=>'width: 200px;',
'data'=>$approvers,
- 'datakeys'=>array('id','name')));
+ 'datakeys'=>array('id','name','email')));
}
$form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
}
if ($request->getParameter('btn_submit')) {
$fields = array('timesheet_id' => $timesheet['id'],
'approver_id' => $approver_id); // TODO: obtain (and check) approver id above during access checks.
- if (ttTimesheetHelper::submitTimesheet($fields)) {
+ if (!ttTimesheetHelper::submitTimesheet($fields))
+ $err->add($i18n->get('error.db'));
+ if ($err->no() && !ttTimesheetHelper::sendSubmitEmail($fields)) {
+ $err->add($i18n->get('error.mail_send'));
+ }
+ if ($err->no()) {
// Redirect to self.
header('Location: timesheet_view.php?id='.$timesheet['id']);
exit();
- } else
- $err->add($i18n->get('error.db'));
+ }
}
if ($request->getParameter('btn_approve')) {