forked from Dzhus111/pollext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Poll.php
129 lines (100 loc) · 3.31 KB
/
Poll.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
<?php
namespace davidjeddy\yii2poll;
use yii;
use yii\base\Widget;
class Poll extends Widget
{
public $answerOptions = [];
public $answerOptionsData;
public $answers = [];
public $isExist;
public $isVote;
public $params = [
'backgroundLinesColor' => '#D3D3D3',
'linesColor' => '#4F9BC7',
'linesHeight' => 15,
'maxLineWidth' => 300,
];
public $pollData;
public $pollName = '';
public $sumOfVoices = 0;
// experiemental ajax success override
public $ajaxSuccess = [];
public function setPollName($name)
{
$this->pollName = $name;
}
public function getDbData()
{
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')
->bindParam(':pollName', $this->pollName);
$this->pollData = $command->queryOne();
$this->answerOptionsData = unserialize($this->pollData['answer_options']);
}
public function setDbData()
{
return Yii::$app->db->createCommand()->insert('poll_question', [
'answer_options' => $this->answerOptionsData,
'poll_name' => $this->pollName,
])->execute();
}
public function setParams($params)
{
$this->params = array_merge($this->params, $params);
}
public function getParams($param)
{
return $this->params[$param];
}
public function init()
{
parent::init();
$pollDB = new PollDb;
$this->isExist = $pollDB->isTableExists();
if (count($this->isExist) == 0) {
$pollDB->createTables();
}
if ($this->answerOptions !== null) {
$this->answerOptionsData = serialize($this->answerOptions);
}
// crate DB TBOs if they do not exist for this poll
if (!$pollDB->isPollExist($this->pollName)) {
$this->setDbData();
}
// check that all Poll answers exist
$pollDB->pollAnswerOptions($this);
if (Yii::$app->request->isAjax) {
if (isset($_POST['VoicesOfPoll'])) {
if ($_POST['poll_name'] == $this->pollName && isset($_POST['VoicesOfPoll']['voice'])) {
$pollDB->updateAnswers(
$this->pollName,
$_POST['VoicesOfPoll']['voice'],
$this->answerOptions
);
$pollDB->updateUsers($this->pollName);
}
}
}
$this->getDbData();
$this->answers = $pollDB->getVoicesData($this->pollName);
for ($i = 0; $i < count($this->answers); $i++) {
$this->sumOfVoices = $this->sumOfVoices + $this->answers[$i]['value'];
}
$this->isVote = $pollDB->isVote($this->pollName);
}
public function run()
{
$model = new VoicesOfPoll;
return $this->render('index', [
'ajaxSuccess' => $this->ajaxSuccess,
'answers' => $this->answerOptions,
'answersData' => $this->answers,
'isVote' => $this->isVote,
'model' => $model,
'params' => $this->params,
'pollData' => $this->pollData,
'sumOfVoices' => $this->sumOfVoices,
]);
}
}