Skip to content

Commit

Permalink
Dev: move default QuestionAttribute to model and use it (#1064)
Browse files Browse the repository at this point in the history
Dev: then remove isset
  • Loading branch information
Shnoulle authored and LouisGac committed May 9, 2018
1 parent 3b152c6 commit ed93ba3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/database.php
Expand Up @@ -569,7 +569,7 @@ private function actionUpdateQuestion($iSurveyID)
$aLanguages = array_merge(array(Survey::model()->findByPk($iSurveyID)->language), Survey::model()->findByPk($iSurveyID)->additionalLanguages);
foreach ($validAttributes as $validAttribute) {
/* Readonly attribute : disable save */
if((isset($validAttribute['readonly']) && !empty($validAttribute['readonly'])) || (isset($validAttribute['readonly_when_active']) && !empty($validAttribute['readonly_when_active']) && Survey::model()->findByPk($iSurveyID)->getIsActive()) ) {
if( $validAttribute['readonly'] || $validAttribute['readonly_when_active'] && Survey::model()->findByPk($iSurveyID)->getIsActive() ) {
continue;
}
if ($validAttribute['i18n']) {
Expand Down
29 changes: 8 additions & 21 deletions application/helpers/questionHelper.php
Expand Up @@ -12,7 +12,7 @@
*
*/
namespace LimeSurvey\Helpers;

use QuestionAttribute;
/**
* General helper class for question + question setting system
*/
Expand Down Expand Up @@ -1607,30 +1607,17 @@ public static function getQuestionAttributesSettings($sType)
self::$questionAttributesSettings[$sType] = array();
self::getAttributesDefinitions(); /* we need to have self::$attributes */
/* Filter to get this question type setting */
$aQuestionTypeAttribute = array_filter(self::$attributes, function($attribute) use ($sType)
{
$aQuestionTypeAttribute = array_filter(self::$attributes, function($attribute) use ($sType) {
return stripos($attribute['types'], $sType) !== false;
});

$default = array(
"caption"=>'',
"inputtype"=>"text",
"options"=>'',
"category"=>gT("Plugins"),
"default"=>'',
"help"=>'',
"sortorder"=>1000,
"i18n"=>false,
"readonly"=>false,
"readonly_when_active"=>false,
"expression"=>null,
);
foreach ($aQuestionTypeAttribute as $attribute=>$settings) {
self::$questionAttributesSettings[$sType][$attribute] = array_merge(
$default,
$settings,
array("name"=>$attribute)
);
self::$questionAttributesSettings[$sType][$attribute] = array_merge(
QuestionAttribute::getDefaultSettings(),
array("category"=>gT("Plugins")),
$aCustomAttribute,
array("name"=>$attribute)
);
}
}
return self::$questionAttributesSettings[$sType];
Expand Down
15 changes: 5 additions & 10 deletions application/models/Question.php
Expand Up @@ -288,18 +288,13 @@ public static function getQuestionTemplateAttributes($aAttributeNames, $aAttribu
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;
}

$aCustomAttribute = array_merge(
QuestionAttribute::getDefaultSettings(),
array("category"=>gT("Template")),
$aCustomAttribute
);
$aAttributeNames[$sAttributeName] = $aCustomAttribute;
}
}
Expand Down
22 changes: 22 additions & 0 deletions application/models/QuestionAttribute.php
Expand Up @@ -297,4 +297,26 @@ public function getSurvey()
{
return $this->question->survey;
}

/**
* Get default settings for an attribut
* @return array
*/
public static function getDefaultSettings()
{
return array(
"name" => null,
"caption" => '',
"inputtype" => "text",
"options" => null,
"category" => gT("Attribute"),
"default" => '',
"help" => '',
"sortorder" => 1000,
"i18n"=> false,
"readonly" => false,
"readonly_when_active" => false,
"expression"=> null,
);
}
}
Expand Up @@ -46,7 +46,7 @@
<!-- Input -->
<div class="">
<?php
$readonly = $aAttribute['readonly'] || (isset($aAttribute['readonly_when_active']) && $aAttribute['readonly_when_active'] && $bIsActive);
$readonly = ( $aAttribute['readonly'] || ($aAttribute['readonly_when_active'] && $bIsActive) );
switch ($aAttribute['inputtype'])
{
// Switch
Expand Down

0 comments on commit ed93ba3

Please sign in to comment.