Skip to content

Commit

Permalink
Fixed issue #7315: Validation regex not being checked / not working (…
Browse files Browse the repository at this point in the history
…logic test ok) in preview mode

Fixed issue: Can not test group if token exist or if survey is not started
New feature: can use SGQA identifier to prefill some answer with URL in preview mode
Dev: add LimeExpressionManager->$sPreviewMode to be used and test if we are in preview mode
Dev: add LimeExpressionManager::SetPreviewMode public function to set previewmode
  • Loading branch information
Shnoulle committed Mar 12, 2013
1 parent c84093b commit c31be8f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 91 deletions.
33 changes: 21 additions & 12 deletions application/controllers/survey/index.php
Expand Up @@ -77,14 +77,22 @@ function action()
$this->_createNewUserSessionAndRedirect($surveyid, $redata, __LINE__, $asMessage);
}


if (isset($param['action']) && (in_array($param['action'],array('previewgroup','previewquestion'))) && !$this->_canUserPreviewSurvey($surveyid))
$previewmode=false;
if (isset($param['action']) && (in_array($param['action'],array('previewgroup','previewquestion'))))
{
$asMessage = array(
$clang->gT('Error'),
$clang->gT("We are sorry but you don't have permissions to do this.")
);
$this->_niceExit($redata, __LINE__, null, $asMessage);
if(!$this->_canUserPreviewSurvey($surveyid))
{
$asMessage = array(
$clang->gT('Error'),
$clang->gT("We are sorry but you don't have permissions to do this.")
);
$this->_niceExit($redata, __LINE__, null, $asMessage);
}
else
{
if((intval($param['qid']) && $param['action']=='previewquestion')) $previewmode='question';
if((intval($param['gid']) && $param['action']=='previewgroup')) $previewmode='group';
}
}

