@@ -766,31 +766,57 @@ static function getGroupedRecordsForInterval($user_id, $start_date, $end_date) {
766766 return $ groupedRecords ;
767767 }
768768
769- /* This is work in progress, not working properly .
770- static function getDurationsForWeek ($user_id, $start_date, $end_date) {
769+ // getDataForWeekView - builds an array to render a table of durations for week view .
770+ static function getDataForWeekView ($ user_id , $ start_date , $ end_date ) {
771771 // Start by obtaining all records in interval.
772- // Then, iterate through them to build an array.
773772 $ records = ttTimeHelper::getRecordsForInterval ($ user_id , $ start_date , $ end_date );
774- $durations_with_labels = array();
775773
774+ $ dataArray = array ();
775+
776+ // Iterate through records and build $dataArray cell by cell.
776777 foreach ($ records as $ record ) {
778+ // Create record id without suffix.
777779 $ record_id_no_suffix = ttTimeHelper::makeRecordIdentifier ($ record );
778780 // Handle potential multiple records with the same attributes by using a numerical suffix.
779781 $ suffix = 0 ;
780782 $ record_id = $ record_id_no_suffix .'_ ' .$ suffix ;
781- while (!empty($durations_with_labels[$record_id][$record['date']])) {
783+ $ day_header = substr ($ record ['date ' ], 8 ); // Day number in month.
784+ while (ttTimeHelper::cellExists ($ record_id , $ day_header , $ dataArray )) {
782785 $ suffix ++;
783786 $ record_id = $ record_id_no_suffix .'_ ' .$ suffix ;
784787 }
785- $groupedRecords[$record_identifier][$record['date']] = array('id'=>$record['id'], 'duration'=>$record['duration']);
786- $groupedRecords[$record_identifier]['client'] = $record['client'];
787- $groupedRecords[$record_identifier]['cf_1_value'] = $record['cf_1_value'];
788- $groupedRecords[$record_identifier]['project'] = $record['project'];
789- $groupedRecords[$record_identifier]['task'] = $record['task'];
790- $groupedRecords[$record_identifier]['billable'] = $record['billable'];
788+ // Find row.
789+ $ pos = ttTimeHelper::findRow ($ record_id , $ dataArray );
790+ if ($ pos < 0 ) {
791+ $ dataArray [] = array ('id ' => $ record_id ,'label ' => ttTimeHelper::makeRecordLabel ($ record )); // Insert row.
792+ $ pos = ttTimeHelper::findRow ($ record_id , $ dataArray );
793+ }
794+ // Insert cell data from $record.
795+ $ dataArray [$ pos ][$ day_header ] = array ('id ' => $ record ['id ' ],'duration ' => $ record ['duration ' ]);
791796 }
797+ return $ dataArray ;
798+ }
799+
800+ // cellExists is a helper function for getDataForWeekView() to see if a cell with a given label
801+ // and a day header already exists.
802+ static function cellExists ($ record_id , $ day_header , $ dataArray ) {
803+ foreach ($ dataArray as $ row ) {
804+ if ($ row ['id ' ] == $ record_id && !empty ($ row [$ day_header ]['duration ' ]))
805+ return true ;
806+ }
807+ return false ;
808+ }
809+
810+ // findRow returns an existing row position in $dataArray, -1 otherwise.
811+ static function findRow ($ record_id , $ dataArray ) {
812+ $ pos = 0 ; // Row position in array.
813+ foreach ($ dataArray as $ row ) {
814+ if ($ row ['id ' ] == $ record_id )
815+ return $ pos ;
816+ $ pos ++; // Increment for search.
817+ }
818+ return -1 ; // Row not found.
792819 }
793- */
794820
795821 // makeRecordIdentifier - builds a string identifying a record for a grouped display (such as a week view).
796822 // For example:
@@ -820,6 +846,39 @@ static function makeRecordIdentifier($record) {
820846 return $ record_identifier ;
821847 }
822848
849+ // makeRecordLabel - builds a human readable label for a row in week view,
850+ // which is a combination ot record properties.
851+ // Client - Project - Task - Custom field 1.
852+ // Note that billable property is not part of the label. Instead, we intend to
853+ // identify such records with a different color in week view.
854+ static function makeRecordLabel ($ record ) {
855+ // TODO: debug this function.
856+ global $ user ;
857+ // Start with client.
858+ if ($ user ->isPluginEnabled ('cl ' ))
859+ $ label = $ record ['client ' ];
860+
861+ // Add project.
862+ $ project = $ record ['project ' ] ? $ record ['project ' ] : '' ;
863+ if (!empty ($ label )) $ label .= ' - ' ;
864+ $ label .= $ project ;
865+
866+ // Add task.
867+ $ task = $ record ['task ' ] ? $ record ['task ' ] : '' ;
868+ if (!empty ($ label )) $ label .= ' - ' ;
869+ $ label .= $ task ;
870+
871+ // Add custom field 1.
872+ if ($ user ->isPluginEnabled ('cf ' )) {
873+ if ($ record ['cf_1_value ' ]) {
874+ if (!empty ($ label )) $ label .= ' - ' ;
875+ $ label .= $ record ['cf_1_value ' ];
876+ }
877+ }
878+
879+ return $ label ;
880+ }
881+
823882 // getGroupedRecordsTotals - returns day totals for grouped records.
824883 static function getGroupedRecordsTotals ($ groupedRecords ) {
825884 $ groupedRecordsTotals = array ();
@@ -843,7 +902,7 @@ static function getGroupedRecordsTotals($groupedRecords) {
843902 static function getDayHeadersForWeek ($ start_date ) {
844903 $ dayHeaders = array ();
845904 $ objDate = new DateAndTime (DB_DATEFORMAT , $ start_date );
846- $ dayHeaders ['day_header_0 ' ] = $ objDate ->getDate ();
905+ $ dayHeaders ['day_header_0 ' ] = ( string ) $ objDate ->getDate (); // It returns an int on first call. Why?
847906 $ objDate ->incDay ();
848907 $ dayHeaders ['day_header_1 ' ] = $ objDate ->getDate ();
849908 $ objDate ->incDay ();
@@ -859,4 +918,28 @@ static function getDayHeadersForWeek($start_date) {
859918 unset($ objDate );
860919 return $ dayHeaders ;
861920 }
921+
922+ // getDayTotals calculates total durations for each day from the existing data in $dataArray.
923+ static function getDayTotals ($ dataArray , $ dayHeaders ) {
924+ $ dayTotals = array ();
925+
926+ // Insert label.
927+ global $ i18n ;
928+ $ dayTotals ['label ' ] = $ i18n ->getKey ('label.total ' );
929+
930+ foreach ($ dataArray as $ row ) {
931+ foreach ($ dayHeaders as $ dayHeader ) {
932+ if (array_key_exists ($ dayHeader , $ row )) {
933+ $ minutes = ttTimeHelper::toMinutes ($ row [$ dayHeader ]['duration ' ]);
934+ $ dayTotals [$ dayHeader ] += $ minutes ;
935+ }
936+ }
937+ }
938+ // Convert minutes to hh:mm for display.
939+ foreach ($ dayHeaders as $ dayHeader ) {
940+ $ dayTotals [$ dayHeader ] = ttTimeHelper::toAbsDuration ($ dayTotals [$ dayHeader ]);
941+ }
942+ return $ dayTotals ;
943+ }
862944}
945+
0 commit comments