@@ -37,6 +37,7 @@ class ExtraField extends Model
37
37
public const FIELD_TYPE_GEOLOCALIZATION_COORDINATES = 25 ;
38
38
public const FIELD_TYPE_SELECT_WITH_TEXT_FIELD = 26 ;
39
39
public const FIELD_TYPE_TRIPLE_SELECT = 27 ;
40
+ public const FIELD_TYPE_DURATION = 28 ;
40
41
41
42
public $ columns = [
42
43
'id ' ,
@@ -871,6 +872,9 @@ public function get_handler_extra_data($itemId)
871
872
$ extra_data ["extra_ $ variable " ]["extra_ {$ variable }_second " ] = $ level2 ;
872
873
$ extra_data ["extra_ $ variable " ]["extra_ {$ variable }_third " ] = $ level3 ;
873
874
break ;
875
+ case self ::FIELD_TYPE_DURATION :
876
+ $ extra_data ['extra_ ' .$ field ['variable ' ]] = self ::formatDuration ((int ) $ field_value );
877
+ break ;
874
878
default :
875
879
$ extra_data ['extra_ ' .$ field ['variable ' ]] = $ field_value ;
876
880
break ;
@@ -889,6 +893,18 @@ public function get_handler_extra_data($itemId)
889
893
return $ extra_data ;
890
894
}
891
895
896
+ /**
897
+ * Formats a duration in seconds into hh:mm:ss.
898
+ */
899
+ private function formatDuration (int $ seconds ): string
900
+ {
901
+ $ hours = floor ($ seconds / 3600 );
902
+ $ minutes = floor (($ seconds % 3600 ) / 60 );
903
+ $ seconds = $ seconds % 60 ;
904
+
905
+ return sprintf ('%02d:%02d:%02d ' , $ hours , $ minutes , $ seconds );
906
+ }
907
+
892
908
/**
893
909
* Get an array of all the values from the extra_field and extra_field_options tables
894
910
* based on the current object's type.
@@ -1882,6 +1898,38 @@ public function set_extra_fields_in_form(
1882
1898
$ freezeElement
1883
1899
);
1884
1900
break ;
1901
+ case self ::FIELD_TYPE_DURATION :
1902
+ $ form ->addElement (
1903
+ 'text ' ,
1904
+ 'extra_ ' .$ variable ,
1905
+ $ field_details ['display_text ' ],
1906
+ ['class ' => 'span2 ' , 'placeholder ' => 'hh:mm:ss ' ]
1907
+ );
1908
+
1909
+ $ form ->addRule (
1910
+ 'extra_ ' .$ variable ,
1911
+ get_lang ('Invalid format ' ),
1912
+ 'callback ' ,
1913
+ 'validate_duration_format '
1914
+ );
1915
+
1916
+ $ form ->applyFilter ('extra_ ' .$ variable , function ($ value ) {
1917
+ if (preg_match ('/^(\d+):([0-5]?\d):([0-5]?\d)$/ ' , $ value , $ matches )) {
1918
+ return ($ matches [1 ] * 3600 ) + ($ matches [2 ] * 60 ) + $ matches [3 ];
1919
+ }
1920
+ return 0 ;
1921
+ });
1922
+
1923
+ if (isset ($ extraData ['extra_ ' .$ variable ]) && is_numeric ($ extraData ['extra_ ' .$ variable ])) {
1924
+ $ form ->setDefaults ([
1925
+ 'extra_ ' .$ variable => self ::formatDuration ((int ) $ extraData ['extra_ ' .$ variable ])
1926
+ ]);
1927
+ }
1928
+
1929
+ if ($ freezeElement ) {
1930
+ $ form ->freeze ('extra_ ' .$ variable );
1931
+ }
1932
+ break ;
1885
1933
}
1886
1934
}
1887
1935
}
@@ -1892,6 +1940,14 @@ public function set_extra_fields_in_form(
1892
1940
return $ return ;
1893
1941
}
1894
1942
1943
+ /**
1944
+ * Validates if a string is in the hh:mm:ss duration format.
1945
+ */
1946
+ function validate_duration_format ($ value ): bool
1947
+ {
1948
+ return (bool ) preg_match ('/^(\d+):([0-5]?\d):([0-5]?\d)$/ ' , $ value );
1949
+ }
1950
+
1895
1951
/**
1896
1952
* @param array $options
1897
1953
*
@@ -2012,6 +2068,7 @@ public static function get_extra_fields_by_handler($handler)
2012
2068
$ types [self ::FIELD_TYPE_GEOLOCALIZATION_COORDINATES ] = get_lang ('Geolocalization by coordinates ' );
2013
2069
$ types [self ::FIELD_TYPE_SELECT_WITH_TEXT_FIELD ] = get_lang ('Select with text field ' );
2014
2070
$ types [self ::FIELD_TYPE_TRIPLE_SELECT ] = get_lang ('Triple select ' );
2071
+ $ types [self ::FIELD_TYPE_DURATION ] = get_lang ('Duration (hh:mm:ss) ' );
2015
2072
2016
2073
switch ($ handler ) {
2017
2074
case 'course ' :
@@ -2540,11 +2597,20 @@ public function getRules(&$columns, &$column_model, $extraFields = [], $checkExt
2540
2597
foreach ($ fields as $ field ) {
2541
2598
$ search_options = [];
2542
2599
$ type = 'text ' ;
2543
- if (in_array ($ field ['value_type ' ], [self ::FIELD_TYPE_SELECT , self ::FIELD_TYPE_DOUBLE_SELECT ])) {
2544
- $ type = 'select ' ;
2545
- $ search_options ['sopt ' ] = ['eq ' , 'ne ' ]; //equal not equal
2546
- } else {
2547
- $ search_options ['sopt ' ] = ['cn ' , 'nc ' ]; //contains not contains
2600
+ switch ($ field ['value_type ' ]) {
2601
+ case self ::FIELD_TYPE_SELECT :
2602
+ case self ::FIELD_TYPE_DOUBLE_SELECT :
2603
+ $ type = 'select ' ;
2604
+ $ search_options ['sopt ' ] = ['eq ' , 'ne ' ];
2605
+ break ;
2606
+ case self ::FIELD_TYPE_DURATION :
2607
+ $ type = 'text ' ;
2608
+ $ search_options ['sopt ' ] = ['cn ' , 'nc ' ];
2609
+ break ;
2610
+ default :
2611
+ $ type = 'text ' ;
2612
+ $ search_options ['sopt ' ] = ['cn ' , 'nc ' ];
2613
+ break ;
2548
2614
}
2549
2615
2550
2616
$ search_options ['searchhidden ' ] = 'true ' ;
0 commit comments