-
Notifications
You must be signed in to change notification settings - Fork 988
/
ThemeQuestionAttributeProvider.php
83 lines (73 loc) · 2.99 KB
/
ThemeQuestionAttributeProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace LimeSurvey\Models\Services;
/**
* Provides question attribute definitions from question themes
*/
class ThemeQuestionAttributeProvider extends QuestionAttributeProvider
{
/** @inheritdoc */
public function getDefinitions($options = [])
{
/** @var string question theme from the filter or, if not set, from the question */
$questionTheme = self::getQuestionTheme($options);
if (empty($questionTheme) || $questionTheme == \Question::DEFAULT_QUESTION_THEME) {
return [];
}
/** @var string question type */
$questionType = self::getQuestionType($options);
if (empty($questionType)) {
return [];
}
return $this->getAttributesFromQuestionTheme($questionTheme, $questionType);
}
/**
* Gets the additional attributes for an extended theme from xml file.
* If there are no attributes, an empty array is returned
*
* @param string $questionThemeName the question theme name (see table question theme "name")
* @param string $questionType the extended typ (see table question_themes "extends")
* @return array<string,array> additional attributes for an extended theme or empty array
*/
protected function getAttributesFromQuestionTheme($questionThemeName, $questionType)
{
/** @var array<string,array> An array of question attributes */
$attributes = array();
$questionTheme = \QuestionTheme::model()->findByAttributes([], 'name = :name AND extends = :extends', ['name' => $questionThemeName, 'extends' => $questionType]);
if ($questionTheme !== null) {
$xmlFilePath = $questionTheme->getXmlPath() . '/config.xml';
$extensionConfig = \ExtensionConfig::loadFromFile($xmlFilePath);
}
if (!empty($extensionConfig)) {
$xmlAttributes = $extensionConfig->getNodeAsArray('attributes');
if (!empty($xmlAttributes['attribute']['name'])) {
// Only one attribute set in config: need an array of attributes
$xmlAttributes['attribute'] = array($xmlAttributes['attribute']);
}
// Create array of attribute with name as key
foreach ($xmlAttributes['attribute'] as $attribute) {
if (!empty($attribute['name'])) {
$attributes[$attribute['name']] = array_merge(self::getBaseDefinition(), $attribute);
}
}
}
return $attributes;
}
/**
* Extracts the question theme from the $options.
* If it's not explicitly set, it tries to use a question object.
*
* @param array<string,mixed> $options
*
* @return
*/
private static function getQuestionTheme($options)
{
if (!empty($options['questionTheme'])) {
return $options['questionTheme'];
}
if (!empty($options['question'])) {
return $options['question']->question_theme_name;
}
return '';
}
}