if ( $this->_surveyCantBeViewedWithCurrentPreviewAccess($surveyid, $isSurveyActive, $surveyExists) )
Expand Down Expand Up @@ -149,6 +157,7 @@ function action()
{
LimeExpressionManager::SetSurveyId($surveyid); // must be called early - it clears internal cache if a new survey is being used
$clang = SetSurveyLanguage( $surveyid, $sTempLanguage);
if($previewmode) LimeExpressionManager::SetPreviewMode($previewmode);
UpdateGroupList($surveyid, $clang->langcode); // to refresh the language strings in the group list session variable
UpdateFieldArray(); // to refresh question titles and question text

Expand Down Expand Up @@ -341,7 +350,7 @@ function sendreq(surveyid)

$timeadjust = Yii::app()->getConfig("timeadjust");
//MAKE SURE SURVEY HASN'T EXPIRED
if ($thissurvey['expiry']!='' and dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust)>$thissurvey['expiry'] && $thissurvey['active']!='N')
if ($thissurvey['expiry']!='' and dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust)>$thissurvey['expiry'] && $thissurvey['active']!='N' && !$previewmode)
{
$redata = compact(array_keys(get_defined_vars()));
$asMessage = array(
Expand All @@ -354,7 +363,7 @@ function sendreq(surveyid)
}

//MAKE SURE SURVEY IS ALREADY VALID
if ($thissurvey['startdate']!='' and dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust)<$thissurvey['startdate'] && $thissurvey['active']!='N')
if ($thissurvey['startdate']!='' and dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust)<$thissurvey['startdate'] && $thissurvey['active']!='N' && !$previewmode)
{
$redata = compact(array_keys(get_defined_vars()));
$asMessage = array(
Expand Down Expand Up @@ -463,7 +472,7 @@ function sendreq(surveyid)

$aRow = Yii::app()->db->createCommand($sQuery)->queryRow();
$tokendata = $aRow;
if (!$aRow || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y'))
if (!$aRow || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y') && !$previewmode)
{
sendCacheHeaders();
doHeader();
Expand All @@ -481,7 +490,7 @@ function sendreq(surveyid)
$this->_niceExit($redata, __LINE__, $thistpl, $asMessage, true);
}
}
if ($tokensexist == 1 && isset($token) && $token && tableExists("{{tokens_".$surveyid."}}")) //check if token is in a valid time frame
if ($tokensexist == 1 && isset($token) && $token && tableExists("{{tokens_".$surveyid."}}") && !$previewmode) //check if token is in a valid time frame
{
// check also if it is allowed to change survey after completion
if ($thissurvey['alloweditaftercompletion'] == 'Y' ) {
Expand Down Expand Up @@ -625,7 +634,7 @@ function sendreq(surveyid)
}
}
// Preview action : Preview right already tested before
if (isset($param['action']) && (in_array($param['action'],array('previewgroup','previewquestion'))))
if ($previewmode)
{
// Unset all SESSION: be sure to have the last version
unset($_SESSION['fieldmap-' . $surveyid . $clang->langcode]);// Needed by createFieldMap: else fieldmap can be outdated
Expand Down
45 changes: 1 addition & 44 deletions application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -889,29 +889,6 @@ function checkconditions(value, name, type, evt_type)
}
ExprMgr_process_relevance_and_tailoring(evt_type,name,type);
END;

if ($previewgrp)
{
// force the group to be visible, even if irrelevant - will not always work
print <<<END
$('#relevanceG' + LEMgseq).val(1);
$(document).ready(function() {
$('#group-' + LEMgseq).show();
});
$(document).change(function() {
$('#group-' + LEMgseq).show();
});
$(document).bind('keydown',function(e) {
if (e.keyCode == 9) {
$('#group-' + LEMgseq).show();
return true;
}
return true;
});
END;
}

print <<<END
}
// -->
Expand Down Expand Up @@ -1001,7 +978,7 @@ function checkconditions(value, name, type, evt_type)
continue; // skip this one
}

if ((!$qinfo['relevant'] && !$previewquestion) || ($qinfo['hidden'] && $qinfo['info']['type'] == '*'))
if ((!$qinfo['relevant']) || ($qinfo['hidden'] && $qinfo['info']['type'] == '*'))
{
$n_q_display = ' style="display: none;"';
}
Expand Down Expand Up @@ -1058,26 +1035,6 @@ function checkconditions(value, name, type, evt_type)

LimeExpressionManager::FinishProcessingGroup($LEMskipReprocessing);
echo LimeExpressionManager::GetRelevanceAndTailoringJavaScript();
if ($previewquestion){
// force the question to be visible, even if irrelevant
echo "
<script type='text/javascript'>
$('#relevance" . $_qid . "').val(1);
$(document).ready(function() {
$('#question" . $_qid . "').show();
});
$(document).change(function() {
$('#question" . $_qid . "').show();
});
$(document).bind('keydown',function(e) {
if (e.keyCode == 9) {
$('#question" . $_qid . "').show();
return true;
}
return true;
});
</script>";
}
LimeExpressionManager::FinishProcessingPage();

if (!$previewgrp && !$previewquestion)
Expand Down
83 changes: 49 additions & 34 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -56,6 +56,11 @@ class LimeExpressionManager {
* @var type
*/
private $debugLevel=0;
/**
* sPreviewMode used for relevance equation force to 1 in preview mode
* @var string
*/
private $sPreviewMode=false;
/**
* Collection of variable attributes, indexed by SGQA code
*
Expand Down Expand Up @@ -686,6 +691,16 @@ public function __clone()
trigger_error('Clone is not allowed.', E_USER_ERROR);
}

/**
* Set the previewmode
*/
public static function SetPreviewMode($previewmode=false)
{
$LEM =& LimeExpressionManager::singleton();
$LEM->sPreviewMode=$previewmode;
//$_SESSION[$LEM->sessid]['previewmode']=$previewmode;
}

/**
* Tells Expression Manager that something has changed enough that needs to eliminate internal caching
*/
Expand Down Expand Up @@ -2734,7 +2749,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
unset($_SESSION['LEMforceRefresh']);
$forceRefresh=true;
}
else if (!$forceRefresh && isset($this->knownVars)) {
else if (!$forceRefresh && isset($this->knownVars) && !$this->sPreviewMode ) {
return false; // means that those variables have been cached and no changes needed
}
$now = microtime(true);
Expand All @@ -2752,7 +2767,6 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
if (!isset($fieldmap)) {
return false; // implies an error occurred
}

$this->knownVars = array(); // mapping of VarName to Value
$this->qcode2sgqa = array();
$this->tempVars = array();
Expand Down Expand Up @@ -2825,6 +2839,8 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
$groupNum = $fieldNameParts[1];
$aid = (isset($fielddata['aid']) ? $fielddata['aid'] : '');
$sqid = (isset($fielddata['sqid']) ? $fielddata['sqid'] : '');
if($this->sPreviewMode=='question') $fielddata['relevance']=1;
if($this->sPreviewMode=='group') $fielddata['grelevance']=1;

$questionId = $fieldNameParts[2];
$questionNum = $fielddata['qid'];
Expand Down Expand Up @@ -3931,41 +3947,42 @@ static function StartProcessingPage($allOnOnePage=false,$initializeVars=false)

/**
* Initialize a survey so can use EM to manage navigation
* @param <type> $surveyid
* @param <type> $surveyMode
* @param <type> $anonymized
* @param <type> $forceRefresh
* @param int $surveyid
* @param string $surveyMode
* @param array $aSurveyOptions
* @param bool $forceRefresh
* @param int $debugLevel
*/
static function StartSurvey($surveyid,$surveyMode='group',$options=NULL,$forceRefresh=false,$debugLevel=0)
static function StartSurvey($surveyid,$surveyMode='group',$aSurveyOptions=NULL,$forceRefresh=false,$debugLevel=0)
{
$LEM =& LimeExpressionManager::singleton();
$LEM->sid=sanitize_int($surveyid);
$LEM->sessid = 'survey_' . $LEM->sid;

$LEM->em->StartProcessingGroup($surveyid);
if (is_null($options)) {
$options = array();
}
$LEM->surveyOptions['active'] = (isset($options['active']) ? $options['active'] : false);
$LEM->surveyOptions['allowsave'] = (isset($options['allowsave']) ? $options['allowsave'] : false);
$LEM->surveyOptions['anonymized'] = (isset($options['anonymized']) ? $options['anonymized'] : false);
$LEM->surveyOptions['assessments'] = (isset($options['assessments']) ? $options['assessments'] : false);
$LEM->surveyOptions['datestamp'] = (isset($options['datestamp']) ? $options['datestamp'] : false);
$LEM->surveyOptions['deletenonvalues'] = (isset($options['deletenonvalues']) ? ($options['deletenonvalues']=='1') : true);
$LEM->surveyOptions['hyperlinkSyntaxHighlighting'] = (isset($options['hyperlinkSyntaxHighlighting']) ? $options['hyperlinkSyntaxHighlighting'] : false);
$LEM->surveyOptions['ipaddr'] = (isset($options['ipaddr']) ? $options['ipaddr'] : false);
$LEM->surveyOptions['radix'] = (isset($options['radix']) ? $options['radix'] : '.');
$LEM->surveyOptions['refurl'] = (isset($options['refurl']) ? $options['refurl'] : NULL);
$LEM->surveyOptions['savetimings'] = (isset($options['savetimings']) ? $options['savetimings'] : '');
$LEM->sgqaNaming = (isset($options['sgqaNaming']) ? ($options['sgqaNaming']=="Y") : true); // TODO default should eventually be false
$LEM->surveyOptions['startlanguage'] = (isset($options['startlanguage']) ? $options['startlanguage'] : 'en');
$LEM->surveyOptions['surveyls_dateformat'] = (isset($options['surveyls_dateformat']) ? $options['surveyls_dateformat'] : 1);
$LEM->surveyOptions['tablename'] = (isset($options['tablename']) ? $options['tablename'] : '{{survey_' . $LEM->sid . '}}');
$LEM->surveyOptions['tablename_timings'] = ((isset($options['savetimings']) && $options['savetimings'] == 'Y') ? '{{survey_' . $LEM->sid . '_timings}}' : '');
$LEM->surveyOptions['target'] = (isset($options['target']) ? $options['target'] : '/temp/files/');
$LEM->surveyOptions['timeadjust'] = (isset($options['timeadjust']) ? $options['timeadjust'] : 0);
$LEM->surveyOptions['tempdir'] = (isset($options['tempdir']) ? $options['tempdir'] : '/temp/');
$LEM->surveyOptions['token'] = (isset($options['token']) ? $options['token'] : NULL);
if (is_null($aSurveyOptions)) {
$aSurveyOptions = array();
}
$LEM->surveyOptions['active'] = (isset($aSurveyOptions['active']) ? $aSurveyOptions['active'] : false);
$LEM->surveyOptions['allowsave'] = (isset($aSurveyOptions['allowsave']) ? $aSurveyOptions['allowsave'] : false);
$LEM->surveyOptions['anonymized'] = (isset($aSurveyOptions['anonymized']) ? $aSurveyOptions['anonymized'] : false);
$LEM->surveyOptions['assessments'] = (isset($aSurveyOptions['assessments']) ? $aSurveyOptions['assessments'] : false);
$LEM->surveyOptions['datestamp'] = (isset($aSurveyOptions['datestamp']) ? $aSurveyOptions['datestamp'] : false);
$LEM->surveyOptions['deletenonvalues'] = (isset($aSurveyOptions['deletenonvalues']) ? ($aSurveyOptions['deletenonvalues']=='1') : true);
$LEM->surveyOptions['hyperlinkSyntaxHighlighting'] = (isset($aSurveyOptions['hyperlinkSyntaxHighlighting']) ? $aSurveyOptions['hyperlinkSyntaxHighlighting'] : false);
$LEM->surveyOptions['ipaddr'] = (isset($aSurveyOptions['ipaddr']) ? $aSurveyOptions['ipaddr'] : false);
$LEM->surveyOptions['radix'] = (isset($aSurveyOptions['radix']) ? $aSurveyOptions['radix'] : '.');
$LEM->surveyOptions['refurl'] = (isset($aSurveyOptions['refurl']) ? $aSurveyOptions['refurl'] : NULL);
$LEM->surveyOptions['savetimings'] = (isset($aSurveyOptions['savetimings']) ? $aSurveyOptions['savetimings'] : '');
$LEM->sgqaNaming = (isset($aSurveyOptions['sgqaNaming']) ? ($aSurveyOptions['sgqaNaming']=="Y") : true); // TODO default should eventually be false
$LEM->surveyOptions['startlanguage'] = (isset($aSurveyOptions['startlanguage']) ? $aSurveyOptions['startlanguage'] : 'en');
$LEM->surveyOptions['surveyls_dateformat'] = (isset($aSurveyOptions['surveyls_dateformat']) ? $aSurveyOptions['surveyls_dateformat'] : 1);
$LEM->surveyOptions['tablename'] = (isset($aSurveyOptions['tablename']) ? $aSurveyOptions['tablename'] : '{{survey_' . $LEM->sid . '}}');
$LEM->surveyOptions['tablename_timings'] = ((isset($aSurveyOptions['savetimings']) && $aSurveyOptions['savetimings'] == 'Y') ? '{{survey_' . $LEM->sid . '_timings}}' : '');
$LEM->surveyOptions['target'] = (isset($aSurveyOptions['target']) ? $aSurveyOptions['target'] : '/temp/files/');
$LEM->surveyOptions['timeadjust'] = (isset($aSurveyOptions['timeadjust']) ? $aSurveyOptions['timeadjust'] : 0);
$LEM->surveyOptions['tempdir'] = (isset($aSurveyOptions['tempdir']) ? $aSurveyOptions['tempdir'] : '/temp/');
$LEM->surveyOptions['token'] = (isset($aSurveyOptions['token']) ? $aSurveyOptions['token'] : NULL);

$LEM->debugLevel=$debugLevel;
$_SESSION[$LEM->sessid]['LEMdebugLevel']=$debugLevel; // need acces to SESSSION to decide whether to cache serialized instance of $LEM
Expand Down Expand Up @@ -7324,7 +7341,6 @@ private function getQuestionAttributesForEM($surveyid=NULL,$qid=NULL, $lang=NULL
$lang = " and a.language='".$lang."' and b.language='".$lang."'";
}


$databasetype = Yii::app()->db->getDriverName();
if ($databasetype=='mssql' || $databasetype=="sqlsrv")
{
Expand Down Expand Up @@ -7420,7 +7436,6 @@ function getGroupInfoForEM($surveyid,$lang=NULL)
if (!is_null($lang)) {
$lang = " and a.language='".$lang."'";
}

$query = "SELECT a.group_name, a.description, a.gid, a.group_order, a.grelevance"
." FROM {{groups}} AS a"
." WHERE a.sid=".$surveyid
Expand All @@ -7432,13 +7447,13 @@ function getGroupInfoForEM($surveyid,$lang=NULL)
$qinfo = array();
$_order=0;
foreach ($data as $d)
{
{
$gid[$d['gid']] = array(
'group_order' => $_order,
'gid' => $d['gid'],
'group_name' => $d['group_name'],
'description' => $d['description'],
'grelevance' => $d['grelevance'],
'grelevance' => (!($this->sPreviewMode=='question' || $this->sPreviewMode=='group')) ? $d['grelevance']:1,
);
$qinfo[$_order] = $gid[$d['gid']];
++$_order;
Expand Down
3 changes: 2 additions & 1 deletion application/helpers/frontend_helper.php
Expand Up @@ -1976,7 +1976,7 @@ function buildsurveysession($surveyid,$preview=false)
// Prefill questions/answers from command line params
$reservedGetValues= array('token','sid','gid','qid','lang','newtest','action');
$startingValues=array();
if (isset($_GET) && !$preview)
if (isset($_GET))
{
foreach ($_GET as $k=>$v)
{
Expand All @@ -1987,6 +1987,7 @@ function buildsurveysession($surveyid,$preview=false)
}
}
$_SESSION['survey_'.$surveyid]['startingValues']=$startingValues;

if (isset($_SESSION['survey_'.$surveyid]['fieldarray'])) $_SESSION['survey_'.$surveyid]['fieldarray']=array_values($_SESSION['survey_'.$surveyid]['fieldarray']);

//Check if a passthru label and value have been included in the query url
Expand Down

0 comments on commit c31be8f

Please sign in to comment.