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] PUT and PATCH methods added to Candidates visit/instruments/flags #6780

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1151d66
[API] PUT and POST methods added to Candidates visit/instruments/flag…
spell00 Jun 29, 2020
3337f3d
[API] instruments endpoint working
spell00 Aug 21, 2020
4d7401e
[API] handlePUT is now working for instruments flags
spell00 Aug 21, 2020
8c3adb6
Removed useless code left behind
spell00 Aug 21, 2020
83ee674
Fixing Travis
spell00 Aug 21, 2020
d65f300
[API] use of NDB_BVL_InstrumentStatus to change flags status with api
spell00 Aug 24, 2020
9b39ff8
[API] The instruments status in flags all need to allow null, not jus…
spell00 Aug 24, 2020
6627506
[API] Fixing Travis
spell00 Aug 24, 2020
34862a2
Adress checking for data key instead of if is null
spell00 Sep 4, 2020
2b29d02
update flags
spell00 Sep 14, 2020
2c19dee
Fixing travis in part
spell00 Sep 14, 2020
13b7822
Removes automatic changes in NDB_BVL_InstrumentStatus.class.inc
spell00 Sep 15, 2020
aad0b22
enabling null entries for put and patch instruments flags
spell00 Sep 17, 2020
488c2a8
Remove line at end of file added by PHPCBF automatic correction
spell00 Sep 17, 2020
ef24d0c
[API] Remove useless required Meta fields in PUT request for candidat…
spell00 Sep 18, 2020
d41003b
[API] Makes handlePUT and handlePATCH methods descriptions more verbose
spell00 Sep 18, 2020
d5d5882
Fix travis
spell00 Sep 18, 2020
e217822
commit in github did not actually add the line after handlePUT
spell00 Oct 30, 2020
1c20313
remove useles continue 2s
spell00 Oct 30, 2020
f71a530
Apply Xavier's suggestions from code review
spell00 Oct 30, 2020
863e5d7
detail
spell00 Oct 30, 2020
cf4b0e0
checking for user permission for instrument
spell00 Nov 5, 2020
9249d1e
fix travis
spell00 Nov 5, 2020
c5bed65
removing not string parameter from string
spell00 Nov 5, 2020
6f9b45a
remove detail
spell00 Nov 6, 2020
7c69af2
final
spell00 Nov 6, 2020
9c12b7c
Check for data_entry
spell00 Nov 6, 2020
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
105 changes: 103 additions & 2 deletions modules/api/php/endpoints/candidate/visit/instrument/flags.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ class Flags extends Endpoint implements \LORIS\Middleware\ETagCalculator
return $this->_handleGET($request);

case 'PUT':
return $this->_handlePUT($request);
spell00 marked this conversation as resolved.
Show resolved Hide resolved
spell00 marked this conversation as resolved.
Show resolved Hide resolved

case 'PATCH':
// TODO :: I don`t think this was working in v0.0.3
return new \LORIS\Http\Response\JSON\NotImplemented();
return $this->_handlePATCH($request);

case 'OPTIONS':
return (new \LORIS\Http\Response())
Expand Down Expand Up @@ -127,6 +128,106 @@ class Flags extends Endpoint implements \LORIS\Middleware\ETagCalculator
);
}

