+ // getDataForWeekView - builds an array to render a table of durations for week view.
+ static function getDataForWeekView($user_id, $start_date, $end_date) {
+ // Start by obtaining all records in interval.
+ $records = ttTimeHelper::getRecordsForInterval($user_id, $start_date, $end_date);
+
+ $dataArray = array();
+
+ // Iterate through records and build $dataArray cell by cell.
+ foreach ($records as $record) {
+ // Create record id without suffix.
+ $record_id_no_suffix = ttTimeHelper::makeRecordIdentifier($record);
+ // Handle potential multiple records with the same attributes by using a numerical suffix.
+ $suffix = 0;
+ $record_id = $record_id_no_suffix.'_'.$suffix;
+ $day_header = substr($record['date'], 8); // Day number in month.
+ while (ttTimeHelper::cellExists($record_id, $day_header, $dataArray)) {
+ $suffix++;
+ $record_id = $record_id_no_suffix.'_'.$suffix;
+ }
+ // Find row.
+ $pos = ttTimeHelper::findRow($record_id, $dataArray);
+ if ($pos < 0) {
+ $dataArray[] = array('id' => $record_id,'label' => ttTimeHelper::makeRecordLabel($record)); // Insert row.
+ $pos = ttTimeHelper::findRow($record_id, $dataArray);
+ }
+ // Insert cell data from $record.
+ $dataArray[$pos][$day_header] = array('id' => $record['id'],'duration' => $record['duration']);
+ }
+ return $dataArray;
+ }
+
+ // cellExists is a helper function for getDataForWeekView() to see if a cell with a given label
+ // and a day header already exists.
+ static function cellExists($record_id, $day_header, $dataArray) {
+ foreach($dataArray as $row) {
+ if ($row['id'] == $record_id && !empty($row[$day_header]['duration']))
+ return true;
+ }
+ return false;
+ }
+
+ // findRow returns an existing row position in $dataArray, -1 otherwise.
+ static function findRow($record_id, $dataArray) {
+ $pos = 0; // Row position in array.
+ foreach($dataArray as $row) {
+ if ($row['id'] == $record_id)
+ return $pos;
+ $pos++; // Increment for search.
+ }
+ return -1; // Row not found.
+ }
+