Skip to content

Commit

Permalink
Dev: survey theme options included in lss export file
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikvitt committed Aug 3, 2018
1 parent 108cf35 commit 0b805f0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
84 changes: 83 additions & 1 deletion application/helpers/export_helper.php
Expand Up @@ -891,6 +891,10 @@ function surveyGetXMLData($iSurveyID, $exclude = array())
}
$xml->endElement();
surveyGetXMLStructure($iSurveyID, $xml, $exclude);
// survey theme configuration - db values
surveyGetThemeConfiguration($iSurveyID, $xml, false, 'themes');
// survey theme configuration - inherited values
surveyGetThemeConfiguration($iSurveyID, $xml, true, 'themes_inherited');
$xml->endElement(); // close columns
$xml->endDocument();
return $xml->outputMemory(true);
Expand Down Expand Up @@ -2630,4 +2634,82 @@ function sortArrayByColumn(&$array, $column_name){
uasort($array, create_function('$a, $b', '
return strnatcmp($a["' . $column_name . '"], $b["' . $column_name . '"]);
'));
}
}

/**
* Write XML from Associative Array, recursive function
* @param object $xml XMLWriter Object
* @param array $aData Associative Data Array
* @param int $sParentKey parent key
*/
function writeXmlFromArray(XMLWriter $xml, $aData, $sParentKey='') {
$bCloseElement = false;
foreach($aData as $key => $value) {
if (!empty($value)){
if(is_array($value)) {

if (is_numeric($key)){
$xml->startElement($sParentKey);
$bCloseElement = true;
} elseif (isAssociativeArray($value)){
$xml->startElement($key);
$bCloseElement = true;
}

if (is_numeric($key)){
writeXmlFromArray($xml, $value, $sParentKey);
} else {
writeXmlFromArray($xml, $value, $key);
}

if ($bCloseElement === true){
$xml->endElement();
$bCloseElement = false;
}
continue;
} elseif (is_numeric($key)){
$xml->writeElement($sParentKey, $value);
} else {
$xml->writeElement($key, $value);
}
}
}
return $xml;
}

/**
* Write XML structure for themes
* @param int $iSurveyId Survey ID
* @param object $oXml XMLWriter Object
* @param bool $bInherit should theme configuration be inherited?
* @param string $sElementName name for XML element
*/
function surveyGetThemeConfiguration($iSurveyId = null, $oXml = null, $bInherit = false, $sElementName = 'themes'){

$aThemeData = array();

if ($iSurveyId != null) {
$aSurveyConfiguration = TemplateConfiguration::getThemeOptionsFromSurveyId($iSurveyId, $bInherit);

foreach ($aSurveyConfiguration as $iThemeKey => $oConfig) {

foreach ($oConfig as $key => $attribute) {

if (is_array($attribute)){
$attribute = (array)$attribute;
} elseif (isJson($attribute)){
$attribute = (array)json_decode($attribute);
}
$aThemeData[$sElementName]['theme'][$iThemeKey][$key] = $attribute;
}
}

}

if ($oXml !== null && !empty($aThemeData)){

writeXmlFromArray($oXml, $aThemeData);

}

}
44 changes: 44 additions & 0 deletions application/models/TemplateConfiguration.php
Expand Up @@ -251,6 +251,50 @@ public static function getInstanceFromSurveyId($iSurveyId, $sTemplateName = null
return $oTemplateConfigurationModel;
}



/**
* Returns a Theme options array based on a surveyID
*
* @param integer $iSurveyId
* @param bool $bInherited should inherited theme option values be used?
* @return array
*/

public static function getThemeOptionsFromSurveyId($iSurveyId = 0, $bInherited = false)
{
$aTemplateConfigurations = array();
// fetch all themes belonging to $iSurveyId
$criteria = new CDbCriteria();
$criteria->addCondition('sid=:sid');
$criteria->params = array('sid' => $iSurveyId);
$oTemplateConfigurations = self::model()->findAll($criteria);

if ($bInherited){ // inherited values
foreach ($oTemplateConfigurations as $key => $oTemplateConfiguration) {
$oTemplateConfiguration->bUseMagicInherit = true;
$oTemplateConfiguration->setOptions();
// set array with values
$aTemplateConfigurations[$key]['id'] = null;
$aTemplateConfigurations[$key]['sid'] = $iSurveyId;
$aTemplateConfigurations[$key]['template_name'] = $oTemplateConfiguration->template_name;
$aTemplateConfigurations[$key]['config']['options'] = (array)$oTemplateConfiguration->oOptions;
}
} else { // db values
foreach ($oTemplateConfigurations as $key => $oTemplateConfiguration) {
$oTemplateConfiguration->bUseMagicInherit = false;
$oAttributes = $oTemplateConfiguration->attributes;
// set array with values
$aTemplateConfigurations[$key]['id'] = null;
$aTemplateConfigurations[$key]['sid'] = $iSurveyId;
$aTemplateConfigurations[$key]['template_name'] = $oAttributes['template_name'];
$aTemplateConfigurations[$key]['config']['options'] = isJson($oAttributes['options'])?(array)json_decode($oAttributes['options']):$oAttributes['options'];
}
}

return $aTemplateConfigurations;
}

/**
* For a given survey, it checks if its theme have a all the needed configuration entries (survey + survey group). Else, it will create it.
* @TODO: recursivity for survey group
Expand Down

0 comments on commit 0b805f0

Please sign in to comment.