-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.php
199 lines (155 loc) · 8.25 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
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
<?php
session_start();
require 'includes/dbh.inc.php';
define('TITLE', "Poll | Orbit");
if (!isset($_SESSION['userId'])) {
header("Location: login.php");
exit();
}
if (isset($_GET['poll'])) {
$pollid = $_GET['poll'];
} else {
header("Location: index.php");
exit();
}
include 'includes/HTML-head.php';
?>
</head>
<body>
<?php
include 'includes/navbar.php';
include 'includes/poll.class.php';
$poll = new Poll;
$pollData = $poll->getPolls($pollid);
?>
<div class="container">
<div class="row">
<div class="col-sm-3">
<?php include 'includes/profile-card.php'; ?>
</div>
<div class="col-sm-9" id="user-section">
<img class="event-cover" src="img/pollpage-cover.png">
<div class="px-5 my-5">
<div class="px-5">
<form action="" method="post" name="pollFrm">
<h1><?php echo $pollData['poll']['subject']; ?></h1>
<br>
<p class="text-muted"><?php echo $pollData['poll']['poll_desc']; ?></p>
<br><br>
<div class="funkyradio">
<?php
$sql2 = 'select v.id '
. 'from poll_votes v join polls p '
. 'on v.poll_id = p.id '
. 'where v.vote_by = ? '
. 'and v.poll_id = ? '
. 'and p.locked = 1;';
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql2);
mysqli_stmt_bind_param($stmt, "ss", $_SESSION['userId'], $pollid);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if (mysqli_stmt_num_rows($stmt) > 0) {
$voteCheck = false;
} else {
$voteCheck = true;
}
$sql2 = 'select poll_option_id from poll_votes where poll_id = ? and vote_by = ?';
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql2);
mysqli_stmt_bind_param($stmt, "ss", $pollid, $_SESSION['userId']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
$voted = $row['poll_option_id'];
foreach ($pollData['options'] as $opt) {
echo '<div class="funkyradio-info">
<input type="radio" name="voteOpt" id="option' . $opt['id'] . '"
value="' . $opt['id'] . '" ';
if ($opt['id'] == $voted) {
echo 'checked="checked" ';
}
if ($voteCheck === false) {
echo 'disabled="disabled" ';
}
echo '>
<label for="option' . $opt['id'] . '">' . $opt['name'] . '</label>
</div>';
}
echo '</div><br><br>';
if ($voteCheck) {
echo '<input type="submit" name="voteSubmit" class="btn btn-lg btn-primary"
value="Submit Vote">
<hr>';
}
if (!empty($voted)) {
// Poll Results
$pollResult = $poll->getResult($_GET['poll']);
echo '<h1>Results</h1>';
if ($pollResult && isset($pollResult['total_votes'])) {
echo '<b class="text-muted">Total Votes: </b>' . $pollResult['total_votes'] . '<br><br>';
echo '<div class="col-md-12 col-lg-8">';
if ($voteCheck && !empty($pollResult['options'])) {
$i = 0;
// Option bar color class array
$barColorArr = array('azure', 'emerald', 'violet', 'yellow', 'red');
// Generate option bars with votes count
foreach ($pollResult['options'] as $opt => $vote) {
// Check if the option exists in the array before accessing its value
if (array_key_exists($opt, $pollResult['options'])) {
// Calculate vote percent
$votePercent = round(($vote / $pollResult['total_votes']) * 100);
$votePercent = !empty($votePercent) ? $votePercent . '' : '0';
// Define bar color class
if (!array_key_exists($i, $barColorArr)) {
$i = 0;
}
$barColor = $barColorArr[$i];
?>
<div class="line-progress margin-b-20" data-prog-percent="<?php echo $votePercent / 100; ?>">
<div></div>
<p class="progress-title"><?php echo $opt . " - <b>" . $vote . ' user(s)'; ?></b></p>
</div><br>
<?php
$i++;
}
}
echo '</div>';
}
} else {
echo '<p>No results available for this poll.</p>';
}
echo '<br><br>
<a href="./poll-voters.php?poll=' . $_GET['poll'] . '" class="btn btn-secondary">View All Votes</a>
<a href="./poll-view.php" class="btn btn-secondary">View All Polls</a>';
}
?>
<input type="hidden" name="pollID" value="<?php echo $pollData['poll']['id']; ?>">
</form>
</div>
</div>
</div>
</div>
</div> <!-- /container -->
<?php
//if vote is submitted
if (isset($_POST['voteSubmit'])) {
$voteData = array(
'poll_id' => $_POST['pollID'],
'poll_option_id' => $_POST['voteOpt'],
'poll_vote_by' => $_SESSION['userId']
);
//insert vote data
$voteSubmit = $poll->vote($voteData);
//header("Location: ./poll.php?poll=".$pollid);
header("Refresh:0");
}
?>
<?php include 'includes/footer.php'; ?>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.waypoints.min.js"></script>
<script src="js/progressbar.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>