Added editable comment fields on week view.
[timetracker.git] / WEB-INF / lib / ttWeekViewHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // ttWeekViewHelper class groups together functions used in week view.
30 class ttWeekViewHelper {
31
32   // getRecordsForInterval - returns time records for a user for a given interval of dates.
33   static function getRecordsForInterval($user_id, $start_date, $end_date) {
34     global $user;
35     $sql_time_format = "'%k:%i'"; //  24 hour format.
36     if ('%I:%M %p' == $user->time_format)
37       $sql_time_format = "'%h:%i %p'"; // 12 hour format for MySQL TIME_FORMAT function.
38
39     $result = array();
40     $mdb2 = getConnection();
41
42     $client_field = null;
43     if ($user->isPluginEnabled('cl'))
44       $client_field = ', c.id as client_id, c.name as client';
45
46     $custom_field_1 = null;
47     if ($user->isPluginEnabled('cf')) {
48       $custom_fields = new CustomFields($user->team_id);
49       $cf_1_type = $custom_fields->fields[0]['type'];
50       if ($cf_1_type == CustomFields::TYPE_TEXT) {
51         $custom_field_1 = ', cfl.value as cf_1_value';
52       } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
53         $custom_field_1 = ', cfo.id as cf_1_id, cfo.value as cf_1_value';
54       }
55     }
56
57     $left_joins = " left join tt_projects p on (l.project_id = p.id)".
58       " left join tt_tasks t on (l.task_id = t.id)";
59     if ($user->isPluginEnabled('cl'))
60       $left_joins .= " left join tt_clients c on (l.client_id = c.id)";
61     if ($user->isPluginEnabled('cf')) {
62       if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
63         $left_joins .= 'left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1) left join tt_custom_field_options cfo on (cfl.value = cfo.id) ';
64       elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
65         $left_joins .= 'left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1) left join tt_custom_field_options cfo on (cfl.option_id = cfo.id) ';
66     }
67
68     $sql = "select l.id as id, l.date as date, TIME_FORMAT(l.start, $sql_time_format) as start,
69       TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), $sql_time_format) as finish,
70       TIME_FORMAT(l.duration, '%k:%i') as duration, p.id as project_id, p.name as project,
71       t.id as task_id, t.name as task, l.comment, l.billable, l.invoice_id $client_field $custom_field_1
72       from tt_log l
73       $left_joins
74       where l.date >= '$start_date' and l.date <= '$end_date' and l.user_id = $user_id and l.status = 1
75       order by l.date, p.name, t.name, l.start, l.id";
76     $res = $mdb2->query($sql);
77     if (!is_a($res, 'PEAR_Error')) {
78       while ($val = $res->fetchRow()) {
79         if($val['duration']=='0:00')
80           $val['finish'] = '';
81         $result[] = $val;
82       }
83     } else return false;
84
85     return $result;
86   }
87
88   // getDataForWeekView - builds an array to render a table of durations and comments for week view.
89   // In a week view we want one row representing the same attributes to have 7 values for each day of week.
90   // We identify simlar records by a combination of client, billable, project, task, and custom field values.
91   // This will allow us to extend the feature when more custom fields are added.
92   //
93   // "cl:546,bl:1,pr:23456,ts:27464,cf_1:example text"
94   // The above means client 546, billable, project 23456, task 27464, custom field text "example text".
95   //
96   // "cl:546,bl:0,pr:23456,ts:27464,cf_1:7623"
97   // The above means client 546, not billable, project 23456, task 27464, custom field option id 7623.
98   //
99   // Daily comments are implemented as alternate rows following week durations.
100   // For example: row_0 - new entry durations, row_1 - new entry daily comments,
101   //              row_2 - existing entry durations, row_3 - existing entry comments, etc.
102   //
103   // Description of $dataArray format that the function returns.
104   // $dataArray = array(
105   //   array( // Row 0. This is a special, one-off row for a new week entry with empty values.
106   //     'row_id' => null, // Row identifier. Null for a new entry.
107   //     'label' => 'New entry', // Human readable label for the row describing what this time entry is for.
108   //     'day_0' => array('control_id' => '0_day_0', 'tt_log_id' => null, 'duration' => null), // control_id is row_id plus day header for column.
109   //     'day_1' => array('control_id' => '0_day_1', 'tt_log_id' => null, 'duration' => null),
110   //     'day_2' => array('control_id' => '0_day_2', 'tt_log_id' => null, 'duration' => null),
111   //     'day_3' => array('control_id' => '0_day_3', 'tt_log_id' => null, 'duration' => null),
112   //     'day_4' => array('control_id' => '0_day_4', 'tt_log_id' => null, 'duration' => null),
113   //     'day_5' => array('control_id' => '0_day_5', 'tt_log_id' => null, 'duration' => null),
114   //     'day_6' => array('control_id' => '0_day_6', 'tt_log_id' => null, 'duration' => null)
115   //   ),
116   //
117   //   TODO: work in progress re-documenting the array for improved week view. Trying to implement this now.
118   //   array( // Row 1. This row represents daily comments for a new entry in row above (row 0).
119   //     'row_id' => null, // Row identifier. See ttWeekViewHelper::makeRowIdentifier().
120   //     'label' => 'Notes', // Human readable label.
121   //     'day_0' => array('control_id' => '1_day_0', 'tt_log_id' => null, 'note' => null),
122   //     'day_1' => array('control_id' => '1_day_1', 'tt_log_id' => null, 'note' => null),
123   //     'day_2' => array('control_id' => '1_day_2', 'tt_log_id' => null, 'note' => null),
124   //     'day_3' => array('control_id' => '1_day_3', 'tt_log_id' => null, 'note' => null),
125   //     'day_4' => array('control_id' => '1_day_4', 'tt_log_id' => null, 'note' => null),
126   //     'day_5' => array('control_id' => '1_day_5', 'tt_log_id' => null, 'note' => null),
127   //     'day_6' => array('control_id' => '1_day_6', 'tt_log_id' => null, 'note' => null)
128   //   ),
129   //   TODO: work in progress...
130   //
131   //   array( // Row 1.
132   //     'row_id' => 'cl:546,bl:1,pr:23456,ts:27464,cf_1:7623_0', // Row identifier. See ttWeekViewHelper::makeRowIdentifier().
133   //     'label' => 'Anuko - Time Tracker - Coding',              // Human readable label for the row describing what this time entry is for.
134   //     'day_0' => array('control_id' => '1_day_0', 'tt_log_id' => 12345, 'duration' => '00:00'), // control_id is row_id plus day header for column.
135   //     'day_1' => array('control_id' => '1_day_1', 'tt_log_id' => 12346, 'duration' => '01:00'),
136   //     'day_2' => array('control_id' => '1_day_2', 'tt_log_id' => 12347, 'duration' => '02:00'),
137   //     'day_3' => array('control_id' => '1_day_3', 'tt_log_id' => null, 'duration' => null),
138   //     'day_4' => array('control_id' => '1_day_4', 'tt_log_id' => 12348, 'duration' => '04:00'),
139   //     'day_5' => array('control_id' => '1_day_5', 'tt_log_id' => 12349, 'duration' => '04:00'),
140   //     'day_6' => array('control_id' => '1_day_6', 'tt_log_id' => null, 'duration' => null)
141   //   ),
142   //   array( // Row 2.
143   //     'row_id' => 'bl:0_0',
144   //     'label' => '', // In this case the label is empty as we don't have anything to put into it, as we only have billable flag.
145   //     'day_0' => array('control_id' => '2_day_0', 'tt_log_id' => null, 'duration' => null),
146   //     'day_1' => array('control_id' => '2_day_1', 'tt_log_id' => 12350, 'duration' => '01:30'),
147   //     'day_2' => array('control_id' => '2_day_2', 'tt_log_id' => null, 'duration' => null),
148   //     'day_3' => array('control_id' => '2_day_3', 'tt_log_id' => 12351,'duration' => '02:30'),
149   //     'day_4' => array('control_id' => '2_day_4', 'tt_log_id' => 12352, 'duration' => '04:00'),
150   //     'day_5' => array('control_id' => '2_day_5', 'tt_log_id' => null, 'duration' => null),
151   //     'day_6' => array('control_id' => '2_day_6', 'tt_log_id' => null, 'duration' => null)
152   //   )
153   // );
154   static function getDataForWeekView($records, $dayHeaders) {
155     global $i18n;
156
157     $dataArray = array();
158
159     // Construct the first row for a brand new entry.
160     $dataArray[] = array('row_id' => null,'label' => $i18n->getKey('form.week.new_entry').':'); // Insert row.
161     // Insert empty cells with proper control ids.
162     for ($i = 0; $i < 7; $i++) {
163       $control_id = '0_'. $dayHeaders[$i];
164       $dataArray[0][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
165     }
166     // Construct the second row for daily comments for a brand new entry.
167     $dataArray[] = array('row_id' => null,'label' => $i18n->getKey('label.notes').':'); // Insert row.
168     // Insert empty cells with proper control ids.
169     for ($i = 0; $i < 7; $i++) {
170       $control_id = '1_'. $dayHeaders[$i];
171       $dataArray[1][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
172     }
173
174     // Iterate through records and build $dataArray cell by cell.
175     foreach ($records as $record) {
176       // Create row id without suffix.
177       $row_id_no_suffix = ttWeekViewHelper::makeRowIdentifier($record);
178       // Handle potential multiple records with the same attributes by using a numerical suffix.
179       $suffix = 0;
180       $row_id = $row_id_no_suffix.'_'.$suffix;
181       $day_header = substr($record['date'], 8); // Day number in month.
182       while (ttWeekViewHelper::cellExists($row_id, $day_header, $dataArray)) {
183         $suffix++;
184         $row_id = $row_id_no_suffix.'_'.$suffix;
185       }
186       // Find row.
187       $pos = ttWeekViewHelper::findRow($row_id, $dataArray);
188       if ($pos < 0) {
189         // Insert row for durations.
190         $dataArray[] = array('row_id' => $row_id,'label' => ttWeekViewHelper::makeRowLabel($record));
191         $pos = ttWeekViewHelper::findRow($row_id, $dataArray);
192         // Insert empty cells with proper control ids.
193         for ($i = 0; $i < 7; $i++) {
194           $control_id = $pos.'_'. $dayHeaders[$i];
195           $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
196         }
197         // Insert row for comments.
198         $dataArray[] = array('row_id' => $row_id.'_notes','label' => $i18n->getKey('label.notes').':');
199         $pos++;
200         // Insert empty cells with proper control ids.
201         for ($i = 0; $i < 7; $i++) {
202           $control_id = $pos.'_'. $dayHeaders[$i];
203           $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
204         }
205         $pos--;
206       }
207       // Insert actual cell data from $record (one cell only).
208       $dataArray[$pos][$day_header] = array('control_id' => $pos.'_'. $day_header, 'tt_log_id' => $record['id'],'duration' => $record['duration']);
209       // Insert existing comment from $record into the duration cell.
210       $pos++;
211       $dataArray[$pos][$day_header] = array('control_id' => $pos.'_'. $day_header, 'tt_log_id' => $record['id'],'note' => $record['comment']);
212     }
213     return $dataArray;
214   }
215
216   // cellExists is a helper function for getDataForWeekView() to see if a cell with a given label
217   // and a day header already exists.
218   static function cellExists($row_id, $day_header, $dataArray) {
219     foreach($dataArray as $row) {
220       if ($row['row_id'] == $row_id && !empty($row[$day_header]['duration']))
221         return true;
222     }
223     return false;
224   }
225
226   // findRow returns an existing row position in $dataArray, -1 otherwise.
227   static function findRow($row_id, $dataArray) {
228     $pos = 0; // Row position in array.
229     foreach($dataArray as $row) {
230       if ($row['row_id'] == $row_id)
231         return $pos;
232       $pos++; // Increment for search.
233     }
234     return -1; // Row not found.
235   }
236
237   // getDayTotals calculates total durations for each day from the existing data in $dataArray.
238   static function getDayTotals($dataArray, $dayHeaders) {
239     $dayTotals = array();
240
241     // Insert label.
242     global $i18n;
243     $dayTotals['label'] = $i18n->getKey('label.day_total').':';
244
245     foreach ($dataArray as $row) {
246       foreach($dayHeaders as $dayHeader) {
247         if (array_key_exists($dayHeader, $row)) {
248           $minutes = ttTimeHelper::toMinutes($row[$dayHeader]['duration']);
249           $dayTotals[$dayHeader] += $minutes;
250         }
251       }
252     }
253     // Convert minutes to hh:mm for display.
254     foreach($dayHeaders as $dayHeader) {
255       $dayTotals[$dayHeader] = ttTimeHelper::toAbsDuration($dayTotals[$dayHeader]);
256     }
257     return $dayTotals;
258   }
259
260   // getDayHeadersForWeek - obtains day column headers for week view, which are simply day numbers in month.
261   static function getDayHeadersForWeek($start_date) {
262     $dayHeaders = array();
263     $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
264     $dayHeaders[] = (string) $objDate->getDate(); // It returns an int on first call.
265     if (strlen($dayHeaders[0]) == 1)              // Which is an implementation detail of DateAndTime class.
266       $dayHeaders[0] = '0'.$dayHeaders[0];        // Add a 0 for single digit day.
267     $objDate->incDay();
268     $dayHeaders[] = $objDate->getDate(); // After incDay it returns a string with leading 0, when necessary.
269     $objDate->incDay();
270     $dayHeaders[] = $objDate->getDate();
271     $objDate->incDay();
272     $dayHeaders[] = $objDate->getDate();
273     $objDate->incDay();
274     $dayHeaders[] = $objDate->getDate();
275     $objDate->incDay();
276     $dayHeaders[] = $objDate->getDate();
277     $objDate->incDay();
278     $dayHeaders[] = $objDate->getDate();
279     unset($objDate);
280     return $dayHeaders;
281   }
282
283   // getLockedDaysForWeek - builds an array of locked days in week.
284   static function getLockedDaysForWeek($start_date) {
285     global $user;
286     $lockedDays = array();
287     $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
288     for ($i = 0; $i < 7; $i++) {
289       $lockedDays[] = $user->isDateLocked($objDate);
290       $objDate->incDay();
291     }
292     unset($objDate);
293     return $lockedDays;
294   }
295
296   // makeRowIdentifier - builds a string identifying a row for a week view from a single record properties.
297   //                     Note that the return value is without a suffix.
298   // For example:
299   // "cl:546,bl:0,pr:23456,ts:27464,cf_1:example text"
300   // "cl:546,bl:1,pr:23456,ts:27464,cf_1:7623"
301   static function makeRowIdentifier($record) {
302     global $user;
303     // Start with client.
304     if ($user->isPluginEnabled('cl'))
305       $row_identifier = $record['client_id'] ? 'cl:'.$record['client_id'] : '';
306     // Add billable flag.
307     if (!empty($row_identifier)) $row_identifier .= ',';
308     $row_identifier .= 'bl:'.$record['billable'];
309     // Add project.
310     $row_identifier .= $record['project_id'] ? ',pr:'.$record['project_id'] : '';
311     // Add task.
312     $row_identifier .= $record['task_id'] ? ',ts:'.$record['task_id'] : '';
313     // Add custom field 1.
314     if ($user->isPluginEnabled('cf')) {
315       if ($record['cf_1_id'])
316         $row_identifier .= ',cf_1:'.$record['cf_1_id'];
317       else if ($record['cf_1_value'])
318         $row_identifier .= ',cf_1:'.$record['cf_1_value'];
319     }
320
321     return $row_identifier;
322   }
323
324   // makeRowLabel - builds a human readable label for a row in week view,
325   // which is a combination ot record properties.
326   // Client - Project - Task - Custom field 1.
327   // Note that billable property is not part of the label. Instead,
328   // we identify such records with a different color in week view.
329   static function makeRowLabel($record) {
330     global $user;
331     // Start with client.
332     if ($user->isPluginEnabled('cl'))
333       $label = $record['client'];
334
335     // Add project.
336     if (!empty($label) && !empty($record['project'])) $label .= ' - ';
337     $label .= $record['project'];
338
339     // Add task.
340     if (!empty($label) && !empty($record['task'])) $label .= ' - ';
341     $label .= $record['task'];
342
343     // Add custom field 1.
344     if ($user->isPluginEnabled('cf')) {
345       if (!empty($label) && !empty($record['cf_1_value'])) $label .= ' - ';
346       $label .= $record['cf_1_value'];
347     }
348
349     return $label;
350   }
351
352   // parseFromWeekViewRow - obtains field value encoded in row identifier.
353   // For example, for a row id like "cl:546,bl:0,pr:23456,ts:27464,cf_1:example text"
354   // requesting a client "cl" should return 546.
355   static function parseFromWeekViewRow($row_id, $field_label) {
356     // Find beginning of label.
357     $pos = strpos($row_id, $field_label);
358     if ($pos === false) return null; // Not found.
359
360     // Strip suffix from row id.
361     $suffixPos = strrpos($row_id, '_');
362     if ($suffixPos)
363       $remaninder = substr($row_id, 0, $suffixPos);
364
365     // Find beginning of value.
366     $posBegin = 1 + strpos($remaninder, ':', $pos);
367     // Find end of value.
368     $posEnd = strpos($remaninder, ',', $posBegin);
369     if ($posEnd === false) $posEnd = strlen($remaninder);
370     // Return value.
371     return substr($remaninder, $posBegin, $posEnd - $posBegin);
372   }
373
374   // dateFromDayHeader calculates date from start date and day header in week view.
375   static function dateFromDayHeader($start_date, $day_header) {
376     $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
377     $currentDayHeader = (string) $objDate->getDate(); // It returns an int on first call.
378     if (strlen($currentDayHeader) == 1)               // Which is an implementation detail of DateAndTime class.
379       $currentDayHeader = '0'.$currentDayHeader;      // Add a 0 for single digit day.
380     $i = 1;
381     while ($currentDayHeader != $day_header && $i < 7) {
382       // Iterate through remaining days to find a match.
383       $objDate->incDay();
384       $currentDayHeader = $objDate->getDate(); // After incDay it returns a string with leading 0, when necessary.
385       $i++;
386     }
387     return $objDate->toString(DB_DATEFORMAT);
388   }
389
390   // insertDurationFromWeekView - inserts a new record in log tables from a week view post.
391   static function insertDurationFromWeekView($fields, $custom_fields, $err) {
392     global $i18n;
393     global $user;
394
395     // Determine date for a new entry.
396     $entry_date = ttWeekViewHelper::dateFromDayHeader($fields['start_date'], $fields['day_header']);
397     $objEntryDate = new DateAndTime(DB_DATEFORMAT, $entry_date);
398
399     // Prohibit creating entries in future.
400     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES) && $fields['browser_today']) {
401       $objBrowserToday = new DateAndTime(DB_DATEFORMAT, $fields['browser_today']);
402       if ($objEntryDate->after($objBrowserToday)) {
403         $err->add($i18n->getKey('error.future_date'));
404         return false;
405       }
406     }
407
408     // Prepare an array of fields for regular insert function.
409     $fields4insert = array();
410     $fields4insert['user_id'] = $user->getActiveUser();
411     $fields4insert['date'] = $entry_date;
412     $fields4insert['duration'] = $fields['duration'];
413     $fields4insert['client'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'cl');
414     $fields4insert['billable'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'bl');
415     $fields4insert['project'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'pr');
416     $fields4insert['task'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'ts');
417     $fields4insert['note'] = $fields['note'];
418
419     // Try to insert a record.
420     $id = ttTimeHelper::insert($fields4insert);
421     if (!$id) return false; // Something failed.
422
423     // Insert custom field if we have it.
424     $result = true;
425     $cf_1 = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'cf_1');
426     if ($custom_fields && $cf_1) {
427       if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
428         $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cf_1);
429       elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
430         $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cf_1, null);
431     }
432
433     return $result;
434   }
435
436   // modifyDurationFromWeekView - modifies a duration of an existing record from a week view post.
437   static function modifyDurationFromWeekView($fields, $err) {
438     global $i18n;
439     global $user;
440
441     // Possible errors: 1) Overlap if the existing record has start time. 2) Going beyond 24 hour boundary.
442     if (!ttWeekViewHelper::canModify($fields['tt_log_id'], $fields['duration'], $err))
443       return false;
444
445     $mdb2 = getConnection();
446     $duration = $fields['duration'];
447     $tt_log_id = $fields['tt_log_id'];
448     $user_id = $user->getActiveUser();
449     $sql = "update tt_log set duration = '$duration' where id = $tt_log_id and user_id = $user_id";
450     $affected = $mdb2->exec($sql);
451     if (is_a($affected, 'PEAR_Error'))
452       return false;
453
454     return true;
455   }
456
457   // canModify - determines if an  already existing tt_log record
458   // can be modified with a new user-provided duration.
459   static function canModify($tt_log_id, $new_duration, $err) {
460     global $i18n;
461     $mdb2 = getConnection();
462
463     // Determine if we have start time in record, as further checking does not makes sense otherwise.
464     $sql = "select user_id, date, start, duration from tt_log  where id = $tt_log_id";
465     $res = $mdb2->query($sql);
466     if (!is_a($res, 'PEAR_Error')) {
467       if (!$res->numRows()) {
468         $err->add($i18n->getKey('error.db')); // This is not expected.
469         return false;
470       }
471       $val = $res->fetchRow();
472       $oldDuration = $val['duration'];
473       if (!$val['start'])
474         return true; // There is no start time in the record, therefore safe to modify.
475     }
476
477     // We do have start time.
478     // Quick test if new duration is less then already existing.
479     $newMinutes = ttTimeHelper::toMinutes($new_duration);
480     $oldMinutes = ttTimeHelper::toMinutes($oldDuration);
481     if ($newMinutes < $oldMinutes)
482       return true; // Safe to modify.
483
484     // Does the new duration put the record beyond 24:00 boundary?
485     $startMinutes = ttTimeHelper::toMinutes($val['start']);
486     $newEndMinutes = $startMinutes + $newMinutes;
487     if ($newEndMinutes > 1440) {
488       // Invalid duration, as new duration puts the record beyond current day.
489       $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
490       return false;
491     }
492
493     // Does the new duration causes the record to overlap with others?
494     $user_id = $val['user_id'];
495     $date = $val['date'];
496     $startMinutes = ttTimeHelper::toMinutes($val['start']);
497     $start = ttTimeHelper::toAbsDuration($startMinutes);
498     $finish = ttTimeHelper::toAbsDuration($newEndMinutes);
499     if (ttTimeHelper::overlaps($user_id, $date, $start, $finish, $tt_log_id)) {
500       $err->add($i18n->getKey('error.overlap'));
501       return false;
502     }
503
504     return true; // There are no conflicts, safe to modify.
505   }
506
507   // modifyCommentFromWeekView - modifies a comment in an existing record from a week view post.
508   static function modifyCommentFromWeekView($fields) {
509     global $user;
510
511     $mdb2 = getConnection();
512     $tt_log_id = $fields['tt_log_id'];
513     $comment = $fields['comment'];
514     $user_id = $user->getActiveUser();
515     $sql = "update tt_log set comment = ".$mdb2->quote($fields['comment'])." where id = $tt_log_id and user_id = $user_id";
516     $affected = $mdb2->exec($sql);
517     if (is_a($affected, 'PEAR_Error'))
518       return false;
519
520     return true;
521   }
522 }