Skip to content

Commit

Permalink
reactification & php endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jesscall committed Oct 5, 2021
1 parent 0bfea67 commit 35fea1d
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 35 deletions.
1 change: 1 addition & 0 deletions SQL/0000-00-00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ CREATE TABLE `diagnosis_evolution` (
`sourceField` varchar(255) DEFAULT NULL,
`orderNumber` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`DxEvolutionID`),
UNIQUE KEY `TrajectoryName` (`Name`),
CONSTRAINT `FK_DxEvolution_1` FOREIGN KEY (`ProjectID`) REFERENCES `Project` (`ProjectID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
1 change: 1 addition & 0 deletions SQL/New_patches/2021-07-28_diagnosis_evolution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE `diagnosis_evolution` (
`sourceField` varchar(255) DEFAULT NULL,
`orderNumber` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`DxEvolutionID`),
UNIQUE KEY `TrajectoryName` (`Name`),
CONSTRAINT `FK_DxEvolution_1` FOREIGN KEY (`ProjectID`) REFERENCES `Project` (`ProjectID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
37 changes: 25 additions & 12 deletions modules/configuration/jsx/DiagnosisEvolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DiagnosisEvolution extends Component {
this.fetchData = this.fetchData.bind(this);
this.setFormData = this.setFormData.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleReset = this.handleReset.bind(this);
this.addSourceField = this.addSourceField.bind(this);
this.removeSourceField = this.removeSourceField.bind(this);
}
Expand Down Expand Up @@ -181,6 +182,7 @@ class DiagnosisEvolution extends Component {
/>
<div>
<ButtonElement
name='submit'
label='Save'
type='submit'
onUserInput={this.handleSubmit}
Expand Down Expand Up @@ -234,13 +236,10 @@ class DiagnosisEvolution extends Component {
</p>
<p>
To configure study subprojects
<a href="{$baseurl}/configuration/subproject/">
click here
<a href="{$baseurl}/configuration/subproject/"> click here
</a>.
To configure study projects
<a href="{$baseurl}/configuration/project/">
click here
</a>.
<a href="{$baseurl}/configuration/project/"> click here</a>.
</p>
<VerticalTabs
tabs={tabList}
Expand All @@ -262,7 +261,6 @@ class DiagnosisEvolution extends Component {
*/
handleSubmit(e) {
e.preventDefault();
console.log('submit');

const tabID = this.state.currentTab;
let formData = tabID == 'new' ?
Expand All @@ -271,23 +269,27 @@ class DiagnosisEvolution extends Component {
console.log(formData);
let formObject = new FormData();
for (let key in formData) {
console.log(key);
if (formData[key] !== '') {
formObject.append(key, formData[key]);
}
}
formObject.append('fire_away', 'Diagnosis Trajectory');
console.log(formObject);
fetch(this.props.submitURL, {
method: 'POST',
cache: 'no-cache',
credentials: 'same-origin',
body: formObject,
}).then((resp) => {
if (resp.ok && resp.status === 201) {
resp.json().then((data) => console.log(data));
if (resp.ok) {
swal('Submission Successful!', '', 'success');
window.location.href =
`${loris.BaseURL}/configuration/diagnosis_evolution`;
} else {
resp.json().then((message) => console.log(message));
resp.json().then((msg) => {
let status = resp.status == 409 ?
'Conflict!' : 'Error!';
swal(status, msg.error, 'error');
});
}
}).catch((error) => {
console.log(error);
Expand All @@ -300,7 +302,18 @@ class DiagnosisEvolution extends Component {
* @param {event} e - Form submission event
*/
handleReset(e) {
e.preventDefault();
const tabID = this.state.currentTab;
const index = tabID == 'new' ?
'new' : 'diagnosisTracks[tabID]';
let formData = this.state.formData[index];
for (let key in formData) {
if (key !== 'DxEvolutionID') {
formData[key] = null;
}
}

this.setState({[index]: formData});
}

/**
Expand All @@ -310,7 +323,7 @@ class DiagnosisEvolution extends Component {
* @param {*} pendingValKey
* @param {*} id
*/
addSourceField(formElement, value, pendingValKey, id) {
addSourceField(formElement, value, pendingValKey) {
const tabID = this.state.currentTab;
let formData = this.state.formData;

Expand Down
144 changes: 121 additions & 23 deletions modules/configuration/php/diagnosis.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,6 @@ class Diagnosis extends \NDB_Page
);
}

/**
* Processes the values & saves to database and return a json response.
*
* @param ServerRequestInterface $request The incoming PSR7 request.
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handlePOST(ServerRequestInterface $request) : ResponseInterface
{
$this->updateDiagnosisTrajectory($request);
return new \LORIS\Http\Response\JsonResponse(
[]
);
}

/**
* Loads the project page.
*
Expand Down Expand Up @@ -130,8 +115,8 @@ class Diagnosis extends \NDB_Page
);

foreach ($diagnosisTracks as $key => $data) {
$diagnosisTracks[$key]['sourceField'] =
explode(",", $diagnosisTracks[$key]['sourceField']);
$diagnosisTracks[$key]['sourceField']
= explode(",", $diagnosisTracks[$key]['sourceField']);
}

$values['projects'] = $projects;
Expand All @@ -144,19 +129,132 @@ class Diagnosis extends \NDB_Page
}

/**
* Processes trajectory data submitted via diagnosis evolution form.
* Processes the values & saves to database and return a json response.
*
* @param ServerRequestInterface $request The incoming PSR7 request.
*
* @return ResponseInterface The outgoing PSR7 response
*/
function updateDiagnosisTrajectory(ServerRequestInterface $request) : ResponseInterface
private function _handlePOST(ServerRequestInterface $request) : ResponseInterface
{
// Parse POST request body.
$values = json_decode((string) $request->getBody(), true) ?? ['hello'];
$values = $request->getParsedBody();

$dxEvolutionID = $values['DxEvolutionID'] ?? null;
$name = $values['Name'] ?? null;
$projectID = $values['ProjectID'] ?? null;
$visit = $values['visitLabel'] ?? null;
$instrumentName = $values['instrumentName'] ?? null;
$sourceFields = explode(',', $values['sourceField']) ?? [];
$orderNumber = $values['orderNumber'] ?? null;

/**
* VALIDATE THE FORM
*/
// Validation: Form is complete
if (!($dxEvolutionID && $name && $visit
&& $instrumentName && $sourceFields && $orderNumber)
) {
return new \LORIS\Http\Response\JSON\BadRequest(
'Please fill out all fields!'
);
}

// Validation: Visit is part of Project's config
$projectVisits = \Utility::getVisitList(new \ProjectID($projectID));
if (!array_key_exists($visit, $projectVisits)) {
return new \LORIS\Http\Response\JSON\Conflict(
'Visit is not defined for selected Project.'
);
}

// Validation: Instrument is part of Visit's test battery
$visitInstruments = \Utility::getVisitInstruments($visit);
if (!array_key_exists($instrumentName, $visitInstruments)) {
return new \LORIS\Http\Response\JSON\Conflict(
'Instrument does not exist in selected visit.'
);
}

// Validation: Source Field belongs to Instrument
$instrumentFields = array_column(
\Utility::getSourcefields($instrumentName),
'Name'
);
$matches = array_intersect($sourceFields, $instrumentFields);
if (count($matches) !== count($sourceFields)) {
return new \LORIS\Http\Response\JSON\Conflict(
'Source Field does not exist in instrument.'
);
}

$set = [
"Name" => $name,
"ProjectID" => $projectID,
"visitLabel" => $visit,
"instrumentName" => $instrumentName,
"sourceField" => implode(',', $sourceFields),
"orderNumber" => $orderNumber
];

return $this->updateDiagnosisTrajectory($dxEvolutionID, $set);
}

/**
* Processes trajectory data submitted via diagnosis evolution form.
*
* @param string $dxEvolutionID The Diagnosis Track ID
* @param array $set The data to insert oir update in the DB
*
* @return ResponseInterface The outgoing PSR7 response
*/
function updateDiagnosisTrajectory(
string $dxEvolutionID,
array $set
) : ResponseInterface {
$DB = \Database::singleton();

// Create or update a Diagnosis Trajectory
if ($dxEvolutionID == 'new') {
// Validation: Name does not already exist
$dxNames = $DB->pselectCol(
"SELECT Name FROM diagnosis_evolution",
[]
);
if (in_array($set['Name'], $dxNames)) {
return new \LORIS\Http\Response\JSON\Conflict(
'Trajectory Name already exists.'
);
}

// TODO: finish implementation
error_log(print_r($values, true));
$DB->insert(
'diagnosis_evolution',
$set
);
return new \LORIS\Http\Response\JSON\OK();
} else {
// Validation: Name does not already exist
$dxNames = $DB->pselectCol(
"SELECT Name FROM diagnosis_evolution
WHERE DxEvolutionID NOT IN ($dxEvolutionID)",
[]
);
if (in_array($set['Name'], $dxNames)) {
return new \LORIS\Http\Response\JSON\Conflict(
'Trajectory Name already exists.'
);
}

// Update Diagnosis Trajectory
$DB->update(
'diagnosis_evolution',
$set,
['DxEvolutionID' => $dxEvolutionID]
);
return new \LORIS\Http\Response\JSON\OK();
}
return new \LORIS\Http\Response\JSON\BadRequest(
'Error'
);
}

/**
Expand Down

0 comments on commit 35fea1d

Please sign in to comment.