/
PollFields.php
144 lines (112 loc) · 3.46 KB
/
PollFields.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
abstract class PollField
{
function __construct( SimpleXMLElement &$tree )
{
$this->setTitle( $tree['title'] );
$this->templateArray['idField'] = PollField::$fieldID++;
$this->parse($tree);
}
public function setTitle( $title )
{
$this->title = $title;
$this->templateArray['title'] = (string) $this->title;
}
public function getTemplateVars()
{
return $this->templateArray;
}
protected function transformStringToID( $id )
{
$id = str_replace('', '-', strtolower( $id ) );
return preg_replace('/[^A-Za-z0-9\-]/', '', $id );
}
abstract protected function parse( SimpleXMLElement &$tree );
protected $title = "";
protected $templateArray;
static protected $fieldID = 0;
}
class TextField extends PollField
{
public function __construct( SimpleXMLElement &$tree )
{
if ( $tree->getName() != 'text' )
throw new Exception('You have to pass a SimpleXMLElement with \'text\' as root element ');
parent::__construct( $tree );
$this->templateArray['questionType'] = 'text';
}
protected function parse( SimpleXMLElement &$tree )
{
}
}
abstract class SelectField extends PollField
{
public function __construct( SimpleXMLElement &$tree )
{
parent::__construct( $tree );
}
protected function addChoice( $choiceName, $choiceID = NULL )
{
if ( is_null( $choiceID ) )
$choiceID = $choiceName;
$this->choices[] = array('label' => $choiceName, 'idOption' => $this->transformStringToID( $choiceID ) );
}
protected $choices = array();
}
class ChoiceField extends SelectField
{
public function __construct( SimpleXMLElement &$tree )
{
if ( $tree->getName() != 'choice' )
throw new Exception('You have to pass a SimpleXMLElement with \'choice\' as root element ');
parent::__construct( $tree );
$this->templateArray['questionType'] = 'choice';
}
protected function setAllowMultiChoice( $allow )
{
$this->allowMultiChoice = (bool) $allow;
}
protected function parse( SimpleXMLElement &$tree )
{
if ( isset( $tree['allowMultiple'] ) )
{
$this->setAllowMultiChoice( ( (string) $tree['allowMultiple'] ) == 'true' );
}
$this->templateArray['multipleChoice'] = $this->allowMultiChoice;
foreach ( $tree->children() as $element )
{
if ( !isset( $element['idOption'] ) )
$this->addChoice( (string) $element );
else
$this->addChoice( (string) $element, (string) $element['idOption'] );
}
$this->templateArray['elements'] = $this->choices;
}
private $allowMultiChoice = false;
}
class RatingField extends SelectField
{
public function __construct( SimpleXMLElement &$tree )
{
if ( $tree->getName() != 'rating' )
throw new Exception('You have to pass a SimpleXMLElement with \'rating\' as root element ');
parent::__construct( $tree );
$this->templateArray['questionType'] = 'rating';
}
protected function fillChoices( $minRating = 1, $maxRating = 5, $numberOfOptions = 5 )
{
$increment = (float) ($maxRating - $minRating) / ( $numberOfOptions - 1 );
for( $i = 0; $i < $numberOfOptions; $i++ )
{
$this->addChoice( ( (string) ($minRating + $increment * $i) ) );
}
}
protected function parse( SimpleXMLElement &$tree )
{
$minRating = (isset($tree['minValue'])) ? (int) $tree['minValue'] : 1;
$maxRating = (isset($tree['maxValue'])) ? (int) $tree['maxValue'] : 5;
$numberOfOptions = (isset($tree['numberOfOptions'])) ? (int) $tree['numberOfOptions'] : 5;
$this->fillChoices( $minRating, $maxRating, $numberOfOptions );
$this->templateArray['elements'] = $this->choices;
}
}