Skip to content

Commit

Permalink
[candidate] Allow dob format Y-m (#8648)
Browse files Browse the repository at this point in the history
When a dob Format of Ym (year and month without day) is set in the config module, candidates could not be created for 2 reasons.

The wrong format (!Y-m-d) was being passed through the function DateTime::createFromFormat in the createNew function of the candidate class, which gave a wrong format error to the user even if they were passing through the expected format of Year-month
Even if this was fixed, the date was inserted into the candidate table as YYYY-MM, but this is not compatible with the SQL Type date
This fixes this issue by getting the date format from config, processing it to the right format and passing it through to DateTime::createFromFormat, as well as adding a '-15' to the end of the date if Ym format is selected in the configuration. This inserts the fifteenth of the month as the arbitrary date for a candidate.

Resolves #8586
  • Loading branch information
CamilleBeau authored May 11, 2023
1 parent 2f52b10 commit 80bfc9c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,25 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
DOB_NOT_SPECIFIED
);
}
$dob = DateTime::createFromFormat('!Y-m-d', $dateOfBirth);

// Get expected format from config
$dobFormat = $config->getSetting('dobFormat');
$dobFormat = '!' . implode("-", str_split($dobFormat, 1));
$dob = DateTime::createFromFormat($dobFormat, $dateOfBirth);

if ($dob === false) {
throw new InvalidArgumentException(
"Date of Birth is invalid (expected format: YYYY-MM-DD)",
DOB_INVALID
);
}

// Add day as first of the month if Y-m dob format
// This allows insert into sql candidate table
if ($dobFormat === '!Y-m') {
$dateOfBirth .= '-15';
}

if ($PSCIDSettings['generation'] == 'user') {
// check pscid is specified
if (empty($PSCID)) {
Expand Down

0 comments on commit 80bfc9c

Please sign in to comment.