Questionnaire

You are here:
< Back

/**
* Questionnaire (QNR) Template
* Version 2.0 Mike 1-Feb-2016
*/

// To generate QNR SQL table (prepend TBL_PREFIX (‘cdc’) to QNR filename without .php)
$crftable = 'cdc_qnr_filename';

// Generate_tables_sql.php run shell_exec with $argv[1]=’all’ to generate QNR column names
if (isset($argv) && $argv[1] == 'all') {
require_once '../libraries/config.php';
runall_pages($crftable);
exit; // stop here for generate sql script
}

// visitId is an essential primary key used for a QNR which appears across multiple visits
$visitId = (int) $_GET['v'];

// Fetch answers from or Insert details of first attempt to QNR table
prepare_instru($crftable, $visitId);

/**
* QNR CODING – STARTS HERE
*/

/**
* Questions
* Format: $qn[‘$fieldname’] = ‘helloworld’;
* Insert ‘helloworld’ into QNR table_dict as ‘data-dictionary’ associated with $fieldname
* for Data Dump (CSV) in Administrator Tools
*/
$qn['radio'] = '1. radio: Radio type question with vertical options on new line after question:';
$qn['select'] = '2. select: Standard drop-down inline with question:';
$qn['text'] = '3. text: Standard text input:';

// Insert or Update data dictionary table _dict
update_dict($crftable, $qn);

/**
* SUBMIT FORM
*/
if (isset($_POST['submit'])) {

// Create ($fname=$value,$fname=$value,…) for SQL Update statement
// and store $_POST values to $_SESSION to pre-load questions with answers later
$sql_update_str = create_update_string_store_session($_POST);

// Update QNR table if ALL validations PASS!
if (startValidate($qn)) {
update_instru($crftable, $visitId, $sql_update_str);
}
}

/**
* Add-on PHP, MySQL, JavaScript codes (optional)
*/

/**
* HEAD
* Add-on codes in <head>HERE</head> for this QNR only (e.g. <script>JavaScript</script>)
* Add-on codes in <body HERE> for this QNR only (e.g. <body onload=”myFunction()”>)
*/
$custom_head = '';
$custom_body = '';

// HTML basic tags, CSS, JavaScript and JQuery
get_head($_SESSION['crfname'], $custom_head, $custom_body);

/**
* CONTENT
* $current_page to display which page?
* &$qn for addElement() functions
*/
function page($current_page, &$qn) {

if ($current_page == 1) {
echo '<h3>Questions</h3>';
$options = array('Yes' => 'Yes', 'No' => 'No');

// Q1
$fname = 'q1_gender';
addElement('radio', $fname, $qn[$fname], null, $options);
echo '<br>';

// Q2
$fname = 'q2_age';
addElement('select', $fname, $qn[$fname], array(), $options);
echo '<br>';

// Submit button
addElement('submit', 'submit', null, array('value'=>'Submit'));

} // if ($current_page == 1) {

if ($current_page == 2) {

// Q3
$fname = 'q3_name';
addElement('text', $fname, $qn[$fname]);
echo '<br>';

// Submit button
addElement('submit', 'submit', null, array('value'=>'Submit'));

} // if ($current_page == 2) {

}

// Construct main body of QNR <form>. . .</form>
load_form($crftable, $visitId, $qn);

/**
* FOOTER
* Add-on codes (e.g. jQuery) just before </body></html>
*/
$custom_footer = '';
get_footer($custom_footer);

/**
* QNR CODING – ENDS HERE
*/

 

/**
* VALIDATION – STARTS HERE
* startValidate($qn): $qn to prepend question to validation message at top of QNR
* $_SESSION[‘current_page’] only call validate functions for mentioned current page
*/
function startValidate($qn) {

$_SESSION['errSummary'] = $_SESSION['errSummary_js'] = ''; // initial

if ($_SESSION['current_page'] == 1) {
validate_required('q1_gender', $qn['radio']);
validate_required('q2_age', $qn['select']);
}

if ($_SESSION['current_page'] == 2) {
validate_required('q3_name', $qn['text']);
}

// Alert and halt at current page if any validations fail
if (!empty($_SESSION['errSummary'])) return FALSE;
else return TRUE;
}

/**
* VALIDATION – ENDS HERE
*/
?>