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] candidate creation #2863

Merged
merged 15 commits into from
Jul 31, 2017
116 changes: 68 additions & 48 deletions htdocs/api/v0.0.2/Candidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,57 +103,75 @@ function ($row) use ($projects) {
*/
public function handlePOST()
{
if (isset($this->RequestData['Candidate'])) {
$data = $this->RequestData;
if ($data === null) {
$this->header("HTTP/1.1 400 Bad Request");
$this->safeExit(0);
}
$data = $this->RequestData;
if ($data === null) {
$this->header("HTTP/1.1 400 Bad Request");
$this->error("Can't parse data");
$this->safeExit(0);
}

// This version of the API does not handle candidate creation
// when users are at multiple sites
$user = \User::singleton();
$centerIDs = $user->getCenterIDs();
$num_sites = count($centerIDs);
if ($num_sites == 0) {
$this->header("HTTP/1.1 401 Unauthorized");
$this->error("You are not affiliated with any site");
$this->safeExit(0);
} else if ($num_sites > 1) {
$this->header("HTTP/1.1 501 Not Implemented");
$this->error(
"This API version does not support candidate creation " .
"by users with multiple site affiliations. This will be ".
"implemented in a future release."
);
$this->safeExit(0);
} else {
$centerID = $centerIDs[0];
$this->verifyField($data, 'Gender', ['Male', 'Female']);
$this->verifyField($data, 'EDC', 'YYYY-MM-DD');
$this->verifyField($data, 'DoB', 'YYYY-MM-DD');
//Candidate::createNew
try {
$candid = $this->createNew(
$centerID,
$data['Candidate']['DoB'],
$data['Candidate']['EDC'],
$data['Candidate']['Gender'],
$data['Candidate']['PSCID']
);
$this->header("HTTP/1.1 201 Created");
$this->JSON = [
'Meta' => ["CandID" => $candid],
];
} catch(\LorisException $e) {
$this->header("HTTP/1.1 400 Bad Request");
$this->safeExit(0);
}
}
} else {
if (!isset($data['Candidate'])) {
$this->header("HTTP/1.1 400 Bad Request");
$this->error("There is no Candidate object in the POST data");
$this->safeExit(0);
}

// This version of the API does not handle candidate creation
// when users are at multiple sites
$user = \User::singleton();
$centerIDs = $user->getCenterIDs();
$num_sites = count($centerIDs);

if ($num_sites == 0) {
$this->header("HTTP/1.1 401 Unauthorized");
$this->error("You are not affiliated with any site");
$this->safeExit(0);
}

if ($num_sites > 1) {
$this->header("HTTP/1.1 501 Not Implemented");
$this->error(
"This API version does not support candidate creation " .
"by users with multiple site affiliations. This will be ".
"implemented in a future release."
);
$this->safeExit(0);
}

$centerID = $centerIDs[0];
$this->verifyField($data, 'Gender', ['Male', 'Female']);
$this->verifyField($data, 'DoB', 'YYYY-MM-DD');

//Candidate::createNew
try {
$candid = $this->createNew(
$centerID,
$data['Candidate']['DoB'],
$data['Candidate']['EDC'],
$data['Candidate']['Gender'],
$data['Candidate']['PSCID']
);

} catch(\LorisException $e) {
$this->header("HTTP/1.1 500 Internal Server Error");
$this->error("Candidate can't be created");
$this->safeExit(0);
}

if (isset($data['Candidate']['Project'])) {
$projectName = $data['Candidate']['Project'];
$project = \Project::singleton($projectName);
if (!empty($project)) {
\Candidate::singleton($candid)->setData(
array('ProjectID' => $project->getId())
);
}
}

$this->header("HTTP/1.1 201 Created");
$this->JSON = [
'Meta' => ["CandID" => $candid],
];
}

/**
Expand All @@ -171,16 +189,19 @@ protected function verifyField($data, $field, $values)
{
if (!isset($data['Candidate'][$field])) {
$this->header("HTTP/1.1 400 Bad Request");
$this->error("Candidate's field missing");
$this->safeExit(0);
}
if (is_array($values) && !in_array($data['Candidate'][$field], $values)) {
$this->header("HTTP/1.1 400 Bad Request");
$this->error("Value not permitted");
$this->safeExit(0);
}
if ($values === 'YYYY-MM-DD'
&& !preg_match("/\d\d\d\d\-\d\d\-\d\d/", $data['Candidate'][$field])
) {
$this->header("HTTP/1.1 400 Bad Request");
$this->error("Invalid date format");
$this->safeExit(0);
}
}
Expand All @@ -198,7 +219,6 @@ protected function verifyField($data, $field, $values)
*/
public function createNew($centerID, $DoB, $edc, $gender, $PSCID)
{
$user = \User::singleton();
return \Candidate::createNew(
$centerID,
$DoB,
Expand Down
9 changes: 9 additions & 0 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define('CANDIDATE_INVALID', 1);
define('PSCID_NOT_SPECIFIED', 2);
define('PSCID_NOT_UNIQUE', 3);
define('PSCID_INVALID_STRUCTURE', 4);
define('EDC_NOT_SPECIFIED', 5);

// id ranges...
define('CANDIDATE_MIN_CANDID', 100000);
Expand Down Expand Up @@ -204,6 +205,14 @@ class Candidate
// figure out how to generate PSCID
$config = $factory->config();
$PSCIDSettings = $config->getSetting('PSCID');
$EDCSettings = $config->getSetting('useEDC');

if (($useEDC === '1' || $useEDC === 'true') && empty($edc)) {
throw new \LorisException(
"EDC must be specified",
EDC_NOT_SPECIFIED
);
}

if ($PSCIDSettings['generation'] == 'user') {
// check pscid is specified
Expand Down