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

[Survey module] Make public module #3609

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
723d27c
get a basic prototype of surveys working with partial save and page v…
jstirling91 Sep 5, 2017
db677b3
fix bug where selects loaded with previous page values if null and sc…
jstirling91 Sep 5, 2017
21def7e
add DirectEntry.js to webpack config file
jstirling91 Sep 18, 2017
ceb5c62
fix errors for instruments and flag errors for dates and select in Di…
jstirling91 Sep 18, 2017
b848120
add ability to finalize the survey
jstirling91 Sep 18, 2017
62a455e
fix bug where form wasnt clearing from main page to page 2 and have s…
jstirling91 Sep 19, 2017
c9d0482
add completion bar
jstirling91 Sep 26, 2017
4a6253d
remove whitespace at top and make text scale to device
jstirling91 Nov 24, 2017
602dbe0
fix bug with running XINRules
jstirling91 Feb 13, 2018
5970a4e
create survey module for public access
jstirling91 Mar 6, 2018
45c4c05
add survey module jsx files to webpack
jstirling91 Mar 6, 2018
bda6eb9
make GET call conform like other api calls
jstirling91 Mar 6, 2018
b88ad69
fix bug in page setup using wrong variable
jstirling91 Mar 6, 2018
9449cb7
refix xinrule error
jstirling91 Mar 6, 2018
9d8fe27
remove second call to _setupForm
jstirling91 Apr 13, 2018
720ab59
fix bug with null labels being passed to Markdown.js
jstirling91 Apr 16, 2018
ba630de
turn question labels red when theres an error
jstirling91 Apr 16, 2018
568e72e
fixing XINRules again... I think
jstirling91 Apr 16, 2018
b1454dc
adding forgotten file
jstirling91 Apr 16, 2018
de61422
Merge remote-tracking branch 'aces/major' into survey_module
jstirling91 Apr 24, 2018
a0acc83
fix error with numerics
jstirling91 Apr 24, 2018
5849fe4
Fixed the wrong page validation for good.... I Hope
jstirling91 Apr 24, 2018
3f20bd7
dont pass score values to frontend
jstirling91 Apr 24, 2018
257f018
handle case when key exists in array but value you is null when check…
jstirling91 Apr 24, 2018
53ddb30
fix dates for browser that are stuck in the bronze age
jstirling91 Apr 24, 2018
8d5351a
render date element for groups
jstirling91 Apr 24, 2018
cbcbace
fix html showing in select option labels
jstirling91 Apr 24, 2018
7d57f9c
fix html displaying for header elements
jstirling91 Apr 24, 2018
7ad1335
fix bug where text elements werent displaying their values
jstirling91 Apr 24, 2018
84ed87c
allow for deselecting select elements
jstirling91 Apr 24, 2018
8a4e7b4
display error messages
jstirling91 Apr 24, 2018
8165c2e
auto scroll to first error on page
jstirling91 Apr 24, 2018
b7f1235
fix phpcs error
jstirling91 Apr 24, 2018
a92f0ad
remove unused file
jstirling91 Apr 24, 2018
a69eecd
fix phpcs errors
jstirling91 Apr 24, 2018
4d63691
remove unwanted file
jstirling91 Apr 24, 2018
6c8f297
remove unused variables
jstirling91 Apr 24, 2018
e2f0c37
Ignoring eslint due to requiring 2spaces as opposed to a tab
jstirling91 Apr 24, 2018
aad4ce1
fix eslint errors
jstirling91 Apr 24, 2018
ad87f45
fix phan errors
jstirling91 Apr 25, 2018
1b19ce8
fix more phan errors and switch null to void
jstirling91 Apr 25, 2018
fe60ba9
fix using of wrong debug statement in js file
jstirling91 Apr 30, 2018
48f1e62
remove error_log
jstirling91 Apr 30, 2018
202d68c
fix weird bug where react set text value to previous value when value…
jstirling91 Apr 30, 2018
9c52c63
fix bug where you couldnt dismiss error alert
jstirling91 Apr 30, 2018
4aef021
json encode data so that it can be passed to frontend
jstirling91 Apr 30, 2018
81b1790
cast select option labels to strings
jstirling91 Apr 30, 2018
17dbbf5
implement time element in groups
jstirling91 Apr 30, 2018
f56569a
fix bug in IE where page wasn't loading due to URL object
jstirling91 Apr 30, 2018
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
364 changes: 364 additions & 0 deletions modules/survey_module/ajax/survey_api.php
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
Copy link
Contributor

Choose a reason for hiding this comment

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

missing ending dot

*
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Contributor

@PapillonMcGill PapillonMcGill Jun 26, 2018

Choose a reason for hiding this comment

The 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']);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the entirety of $this->tpl_data is being json_encoded, is the json_encode on 129 necessary? I believe json_encode works recursively.


// $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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
?>
8 changes: 8 additions & 0 deletions modules/survey_module/js/CheckInput.js
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;
}
2 changes: 2 additions & 0 deletions modules/survey_module/js/DirectEntry.js

Large diffs are not rendered by default.

Loading