From 35fea1d4a56a5e518998ff7b93dd26e6cb072bc1 Mon Sep 17 00:00:00 2001 From: jesscall Date: Tue, 5 Oct 2021 15:47:44 -0400 Subject: [PATCH] reactification & php endpoint --- SQL/0000-00-00-schema.sql | 1 + .../2021-07-28_diagnosis_evolution.sql | 1 + .../configuration/jsx/DiagnosisEvolution.js | 37 +++-- modules/configuration/php/diagnosis.class.inc | 144 +++++++++++++++--- 4 files changed, 148 insertions(+), 35 deletions(-) diff --git a/SQL/0000-00-00-schema.sql b/SQL/0000-00-00-schema.sql index 1d3f5ed6ef5..5f36b2c9b87 100644 --- a/SQL/0000-00-00-schema.sql +++ b/SQL/0000-00-00-schema.sql @@ -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; diff --git a/SQL/New_patches/2021-07-28_diagnosis_evolution.sql b/SQL/New_patches/2021-07-28_diagnosis_evolution.sql index d9a80c83b5f..7824a4bd996 100644 --- a/SQL/New_patches/2021-07-28_diagnosis_evolution.sql +++ b/SQL/New_patches/2021-07-28_diagnosis_evolution.sql @@ -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; diff --git a/modules/configuration/jsx/DiagnosisEvolution.js b/modules/configuration/jsx/DiagnosisEvolution.js index daf82d0cac3..e57c0b2f153 100644 --- a/modules/configuration/jsx/DiagnosisEvolution.js +++ b/modules/configuration/jsx/DiagnosisEvolution.js @@ -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); } @@ -181,6 +182,7 @@ class DiagnosisEvolution extends Component { />

To configure study subprojects - - click here + click here . To configure study projects - - click here - . + click here.

{ - 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); @@ -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}); } /** @@ -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; diff --git a/modules/configuration/php/diagnosis.class.inc b/modules/configuration/php/diagnosis.class.inc index 123fc8c188a..f788db64237 100644 --- a/modules/configuration/php/diagnosis.class.inc +++ b/modules/configuration/php/diagnosis.class.inc @@ -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. * @@ -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; @@ -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' + ); } /**