Skip to content

Commit

Permalink
Dev: add custom attributes to question templates
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Dec 22, 2016
1 parent 619614a commit 19e7d51
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
12 changes: 12 additions & 0 deletions application/controllers/admin/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,12 @@ private function actionUpdateQuestion($iSurveyID)
$criteria = new CDbCriteria;
$criteria->compare('qid',$this->iQuestionID);
$validAttributes=\ls\helpers\questionHelper::getQuestionAttributesSettings($sQuestionType);

// If the question has a custom template, we first check if it provides custom attributes
$oAttributeValues = QuestionAttribute::model()->find("qid=:qid and attribute='question_template'",array('qid'=>$cqr->qid));
$aAttributeValues['question_template'] = ($oAttributeValues->value)?$oAttributeValues->value:'core';
$validAttributes = Question::getQuestionTemplateAttributes($validAttributes, $aAttributeValues, $cqr );

foreach ($validAttributes as $validAttribute)
{
$criteria->compare('attribute', '<>'.$validAttribute['name']);
Expand Down Expand Up @@ -1290,6 +1296,12 @@ private function actionInsertCopyQuestion($iSurveyID)
}
} else {
$validAttributes=\ls\helpers\questionHelper::getQuestionAttributesSettings(Yii::app()->request->getPost('type'));

// If the question has a custom template, we first check if it provides custom attributes
$oAttributeValues = QuestionAttribute::model()->find("qid=:qid and attribute='question_template'",array('qid'=>$cqr->qid));
$aAttributeValues['question_template'] = ($oAttributeValues->value)?$oAttributeValues->value:'core';
$validAttributes = Question::getQuestionTemplateAttributes($validAttributes, $aAttributeValues, $cqr );

$aLanguages=array_merge(array(Survey::model()->findByPk($iSurveyID)->language),Survey::model()->findByPk($iSurveyID)->additionalLanguages);

foreach ($validAttributes as $validAttribute)
Expand Down
39 changes: 39 additions & 0 deletions application/models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ public function getAdvancedSettingsWithValues($iQuestionID, $sQuestionType, $iSu
}
$aAttributeValues=QuestionAttribute::model()->getQuestionAttributes($iQuestionID,$sLanguage);
$aAttributeNames = \ls\helpers\questionHelper::getQuestionAttributesSettings($sQuestionType);

// If the question has a custom template, we first check if it provides custom attributes
// TODO: move getQuestionAttributesSettings() to QuestionAttribute model to avoid code duplication

if (!is_null($sLanguage)){
$oQuestion = Question::model()->findByPk(array('qid'=>$iQuestionID, 'language'=>$sLanguage));
}else{
$oQuestion = Question::model()->find(array('condition'=>'qid=:qid','params'=>array(':qid'=>$iQuestionID)));
}
$aAttributeNames = self::getQuestionTemplateAttributes($aAttributeNames, $aAttributeValues, $oQuestion );

uasort($aAttributeNames, 'categorySort');
foreach ($aAttributeNames as $iKey => $aAttribute)
{
Expand Down Expand Up @@ -255,6 +266,34 @@ public function getAdvancedSettingsWithValues($iQuestionID, $sQuestionType, $iSu
return $aAttributeNames;
}

public static function getQuestionTemplateAttributes($aAttributeNames, $aAttributeValues, $oQuestion)
{
if (isset($aAttributeValues['question_template'])){
if($aAttributeValues['question_template'] != 'core'){

$oQuestionTemplate = QuestionTemplate::getInstance($oQuestion);
if ($oQuestionTemplate->bHasCustomAttributes){
// Add the custom attributes to the list
foreach($oQuestionTemplate->oConfig->custom_attributes->attribute as $oCustomAttribute){

$sAttributeName = (string) $oCustomAttribute->name;
$aCustomAttribute = json_decode(json_encode( (array) $oCustomAttribute), 1);

if (!isset($aCustomAttribute['i18n'])){
$aCustomAttribute['i18n'] = false;
}

if (!isset($aCustomAttribute['readonly'])){
$aCustomAttribute['readonly'] = false;
}

$aAttributeNames[$sAttributeName] = $aCustomAttribute;
}
}
}
}
return $aAttributeNames;
}

/**
* TODO: replace this function call by $oSurvey->questions defining a relation in SurveyModel
Expand Down
5 changes: 5 additions & 0 deletions application/models/QuestionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function getQuestionAttributes($iQuestionID,$sLanguage=null)

/* Get whole existing attribute for this question in an array*/
$oAttributeValues = QuestionAttribute::model()->findAll("qid=:qid",array('qid'=>$iQuestionID));

$aAttributeValues=array();
foreach($oAttributeValues as $oAttributeValue)
{
Expand All @@ -211,6 +212,10 @@ public function getQuestionAttributes($iQuestionID,$sLanguage=null)
$aAttributeValues[$oAttributeValue->attribute]['']=$oAttributeValue->value;
}
}

// If the question has a custom template, we first check if it provides custom attributes
$aAttributeNames = Question::getQuestionTemplateAttributes($aAttributeNames, $aAttributeValues, $oQuestion );

// Fill with aQuestionAttributes with default attribute or with aAttributeValues
// Can not use array_replace due to i18n
foreach($aAttributeNames as $aAttribute)
Expand Down
27 changes: 16 additions & 11 deletions application/models/QuestionTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class QuestionTemplate extends CFormModel
public $sTemplateFolderName; // The folder of the template applied to this question (if no template applied, it's false)
public $aViews; // Array of views the template can handle ($aViews['path_to_my_view']==true)

public $oConfig;
public $bHasCustomAttributes; // Does the template provides custom attributes?
private $sTemplatePath; // The path to the template
private $sTemplateQuestionPath; // The path to the folder corresponding to the current question type
private $bHasConfigFile;
Expand All @@ -49,7 +52,6 @@ public static function getNewInstance($oQuestion)
self::$instance->aViews = array();
self::$instance->getQuestionTemplateFolderName(); // Will initiate $sTemplateFolderName and $bHasTemplate.
self::$instance->setConfig();
self::$instance->registerAssets();
return self::$instance;
}

