help making a skip page sistem in HTML_QUICKFORM_CONTROLLER
| From: | Valentin Ceaprazaru | Date: | Mon, 23 Apr 2007 19:49:16 +0000 |
| Subject: | help making a skip page sistem in HTML_QUICKFORM_CONTROLLER | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-27124@lists.php.net to get a copy of this message | ||
I need to make a sistem so that i can skip portions of a survey
I have attached the script. I get the questions dynamicaly from a DB, i'
attached this also.
please help.
<?php
ini_set("include_path", "pear");
require_once 'HTML/QuickForm/Controller.php';
session_start();
require_once 'HTML/QuickForm/Action/Next.php';
require_once 'HTML/QuickForm/Action/Back.php';
require_once 'HTML/QuickForm/Action/Jump.php';
require_once 'HTML/QuickForm/Action/Display.php';
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/Renderer/ITDynamic.php';
require_once 'HTML/Template/ITX.php';
require_once 'HTML/QuickForm/Rule.php';
class RuleNumericRange extends HTML_QuickForm_Rule
{
function validate($value, $options)
{
if (isset($options['min']) && floatval($value) < $options['min']) {
return false;
}
if (isset($options['max']) && floatval($value) > $options['max']) {
return false;
}
return true;
}
function getValidationScript($options = null)
{
$jsCheck = array();
if (isset($options['min'])) {
$jsCheck[] = 'Number({jsVar}) >= ' . $options['min'];
}
if (isset($options['max'])) {
$jsCheck[] = 'Number({jsVar}) <= ' . $options['max'];
}
return array('', "{jsVar} != '' && !(" . implode(' && ', $jsCheck) .
')');
} // end func getValidationScript
}
function verify100percent ($values, $options)
{
$sum = 0;
foreach ($_REQUEST[$options] as $k => $v)
{
if (is_numeric($v))
$sum += $v;
else
return false;
}
if ($sum == 100)
return true;else
return false;} function getQuestions() { require_once 'DB.php'; //$dsn = 'mysql://valicp:aleluia@mysql.softhint.com/worship_ro'; $dsn = 'mysql://root:@localhost/survey_sistem'; $db =& DB::connect($dsn, $options); if (PEAR::isError($db)) {
die($db->getMessage());} $db->setFetchMode(DB_FETCHMODE_ASSOC); $res =& $db->query('SELECT * FROM '. $_SESSION['questionaire']); if (PEAR::isError($res)) {
die($res->getMessage());} while ($res->fetchInto($row)) {
$question[$row['question_id']]['question_id'] =$row['question_id'];
$question[$row['question_id']]['question_text'] =$row['question_text'];
$question[$row['question_id']]['question_type'] =$row['question_type'];
$question[$row['question_id']]['question_options'] =explode('||', $row['question_options']);
$question[$row['question_id']]['question_validation'] =$row['question_validation'];
$temp = explode('||', $row['question_extra']);
if (count($temp) > 1)
foreach ($temp as $k => $v)
{
$key = substr($v, 0, strpos($v, '+'));
$values = explode(';;', substr($v, (strpos($v, '+') + 2)));
$array[$key] = $values;
$question[$row['question_id']]['question_extra'][$key] =
$values;
}
unset($question[$row['question_id']]['question_extra']['0']);
}
$res->free();
$db->disconnect();
return $question;
}
function insertAnswers($values)
{
global $question;
require_once 'DB.php';
$dsn = 'mysql://valicp:aleluia@mysql.softhint.com/worship_ro';
$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());} $db->setFetchMode(DB_FETCHMODE_ASSOC); $answers = array(); foreach ($values as $key => $value) {
$insert = array();
$id = substr($key, 8);
if(is_array($value))
if ($question[$id]['question_type'] == 'checkbox')
{
$keys = array();
foreach ($value as $k => $v)
{
$keys[] = $k;
}
$answer = implode('||', $keys);
}
else
$answer = implode('||', $value);
else
$answer = $value;
$insert[] = '';
$insert[] = $_SESSION['questionaire'];
$insert[] = $question[$id]['question_text'];
$insert[] = $answer;
$insert[] = $_SESSION['username'] .''; // $_SESSION['clientid']
$insert[] = '';
$answers[] = $insert;
}
$sth = $db->prepare('INSERT INTO answers ( id , questionaire ,
question , answer , date , username , extra ) VALUES (?, ?, ?, ?,
NOW(), ?, ?)');
$res =& $db->executeMultiple($sth, $answers);
if (PEAR::isError($res)) {
die($res->getMessage());} else
return true;} class FirstPage extends HTML_QuickForm_Page { function buildForm() {
global $pageNo;
global $question;
global $numberOfPages;
global $questionsPerPage;
global $form_name;
$this->_formBuilt = true;
$start = ($pageNo-1) * $questionsPerPage ;
$groups = array();
$_SESSION['groups'] = array();
for ($x = $start; $x < $start + $questionsPerPage; $x++)
{
$value = $question[$x + 1];
$q_nn = $value['question_extra']['noshow'][0];
if ($_SESSION['_'. $form_name..'_container']['values']['page'.($q_nn/$questionsPerPage)]['question'. $q_nn] != $value['question_extra']['noshow_answer']['0'])
{
$value = $question[$x + 2];
}
$value['question_label'] = ($x).'. '.$value['question_text'];
switch ($value['question_type'])
{
case 'checkbox':
$this->addElement('header', null,
$value['question_label']);
$checkbox = array();
$counter = 0;
foreach ($value['question_options'] as $k => $v)
{
$counter++;
$checkbox[] = &$this->createElement('checkbox', $v,
null, $v);
}
if ($counter > 3)
$groups['question'. $value['question_id']] =
'qf_fancygroup';
$this->addGroup($checkbox, 'question'.$value['question_id']);
break;
case 'radio':
$this->addElement('header', null,
$value['question_label']);
$radio = array();
$counter = 0;
foreach ($value['question_options'] as $k => $v)
{
$counter++;
$radio[] = &$this->createElement('radio', null,
null, $v, $v);
}
if ($counter > 3)
$groups['question'. $value['question_id']] =
'qf_fancygroup';
$this->addGroup($radio, 'question'.$value['question_id']);
break;
case 'open_answer':
$this->addElement('header', null,
$value['question_label']);
$this->addElement('text', 'question'.
$value['question_id'], '', array('id' => $v));
break;
case 'multiple_open_answer':
$this->addElement('header', null,
$value['question_label']);
$multiple_open = array();
if (substr($value['question_options'][0], 0, 10) ==
'question__')
{
$q_n = substr($value['question_options'][0],10);
$value['question_options'] = @
array_keys($_SESSION['_'. $form_name .'_container']['values']['page'.
($q_n/$questionsPerPage)]['question'. $q_n]);
}
foreach ($value['question_options'] as $k => $v)
{
$multiple_open[] =
&HTML_QuickForm::createElement('text', $k, $v);
}
$this->addGroup($multiple_open, 'question'.$value['question_id']);
$groups['question'. $value['question_id']] ='qf_fancygroup';
break;
case 'multiple_open_answer_horizontal':
$this->addElement('header', null,
$value['question_label']);
$multiple_open = array();
if (substr($value['question_options'][0], 0, 10) ==
'question__')
{
$q_n = substr($value['question_options'][0],10);
$value['question_options'] = @
array_keys($_SESSION['_'. $form_name
..'_container']['values']['page'.($q_n/$questionsPerPage)]['question'.
$q_n]);
}
foreach ($value['question_options'] as $k => $v)
{
$multiple_open = array();
foreach ($value['question_extra']['horizontal'] as
$kk => $vv)
{
$multiple_open[] =
&HTML_QuickForm::createElement('text', $vv, $vv);
}
$this->addGroup($multiple_open, 'question'.
$value['question_id'] .'_'. $k, $v);
$groups['question'. $value['question_id'] .'_'. $k]= 'qf_fancygrouphor';
}
break;
case 'simple_text':
$this->addElement('static', 'question'.
$value['question_id'], '', $value['question_text']);
$groups['question'. $value['question_id']] ='qf_element_simple';
break;
default :
echo 'Error';
break;
}
switch ($value['question_validation'])
{
case 'required':
if ($value['question_type'] == 'checkbox' ||
$value['question_type'] == 'radio')
$this->addRule('question'. $value['question_id'],
'Check at least one option', 'required');
elseif ($value['question_type'] == 'text')
$this->addRule('question'. $value['question_id'],
'Fill this field', 'required');
elseif ($value['question_type'] =='multiple_open_answer')
$this->addGroupRule('question'.
$value['question_id'], 'Fill this fields', 'required');
break;
case 'min_max':
$this->registerRule('numRange', null,
'RuleNumericRange');
$this->addRule('question'. $value['question_id'],
'Enter number from '.
$value['question_extra']['min_max'][0] .' to '.
$value['question_extra']['min_max'][1],
'numRange',
array('min' =>
$value['question_extra']['min_max'][0], 'max' =>
$value['question_extra']['min_max'][1]));
break;
case '100percent':
if ($value['question_type'] ==
'multiple_open_answer_horizontal')
{
foreach ($value['question_options'] as $k => $v)
{
$this->registerRule('percent100', 'callback',
'verify100percent');
$this->addGroupRule('question'.
$value['question_id'] .'_'. $k, 'The sum must be 100', 'percent100',
'question'. $value['question_id'] .'_'. $k);
}
}else {
$this->registerRule('percent100', 'callback',
'verify100percent');
$this->addGroupRule('question'.
$value['question_id'], 'The sum must be 100', 'percent100', 'question'.
$value['question_id'] );
}
break;
}
}
$_SESSION['groups'] = $groups;
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->setDefaultAction('next');
}
}
class ActionProcess extends HTML_QuickForm_Action
{
function perform(&$page, $actionName)
{
if (insertAnswers($page->controller->exportValues()) == true)
echo '<div style="margin:50px; text-align:center;
color:green;">Submit successful!<br /><br /><a href="javascript:window.close
()">Close</a></div>';
}
}
class ActionDisplay extends HTML_QuickForm_Action_Display
{
function _renderForm(&$page)
{
$tpl =& new HTML_Template_ITX('./templates');
$tpl->loadTemplateFile('it-dynamic.html', true, true);
$renderer =& new HTML_QuickForm_Renderer_ITDynamic($tpl);
$renderer->setElementBlock($_SESSION['groups']);
$page->accept($renderer);
$tpl->show();
}
}
class PageActionNext extends HTML_QuickForm_Action_Next
{
var $param;
function PageActionNext($temp)
{
$this->param = $temp;} function perform(&$page, $actionName) {
// save the form values and validation status to the session
$page->isFormBuilt() or $page->buildForm();
$pageName = $page->getAttribute('id');
$data =& $page->controller->container();
$data['values'][$pageName] = $page->exportValues();
if (PEAR::isError($valid = $page->validate())) {
return $valid;
}
$data['valid'][$pageName] = $valid;
global $pageNo;
global $question;
// Modal form and page is invalid: don't go further
if ($page->controller->isModal() && !$data['valid'][$pageName]) {
return $page->handle('display');
}
$nextName = $data['values'][$pageName]['question'.$pageNo];
if (empty($nextName)) {
$nextName = 'question'. $pageNo;
}
if ($nextName ==$question[$pageNo]['question_extra']['goto_answer']) {
$data['valid']['page'.$question[$pageNo]['question_extra']['goto']] = true;
} else {
$data['valid']['page'. ($pageNo + 1)] = true;
}
$next =& $page->controller->getPage($nextName);
$next->handle('jump');
}
}
$questionsPerPage = 1;
//$_SESSION['questionaire'] = 'questionaire_number_one';
$_SESSION['questionaire'] = 'questions_set_one';
$form_name = $_SESSION['questionaire'];
$question = getQuestions();
$wizard = &new HTML_QuickForm_Controller($form_name); $numberOfPages = ceil( count($question) / $questionsPerPage);$pageNo = 0; for ($xx = 1; $xx <= $numberOfPages; $xx++) { $page =& new FirstPage('page'. $xx); $wizard->addPage($page); if (isset($question[$xx]['question_extra']['goto_answer']))
$page->addAction('next', new PageActionNext('dede'));
if (!@$_SESSION['_'. $form_name .'_container']['valid']['page'.$xx]) {
if (!$pageNo) {
$pageNo = $xx;
}
}
}
$wizard->addAction('display', new ActionDisplay());
$wizard->addAction('next', new HTML_QuickForm_Action_Next());
$wizard->addAction('back', new HTML_QuickForm_Action_Back());
$wizard->addAction('jump', new HTML_QuickForm_Action_Jump());
$wizard->addAction('process', new ActionProcess());
$wizard->run();
?>
DB :
CREATE TABLE questions_set_one (
question_id int(11) NOT NULL auto_increment,
question_text text NOT NULL,
question_type enum('simple_text','open_answer','radio','checkbox','
multiple_open_answer','multiple_open_answer_horizontal') NOT NULL default
'radio',
question_options text NOT NULL,
question_validation enum('required','min_max','100percent') NOT NULL
default 'required',
question_extra text NOT NULL,
PRIMARY KEY (question_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;