Skip to content

Commit d549725

Browse files
authored
[LINST] modify LINST class to only strip '_date' from standard dates (#7260)
These changes were made to reflect the #6923 which was issued to the main branch Additional changes not in #6923: modified the "stripping" function to only remove _date if it is positioned at the end of the field name. For example if the user names their date field my_date_of_release_date, previously the function would strip the field name to my; now it would only strip the last _date to become my_date_of_release removed the check for isset($dateformat) since the code is already converting a null $dateformat to an empty string 2 lines above ! reorganized the if statements to reduce over all complexity added an exception if the date format does not match any known formats
1 parent e8eeebc commit d549725

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

php/libraries/NDB_BVL_Instrument_LINST.class.inc

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,6 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
558558
$this->LinstQuestions[$pieces[1]] = array('type' => 'textarea');
559559
break;
560560
case 'date':
561-
if (strpos($pieces[1], "_date") !== false) {
562-
$pieces[1] = substr(
563-
$pieces[1],
564-
0,
565-
strpos($pieces[1], "_date")
566-
);
567-
}
568561
if ($addElements) {
569562
if ($pieces[3] == 1900 && $pieces[4] == 2100) {
570563
$dateOptions = null;
@@ -582,42 +575,53 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
582575
// Set date format
583576
$dateFormat = isset($pieces[5]) ? trim($pieces[5]) : "";
584577

585-
if (isset($dateFormat) && $dateFormat != "") {
586-
if ($dateFormat === 'MonthYear') {
587-
// Shows date without day of month
588-
$this->addMonthYear(
589-
$pieces[1],
590-
$pieces[2],
591-
$dateOptions
592-
);
593-
}
594-
if ($dateFormat === 'BasicDate') {
595-
// Shows date without not answered dropdown
596-
$this->addBasicDate(
578+
if ($dateFormat === 'MonthYear') {
579+
// Shows date without day of month
580+
$this->addMonthYear(
581+
$pieces[1],
582+
$pieces[2],
583+
$dateOptions
584+
);
585+
} elseif ($dateFormat === 'BasicDate') {
586+
// Shows date without not answered dropdown
587+
$this->addBasicDate(
588+
$pieces[1],
589+
$pieces[2],
590+
$dateOptions
591+
);
592+
} elseif ($dateFormat === 'Date' || $dateFormat === "") {
593+
// The dateformat for a standard date should be
594+
// explicitly set to `Date` but for backwards
595+
// compatibility we support a null/empty dateformat and
596+
// default to standard date
597+
598+
// Check that the field name ENDS with `_date` as it
599+
// should for standard dates. Then strip the `_date` so
600+
// it is not duplicated by the addDateElement function
601+
if (substr($pieces[1], -5) === "_date") {
602+
$pieces[1] = substr(
597603
$pieces[1],
598-
$pieces[2],
599-
$dateOptions
604+
0,
605+
-5
600606
);
601-
}
602-
if ($dateFormat === 'Date') {
603-
// Shows standard date
604-
$this->addDateElement(
605-
$pieces[1],
606-
$pieces[2],
607-
$dateOptions
607+
} else {
608+
throw new \LorisException(
609+
"Standard Date format field `$pieces[1]` ".
610+
"in LINST file `$filename` must end with ".
611+
"'_date'."
608612
);
609613
}
610-
} else {
611-
// it will only enter here if the date format is not
612-
// explicitly set. This offers backwards compatibility
613-
// for date elements created before the date formats were
614-
// explicitly specified. An empty date format will be
615-
// handled as a `Date` element
614+
// Shows standard date
616615
$this->addDateElement(
617616
$pieces[1],
618617
$pieces[2],
619618
$dateOptions
620619
);
620+
} else {
621+
throw new \LorisException(
622+
"Unsupported dateformat `$dateFormat` in LINST ".
623+
"file `$filename` for date element `$pieces[1]`."
624+
);
621625
}
622626
}
623627
if ($firstFieldOfPage) {
@@ -956,5 +960,3 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
956960

957961

958962
}
959-
960-

test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function testAllElements() {
8888
$instrument .= "select{@}textarea_status{@}{@}NULL=>''{-}'not_answered'=>'Not Answered'\n";
8989
$instrument .= "select{@}FieldName{@}Field Description{@}NULL=>''{-}'option_1'=>'Option 1'{-}'option_2'=>'Option 2'{-}'option_3'=>'Option 3'{-}'not_answered'=>'Not Answered'\n";
9090
$instrument .= "selectmultiple{@}FieldName{@}Field Description{@}NULL=>''{-}'option_1'=>'Option 1'{-}'option_2'=>'Option 2'{-}'option_3'=>'Option 3'{-}'option_4'=>'Option 4'{-}'not_answered'=>'Not Answered'\n";
91-
$instrument .= "date{@}FieldName{@}Field Description{@}2003{@}2014\n";
91+
$instrument .= "date{@}FieldName_date{@}Field Description{@}2003{@}2014\n";
9292
$instrument .= "select{@}date_status{@}{@}NULL=>''{-}'not_answered'=>'Not Answered'\n";
9393
$instrument .= "numeric{@}FieldName{@}Field Description{@}0{@}20\n";
9494
$instrument .= "select{@}numeric_status{@}{@}NULL=>''{-}'not_answered'=>'Not Answered'";
@@ -182,7 +182,7 @@ function testAllElements() {
182182
],
183183
[
184184
'Type' => "date",
185-
"Name" => "FieldName",
185+
"Name" => "FieldName_date",
186186
"Description" => "Field Description",
187187
"Options" => [
188188
"MinDate" => "2003-01-01",

0 commit comments

Comments
 (0)