-
Notifications
You must be signed in to change notification settings - Fork 173
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
[Survey module] Make public module #3609
Changes from 40 commits
723d27c
db677b3
21def7e
ceb5c62
b848120
62a455e
c9d0482
4a6253d
602dbe0
5970a4e
45c4c05
bda6eb9
b88ad69
9449cb7
9d8fe27
720ab59
ba630de
568e72e
b1454dc
de61422
a0acc83
5849fe4
3f20bd7
257f018
53ddb30
8d5351a
cbcbace
7d57f9c
7ad1335
84ed87c
8a4e7b4
8165c2e
b7f1235
a92f0ad
a69eecd
4d63691
6c8f297
e2f0c37
aad4ce1
ad87f45
1b19ce8
fe60ba9
48f1e62
202d68c
9c52c63
4aef021
81b1790
17dbbf5
f56569a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,364 @@ | ||
<?php | ||
/** | ||
* This is used by the Loris survey module to retrieve the email | ||
* template for the current instrument. It is used in the survey_accounts | ||
* page via AJAX to update the email template with the current page | ||
* | ||
* PHP Version 7 | ||
* | ||
* @category Survey | ||
* @package Loris | ||
* @author Jordan Stirling <jstirling91@gmail.com> | ||
* @license Loris license | ||
* @link https://www.github.com/aces/Loris/ | ||
*/ | ||
|
||
/** | ||
* Class to handle request types | ||
* | ||
* PHP Version 7 | ||
* | ||
* @category Survey | ||
* @package Loris | ||
* @author Jordan Stirling <jstirling91@gmail.com> | ||
* @license Loris license | ||
* @link https://www.github.com/aces/Loris/ | ||
*/ | ||
class DirectDataEntryMainPage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. htdocs/survey.php still exists |
||
{ | ||
var $tpl_data = array(); | ||
|
||
/** | ||
* Initialize all of the class variables and things required from the | ||
* REQUEST. | ||
* | ||
* @return null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "null" means it returns the value null. I'm pretty sure you're looking for "void" |
||
*/ | ||
function initialize() | ||
{ | ||
ob_start('ob_gzhandler'); | ||
$client = new NDB_Client(); | ||
$client->makeCommandLine(); | ||
$client->initialize(); | ||
|
||
$factory = NDB_Factory::singleton(); | ||
$config = $factory->config(); | ||
$settings = $factory->settings(); | ||
|
||
$this->tpl_data['baseurl'] = $settings->getBaseURL(); | ||
$this->key = $_REQUEST['key']; | ||
|
||
$DB = Database::singleton(); | ||
$this->SurveyInfo = $DB->pselect( | ||
"SELECT Status, Test_name, CommentID FROM participant_accounts | ||
WHERE OneTimePassword=:key", | ||
array('key' => $this->key) | ||
); | ||
|
||
if (empty($this->SurveyInfo)) { | ||
throw new Exception("The given survey doesn't exist", 404); | ||
} else if (count($this->SurveyInfo) > 1) { | ||
throw new Exception("Well looks like we made a mistake :(", 500); | ||
} else if ($this->SurveyInfo[0]['Status'] === 'Complete') { | ||
throw new Exception("Data has already been submitted.", 403); | ||
} | ||
|
||
$this->SurveyInfo = $this->SurveyInfo[0]; | ||
|
||
} | ||
|
||
/** | ||
* Handles a request by delegating to the appropriate | ||
* handle method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should have an ending dot to stay consistant with other comments |
||
* | ||
* @return null | ||
*/ | ||
function handleRequest() | ||
{ | ||
switch($_SERVER['REQUEST_METHOD']) { | ||
case 'GET': | ||
$this->handleGET(); | ||
break; | ||
case 'PUT': | ||
$this->handlePUT(); | ||
break; | ||
case 'PATCH': | ||
$this->handlePATCH(); | ||
break; | ||
case 'POST': | ||
$this->handlePOST(); | ||
break; | ||
default: | ||
$this->header("HTTP/1.1 501 Not Implemented"); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Handle a GET request. This will render and display the page. | ||
* | ||
* @return null | ||
*/ | ||
function handleGET() | ||
{ | ||
|
||
try { | ||
$this->Instrument = \NDB_BVL_Instrument::factory( | ||
$this->SurveyInfo['Test_name'], | ||
$this->SurveyInfo['CommentID'], | ||
null, | ||
true | ||
); | ||
} catch(\Exception $e) { | ||
throw new Exception("Instrument not found", 405); | ||
} | ||
|
||
$this->tpl_data['InstrumentJSON'] = $this->Instrument->toDirectJSON(); | ||
|
||
$Values = \NDB_BVL_Instrument::loadInstanceData($this->Instrument); | ||
|
||
unset($Values['CommentID']); | ||
unset($Values['UserID']); | ||
// unset($Values['Testdate']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this meant to be un-commented eventually, or should it be removed? |
||
unset($Values['Data_entry_completion_status']); | ||
|
||
// Unset score values | ||
$json_instrument = json_decode($this->tpl_data['InstrumentJSON']); | ||
$this->unsetScores($Values, $json_instrument->Elements); | ||
|
||
$this->tpl_data['Values'] = json_encode($Values); | ||
|
||
echo json_encode($this->tpl_data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the entirety of |
||
|
||
// $this->display(); | ||
} | ||
|
||
/** | ||
* Unsets Score values so that they are not transferred to the frontend | ||
* | ||
* @param pointer $values pointer to the values array | ||
* @param array $elements the elements to check if | ||
* score fields are present | ||
* | ||
* @return null | ||
*/ | ||
function unsetScores(&$values, $elements) | ||
{ | ||
foreach ($elements as $element) { | ||
if ($element->Type === 'ElementGroup') { | ||
$this->unsetScores($values, $element->Elements); | ||
} else if ($element->Type === 'label' | ||
&& array_key_exists($element->Name, $values) | ||
) { | ||
unset($values[$element->Name]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Handle a PATCH request. This will update a single field | ||
* | ||
* @return null | ||
*/ | ||
function handlePATCH() | ||
{ | ||
|
||
try { | ||
$this->Instrument = \NDB_BVL_Instrument::factory( | ||
$this->SurveyInfo['Test_name'], | ||
$this->SurveyInfo['CommentID'], | ||
null, | ||
true | ||
); | ||
} catch(\Exception $e) { | ||
throw new Exception("Instrument not found", 405); | ||
} | ||
|
||
$fp = fopen("php://input", "r"); | ||
$data = ''; | ||
while (!feof($fp)) { | ||
$data .= fread($fp, 1024); | ||
} | ||
fclose($fp); | ||
|
||
$data = json_decode($data); | ||
$instrument_name = $this->Instrument->testName; | ||
|
||
if (count($data) !== 1) { | ||
// The survey module will only PATCH one variable at a time. | ||
header("HTTP/1.0 400 Bad Request"); | ||
} | ||
|
||
if ($this->Instrument->validate($data)) { | ||
try { | ||
$this->Instrument->_save($data); | ||
|
||
} catch (Exception $e) { | ||
header("HTTP/1.0 400 Bad Request"); | ||
} | ||
} else { | ||
$this->Header("HTTP/1.1 403 Forbidden"); | ||
if (!$this->Instrument->determineDataEntryAllowed()) { | ||
$msg = "Can not update instruments that" | ||
. " are flagged as complete"; | ||
// $this->JSON = array('error' => $msg); | ||
} else { | ||
// $this->JSON = array("error" => "Could not update."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Handle a PATCH request. This will update a single field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :s/PATCH/PUT/ |
||
* | ||
* @return null | ||
*/ | ||
function handlePUT() | ||
{ | ||
|
||
$fp = fopen("php://input", "r"); | ||
$data = ''; | ||
while (!feof($fp)) { | ||
$data .= fread($fp, 1024); | ||
} | ||
fclose($fp); | ||
|
||
$data = json_decode($data, true); | ||
$subtest = null; | ||
|
||
if ($data['page'] !== 0) { | ||
$subtest = $data['page']; | ||
} | ||
|
||
try { | ||
$this->Instrument = \NDB_BVL_Instrument::factory( | ||
$this->SurveyInfo['Test_name'], | ||
$this->SurveyInfo['CommentID'], | ||
$subtest, | ||
true | ||
); | ||
} catch(\Exception $e) { | ||
throw new Exception("Instrument not found", 405); | ||
} | ||
|
||
$this->Instrument->form->directEntry = true; | ||
$this->Instrument->form->directValues = $data['data']; | ||
|
||
if ($this->Instrument->form->validate()) { | ||
if ($data['FinalPage']) { | ||
echo $this->Instrument->getReactReview(); | ||
} else { | ||
echo $this->Instrument->toDirectJSON(); | ||
} | ||
} else { | ||
header("HTTP/1.0 400 Bad Request"); | ||
echo json_encode($this->Instrument->form->errors); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Handle a PATCH request. This will update a single field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :s/PATCH/POST/ |
||
* | ||
* @return null | ||
*/ | ||
function handlePOST() | ||
{ | ||
|
||
$fp = fopen("php://input", "r"); | ||
$data = ''; | ||
while (!feof($fp)) { | ||
$data .= fread($fp, 1024); | ||
} | ||
fclose($fp); | ||
|
||
$data = json_decode($data, true); | ||
|
||
try { | ||
$this->Instrument = \NDB_BVL_Instrument::factory( | ||
$this->SurveyInfo['Test_name'], | ||
$this->SurveyInfo['CommentID'], | ||
null, | ||
true | ||
); | ||
} catch(\Exception $e) { | ||
throw new Exception("Instrument not found", 405); | ||
} | ||
|
||
$valid = $this->Instrument->directEntryValidation(); | ||
|
||
if ($valid === true) { | ||
header("HTTP/1.0 200 OK"); | ||
$DB = Database::singleton(); | ||
$DB->update( | ||
"participant_accounts", | ||
array('Status' => "Complete"), | ||
array('OneTimePassword' => $this->key) | ||
); | ||
} else { | ||
header("HTTP/1.0 400 Bad Request"); | ||
echo json_encode($valid); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Run the current page, consists of initializing and then displaying the page | ||
* | ||
* @return null | ||
*/ | ||
function run() | ||
{ | ||
try { | ||
$this->initialize(); | ||
$this->handleRequest(); | ||
} catch(Exception $e) { | ||
$this->displayError($e); | ||
} | ||
} | ||
|
||
/** | ||
* Loads the correct page and renders it to the user | ||
* | ||
* @return null | ||
*/ | ||
function display() | ||
{ | ||
$smarty = new Smarty_neurodb; | ||
$smarty->assign($this->tpl_data); | ||
$smarty->display('directentryreact.tpl'); | ||
} | ||
|
||
/** | ||
* Display an error page in the event of an exception. | ||
* | ||
* @param Exception $e The exception which was thrown by the code | ||
* | ||
* @return null, but as a side-effect changes the HTTP return code | ||
*/ | ||
function displayError($e) | ||
{ | ||
switch($e->getCode()) | ||
{ | ||
case 404: | ||
header("HTTP/1.1 404 Not Found"); | ||
break; | ||
case 403: | ||
header("HTTP/1.1 403 Forbidden"); | ||
break; | ||
} | ||
|
||
$this->tpl_data['workspace'] = $e->getMessage(); | ||
$this->tpl_data['complete'] = false; | ||
$smarty = new Smarty_neurodb; | ||
$smarty->assign($this->tpl_data); | ||
$smarty->display('directentry.tpl'); | ||
|
||
} | ||
} | ||
|
||
if (!class_exists('UnitTestCase')) { | ||
$Runner = new DirectDataEntryMainPage(); | ||
$Runner->run(); | ||
} | ||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Check if the input is supported by the Browser | ||
*/ | ||
function checkInput(type) { | ||
var input = document.createElement("input"); | ||
input.setAttribute("type", type); | ||
return input.type == type; | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing ending dot