]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttTimeHelper.class.php
Added a helper function to build an array of day headears for week view.
[timetracker.git] / WEB-INF / lib / ttTimeHelper.class.php
index 50904886b8c5556cca9b0e5de5203fd0077dd4f5..d186696def7be94f864e093134838d7ad401acac 100644 (file)
@@ -160,13 +160,11 @@ class ttTimeHelper {
     return (int)@$time_a[1] + ((int)@$time_a[0]) * 60;
   }
 
-  // toAbsDuration - converts a number of minutes to format 00:00
+  // toAbsDuration - converts a number of minutes to format 0:00
   // even if $minutes is negative.
   static function toAbsDuration($minutes){
     $hours = (string)((int)abs($minutes / 60));
     $mins = (string)(abs($minutes % 60));
-    if (strlen($hours) == 1)
-      $hours = '0'.$hours;
     if (strlen($mins) == 1)
       $mins = '0' . $mins;
     return $hours.':'.$mins;
@@ -768,6 +766,32 @@ class ttTimeHelper {
     return $groupedRecords;
   }
 
+  /* This is work in progress, not working properly.
+  static function getDurationsForWeek($user_id, $start_date, $end_date) {
+    // Start by obtaining all records in interval.
+    // Then, iterate through them to build an array.
+    $records = ttTimeHelper::getRecordsForInterval($user_id, $start_date, $end_date);
+    $durations_with_labels = array();
+
+    foreach ($records as $record) {
+      $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;
+      while (!empty($durations_with_labels[$record_id][$record['date']])) {
+        $suffix++;
+        $record_id = $record_id_no_suffix.'_'.$suffix;
+      }
+      $groupedRecords[$record_identifier][$record['date']] = array('id'=>$record['id'], 'duration'=>$record['duration']);
+      $groupedRecords[$record_identifier]['client'] = $record['client'];
+      $groupedRecords[$record_identifier]['cf_1_value'] = $record['cf_1_value'];
+      $groupedRecords[$record_identifier]['project'] = $record['project'];
+      $groupedRecords[$record_identifier]['task'] = $record['task'];
+      $groupedRecords[$record_identifier]['billable'] = $record['billable'];
+    }
+  }
+  */
+
   // makeRecordIdentifier - builds a string identifying a record for a grouped display (such as a week view).
   // For example:
   // "cl:546,bl:0,pr:23456,ts:27464,cf_1:example text"
@@ -795,4 +819,44 @@ class ttTimeHelper {
 
     return $record_identifier;
   }
+
+  // getGroupedRecordsTotals - returns day totals for grouped records.
+  static function getGroupedRecordsTotals($groupedRecords) {
+    $groupedRecordsTotals = array();
+    foreach ($groupedRecords as $groupedRecord) {
+      foreach($groupedRecord as $key => $dayEntry) {
+        if ($dayEntry['duration']) {
+          $minutes = ttTimeHelper::toMinutes($dayEntry['duration']);
+          $groupedRecordsTotals[$key] += $minutes;
+        }
+      }
+    }
+    // Convert minutes to hh:mm for display.
+    foreach ($groupedRecordsTotals as $key => $single_total) {
+      $groupedRecordsTotals[$key] = ttTimeHelper::toAbsDuration($single_total);
+    }
+
+    return $groupedRecordsTotals;
+  }
+
+  // getDayHeadersForWeek - obtains day column headers for week view, which are simply day numbers in month.
+  static function getDayHeadersForWeek($start_date) {
+    $dayHeaders = array();
+    $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
+    $dayHeaders['day_header_0'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_1'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_2'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_3'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_4'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_5'] = $objDate->getDate();
+    $objDate->incDay();
+    $dayHeaders['day_header_6'] = $objDate->getDate();
+    unset($objDate);
+    return $dayHeaders;
+  }
 }