-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfillform.php
149 lines (135 loc) · 4.64 KB
/
fillform.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
<?php
include("initial.php");
include("classes/input.php");
include("classes/buildform.php");
session_start();
if(isset($_GET['id'])){
$_SESSION['current_form'] = $_GET['id'];
//get form details from id in url
$stmt = $con->prepare("SELECT submissions_per_user,form_validity,date_created,status FROM form_list WHERE id = ?");
$stmt->bind_param('i',$_SESSION['current_form']);
$stmt->execute();
$row = $stmt->get_result();
$getMaxCount = $row->fetch_assoc();
//check if form is open
if($getMaxCount['status'] == 0){
die("form has been closed");
}
else {
$timeNow = time();
if($getMaxCount['form_validity'] != 0 && ($timeNow - (strtotime('+'.$getMaxCount['form_validity']." day", strtotime($getMaxCount['date_created'])))) > 0){
//if time over then close form
$stmt = $con->prepare("UPDATE form_list SET status='0' WHERE id=?");
$stmt->bind_param('i',$_GET['id']);
$stmt->execute();
$stmt->close();
die("Form has been closed");
}
}
}
//check if user logged in
if(isset($_SESSION['userLoggedIn'])){
$stmt = $con->prepare("SELECT count(*) as count FROM answers WHERE form_id = ? GROUP BY question_id,username HAVING username = ? LIMIT 1");
$stmt->bind_param('is',$_GET['id'],$_SESSION['userLoggedIn']);
$stmt->execute();
$getCount = $stmt->get_result();
$row = $getCount->fetch_assoc();
$currentCount = $row['count'];
$stmt = $con->prepare("SELECT submissions_per_user,form_validity,date_created,status FROM form_list WHERE id = ?");
$stmt->bind_param('i',$_SESSION['current_form']);
$stmt->execute();
$row = $stmt->get_result();
$getMaxCount = $row->fetch_assoc();
$maxCount = $getMaxCount['submissions_per_user'];
//check if user has exceeded max submissions or not
if($maxCount > 0 && $currentCount == $maxCount){
header("location: afterSubmit.php");
}
$stmt->close();
}
else {
$_SESSION['logInToFillForm'] = false;
}
//if filling form
if(isset($_GET['id'])){
$_SESSION['current_form'] = $_GET['id'];
$id = $_GET['id'];
$buildForm = new FormBuilder($id,"fillform.php",array('method' => 'POST','enctype' => 'multipart/form-data'));
}
//after submit click
else {
if(isset($_SESSION['form'])){
$buildForm = unserialize($_SESSION['form']);
$buildForm->validate();
}
else {
die("invalid url");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php if(isset($_SESSION['userLoggedIn'])){echo $buildForm->getTitle(); }?> -Forms</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<link rel="stylesheet" type="text/css" href="assets/stylesheets/fillform.css">
</head>
<body>
<nav class="navbar navbar-expand-xl navbar-light" id="navBar">
<ul class="navbar-nav ml-auto align-items-center">
<li class="nav-item">
<form method="POST" action="logout.php">
<button name="signOut" class="btn">SIGN OUT</button>
</form>
</li>
</ul>
</nav>
<!-- display form -->
<?php
echo $buildForm->buildForm();
$_SESSION['form'] = serialize($buildForm);
?>
<!--------->
<!-- modal-->
<div class="modal" id="modal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" aria-labelledby="ModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body">
<p>Please Login To Fill The Form</p>
</div>
<div class="modal-footer">
<button type="button" name = "loginToFill" onclick="location.href='register.php'" class="btn">Log In</button>
</div>
</div>
</div>
</div>
<div id="backdrop" class="hidden"></div>
<!-- modal trigger -->
<button type="button" class="hidden" id="showmodal" data-toggle="modal" data-target="#modal">
Launch demo modal
</button>
<!------>
<?php
//if user not logged in trigger modal
if(!isset($_SESSION['userLoggedIn'])){
echo '<script>
var hidden = document.querySelector(".hidden");
var modal = document.querySelector("#modal");
var showmodal = document.querySelector("#showmodal");
modal.style.display = "block";
document.querySelector("#backdrop").classList.remove("hidden");
showmodal.click();
showmodal.addEventListener("click",function(){
modal.classList.add("fade");
});
</script>';
$_SESSION['logInToFillForm'] = true;
$_SESSION['url'] = "http://localhost/form%20builder/fillform.php?&id=".$_GET['id'];
}
?>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>