Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Add cohorts to project info #8774

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/api/docs/LorisRESTAPI_v0.0.4-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ The JSON object is of the form:
"CandID" : CandID,
"Visit" : VisitLabel,
"Site" : SiteName,
"Battery": "NameOfCohort",
"Cohort": "NameOfCohort",
"Project" : ProjectName
},
"Stages" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Visit_0_0_4_Dev extends Endpoint implements \LORIS\Middleware\ETagCalculat
}

return new \LORIS\Http\Response\JsonResponse(
(new \LORIS\api\Views\Visit($this->_visit))
(new \LORIS\api\Views\Visit($this->_visit, 'v0.0.4-dev'))
->toArray()
);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ class Visit_0_0_4_Dev extends Endpoint implements \LORIS\Middleware\ETagCalculat
'CandID',
'Visit',
'Site',
'Battery',
'Cohort',
'Project',
];

Expand Down Expand Up @@ -278,7 +278,7 @@ class Visit_0_0_4_Dev extends Endpoint implements \LORIS\Middleware\ETagCalculat
}

$cohortid = array_search(
$visitinfo['Battery'],
$visitinfo['Cohort'],
\Utility::getCohortList()
);

Expand Down
15 changes: 10 additions & 5 deletions modules/api/php/endpoints/project/project.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ class Project extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$pathparts = $request->getAttribute('pathparts');
$apiversion = $request->getAttribute("LORIS-API-Version") ?? "unknown";
$pathparts = $request->getAttribute('pathparts');

if (count($pathparts) === 0) {
switch ($request->getMethod()) {
case 'GET':
return $this->_handleGET($request);
return $this->_handleGET($apiversion);

case 'OPTIONS':
return (new \LORIS\Http\Response())
Expand Down Expand Up @@ -133,15 +135,17 @@ class Project extends Endpoint implements \LORIS\Middleware\ETagCalculator
* Generates a JSON representation of this project following the API
* specification.
*
* @param string $version The version of the API the GET is for.
*
* @return ResponseInterface
*/
private function _handleGET(): ResponseInterface
private function _handleGET($version): ResponseInterface
{
if (isset($this->_cache)) {
return $this->_cache;
}

$array = (new \LORIS\api\Views\Project($this->_project))
$array = (new \LORIS\api\Views\Project($this->_project, $version))
->toArray();

$this->_cache = new \LORIS\Http\Response\JsonResponse($array);
Expand All @@ -158,6 +162,7 @@ class Project extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
public function ETag(ServerRequestInterface $request) : string
{
return md5(json_encode($this->_handleGET($request)->getBody()));
$apiversion = $request->getAttribute("LORIS-API-Version") ?? "unknown";
return md5(json_encode($this->_handleGET($apiversion)->getBody()));
}
}
16 changes: 13 additions & 3 deletions modules/api/php/views/project.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class Project
/**
* Constructor
*
* @param \Project $project The project to format
* @param \Project $project The project to format
* @param string $apiversion The version of the API being accessed
*/
public function __construct(\Project $project)
public function __construct(\Project $project, private string $apiversion = '')
{
$this->_project = $project;
}
Expand All @@ -51,12 +52,21 @@ class Project
{
$meta = ['Project' => $this->_project->getName()];

return [
$obj = [
'Meta' => $meta,
'Candidates' => $this->_project->getCandidateIds(),
'Instruments' => array_keys(\Utility::getAllInstruments()),
'Visits' => $this->_getVisits(),
];
if ($this->apiversion == 'v0.0.4-dev') {
driusan marked this conversation as resolved.
Show resolved Hide resolved
$obj['Cohorts'] = array_map(
function ($cohort) {
return $cohort['title'];
},
$this->_project->getCohorts()
);
}
return $obj;
}

/**
Expand Down
22 changes: 13 additions & 9 deletions modules/api/php/views/visit.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ class Visit
/**
* Constructor which sets the instance variables based on the provided timepoint
*
* @param \Timepoint $timepoint The timepoint to represent
* @param \Timepoint $timepoint The timepoint to represent
* @param string $apiversion The version of the API being used
*/
public function __construct(\Timepoint $timepoint)
{
public function __construct(
\Timepoint $timepoint,
private string $apiversion = 'v0.0.3'
) {
$this->_timepoint = $timepoint;
}

Expand All @@ -43,12 +46,13 @@ class Visit
*/
public function toArray(): array
{
$meta = [
'CandID' => $this->_timepoint->getCandID(),
'Visit' => $this->_timepoint->getVisitLabel(),
'Site' => $this->_timepoint->getPSC(),
'Battery' => $this->_timepoint->getData('CohortTitle'),
'Project' => $this->_timepoint->getProject(),
$cohortKey = $this->apiversion == 'v0.0.4-dev' ? 'Cohort' : 'Battery';
$meta = [
'CandID' => $this->_timepoint->getCandID(),
'Visit' => $this->_timepoint->getVisitLabel(),
'Site' => $this->_timepoint->getPSC(),
$cohortKey => $this->_timepoint->getData('CohortTitle'),
driusan marked this conversation as resolved.
Show resolved Hide resolved
'Project' => $this->_timepoint->getProject(),
];

$stages = [];
Expand Down
10 changes: 5 additions & 5 deletions raisinbread/test/api/LorisApiVisitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testGetCandidatesCandidVisit(): void
'string'
);
$this->assertSame(
gettype($candidatesVisitArray['Meta']['Battery']),
gettype($candidatesVisitArray['Meta']['Cohort']),
'string'
);
$this->assertSame(
Expand All @@ -90,7 +90,7 @@ public function testGetCandidatesCandidVisit(): void
$this->assertArrayHasKey('CandID', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Project', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Site', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Battery', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Cohort', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Project', $candidatesVisitArray['Meta']);
$this->assertArrayHasKey('Stages', $candidatesVisitArray);
$this->assertArrayHasKey(
Expand Down Expand Up @@ -119,7 +119,7 @@ public function testPutCandidatesCandidVisit(): void
$json = ['CandID' => '900000',
'Visit' => "V1",
'Site' => "Data Coordinating Center",
'Battery' => "High Yeast",
'Cohort' => "High Yeast",
'Project' => "Rye",
];
$response = $this->client->request(
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testPutCandidatesCandidVisit(): void
$json = ['CandID' => "115788",
'Visit' => "V3",
'Site' => "Data Coordinating Center",
'Battery' => "Stale",
'Cohort' => "Stale",
'Project' => "Pumpernickel",
];
$response = $this->client->request(
Expand All @@ -196,7 +196,7 @@ public function testPutCandidatesCandidVisit(): void
$json = ['CandID' => '900000',
'Visit' => "V1",
'Site' => "Montreal",
'Battery' => "Stale",
'Cohort' => "Stale",
'Project' => "Pumpernickel",
];
$response = $this->client->request(
Expand Down
Loading