Skip to content

Extrafield: Support duration extra field (hh:mm:ss) - refs #5634 #6238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 71 additions & 5 deletions public/main/inc/lib/extra_field.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ExtraField extends Model
public const FIELD_TYPE_GEOLOCALIZATION_COORDINATES = 25;
public const FIELD_TYPE_SELECT_WITH_TEXT_FIELD = 26;
public const FIELD_TYPE_TRIPLE_SELECT = 27;
public const FIELD_TYPE_DURATION = 28;

public $columns = [
'id',
Expand Down Expand Up @@ -871,6 +872,9 @@ public function get_handler_extra_data($itemId)
$extra_data["extra_$variable"]["extra_{$variable}_second"] = $level2;
$extra_data["extra_$variable"]["extra_{$variable}_third"] = $level3;
break;
case self::FIELD_TYPE_DURATION:
$extra_data['extra_'.$field['variable']] = self::formatDuration((int) $field_value);
break;
default:
$extra_data['extra_'.$field['variable']] = $field_value;
break;
Expand All @@ -889,6 +893,18 @@ public function get_handler_extra_data($itemId)
return $extra_data;
}

/**
* Formats a duration in seconds into hh:mm:ss.
*/
private function formatDuration(int $seconds): string
{
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);
$seconds = $seconds % 60;

return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}

/**
* Get an array of all the values from the extra_field and extra_field_options tables
* based on the current object's type.
Expand Down Expand Up @@ -1882,6 +1898,38 @@ public function set_extra_fields_in_form(
$freezeElement
);
break;
case self::FIELD_TYPE_DURATION:
$form->addElement(
'text',
'extra_'.$variable,
$field_details['display_text'],
['class' => 'span2', 'placeholder' => 'hh:mm:ss']
);

$form->addRule(
'extra_'.$variable,
get_lang('Invalid format'),
'callback',
'validate_duration_format'
);

$form->applyFilter('extra_'.$variable, function ($value) {
if (preg_match('/^(\d+):([0-5]?\d):([0-5]?\d)$/', $value, $matches)) {
return ($matches[1] * 3600) + ($matches[2] * 60) + $matches[3];
}
return 0;
});

if (isset($extraData['extra_'.$variable]) && is_numeric($extraData['extra_'.$variable])) {
$form->setDefaults([
'extra_'.$variable => self::formatDuration((int) $extraData['extra_'.$variable])
]);
}

if ($freezeElement) {
$form->freeze('extra_'.$variable);
}
break;
}
}
}
Expand All @@ -1892,6 +1940,14 @@ public function set_extra_fields_in_form(
return $return;
}

/**
* Validates if a string is in the hh:mm:ss duration format.
*/
function validate_duration_format($value): bool
{
return (bool) preg_match('/^(\d+):([0-5]?\d):([0-5]?\d)$/', $value);
}

/**
* @param array $options
*
Expand Down Expand Up @@ -2012,6 +2068,7 @@ public static function get_extra_fields_by_handler($handler)
$types[self::FIELD_TYPE_GEOLOCALIZATION_COORDINATES] = get_lang('Geolocalization by coordinates');
$types[self::FIELD_TYPE_SELECT_WITH_TEXT_FIELD] = get_lang('Select with text field');
$types[self::FIELD_TYPE_TRIPLE_SELECT] = get_lang('Triple select');
$types[self::FIELD_TYPE_DURATION] = get_lang('Duration (hh:mm:ss)');

switch ($handler) {
case 'course':
Expand Down Expand Up @@ -2540,11 +2597,20 @@ public function getRules(&$columns, &$column_model, $extraFields = [], $checkExt
foreach ($fields as $field) {
$search_options = [];
$type = 'text';
if (in_array($field['value_type'], [self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT])) {
$type = 'select';
$search_options['sopt'] = ['eq', 'ne']; //equal not equal
} else {
$search_options['sopt'] = ['cn', 'nc']; //contains not contains
switch ($field['value_type']) {
case self::FIELD_TYPE_SELECT:
case self::FIELD_TYPE_DOUBLE_SELECT:
$type = 'select';
$search_options['sopt'] = ['eq', 'ne'];
break;
case self::FIELD_TYPE_DURATION:
$type = 'text';
$search_options['sopt'] = ['cn', 'nc'];
break;
default:
$type = 'text';
$search_options['sopt'] = ['cn', 'nc'];
break;
}

$search_options['searchhidden'] = 'true';
Expand Down
9 changes: 5 additions & 4 deletions src/CoreBundle/Entity/ExtraField.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ExtraField
public const FIELD_TYPE_FILE = 18;
public const FIELD_TYPE_GEOLOCALIZATION = 24;
public const FIELD_TYPE_GEOLOCALIZATION_COORDINATES = 25;
public const FIELD_TYPE_DURATION = 28;

#[Groups(['extra_field:read'])]
#[ORM\Column(name: 'id', type: 'integer')]
Expand Down Expand Up @@ -253,7 +254,7 @@ public function setFieldOrder(int $fieldOrder): self

public function isChangeable(): ?bool
{
return $this->changeable;
return (bool) $this->changeable;
}

public function setChangeable(bool $changeable): self
Expand All @@ -265,7 +266,7 @@ public function setChangeable(bool $changeable): self

public function isFilter(): bool
{
return $this->filter;
return (bool) $this->filter;
}

public function setFilter(bool $filter): self
Expand All @@ -277,7 +278,7 @@ public function setFilter(bool $filter): self

public function isVisibleToSelf(): bool
{
return $this->visibleToSelf;
return (bool) $this->visibleToSelf;
}

public function setVisibleToSelf(bool $visibleToSelf): self
Expand All @@ -289,7 +290,7 @@ public function setVisibleToSelf(bool $visibleToSelf): self

public function isVisibleToOthers(): bool
{
return $this->visibleToOthers;
return (bool) $this->visibleToOthers;
}

public function setVisibleToOthers(bool $visibleToOthers): self
Expand Down
Loading