Skip to content

Commit

Permalink
Dev WIP Question-by-question mode partially working!
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Jul 24, 2015
1 parent ba0d9d1 commit 2af9e60
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 947 deletions.
68 changes: 61 additions & 7 deletions application/components/SurveySession.php
Expand Up @@ -46,6 +46,12 @@ class SurveySession extends CComponent {
protected $_templateDir;
protected $_postKey;
protected $_finished = false;
/**
* This is used to decide if we should display errors.
* It gets reset when changing the step.
* @var int The number of times this step has been viewed without viewing other steps.
*/
protected $_viewCount = 1;
protected $_token;
/**
* @param int $surveyId
Expand Down Expand Up @@ -177,7 +183,21 @@ public function getQuestionIndex($id) {
* @return Question
*/
public function getQuestionByIndex($index) {
return array_values($this->survey->questions)[$index];
$i = 0;
// Get groups in order.
foreach($this->getGroups() as $group) {
foreach($this->getQuestions($group) as $question) {
if ($index == $i) {
$result = $question;
break 2;
}
$i++;
}
}
if (!isset($result)) {
throw new \Exception("Invalid step index: $index");
}
return $result;
}

public function getStepCount() {
Expand Down Expand Up @@ -207,18 +227,32 @@ public function getStep() {

public function setStep($value) {
if (!is_int($value)) {
throw new \BadMethodCallException('Parameter $value must be an integer.');
throw new \InvalidArgumentException('Parameter $value must be an integer.');
}
if ($value > $this->stepCount) {
throw new \InvalidArgumentException("Cannot set step to a value greater than stepCount");
}

if ($value != $this->_step) {
$this->_viewCount = 1;
$this->_prevStep = $this->_step;
$this->_step = $value;
$this->_maxStep = max($this->_step, $this->_maxStep);
}
$this->_step = $value > 0 ? $value : 0;
$this->_prevStep = $this->_step;
$this->_maxStep = max($this->_step, $this->_maxStep);
}


public function getMaxStep() {
return $this->_maxStep;
}

/**
* @return int The number of times the current page has been viewed.
*/
public function getViewCount() {
return $this->_viewCount;
}

public function getPrevStep() {
return $this->_prevStep;
}
Expand All @@ -227,6 +261,10 @@ public function setPrevStep($value) {
$this->_prevStep = $value;
}

public function __wakeup() {
$this->_viewCount++;
}

public function __sleep() {
return [
'surveyId',
Expand All @@ -238,7 +276,8 @@ public function __sleep() {
'_finished',
'_language',
'_postKey',
'_token'
'_token',
'_viewCount'
];
}

Expand Down Expand Up @@ -346,7 +385,7 @@ public function getFormat() {


/**
* Returns the questions in group $group, indexed by primary key.
* Returns the questions in group $group, indexed by primary key, ordered as they are shown in the survey.
* @param QuestionGroup $group
* @return Question[]
*/
Expand Down Expand Up @@ -389,6 +428,7 @@ public function getQuestions(QuestionGroup $group) {

/**
* This function will be deprecated, for now it is provided as a replacement of direct session access.
* @deprecated
*/
public function getFieldArray() {
$result = [];
Expand Down Expand Up @@ -472,10 +512,24 @@ public function getPostKey() {
}
return $this->_postKey;
}

public function setPostKey($value) {
$this->_postKey = $value;
}

public function getCurrentGroup() {
if ($this->format == Survey::FORMAT_ALL_IN_ONE) {
throw new \UnexpectedValueException("An all in one survey does not have a current group.");
}

if ($this->format == Survey::FORMAT_GROUP) {
$result = $this->getGroupByIndex($this->step);
} else {
$result = $this->getGroup($this->getQuestionByIndex($this->step)->gid);
}
return $result;
}



}
89 changes: 26 additions & 63 deletions application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -229,13 +229,7 @@ function run($surveyid,$args) {
buildsurveysession($session->surveyId);
$sTemplatePath = $session->templateDir;
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions, false, $LEMdebugLevel);
if ($session->format == Survey::FORMAT_ALL_IN_ONE) {
$moveResult = LimeExpressionManager::JumpTo(1, false, false, true);
} elseif (!$session->survey->bool_showwelcome) {

$moveResult = LimeExpressionManager::JumpTo(1, false, false, true);
$session->step = 1;
}
$moveResult = LimeExpressionManager::JumpTo(0, false, false, true);
}


Expand All @@ -262,25 +256,17 @@ function run($surveyid,$args) {

if(isset($move) && $move=="clearcancel")
{
$moveResult = LimeExpressionManager::JumpTo($session->getStep(), false, true, false, true);
//$backpopup=gT("Clear all need confirmation.");
}
if (isset($move))
{
if(!in_array($move,array("changelang","saveall","reload")))
$session->prevStep = $session->getStep();
else // Accepted $move without error
$session->prevStep = $move;
$moveResult = LimeExpressionManager::JumpTo($session->step, false, true, false, true);
}

if (isset($_SESSION[$LEMsessid]['LEMtokenResume']))
{
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions, false,$LEMdebugLevel);
if($ssm->current->getMaxStep() > $ssm->current->getStep())
if($session->maxStep > $session->step)
{
LimeExpressionManager::JumpTo($ssm->current->getMaxStep(), false, false);
LimeExpressionManager::JumpTo($session->maxStep, false, false);
}
$moveResult = LimeExpressionManager::JumpTo($ssm->current->getStep(),false,false); // if late in the survey, will re-validate contents, which may be overkill
$moveResult = LimeExpressionManager::JumpTo($session->step,false,false); // if late in the survey, will re-validate contents, which may be overkill
unset($_SESSION[$LEMsessid]['LEMtokenResume']);
}
else if (!$LEMskipReprocessing)
Expand Down Expand Up @@ -340,21 +326,18 @@ function run($surveyid,$args) {
if (isset($moveResult) && isset($moveResult['seq']) )// Reload at first page (welcome after click previous fill an empty $moveResult array
{
// With complete index, we need to revalidate whole group bug #08806. It's actually the only mode where we JumpTo with force
if($moveResult['finished'] == true && $session->survey->questionindex == Survey::INDEX_FULL)
{
//LimeExpressionManager::JumpTo(-1, false, false, true);
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions);
$moveResult = LimeExpressionManager::JumpTo($session->getStepCount() + 1, false, false, false);// no preview, no save data and NO force
if(!$moveResult['mandViolation'] && $moveResult['valid'] && empty($moveResult['invalidSQs']))
$moveResult['finished'] = true;
}
if ($moveResult['finished'] == true)
if($moveResult['finished'])
{
if ($session->survey->questionindex == Survey::INDEX_FULL) {
//LimeExpressionManager::JumpTo(-1, false, false, true);
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions);
$moveResult = LimeExpressionManager::JumpTo($session->getStepCount() + 1, false, false, false);// no preview, no save data and NO force
if(!$moveResult['mandViolation'] && $moveResult['valid'] && empty($moveResult['invalidSQs']))
$moveResult['finished'] = true;
}
$move = 'movesubmit';
}
else
{
$session->setStep($moveResult['seq'] + 1); // step is index base 1
} else {
$session->setStep($moveResult['seq']);
$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['seq']);
}
if ($move == "movesubmit" && $moveResult['finished'] == false)
Expand All @@ -364,10 +347,6 @@ function run($surveyid,$args) {
$invalidLastPage = true;
}
}
if ($session->format != Survey::FORMAT_ALL_IN_ONE && $session->getStep() == 0)
{
$moveResult = LimeExpressionManager::JumpTo(1, false, false, true);
}

// TODO FIXME
if ($session->survey->bool_active) {
Expand Down Expand Up @@ -411,9 +390,6 @@ function run($surveyid,$args) {

// TODO - does this work automatically for token answer persistence? Used to be savedsilent()
}
//Now, we check mandatory questions if necessary
//CHECK IF ALL CONDITIONAL MANDATORY QUESTIONS THAT APPLY HAVE BEEN ANSWERED
global $notanswered;

if (isset($moveResult) && !$moveResult['finished'])
{
Expand Down Expand Up @@ -645,21 +621,15 @@ function run($surveyid,$args) {
$gid = $stepInfo['gid'];
$groupname = $stepInfo['gname'];
$groupdescription = $stepInfo['gtext'];
}
else
{
if ($session->format != Survey::FORMAT_ALL_IN_ONE)
{
if ($previewquestion) {
$_qid = sanitize_int($param['qid']);
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions, false, $LEMdebugLevel);
$qSec = LimeExpressionManager::GetQuestionSeq($_qid);
$moveResult = LimeExpressionManager::JumpTo($qSec+1,true,false,true);
$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['qseq']);
} else {
$stepInfo = LimeExpressionManager::GetStepIndexInfo($session->step + 1);
}
} elseif ($session->format == Survey::FORMAT_QUESTION) {
if ($previewquestion) {
$_qid = sanitize_int($param['qid']);
LimeExpressionManager::StartSurvey($session->surveyId, $session->format, $surveyOptions, false, $LEMdebugLevel);
$qSec = LimeExpressionManager::GetQuestionSeq($_qid);
$moveResult = LimeExpressionManager::JumpTo($qSec+1,true,false,true);
}

$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['seq']);
}


Expand All @@ -680,7 +650,7 @@ function run($surveyid,$args) {
//PRESENT SURVEY
//******************************************************************************************************

$okToShowErrors = (!$previewgrp && (isset($invalidLastPage) || $session->prevStep == $session->step));
$okToShowErrors = (!$previewgrp && (isset($invalidLastPage) || !$moveResult['valid'] || $moveResult['mandViolation']));
App()->loadHelper('qanda');
setNoAnswerMode($thissurvey);

Expand Down Expand Up @@ -765,16 +735,9 @@ function run($surveyid,$args) {
}


if ($session->format != Survey::FORMAT_ALL_IN_ONE && isset($thissurvey['showprogress']) && $thissurvey['showprogress'] == 'Y')
if ($session->format != Survey::FORMAT_ALL_IN_ONE && $session->survey->bool_showprogress)
{
if ($show_empty_group)
{
$percentcomplete = makegraph($session->getStepCount() + 1, $session->getStepCount());
}
else
{
$percentcomplete = makegraph($session->step, $session->getStepCount());
}
$percentcomplete = makegraph($session->step, $session->stepCount);
}
if (!(isset($languagechanger) && strlen($languagechanger) > 0) && function_exists('makeLanguageChangerSurvey'))
{
Expand Down

0 comments on commit 2af9e60

Please sign in to comment.