Skip to content

Commit

Permalink
Fixed #7011: It is not possible to randomize the order of groups even…
Browse files Browse the repository at this point in the history
… when a randomization groupname is set

dev: ended up doing maybe a little more then needed but basically, the duplicate updatesessiongrouplist was removed since it messed up the order and em sometimes looks at the grouplist for ordering. Also the randmaster is saved to the right place so the fieldmap can pick up on it
  • Loading branch information
mennodekker committed Jan 24, 2013
1 parent 6b971cc commit 0ed8391
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 107 deletions.
2 changes: 1 addition & 1 deletion application/controllers/survey/index.php
Expand Up @@ -149,7 +149,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);
UpdateSessionGroupList($surveyid, $sTempLanguage); // to refresh the language strings in the group list session variable
UpdateGroupList($surveyid, $sTempLanguage); // to refresh the language strings in the group list session variable
UpdateFieldArray(); // to refresh question titles and question text

}
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -251,7 +251,7 @@ function run($surveyid,$args) {
{
$_SESSION[$LEMsessid]['test']=time();
display_first_page();
exit;
Yii::app()->end(); // So we can still see debug messages
}

//CHECK IF ALL MANDATORY QUESTIONS HAVE BEEN ANSWERED ############################################
Expand Down
39 changes: 6 additions & 33 deletions application/helpers/common_helper.php
Expand Up @@ -2820,39 +2820,12 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
{
if (isset($fieldmap[$fieldname]))
{
$f = $fieldmap[$fieldname];
if (isset($f['question']))
{
$mf['question'] = $f['question'];
}
if (isset($f['subquestion']))
{
$mf['subquestion'] = $f['subquestion'];
}
if (isset($f['subquestion1']))
{
$mf['subquestion1'] = $f['subquestion1'];
}
if (isset($f['subquestion2']))
{
$mf['subquestion2'] = $f['subquestion2'];
}
if (isset($f['group_name']))
{
$mf['group_name'] = $f['group_name'];
}
if (isset($f['answerList']))
{
$mf['answerList'] = $f['answerList'];
}
if (isset($f['defaultvalue']))
{
$mf['defaultvalue'] = $f['defaultvalue'];
}
if (isset($f['help']))
{
$mf['help'] = $f['help'];
}
// This array holds the keys of translatable attributes
$translatable = array_flip(array('question', 'subquestion', 'subquestion1', 'subquestion2', 'group_name', 'answerList', 'defaultValue', 'help'));
// We take all translatable attributes from the new fieldmap
$newText = array_intersect_key($fieldmap[$fieldname], $translatable);
// And merge them with the other values from the random fieldmap like questionSeq, groupSeq etc.
$mf = $newText + $mf;
}
$mfieldmap[$fieldname] = $mf;
}
Expand Down
22 changes: 13 additions & 9 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -7444,14 +7444,18 @@ function getGroupInfoForEM($surveyid,$lang=NULL)
$qinfo = array();
$_order=0;
foreach ($data as $d)
{
$gid[$d['gid']] = array(
'gid' => $d['gid'],
'group_name' => $d['group_name'],
'description' => $d['description'],
'grelevance' => $d['grelevance'],
);
}
foreach ($_SESSION['survey_'.$surveyid]['grouplist'] as $orderedGid => $info)
{
$qinfo[$_order] = array(
'group_order' => $_order,
'gid' => $d['gid'],
'group_name' => $d['group_name'],
'description' => $d['description'],
'grelevance' => $d['grelevance'],
);
$gid[$orderedGid]['group_order'] = $_order;
$qinfo[$_order] = $gid[$orderedGid];
++$_order;
}

Expand Down Expand Up @@ -8963,7 +8967,7 @@ public static function getLEMsurveyId() {
$LEM =& LimeExpressionManager::singleton();
return $LEM->sid;
}

}

