-
Notifications
You must be signed in to change notification settings - Fork 7
/
pollDb.php
143 lines (120 loc) · 5.21 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
<?php
namespace pollext\poll;
use yii;
class PollDb{
public function isPollExist($pollName){
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM polls WHERE poll_name=:pollName')->
bindParam(':pollName',$pollName);
$pollData = $command->queryOne();
if($pollData==null){
return false;
}else {
return true;
}
}
public function setVoicesData($pollName, $answerOptions){
$db = Yii::$app->db;
$answersList = array();
for($i=0; $i<count($answerOptions); $i++){
$command = $db->createCommand()->insert('voices_of_poll', [
'poll_name' => $pollName,
'answers' => $answerOptions[$i],
'value' => 0
])->execute();
}
}
public function getVoicesData($pollName){
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM voices_of_poll WHERE poll_name=:pollName')->
bindParam(':pollName',$pollName);
$voicesData = $command->queryAll();
return $voicesData;
}
public function updateAnswers($pollName, $voise, $answerOptions){
if(isset($_POST['VoicesOfPoll']['voice'])){
$db = Yii::$app->db;
;
/*if(array_search(Yii::$app->user->getId(),$ids)==false){
}else{
return false;
}
*/
$command = $db->createCommand("UPDATE voices_of_poll SET value=value +1
WHERE poll_name='$pollName' AND answers='$answerOptions[$voise]'")->execute();
}
}
public function updateUsers($pollName){
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM polls 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('users_id', [
'poll_id' => $pollData['id'],
'user_id' => $userId
])->execute();
}
public function isVote($pollName){
$db = Yii::$app->db;
$command = $db->createCommand('SELECT * FROM polls 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 users_id 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;
$command_1 = $db->createCommand("
CREATE TABLE IF NOT EXISTS `users_id` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `poll_id` (`poll_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
$command_2 = $db->createCommand("
CREATE TABLE IF NOT EXISTS `polls` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_name` varchar(500) NOT NULL,
`answer_options` text NOT NULL,
PRIMARY KEY (`id`),
KEY `poll_name` (`poll_name`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
$command_3 = $db->createCommand("
CREATE TABLE IF NOT EXISTS `voices_of_poll` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_name` varchar(500) NOT NULL,
`answers` varchar(100) CHARACTER SET utf8mb4 NOT NULL,
`value` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
)->execute();
}
public function isTableExists(){
$db = Yii::$app->db;
$command = $db->createCommand("SHOW TABLES LIKE 'polls'");
$res = $command->queryAll();
return $res;
}
}