-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
86 lines (80 loc) · 2.31 KB
/
index.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
<?php
session_start();
if (isset($_POST['logout'])) {
session_destroy();
header("Location: index.php");
exit();
}
if (isset($_POST['login'])) {
header("Location: login.php");
exit();
}
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in']) {
echo $_SESSION['username'];
}
$jsonData = file_get_contents("polls.json");
$polls = json_decode($jsonData, true);
$activePoll = array();
$expiredPoll = array();
foreach ($polls as $poll) {
if (strtotime($poll['expiry']) > time()) {
$activePoll[] = $poll;
} else {
$expiredPoll[] = $poll;
}
}
usort($activePoll, function($a, $b) {
return ($b['id']) - ($a['id']);
});
usort($expiredPoll, function($a, $b) {
return ($b['id']) - ($a['id']);
});
?>
<!DOCTYPE html>
<html>
<head>
<title>Poll Application</title>
</head>
<body>
<?php
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in']) {
echo '<form method="post">';
echo '<input type="submit" name="logout" value="Logout">';
echo '</form>';
}
?>
<?php
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
echo '<form method="post">';
echo '<input type="submit" name="login" value="Login/Register">';
echo '</form>';
}
?>
<h1>Poll WEB APPLICATION</h1>
<p>A web application where logged-in users can cast their votes on polls (questionnaires/forms)</p>
<h2>Active Polls</h2>
<ul>
<?php foreach ($activePoll as $poll): ?>
<li>Poll #<?php echo $poll['id']; ?> - <?php echo $poll['question']; ?>
<?php echo $poll['startedAt']; ?> - <?php echo $poll['expiry']; ?>
<a href="vote.php?poll_id=<?php echo $poll['id']; ?>">Vote</a>
</li>
<?php endforeach; ?>
</ul>
<h2>Expired Polls</h2>
<ul>
<?php foreach ($expiredPoll as $poll): ?>
<li>Poll #<?php echo $poll['id']; ?> - <?php echo $poll['question']; ?>
<?php echo $poll['startedAt']; ?> - <?php echo $poll['expiry']; ?>
<a href="result.php?poll_id=<?php echo $poll['id']; ?>">Results</a>
</li>
<?php endforeach; ?>
</ul>
<?php
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'])
{
echo '<a href="make.php" class="create-poll-button">Create Poll</a>';
}
?>
</body>
</html>