Expand Down Expand Up @@ -213,7 +215,7 @@ public function templateLoadsCorePackage()
*/
public function setConfig()
{
if (!isset($this->config)){
if (!isset($this->oConfig)){
$oQuestion = $this->oQuestion;
$sTemplatePath = $this->getTemplatePath();
$sFolderName = self::getFolderName($oQuestion->type);
Expand All @@ -224,11 +226,12 @@ public function setConfig()
if ($this->bHasConfigFile){
$sXMLConfigFile = file_get_contents( realpath ($xmlFile)); // Entity loader is disabled, so we can't use simplexml_load_file; so we must read the file with file_get_contents and convert it as a string
$this->xmlFile = $xmlFile;
$this->config = simplexml_load_string($sXMLConfigFile);
$this->oConfig = simplexml_load_string($sXMLConfigFile);

$this->bLoadCoreJs = $this->config->engine->load_core_js;
$this->bLoadCoreCss = $this->config->engine->load_core_css;
$this->bLoadCorePackage = $this->config->engine->load_core_package;
$this->bLoadCoreJs = $this->oConfig->engine->load_core_js;
$this->bLoadCoreCss = $this->oConfig->engine->load_core_css;
$this->bLoadCorePackage = $this->oConfig->engine->load_core_package;
$this->bHasCustomAttributes = !empty($this->oConfig->custom_attributes);
}
}
}
Expand All @@ -237,8 +240,8 @@ public function registerAssets()
{
if ($this->bHasConfigFile){
// Load the custom JS/CSS
$aCssFiles = (array) $this->config->files->css->filename; // The CSS files of this template
$aJsFiles = (array) $this->config->files->js->filename; // The JS files of this template
$aCssFiles = (array) $this->oConfig->files->css->filename; // The CSS files of this template
$aJsFiles = (array) $this->oConfig->files->js->filename; // The JS files of this template

if (!empty($aCssFiles) || !empty($aJsFiles) ){
// It will create the asset directory, and publish the css and js files
Expand Down Expand Up @@ -285,9 +288,11 @@ static public function getQuestionTemplateList($type)

static public function getFolderName($type)
{
$aTypeToFolder = self::getTypeToFolder();
$sFolderName = $aTypeToFolder[$type];
return $sFolderName;
if ($type){
$aTypeToFolder = self::getTypeToFolder();
$sFolderName = $aTypeToFolder[$type];
return $sFolderName;
}
}

/**
Expand Down

0 comments on commit 19e7d51

Please sign in to comment.