/**
* Handles a PUT request.
* Creates or updates all statuses for a given instrument with new values.
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handlePUT(ServerRequestInterface $request) : ResponseInterface
{
$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus();
$this->_instrumentStatus->select($this->_instrument->commentID);
spell00 marked this conversation as resolved.
Show resolved Hide resolved

$data = json_decode((string) $request->getBody(), true);

xlecours marked this conversation as resolved.
Show resolved Hide resolved
try {

$requiredfields = [
'Data_entry',
'Administration',
'Validity',
];

$diff = array_diff($requiredfields, array_keys($data['Flags']));
if (!empty($diff)) {
return new \LORIS\Http\Response\JSON\BadRequest(
'Field(s) missing in Flags: ' . implode(', ', $diff)
);
}
$this->_instrumentStatus->setDataEntryStatus(
$data['Flags']['Data_entry']
);
$this->_instrumentStatus->setAdministrationStatus(
$data['Flags']['Administration']
);
$this->_instrumentStatus->setValidityStatus(
$data['Flags']['Validity']
);

} catch (\Throwable $e) {
error_log($e->getMessage());
return new \LORIS\Http\Response\JSON\InternalServerError();
}
return (new \LORIS\Http\Response())
->withStatus(204);
}

/**
* Handles a PATCH request.
* Creates or updates some or all statuses for a given instrument. If all
* statuses are requested to be changed, this method is equivalent to handlePUT.
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handlePATCH(ServerRequestInterface $request): ResponseInterface
{
$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus();
$this->_instrumentStatus->select($this->_instrument->commentID);

if (!$this->_instrument->determineDataEntryAllowed()) {
xlecours marked this conversation as resolved.
Show resolved Hide resolved
return new \LORIS\Http\Response\JSON\Forbidden(
'Can not update instruments that are flagged as complete.'
);
}

$data = json_decode((string) $request->getBody(), true);

if (!$this->_instrument->validate($data)) {
return new \LORIS\Http\Response\JSON\Forbidden(
'Could not update.'
);
}

try {
if (array_key_exists('Data_entry', $data['Flags'])) {
$this->_instrumentStatus->setDataEntryStatus(
$data['Flags']['Data_entry']
);
}
if (array_key_exists('Administration', $data['Flags'])) {
$this->_instrumentStatus->setAdministrationStatus(
$data['Flags']['Administration']
);
}
if (array_key_exists('Validity', $data['Flags'])) {
$this->_instrumentStatus->setValidityStatus(
$data['Flags']['Validity']
);
}
} catch (\Throwable $e) {
error_log($e->getMessage());
return new \LORIS\Http\Response\JSON\InternalServerError();
}
return (new \LORIS\Http\Response())
->withStatus(204);
}


/**
* Implements the ETagCalculator interface
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class Instrument extends Endpoint implements \LORIS\Middleware\ETagCalculator
{
$pathparts = $request->getAttribute('pathparts');
$flags = array_search('flags', $pathparts) !== false;
$user = $request->getAttribute('user');

if ($this->_instrument->_hasAccess($user)) {
return new \LORIS\Http\Response\JSON\Forbidden(
'Can not update instruments for user' . $user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think $user is a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_hasAccess does not take a string

function _hasAccess(\User $user) : bool

Copy link
Contributor Author

@spell00 spell00 Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind I just got it, I'll remove the parameter $user from the message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

);
}

if ($flags) {
// Delegate to sub-endpoints
Expand Down Expand Up @@ -150,8 +157,6 @@ class Instrument extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
private function _handlePUT(ServerRequestInterface $request) : ResponseInterface
{
// TODO :: Check permissions. How??

if (!$this->_instrument->determineDataEntryAllowed()) {
return new \LORIS\Http\Response\JSON\Forbidden(
'Can not update instruments that are flagged as complete.'
Expand Down Expand Up @@ -188,8 +193,6 @@ class Instrument extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
private function _handlePATCH(ServerRequestInterface $request): ResponseInterface
{
// TODO :: Check permissions. How??

if (!$this->_instrument->determineDataEntryAllowed()) {
return new \LORIS\Http\Response\JSON\Forbidden(
'Can not update instruments that are flagged as complete.'
Expand Down
3 changes: 3 additions & 0 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,10 @@ abstract class NDB_BVL_Instrument extends NDB_Page
foreach ($columns as $row) {
switch ($row['COLUMN_NAME']) {
case 'CommentID':
case 'ID':
case 'SessionID':
case 'UserID':
case 'Test_name':
continue 2;
case 'Data_entry_completion_status':
$values[$row['COLUMN_NAME']] = 'Incomplete';
Expand Down
18 changes: 9 additions & 9 deletions php/libraries/NDB_BVL_InstrumentStatus.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ class NDB_BVL_InstrumentStatus
/**
* Sets the current data entry status of the instrument
*
* @param string $status the new status from the set (NULL,
* 'In Progress','Not Complete','Complete')
* @param ?string $status the new status from the set (NULL,
* 'In Progress','Not Complete','Complete')
*
* @return void
*/
function setDataEntryStatus(string $status): void
function setDataEntryStatus(?string $status): void
spell00 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!isset($this->_commentID)) {
throw new Exception("No instrument instance selected");
Expand Down Expand Up @@ -236,12 +236,12 @@ class NDB_BVL_InstrumentStatus
/**
* Sets the current administration status of the instrument
*
* @param string $status the new status from the set
* (NULL, 'None','Partial','All')
* @param ?string $status the new status from the set
* (NULL, 'None','Partial','All')
*
* @return void
*/
function setAdministrationStatus(string $status): void
function setAdministrationStatus(?string $status): void
spell00 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!isset($this->_commentID)) {
throw new Exception("No instrument instance selected");
Expand All @@ -262,12 +262,12 @@ class NDB_BVL_InstrumentStatus
/**
* Sets the current validity status of the instrument
*
* @param string $status the new status from the set
* (NULL, 'Questionable', 'Invalid', 'Valid')
* @param ?string $status the new status from the set
* (NULL, 'Questionable', 'Invalid', 'Valid')
*
* @return void
*/
function setValidityStatus(string $status): void
function setValidityStatus(?string $status): void
spell00 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!isset($this->_commentID)) {
throw new Exception("No instrument instance selected");
Expand Down