Repository navigation
Expand file tree
/
Copy pathvote.php
More file actions
229 lines (220 loc) · 6.4 KB
/
Copy pathvote.php
File metadata and controls
229 lines (220 loc) · 6.4 KB
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
<?php
require_once("include/main.php");
requireLogin();
requireOpen();
require_once("include/vote.php");
$state = getState();
$errors = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if($state === STATE::IDEAS) {
if((!isset($_POST["idea"]) || trim($_POST["idea"]) === '') || (!isset($_GET["id"]) || trim($_GET["id"]) === '')) {
array_push($errors, "No value given");
} else if(strlen($_POST["idea"]) > 50) {
array_push($errors, "String too long");
}
if(empty($errors)) {
if(!getUser($_GET["id"], $conn) == 0) {
addIdea($_SESSION["id"], $_GET["id"], $_POST["idea"], $conn);
}
}
} else if(getState() === STATE::VOTING) {
$voting = array();
foreach($_POST as $key => $value) {
if(is_int($key)) {
if($value > 0 && $value < 4) {
if(array_key_exists($value, $voting)) {
array_push($errors, "Repeat of value");
break;
}
$voting[$value] = $key;
} else if($value != 0) {
array_push($errors, "Unknown value given");
break;
}
}
}
if(empty($voting)) {
array_push($errors, "No vote given.");
}
if(empty($errors)) {
if(ideaExist($_GET["id"], $voting)) {
addScore($_GET["id"], $voting, $conn);
header("Location: /redirectvote.php");
exit;
} else {
array_push($errors, "Idea id given does not exists");
}
}
}
}
$users = getUsers($conn);
?>
<!DOCTYPE html>
<html>
<head>
<?php
include("include/meta.php");
?>
</head>
<body>
<?php
if(!isset($_GET["id"]) || trim($_GET["id"]) === '') {
if($state === state::IDEAS) {?>
<div>
<h1>Uppástungur</h1>
<div class="list_of_voters">
<table>
<tr>
<th>Nafn</th>
</tr>
<?php
foreach($users as $user) {?>
<tr>
<th><a href="/vote.php?id=<?php echo $user["id"];?>"><?php echo $user["name"]; ?></th>
</tr>
<?php } ?>
</table>
<p style="color: white;">ATH: Þetta er aðeins uppástungur. Kosning verður tilkynnt bráðlega.</p>
</div>
</div>
<?php } else { ?>
<div class="start_vote">
<h1>Kosning</h1>
<div>
<form method="POST" action="/redirectvote.php">
<input type="submit" name="submit" value="Hefja kosningu"/>
</form>
</div>
</div>
<?php }
} else {
if($_GET["id"] == $_SESSION["id"]) {
header("Location: /");
die();
}
$user = getUser($_GET["id"], $conn);
if($user === 0) {
die('hello');
header("Location: /");
}
if($state === state::IDEAS) {
$ideas = getIdeas($_GET["id"], $conn);
?>
<div>
<a href="/vote.php" class="back">Til baka</a>
<h1 class="name"><?php echo $user["name"] ?></h1>
<div class="list_of_voters">
<table>
<tr>
<th>Hugmyndir:</th>
</tr>
<?php
$exists = false;
foreach($ideas as $idea) {
$exists = true;
?>
<tr>
<th><?php echo htmlspecialchars($idea["idea"], ENT_QUOTES, 'UTF-8'); ?></th>
</tr>
<?php
}
if(!$exists) {
?>
<tr>
<th>Engar hugmyndir enn :(</th>
</tr>
<?php
}
?>
</table>
</div>
<?php
foreach($errors as $message) {
?>
<div class="error-box">
<img src="/assets/icons/error-24px.svg"/>
<p><?php echo $message ?></p>
</div>
<?php
}
?>
<div class="idea">
<p>Sentu inn hugmynd</p>
<form method="POST" action="/vote.php?id=<?php echo $_GET["id"] ?>">
<input type="text" name="idea" placeholder="Hugmynd">
<input type="submit" name="submit" placeholder="Senda inn">
</form>
</div>
<?php
} else if($state === state::VOTING) {
if(checkIfVoted($_GET["id"], $conn)) {
header("Location: /");
}
$ideas = getIdeas($_GET["id"], $conn);
?>
<div>
<a href="/vote.php" class="back">Til baka</a>
<h1 class="name"><?php echo $user["name"] ?></h1>
<div class="list_of_voters">
<form id="score-form" method="POST" action="/vote.php?id=<?php echo $_GET["id"] ?>">
<table>
<tr>
<th>Hugmyndir:</th>
</tr>
<?php
$exists = false;
$length = count($ideas);
foreach($ideas as $idea) {
$exists = true;
?>
<tr>
<th>
<select class="score-option" name="<?php echo $idea["id"]; ?>">
<option value="0"></option>
<?php
for($i = 1; $i <= $length && $i <= 3; $i++) {
echo '<option value="' . $i . '">' . $i .'</option>';
}
?>
</select>
<?php echo htmlspecialchars($idea["idea"], ENT_QUOTES, 'UTF-8'); ?>
</th>
</tr>
<?php
}
if(!$exists) {
?>
<tr>
<th>Engar hugmyndir enn :(</th>
</tr>
<?php
}
?>
</table>
</form>
</div>
<?php
foreach($errors as $message) {
?>
<div class="error-box">
<img src="/assets/icons/error-24px.svg"/>
<p><?php echo $message ?></p>
</div>
<?php
}
?>
<div class="idea">
<p>Senda inn atkvæði</p>
<button form="score-form" type="submit" name="submit" placeholder="Senda inn">Submit</button>
<p>ATH: Veldu nr. 1 sem þér finnst best.</p>
</div>
<?php
}
}
?>
<script src="/js/main.js"></script>
</body>
</html>
<?php
$conn->close();
?>