forked from Dzhus111/pollext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PollDb.php
232 lines (204 loc) · 6.62 KB
/
PollDb.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace davidjeddy\yii2poll;
use yii;
class PollDb
{
/**
*
*/
public function isPollExist($pollName)
{
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')
->bindParam(':pollName', $pollName);
$pollData = $command->queryOne();
return (empty($pollData) ? true : false);
}
/**
* [setVoicesData description]
*
* @deprecated 2.0.2 Use pollAnswerOptions()
*
* @param [type] $pollName [description]
* @param [type] $answerOptions [description]
*/
public function setVoicesData($pollName, $answerOptions)
{
$db = Yii::$app->db;
$count = count($answerOptions);
for ($i = 0; $i < $count; $i++) {
$db->createCommand()->insert('poll_response', [
'answers' => $answerOptions[$i],
'poll_name' => $pollName,
'value' => 0,
])->execute();
}
}
/**
* poll_response TBO logic
* ADDS new answers dynamically.
* REMOVES answers that are not part of $pollObj->answerOptionsData
*
* @param [type] $pollObj [description]
*
* @return [type] [description]
*/
public function pollAnswerOptions(\davidjeddy\yii2poll\Poll $pollObj)
{
$db = Yii::$app->db;
foreach ($pollObj->answerOptions as $key => $value) {
$answer = (new \yii\db\Query())
->select(['answers'])
->from('poll_response')
->andWhere(['poll_name' => $pollObj->pollName])
->andWhere(['answers' => $value])
->one();
if (!$answer) {
$db->createCommand()->insert('poll_response', [
'answers' => $value,
'poll_name' => $pollObj->pollName,
'value' => 0,
])->execute();
}
}
// remove answers that are no longer a part of the poll answer_options
// source http://stackoverflow.com/questions/31672033/how-do-i-delete-rows-in-yii2
return (new \yii\db\Query())
->createCommand()
->delete('poll_response')
->where(['poll_name' => $pollObj->pollName])
->where(['NOT IN', 'answers', implode($pollObj->answerOptions, "', '")])
->execute();
}
/**
*
*/
public function getVoicesData($pollName)
{
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM poll_response WHERE poll_name=:pollName')->
bindParam(':pollName', $pollName);
$voicesData = $command->queryAll();
return $voicesData;
}
/**
* [updateAnswers description]
*
* @version 2.0.7
* @since na
*
* @param string $pollName Poll name
* @param integer $voice Integer of chosen key
* @param array $answerOptions Array fo possible options
*
* @return object
*/
public function updateAnswers($pollName, $voice, $answerOptions)
{
return Yii::$app->db->createCommand("
UPDATE poll_response
SET value = value +1
WHERE poll_name = '$pollName'
AND answers = '$answerOptions[$voice]'")
->execute();
}
/**
*
*/
public function updateUsers($pollName)
{
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')->
bindParam(':pollName', $pollName);
$userId;
if (Yii::$app->user->getId() === null) {
$userId = 0;
} else {
$userId = Yii::$app->user->getId();
}
$pollData = $command->queryOne();
$command = $db->createCommand()->insert('poll_user', [
'poll_id' => $pollData['id'],
'user_id' => $userId
])->execute();
}
/**
*
*/
public function isVote($pollName)
{
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')->
bindParam(':pollName', $pollName);
$pollData = $command->queryOne();
$userId;
if (Yii::$app->user->getId() === null) {
$userId = 0;
} else {
$userId = Yii::$app->user->getId();
}
$db = Yii::$app->db;
$command = $db->createCommand("SELECT * FROM poll_user WHERE user_id='$userId' AND poll_id=:pollId")->
bindParam(':pollId', $pollData['id']);
$result = $command->queryOne();
if ($result === null) {
return false;
} else {
return true;
}
}
/**
*
*/
public function createTables()
{
$db = Yii::$app->db;
$db->createCommand("
CREATE TABLE IF NOT EXISTS `poll_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
'created_at' int(11) NOT NULL,
'created_at' int(11) NULL,
'created_at' int(11) NULL,
PRIMARY KEY (`id`),
KEY `poll_id` (`poll_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
$db->createCommand("
CREATE TABLE IF NOT EXISTS `poll_question` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_name` varchar(128) NOT NULL,
`answer_options`text NOT NULL,
'created_at' int(11) NOT NULL,
'created_at' int(11) NULL,
'created_at' int(11) NULL,
PRIMARY KEY (`id`),
KEY `poll_name` (`poll_name`(128))
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
$db->createCommand("
CREATE TABLE IF NOT EXISTS `poll_response` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_name` varchar(128) NOT NULL,
`answers` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`value` int(11) NOT NULL,
'created_at' int(11) NOT NULL,
'created_at' int(11) NULL,
'created_at' int(11) NULL,
PRIMARY KEY (`id`),
KEY `poll_name` (`poll_name`(128))
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
}
/**
*
*/
public function isTableExists()
{
$db = Yii::$app->db;
$command = $db->createCommand("SHOW TABLES LIKE 'poll_question'");
$res = $command->queryAll();
return $res;
}
}