/**
Expand All @@ -8988,6 +8992,6 @@ function cmpQuestionSeq($a, $b)
}
return ($a['qseq'] < $b['qseq']) ? -1 : 1;
}


?>
124 changes: 61 additions & 63 deletions application/helpers/frontend_helper.php
Expand Up @@ -1625,7 +1625,7 @@ function buildsurveysession($surveyid,$preview=false)
}


UpdateSessionGroupList($surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);
UpdateGroupList($surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);

$sQuery = "SELECT count(*)\n"
." FROM {{groups}} INNER JOIN {{questions}} ON {{groups}}.gid = {{questions}}.gid\n"
Expand Down Expand Up @@ -1729,20 +1729,16 @@ function buildsurveysession($surveyid,$preview=false)
shuffle($aShuffledIDs);
$aGIDCompleteMap=$aGIDCompleteMap+array_combine($aGIDs,$aShuffledIDs);
}
$_SESSION['survey_'.$surveyid]['groupReMap']=$aGIDCompleteMap;
$_SESSION['survey_' . $surveyid]['groupReMap'] = $aGIDCompleteMap;

$randomized = false; // So we can trigger reorder once for group and question randomization
// Now adjust the grouplist
if (count($aRandomGroups)>0)
{
$randomized = true; // So we can trigger reorder once for group and question randomization
// Now adjust the grouplist
foreach ($aGIDCompleteMap as $iOldGid=>$iNewGid)
{
if (!isset($_SESSION['survey_'.$surveyid]['grouplist'][$iOldGid]['shuffled']) && $iOldGid!=$iNewGid)
{
$_SESSION['survey_'.$surveyid]['grouplist']=arraySwapAssoc($iOldGid,$iNewGid,$_SESSION['survey_'.$surveyid]['grouplist']);
}
$_SESSION['survey_'.$surveyid]['grouplist'][$iNewGid]['shuffled']=true;
}
Yii::import('application.helpers.frontend_helper', true); // make sure frontend helper is loaded
UpdateGroupList($surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);
// ... and the fieldmap

// First create a fieldmap with GID as key
Expand All @@ -1751,27 +1747,31 @@ function buildsurveysession($surveyid,$preview=false)
if (isset($aField['gid']))
{
$GroupFieldMap[$aField['gid']][]=$aField;
}
}
else{
$GroupFieldMap['other'][]=$aField;
}
}
// swap it
foreach ($aGIDCompleteMap as $iOldGid=>$iNewGid)
foreach ($GroupFieldMap as $iOldGid => $fields)
{
if (!isset($GroupFieldMap[$iOldGid]['shuffled']) && $iOldGid!=$iNewGid)
$iNewGid = $iOldGid;
if (isset($aGIDCompleteMap[$iOldGid]))
{
$GroupFieldMap=arraySwapAssoc($iOldGid,$iNewGid,$GroupFieldMap);
$iNewGid = $aGIDCompleteMap[$iOldGid];
}
$GroupFieldMap[$iNewGid]['shuffled']=true;
$newGroupFieldMap[$iNewGid] = $GroupFieldMap[$iNewGid];
}
$GroupFieldMap = $newGroupFieldMap;
// and convert it back to a fieldmap
unset($fieldmap);
foreach($GroupFieldMap as $aGroupFields)
{
foreach ($aGroupFields as $aField)
{
if (isset($aField['fieldname'])) $fieldmap[$aField['fieldname']]=$aField; // isset() because of the shuffled flag above
if (isset($aField['fieldname'])) {
$fieldmap[$aField['fieldname']] = $aField; // isset() because of the shuffled flag above
}
}
}
unset($GroupFieldMap);
Expand Down Expand Up @@ -1799,7 +1799,8 @@ function buildsurveysession($surveyid,$preview=false)
// If we have randomization groups set, then lets cycle through each group and
// replace questions in the group with a randomly chosen one from the same group
if (count($randomGroups) > 0)
{
{
$randomized = true; // So we can trigger reorder once for group and question randomization
$copyFieldMap = array();
$oldQuestOrder = array();
$newQuestOrder = array();
Expand All @@ -1817,17 +1818,19 @@ function buildsurveysession($surveyid,$preview=false)
foreach ($fieldmap as $fieldkey => $fieldval)
{
$found = 0;
foreach ($randomGroups as $gkey=>$gval)
foreach ($randomGroups as $gkey => $gval)
{
// We found a qid that is in the randomization group
if (isset($fieldval['qid']) && in_array($fieldval['qid'],$oldQuestOrder[$gkey]))
{
// Get the swapped question
$oldQuestFlip = array_flip($oldQuestOrder[$gkey]);
foreach($fieldmap as $key => $field) {
if (isset($field['qid']) && $field['qid'] == $newQuestOrder[$gkey][$oldQuestFlip[$fieldval['qid']]]) {
$field['random_gid'] = $fieldval['gid'];
$copyFieldMap[$key] = $field;
foreach ($fieldmap as $key => $field)
{
if (isset($field['qid']) && $field['qid'] == $newQuestOrder[$gkey][$oldQuestFlip[$fieldval['qid']]])
{
$field['random_gid'] = $fieldval['gid']; // It is possible to swap to another group
$copyFieldMap[$key] = $field;
}
}
$found = 1;
Expand All @@ -1843,27 +1846,30 @@ function buildsurveysession($surveyid,$preview=false)
}
reset($randomGroups);
}
$fieldMap = $copyFieldMap;
}

if ($randomized === true)
{
// reset the sequencing counts
$gseq=-1;
$_gid=-1;
$qseq=-1;
$_qid=-1;
$copyFieldMap2 = array();
foreach ($copyFieldMap as $key=>$val)
$gseq = -1;
$_gid = -1;
$qseq = -1;
$_qid = -1;
$copyFieldMap = array();
foreach ($fieldmap as $key => $val)
{
if (isset($val['random_gid']))
if ($val['gid'] != '')
{
if ($val['gid'] != '' && $val['random_gid'] != '' && $val['random_gid'] != $_gid)
if (isset($val['random_gid']))
{
$_gid = $val['random_gid'];
++$gseq;
$gid = $val['random_gid'];
} else {
$gid = $val['gid'];
}
}
else
{
if ($val['gid'] != '' && $val['gid'] != $_gid)
if ($gid != $_gid)
{
$_gid = $val['gid'];
$_gid = $gid;
++$gseq;
}
}
Expand All @@ -1873,20 +1879,22 @@ function buildsurveysession($surveyid,$preview=false)
$_qid = $val['qid'];
++$qseq;
}

if ($val['gid'] != '' && $val['qid'] != '')
{
$val['groupSeq'] = $gseq;
$val['groupSeq'] = $gseq;
$val['questionSeq'] = $qseq;
}
$copyFieldMap2[$key] = $val;

$copyFieldMap[$key] = $val;
}
$fieldmap = $copyFieldMap;
unset($copyFieldMap);
$fieldmap=$copyFieldMap2;

$_SESSION['survey_'.$surveyid]['fieldmap-' . $surveyid . $_SESSION['survey_'.$surveyid]['s_lang']] = $fieldmap;
$_SESSION['survey_'.$surveyid]['fieldmap-' . $surveyid . '-randMaster'] = 'fieldmap-' . $surveyid . $_SESSION['survey_'.$surveyid]['s_lang'];
$_SESSION['fieldmap-' . $surveyid . $_SESSION['survey_'.$surveyid]['s_lang']] = $fieldmap;
$_SESSION['fieldmap-' . $surveyid . '-randMaster'] = 'fieldmap-' . $surveyid . $_SESSION['survey_'.$surveyid]['s_lang'];
}

// TMSW Conditions->Relevance: don't need hasconditions, or usedinconditions

$_SESSION['survey_'.$surveyid]['fieldmap']=$fieldmap;
Expand Down Expand Up @@ -2257,18 +2265,21 @@ function UpdateGroupList($surveyid, $language)
{
$_SESSION['survey_'.$surveyid]['grouplist'][$row['gid']]=array($row['gid'], $row['group_name'], $row['description']);
}
$groupList = $_SESSION['survey_'.$surveyid]['grouplist'];
if (isset($_SESSION['survey_'.$surveyid]['groupReMap']) && count($_SESSION['survey_'.$surveyid]['groupReMap'])>0)
{
// Now adjust the grouplist
foreach ($_SESSION['survey_'.$surveyid]['groupReMap'] as $iOldGid=>$iNewGid)
{
if (!isset($_SESSION['survey_'.$surveyid]['grouplist'][$iOldGid]['shuffled']) && $iOldGid!=$iNewGid)
{
$_SESSION['survey_'.$surveyid]['grouplist']=arraySwapAssoc($iOldGid,$iNewGid,$_SESSION['survey_'.$surveyid]['grouplist']);
$groupRemap = $_SESSION['survey_'.$surveyid]['groupReMap'];
unset ($_SESSION['survey_'.$surveyid]['grouplist']);

foreach ($groupList as $groupId => $info) {
$newId = $groupId;
if (isset($groupRemap[$groupId])) {
$newId = $groupRemap[$groupId];
}
$_SESSION['survey_'.$surveyid]['grouplist'][$iNewGid]['shuffled']=true;
$_SESSION['survey_'.$surveyid]['grouplist'][$newId] = $groupList[$newId];
}
}
}
}

/**
Expand Down Expand Up @@ -2660,19 +2671,6 @@ function resetTimers()
Yii::app()->request->cookies['limesurvey_timers'] = $cookie;
}

function UpdateSessionGroupList($surveyid, $language)
//1. SESSION VARIABLE: grouplist
//A list of groups in this survey, ordered by group name.
{
unset ($_SESSION['survey_'.$surveyid]['grouplist']);
$query = "SELECT * FROM {{groups}} WHERE sid={$surveyid} AND language='".$language."' ORDER BY group_order";
$result = dbExecuteAssoc($query) or safeDie ("Couldn't get group list<br />$query<br />".$connect->ErrorMsg()); //Checked
foreach($result->readAll() as $row)
{
$_SESSION['survey_'.$surveyid]['grouplist'][]=array($row['gid'], $row['group_name'], $row['description']);
}
}

//For multilanguage surveys
// If null or 0 is given for $surveyid then the default language from config-defaults.php is returned
function SetSurveyLanguage($surveyid, $language)
Expand Down

0 comments on commit 0ed8391

Please sign in to comment.