Skip to content

Commit

Permalink
[LINST] Remove '_date' only for Standard Format (#6923)
Browse files Browse the repository at this point in the history
This fixes the removal of '_date' by only doing it for Standard Date formats.

Currently, all date elements (standard, basic, and month/year) in linst instruments have their field names redefined as the substring appearing before '_date'. For example, if a date element is called 'enrolment_date_v2', the instrument field name becomes enrolment. This results in the save function trying to update a field called 'enrolment' in the instrument table which does not exist.

The reason why this functionality existed is because the Standard Date format requires a field name ending with '_date'. Linst Standard Date formats are created by calling the NDB_BVL_Instrument class addDateElement() function which auto appends '_date' to the date element name. In order to avoid '_date' appearing twice, the NDB_BVL_Instrument_LINST class removes '_date' from the element name before calling addDateElement().

Basic Date and Month/Year formats call the NDB_Page class addBasicDate() function and the NDB_BVL_Instrument class addMonthYear) function respectively, both of which do not seem to auto append '_date' to the field name.

With this change, if a Standard Date format field name does not already end with '_date' , a LorisException is thrown.
  • Loading branch information
zaliqarosli authored Dec 21, 2020
1 parent 5166ada commit 434fd64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions php/libraries/NDB_BVL_Instrument_LINST.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,6 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$this->LinstQuestions[$pieces[1]] = ['type' => 'textarea'];
break;
case 'date':
if (strpos($pieces[1], "_date") !== false) {
$pieces[1] = substr(
$pieces[1],
0,
strpos($pieces[1], "_date")
);
}
if ($addElements) {
if ($pieces[3] == 1900 && $pieces[4] == 2100) {
$dateOptions = null;
Expand All @@ -582,7 +575,9 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
// Set date format
$dateFormat = isset($pieces[5]) ? trim($pieces[5]) : "";

if (isset($dateFormat) && $dateFormat != "") {
if (isset($dateFormat) && $dateFormat != ""
&& $dateFormat !== "Date"
) {
if ($dateFormat === 'MonthYear') {
// Shows date without day of month
$this->addMonthYear(
Expand All @@ -599,6 +594,20 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
);
}
} else {
// addDateElement appends '_date' to the field name so
// remove it first here
if (substr($pieces[1], -5) == "_date") {
$pieces[1] = substr(
$pieces[1],
0,
strpos($pieces[1], "_date")
);
} else {
throw new \LorisException(
"Standard Date format field $pieces[1] must end"
. " with '_date'."
);
}
// Shows standard date
$this->addDateElement(
$pieces[1],
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function testAllElements()
. "'option_1'=>'Option 1'{-}'option_2'=>'Option 2'{-}"
. "'option_3'=>'Option 3'{-}'option_4'=>'Option 4'{-}"
. "'not_answered'=>'Not Answered'\n";
$instrument .= "date{@}FieldName{@}Field Description{@}2003{@}2014\n";
$instrument .= "date{@}FieldName_date{@}Field Description{@}2003{@}2014\n";
$instrument .= "select{@}date_status{@}{@}NULL=>''{-}"
. "'not_answered'=>'Not Answered'\n";
$instrument .= "numeric{@}FieldName{@}Field Description{@}0{@}20\n";
Expand Down Expand Up @@ -252,7 +252,7 @@ function testAllElements()
],
[
'Type' => "date",
"Name" => "FieldName",
"Name" => "FieldName_date",
"Description" => "Field Description",
"Options" => [
"MinDate" => "2003-01-01",
Expand Down

0 comments on commit 434fd64

Please # to comment.