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('DateAndTime');
31 // The ttTimeHelper is a class to help with time-related values.
34 // isWeekend determines if $date falls on weekend.
35 static function isWeekend($date) {
36 $weekDay = date('w', strtotime($date));
37 return ($weekDay == WEEKEND_START_DAY || $weekDay == (WEEKEND_START_DAY + 1) % 7);
40 // isHoliday determines if $date falls on a holiday.
41 static function isHoliday($date) {
45 if (!$user->show_holidays) return false;
47 // $date is expected as string in DB_DATEFORMAT.
48 $month = date('m', strtotime($date));
49 $day = date('d', strtotime($date));
50 if (in_array($month.'/'.$day, $i18n->holidays))
56 // isValidTime validates a value as a time string.
57 static function isValidTime($value) {
58 if (strlen($value)==0 || !isset($value)) return false;
61 if ($value == '24:00' || $value == '2400') return true;
63 if (preg_match('/^([0-1]{0,1}[0-9]|[2][0-3]):?[0-5][0-9]$/', $value )) { // 0:00 - 23:59, 000 - 2359
66 if (preg_match('/^([0-1]{0,1}[0-9]|[2][0-4])$/', $value )) { // 0 - 24
71 if (preg_match('/^[1-9]\s?(am|AM|pm|PM)$/', $value)) { // 1 - 9 am
74 if (preg_match('/^(0[1-9]|1[0-2])\s?(am|AM|pm|PM)$/', $value)) { // 01 - 12 am
77 if (preg_match('/^[1-9]:?[0-5][0-9]\s?(am|AM|pm|PM)$/', $value)) { // 1:00 - 9:59 am, 100 - 959 am
80 if (preg_match('/^(0[1-9]|1[0-2]):?[0-5][0-9]\s?(am|AM|pm|PM)$/', $value)) { // 01:00 - 12:59 am, 0100 - 1259 am
87 // isValidDuration validates a value as a time duration string (in hours and minutes).
88 static function isValidDuration($value) {
89 if (strlen($value) == 0 || !isset($value)) return false;
91 if ($value == '24:00' || $value == '2400') return true;
93 if (preg_match('/^([0-1]{0,1}[0-9]|2[0-3]):?[0-5][0-9]$/', $value )) { // 0:00 - 23:59, 000 - 2359
96 if (preg_match('/^([0-1]{0,1}[0-9]|2[0-4])h?$/', $value )) { // 0, 1 ... 24
101 $localizedPattern = '/^([0-1]{0,1}[0-9]|2[0-3])?['.$user->decimal_mark.'][0-9]{1,4}h?$/';
102 if (preg_match($localizedPattern, $value )) { // decimal values like 0.5, 1.25h, ... .. 23.9999h (or with comma)
109 // postedDurationToMinutes - converts a value representing a duration
110 // (usually enetered in a form by a user) to an integer number of minutes.
113 // $duration - user entered duration string. Valid strings are:
114 // 3 or 3h - means 3 hours. Note: h and m letters are not localized.
115 // 0.25 or 0.25h or .25 or .25h - means a quarter of hour.
116 // 0,25 or 0,25h or ,25 or ,25h - same as above for users with comma ad decimal mark.
117 // 1:30 - means 1 hour 30 minutes.
118 // 25m - means 25 minutes.
119 // $max - maximum number of minutes that is valid.
121 // At the moment, we have 2 variations of duration types:
122 // 1) A duration within a day, such as in a time entry.
123 // These are less or equal to 24*60 minutes.
125 // 2) A duration of a monthly quota, with max value of 31*24*60 minutes.
127 // This function is generic to be used for both types.
129 // Returns false if the value cannot be converted.
130 static function postedDurationToMinutes($duration, $max = 1440) {
131 // Handle empty value.
132 if (!isset($duration) || strlen($duration) == 0)
133 return null; // Value is not set. Caller decides whether it is valid or not.
135 // Handle whole hours.
136 if (preg_match('/^\d{1,3}h?$/', $duration )) { // 0 - 999, 0h - 999h
137 $minutes = 60 * trim($duration, 'h');
138 return $minutes > $max ? false : $minutes;
141 // Handle a normalized duration value.
142 if (preg_match('/^\d{1,3}:[0-5][0-9]$/', $duration )) { // 0:00 - 999:59
143 $time_array = explode(':', $duration);
144 $minutes = (int)@$time_array[1] + ((int)@$time_array[0]) * 60;
145 return $minutes > $max ? false : $minutes;
148 // Handle localized fractional hours.
150 $localizedPattern = '/^(\d{1,3})?['.$user->decimal_mark.'][0-9]{1,4}h?$/';
151 if (preg_match($localizedPattern, $duration )) { // decimal values like .5, 1.25h, ... .. 999.9999h (or with comma)
152 if ($user->decimal_mark == ',')
153 $duration = str_replace (',', '.', $duration);
155 $minutes = (int)round(60 * floatval($duration));
156 return $minutes > $max ? false : $minutes;
159 // Handle minutes. Some users enter durations like 10m (meaning 10 minutes).
160 if (preg_match('/^\d{1,5}m$/', $duration )) { // 0m - 99999m
161 $minutes = (int) trim($duration, 'm');
162 return $minutes > $max ? false : $minutes;
165 // Everything else is not a valid duration.
169 // minutesToDuration converts an integer number of minutes into duration string.
170 // Formats returned HH:MM, HHH:MM, HH, or HHH.
171 static function minutesToDuration($minutes, $abbreviate = false) {
172 if ($minutes < 0) return false;
174 $hours = (string) (int)($minutes / 60);
175 $mins = (string) round(fmod($minutes, 60));
176 if (strlen($mins) == 1)
178 if ($abbreviate && $mins == '00')
181 return $hours.':'.$mins;
184 // toMinutes - converts a time string in format 00:00 to a number of minutes.
185 static function toMinutes($value) {
186 $time_a = explode(':', $value);
187 return (int)@$time_a[1] + ((int)@$time_a[0]) * 60;
190 // toAbsDuration - converts a number of minutes to format 0:00
191 // even if $minutes is negative.
192 static function toAbsDuration($minutes, $abbreviate = false){
193 $hours = (string)((int)abs($minutes / 60));
194 $mins = (string) round(abs(fmod($minutes, 60)));
195 if (strlen($mins) == 1)
197 if ($abbreviate && $mins == '00')
200 return $hours.':'.$mins;
203 // toDuration - calculates duration between start and finish times in 00:00 format.
204 static function toDuration($start, $finish) {
205 $duration_minutes = ttTimeHelper::toMinutes($finish) - ttTimeHelper::toMinutes($start);
206 if ($duration_minutes <= 0) return false;
208 return ttTimeHelper::toAbsDuration($duration_minutes);
211 // The to12HourFormat function converts a 24-hour time value (such as 15:23) to 12 hour format (03:23 PM).
212 static function to12HourFormat($value) {
213 if ('24:00' == $value) return '12:00 AM';
215 $time_a = explode(':', $value);
217 $res = (string)((int)$time_a[0] - 12).':'.$time_a[1].' PM';
218 elseif ($time_a[0] == 12)
220 elseif ($time_a[0] == 0)
221 $res = '12:'.$time_a[1].' AM';
227 // The to24HourFormat function attempts to convert a string value (human readable notation of time of day)
228 // to a 24-hour time format HH:MM.
229 static function to24HourFormat($value) {
232 // Algorithm: use regular expressions to find a matching pattern, starting with most popular patterns first.
233 $tmp_val = trim($value);
236 if (preg_match('/^([01][0-9]|2[0-3]):[0-5][0-9]$/', $tmp_val)) { // 00:00 - 23:59
237 // We already have a 24-hour format. Just return it.
241 if (preg_match('/^[0-9]:[0-5][0-9]$/', $tmp_val)) { // 0:00 - 9:59
242 // This is a 24-hour format without a leading zero. Add 0 and return.
246 if (preg_match('/^[0-9]$/', $tmp_val)) { // 0 - 9
247 // Single digit. Assuming hour number.
248 $res = '0'.$tmp_val.':00';
251 if (preg_match('/^([01][0-9]|2[0-4])$/', $tmp_val)) { // 00 - 24
252 // Two digit hour number.
253 $res = $tmp_val.':00';
256 if (preg_match('/^[0-9][0-5][0-9]$/', $tmp_val)) { // 000 - 959
257 // Missing colon. We'll assume the first digit is the hour, the rest is minutes.
258 $tmp_arr = str_split($tmp_val);
259 $res = '0'.$tmp_arr[0].':'.$tmp_arr[1].$tmp_arr[2];
262 if (preg_match('/^([01][0-9]|2[0-3])[0-5][0-9]$/', $tmp_val)) { // 0000 - 2359
263 // Missing colon. We'll assume the first 2 digits are the hour, the rest is minutes.
264 $tmp_arr = str_split($tmp_val);
265 $res = $tmp_arr[0].$tmp_arr[1].':'.$tmp_arr[2].$tmp_arr[3];
268 // Special handling for midnight.
269 if ($tmp_val == '24:00' || $tmp_val == '2400')
272 // 12 hour AM patterns.
273 if (preg_match('/.(am|AM)$/', $tmp_val)) {
275 // The $value ends in am or AM. Strip it.
276 $tmp_val = rtrim(substr($tmp_val, 0, -2));
278 // Special case to handle 12, 12:MM, and 12MM AM.
279 if (preg_match('/^12:?([0-5][0-9])?$/', $tmp_val))
280 $tmp_val = '00'.substr($tmp_val, 2);
282 // We are ready to convert AM time.
283 if (preg_match('/^(0[0-9]|1[0-1]):[0-5][0-9]$/', $tmp_val)) { // 00:00 - 11:59
284 // We already have a 24-hour format. Just return it.
288 if (preg_match('/^[1-9]:[0-5][0-9]$/', $tmp_val)) { // 1:00 - 9:59
289 // This is a 24-hour format without a leading zero. Add 0 and return.
293 if (preg_match('/^[1-9]$/', $tmp_val)) { // 1 - 9
294 // Single digit. Assuming hour number.
295 $res = '0'.$tmp_val.':00';
298 if (preg_match('/^(0[0-9]|1[0-1])$/', $tmp_val)) { // 00 - 11
299 // Two digit hour number.
300 $res = $tmp_val.':00';
303 if (preg_match('/^[1-9][0-5][0-9]$/', $tmp_val)) { // 100 - 959
304 // Missing colon. Assume the first digit is the hour, the rest is minutes.
305 $tmp_arr = str_split($tmp_val);
306 $res = '0'.$tmp_arr[0].':'.$tmp_arr[1].$tmp_arr[2];
309 if (preg_match('/^(0[0-9]|1[0-1])[0-5][0-9]$/', $tmp_val)) { // 0000 - 1159
310 // Missing colon. We'll assume the first 2 digits are the hour, the rest is minutes.
311 $tmp_arr = str_split($tmp_val);
312 $res = $tmp_arr[0].$tmp_arr[1].':'.$tmp_arr[2].$tmp_arr[3];
315 } // AM cases handling.
317 // 12 hour PM patterns.
318 if (preg_match('/.(pm|PM)$/', $tmp_val)) {
320 // The $value ends in pm or PM. Strip it.
321 $tmp_val = rtrim(substr($tmp_val, 0, -2));
323 if (preg_match('/^[1-9]$/', $tmp_val)) { // 1 - 9
324 // Single digit. Assuming hour number.
325 $hour = (string)(12 + (int)$tmp_val);
329 if (preg_match('/^((0[1-9])|(1[0-2]))$/', $tmp_val)) { // 01 - 12
330 // Double digit hour.
331 if ('12' != $tmp_val)
332 $tmp_val = (string)(12 + (int)$tmp_val);
333 $res = $tmp_val.':00';
336 if (preg_match('/^[1-9][0-5][0-9]$/', $tmp_val)) { // 100 - 959
337 // Missing colon. We'll assume the first digit is the hour, the rest is minutes.
338 $tmp_arr = str_split($tmp_val);
339 $hour = (string)(12 + (int)$tmp_arr[0]);
340 $res = $hour.':'.$tmp_arr[1].$tmp_arr[2];
343 if (preg_match('/^(0[1-9]|1[0-2])[0-5][0-9]$/', $tmp_val)) { // 0100 - 1259
344 // Missing colon. We'll assume the first 2 digits are the hour, the rest is minutes.
345 $hour = substr($tmp_val, 0, -2);
346 $min = substr($tmp_val, 2);
348 $hour = (string)(12 + (int)$hour);
349 $res = $hour.':'.$min;
352 if (preg_match('/^[1-9]:[0-5][0-9]$/', $tmp_val)) { // 1:00 - 9:59
353 $hour = substr($tmp_val, 0, -3);
354 $min = substr($tmp_val, 2);
355 $hour = (string)(12 + (int)$hour);
356 $res = $hour.':'.$min;
359 if (preg_match('/^(0[1-9]|1[0-2]):[0-5][0-9]$/', $tmp_val)) { // 01:00 - 12:59
360 $hour = substr($tmp_val, 0, -3);
361 $min = substr($tmp_val, 3);
363 $hour = (string)(12 + (int)$hour);
364 $res = $hour.':'.$min;
367 } // PM cases handling.
372 // isValidInterval - checks if finish time is greater than start time.
373 static function isValidInterval($start, $finish) {
374 $start = ttTimeHelper::to24HourFormat($start);
375 $finish = ttTimeHelper::to24HourFormat($finish);
376 if ('00:00' == $finish) $finish = '24:00';
378 $minutesStart = ttTimeHelper::toMinutes($start);
379 $minutesFinish = ttTimeHelper::toMinutes($finish);
380 if ($minutesFinish > $minutesStart)
386 // insert - inserts a time record into log table. Does not deal with custom fields.
387 static function insert($fields)
390 $mdb2 = getConnection();
392 $user_id = $fields['user_id'];
393 $date = $fields['date'];
394 $start = $fields['start'];
395 $finish = $fields['finish'];
396 $duration = $fields['duration'];
398 $minutes = ttTimeHelper::postedDurationToMinutes($duration);
399 $duration = ttTimeHelper::minutesToDuration($minutes);
401 $client = $fields['client'];
402 $project = $fields['project'];
403 $task = $fields['task'];
404 $invoice = $fields['invoice'];
405 $note = $fields['note'];
406 $billable = $fields['billable'];
407 $paid = $fields['paid'];
408 if (array_key_exists('status', $fields)) { // Key exists and may be NULL during migration of data.
409 $status_f = ', status';
410 $status_v = ', '.$mdb2->quote($fields['status']);
413 $start = ttTimeHelper::to24HourFormat($start);
415 $finish = ttTimeHelper::to24HourFormat($finish);
416 if ('00:00' == $finish) $finish = '24:00';
419 $created_v = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
421 if (!$billable) $billable = 0;
422 if (!$paid) $paid = 0;
425 $sql = "insert into tt_log (user_id, date, duration, client_id, project_id, task_id, invoice_id, comment, billable, paid, created, created_ip, created_by $status_f) ".
426 "values ($user_id, ".$mdb2->quote($date).", '$duration', ".$mdb2->quote($client).", ".$mdb2->quote($project).", ".$mdb2->quote($task).", ".$mdb2->quote($invoice).", ".$mdb2->quote($note).", $billable, $paid $created_v $status_v)";
427 $affected = $mdb2->exec($sql);
428 if (is_a($affected, 'PEAR_Error'))
431 $duration = ttTimeHelper::toDuration($start, $finish);
432 if ($duration === false) $duration = 0;
433 if (!$duration && ttTimeHelper::getUncompleted($user_id)) return false;
435 $sql = "insert into tt_log (user_id, date, start, duration, client_id, project_id, task_id, invoice_id, comment, billable, paid, created, created_ip, created_by $status_f) ".
436 "values ($user_id, ".$mdb2->quote($date).", '$start', '$duration', ".$mdb2->quote($client).", ".$mdb2->quote($project).", ".$mdb2->quote($task).", ".$mdb2->quote($invoice).", ".$mdb2->quote($note).", $billable, $paid $created_v $status_v)";
437 $affected = $mdb2->exec($sql);
438 if (is_a($affected, 'PEAR_Error'))
442 $id = $mdb2->lastInsertID('tt_log', 'id');
446 // update - updates a record in log table. Does not update its custom fields.
447 static function update($fields)
450 $mdb2 = getConnection();
453 $date = $fields['date'];
454 $user_id = $fields['user_id'];
455 $client = $fields['client'];
456 $project = $fields['project'];
457 $task = $fields['task'];
458 $start = $fields['start'];
459 $finish = $fields['finish'];
460 $duration = $fields['duration'];
462 $minutes = ttTimeHelper::postedDurationToMinutes($duration);
463 $duration = ttTimeHelper::minutesToDuration($minutes);
465 $note = $fields['note'];
468 if ($user->isPluginEnabled('iv')) {
469 $billable_part = $fields['billable'] ? ', billable = 1' : ', billable = 0';
472 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
473 $paid_part = $fields['paid'] ? ', paid = 1' : ', paid = 0';
475 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
477 $start = ttTimeHelper::to24HourFormat($start);
478 $finish = ttTimeHelper::to24HourFormat($finish);
479 if ('00:00' == $finish) $finish = '24:00';
481 if ($start) $duration = '';
484 $sql = "UPDATE tt_log set start = NULL, duration = '$duration', client_id = ".$mdb2->quote($client).", project_id = ".$mdb2->quote($project).", task_id = ".$mdb2->quote($task).", ".
485 "comment = ".$mdb2->quote($note)."$billable_part $paid_part $modified_part, date = '$date' WHERE id = $id";
486 $affected = $mdb2->exec($sql);
487 if (is_a($affected, 'PEAR_Error'))
490 $duration = ttTimeHelper::toDuration($start, $finish);
491 if ($duration === false)
493 $uncompleted = ttTimeHelper::getUncompleted($user_id);
494 if (!$duration && $uncompleted && ($uncompleted['id'] != $id))
497 $sql = "UPDATE tt_log SET start = '$start', duration = '$duration', client_id = ".$mdb2->quote($client).", project_id = ".$mdb2->quote($project).", task_id = ".$mdb2->quote($task).", ".
498 "comment = ".$mdb2->quote($note)."$billable_part $paid_part $modified_part, date = '$date' WHERE id = $id";
499 $affected = $mdb2->exec($sql);
500 if (is_a($affected, 'PEAR_Error'))
506 // delete - deletes a record from tt_log table and its associated custom field values.
507 static function delete($id, $user_id) {
508 $mdb2 = getConnection();
510 $sql = "update tt_log set status = NULL where id = $id and user_id = $user_id";
511 $affected = $mdb2->exec($sql);
512 if (is_a($affected, 'PEAR_Error'))
515 $sql = "update tt_custom_field_log set status = NULL where log_id = $id";
516 $affected = $mdb2->exec($sql);
517 if (is_a($affected, 'PEAR_Error'))
523 // getTimeForDay - gets total time for a user for a specific date.
524 static function getTimeForDay($user_id, $date) {
525 $mdb2 = getConnection();
527 $sql = "select sum(time_to_sec(duration)) as sm from tt_log where user_id = $user_id and date = '$date' and status = 1";
528 $res = $mdb2->query($sql);
529 if (!is_a($res, 'PEAR_Error')) {
530 $val = $res->fetchRow();
531 return sec_to_time_fmt_hm($val['sm']);
536 // getTimeForWeek - gets total time for a user for a given week.
537 static function getTimeForWeek($user_id, $date) {
539 $mdb2 = getConnection();
541 $period = new Period(INTERVAL_THIS_WEEK, $date);
542 $sql = "select sum(time_to_sec(duration)) as sm from tt_log where user_id = $user_id and date >= '".$period->getStartDate(DB_DATEFORMAT)."' and date <= '".$period->getEndDate(DB_DATEFORMAT)."' and status = 1";
543 $res = $mdb2->query($sql);
544 if (!is_a($res, 'PEAR_Error')) {
545 $val = $res->fetchRow();
546 return sec_to_time_fmt_hm($val['sm']);
551 // getTimeForMonth - gets total time for a user for a given month.
552 static function getTimeForMonth($user_id, $date){
554 $mdb2 = getConnection();
556 $period = new Period(INTERVAL_THIS_MONTH, $date);
557 $sql = "select sum(time_to_sec(duration)) as sm from tt_log where user_id = $user_id and date >= '".$period->getStartDate(DB_DATEFORMAT)."' and date <= '".$period->getEndDate(DB_DATEFORMAT)."' and status = 1";
558 $res = $mdb2->query($sql);
559 if (!is_a($res, 'PEAR_Error')) {
560 $val = $res->fetchRow();
561 return sec_to_time_fmt_hm($val['sm']);
566 // getUncompleted - retrieves an uncompleted record for user, if one exists.
567 static function getUncompleted($user_id) {
568 $mdb2 = getConnection();
570 $sql = "select id, start from tt_log
571 where user_id = $user_id and start is not null and time_to_sec(duration) = 0 and status = 1";
572 $res = $mdb2->query($sql);
573 if (!is_a($res, 'PEAR_Error')) {
574 if (!$res->numRows()) {
577 if ($val = $res->fetchRow()) {
584 // overlaps - determines if a record overlaps with an already existing record.
587 // $user_id - user id for whom to determine overlap
589 // $start - new record start time
590 // $finish - new record finish time, may be null
591 // $record_id - optional record id we may be editing, excluded from overlap set
592 static function overlaps($user_id, $date, $start, $finish, $record_id = null) {
593 // Do not bother checking if we allow overlaps.
595 if ($user->allow_overlap) return false;
597 $mdb2 = getConnection();
599 $start = ttTimeHelper::to24HourFormat($start);
601 $finish = ttTimeHelper::to24HourFormat($finish);
602 if ('00:00' == $finish) $finish = '24:00';
604 // Handle these 3 overlap situations:
605 // - start time in existing record
606 // - end time in existing record
607 // - record fully encloses existing record
608 $sql = "select id from tt_log
609 where user_id = $user_id and date = ".$mdb2->quote($date)."
610 and start is not null and duration is not null and status = 1 and (
611 (cast(".$mdb2->quote($start)." as time) >= start and cast(".$mdb2->quote($start)." as time) < addtime(start, duration))";
613 $sql .= " or (cast(".$mdb2->quote($finish)." as time) <= addtime(start, duration) and cast(".$mdb2->quote($finish)." as time) > start)
614 or (cast(".$mdb2->quote($start)." as time) < start and cast(".$mdb2->quote($finish)." as time) > addtime(start, duration))";
618 $sql .= " and id <> $record_id";
620 $res = $mdb2->query($sql);
621 if (!is_a($res, 'PEAR_Error')) {
622 if (!$res->numRows()) {
625 if ($val = $res->fetchRow()) {
632 // getRecord - retrieves a time record identified by its id.
633 static function getRecord($id, $user_id) {
635 $sql_time_format = "'%k:%i'"; // 24 hour format.
636 if ('%I:%M %p' == $user->time_format)
637 $sql_time_format = "'%h:%i %p'"; // 12 hour format for MySQL TIME_FORMAT function.
639 $mdb2 = getConnection();
641 $sql = "select l.id as id, TIME_FORMAT(l.start, $sql_time_format) as start,
642 TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), $sql_time_format) as finish,
643 TIME_FORMAT(l.duration, '%k:%i') as duration,
644 p.name as project_name, t.name as task_name, l.comment, l.client_id, l.project_id, l.task_id, l.invoice_id, l.billable, l.paid, l.date
646 left join tt_projects p on (p.id = l.project_id)
647 left join tt_tasks t on (t.id = l.task_id)
648 where l.id = $id and l.user_id = $user_id and l.status = 1";
649 $res = $mdb2->query($sql);
650 if (!is_a($res, 'PEAR_Error')) {
651 if (!$res->numRows()) {
654 if ($val = $res->fetchRow()) {
661 // getAllRecords - returns all time records for a certain user.
662 static function getAllRecords($user_id) {
665 $mdb2 = getConnection();
667 $sql = "select l.id, l.user_id, l.date, TIME_FORMAT(l.start, '%k:%i') as start,
668 TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish,
669 TIME_FORMAT(l.duration, '%k:%i') as duration,
670 l.client_id, l.project_id, l.task_id, l.invoice_id, l.comment, l.billable, l.paid, l.status
671 from tt_log l where l.user_id = $user_id order by l.id";
672 $res = $mdb2->query($sql);
673 if (!is_a($res, 'PEAR_Error')) {
674 while ($val = $res->fetchRow()) {
682 // getRecords - returns time records for a user for a given date.
683 static function getRecords($user_id, $date) {
685 $sql_time_format = "'%k:%i'"; // 24 hour format.
686 if ('%I:%M %p' == $user->time_format)
687 $sql_time_format = "'%h:%i %p'"; // 12 hour format for MySQL TIME_FORMAT function.
690 $mdb2 = getConnection();
692 $client_field = null;
693 if ($user->isPluginEnabled('cl'))
694 $client_field = ", c.name as client";
696 $left_joins = " left join tt_projects p on (l.project_id = p.id)".
697 " left join tt_tasks t on (l.task_id = t.id)";
698 if ($user->isPluginEnabled('cl'))
699 $left_joins .= " left join tt_clients c on (l.client_id = c.id)";
701 $sql = "select l.id as id, TIME_FORMAT(l.start, $sql_time_format) as start,
702 TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), $sql_time_format) as finish,
703 TIME_FORMAT(l.duration, '%k:%i') as duration, p.name as project, t.name as task, l.comment, l.billable, l.invoice_id $client_field
706 where l.date = '$date' and l.user_id = $user_id and l.status = 1
707 order by l.start, l.id";
708 $res = $mdb2->query($sql);
709 if (!is_a($res, 'PEAR_Error')) {
710 while ($val = $res->fetchRow()) {
711 if($val['duration']=='0:00')