-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
279 lines (208 loc) · 11 KB
/
create.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
require('./templates/header.php');
// redirect signed out users to login page
if (!isset($_SESSION['user']))
{
header("location: login.php?flow=create");
}
$description_placeholder = "e.g. Think you know everything about technology? Take this quiz and prove it, hotshot.";
if (isset($_POST['initial_submit']))
{
$title = mysqli_escape_string($conn, $_POST['title']);
$description = mysqli_escape_string($conn, $_POST['description']);
$category = substr($_POST['category'], 7);
$category = mysqli_escape_string($conn, $category);
$timer = substr($_POST['timer'], 7)*60;
$title_terms = explode(' ', $title);
$sql = "SELECT * FROM quizzes WHERE title LIKE '%";
$sql .= implode("%' OR title LIKE '%", $title_terms)."%'";;
$result = mysqli_query($conn, $sql);
$arr_json = json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));
}
if (isset($_POST['post_submit']))
{
$id = uniqid();
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['category'];
$category = $category;
$timer = $_POST['timer'];
$created_by = $_SESSION['user'];
$sql = "INSERT INTO quizzes(id, title, description, category, timer, created_by) VALUES('$id', '$title', '$description', '$category', '$timer', '$created_by')";
if (mysqli_query($conn, $sql))
{
header("Location: create-qs.php?id=$id");
}
else
{
echo mysqli_error($conn);
}
}
?>
<div class="container bg-light rounded py-4 my-3" ng-controller="quizCreateController">
<div class="text-center text-primary">
<h1 class="display-3">Create Quiz</h1>
<h6 class="text-muted">Step 1/3</h6>
</div>
<?php if (!isset($_POST['initial_submit'])): ?>
<div class="row justify-content-center">
<div class="col-sm-10 col-lg-6 row justify-content-center">
<form action="create.php" method="POST" name="quizCreateForm" novalidate>
<!-- title field -->
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter a title for your quiz">
<i class="bi bi-card-heading"></i>
</span>
</span>
<div class="form-floating col-10">
<input type="text" ng-required="true" ng-pattern="titleReg" ng-model="quiz.title" name="title" id="title"
placeholder="Quiz title" class="form-control rounded-0 rounded-end">
<label for="title">Quiz title</label>
</div>
</div>
<!-- invalid title warning -->
<div class="text-center rounded-pill bg-danger text-light col-7 py-2 border-1 mt-1"
ng-show="quizCreateForm.title.$touched && quizCreateForm.title.$invalid">
Title must be 3-220 characters long and cannot contain double quotes
</div>
</div>
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter a short description for your quiz">
<i class="bi bi-text-paragraph"></i>
</span>
</span>
<div class="form-floating col-10">
<textarea class="form-control rounded-0 rounded-end" name="description"
ng-pattern="descriptionReg" ng-required="true" ng-model="quiz.description"
id="description" placeholder="A short description of your quiz"
style="height: 100px; resize: none">
</textarea>
<label for="description">A short description</label>
</div>
</div>
<div class="text-center rounded-pill bg-danger text-light col-9 py-2 border-1 mt-1"
ng-show="quizCreateForm.description.$touched && quizCreateForm.description.$invalid">
Description can be 3-500 characters long and cannot contain double quotes
</div>
</div>
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter the category of your quiz">
<i class="bi bi-tags"></i>
</span>
</span>
<div class="col-10">
<select name="category" id="category" class="form-select rounded-0 rounded-end" aria-label="Default select example"
ng-required="true" ng-model="quiz.category">
<option value="" selected disabled>Category</option>
<option ng-repeat="option in categories" ng-value="option">{{option}}</option>
</select>
</div>
</div>
</div>
<!-- timer field -->
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter an email that you can use to login">
<i class="bi bi-stopwatch"></i>
</span>
</span>
<div class="col-10">
<select name="timer" id="timer" ng-required="true" ng-model="quiz.timer" class="form-select rounded-0 rounded-end">
<option value="" disabled selected>Timer (Mins)</option>
<option ng-repeat = "option in timings" ng-value="option">{{option}}</option>
</select>
</div>
</div>
</div>
<div class="text-muted text-center small col-10">
Created quizzes remain private by default. You can make it public once you are done creating the quiz or anytime later.
</div>
<div class="my-3 text-center">
<input type="submit" class="btn btn-primary" name="initial_submit" value="Next" ng-disabled="quizCreateForm.$invalid">
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php if (isset($_POST['initial_submit'])): ?>
<div class="container row justify-content-center">
<div ng-show="similarQuizzesJson.length > 1" class="lead">
The following quizzes look similar to the one you want to create. Do you wish to proceed?
</div>
<div ng-show="similarQuizzesJson.length == 1" class="lead">
The following quiz looks similar to the one you want to create. Do you wish to proceed?
</div>
<small class="text-muted">This is done to prevent duplicate quizzes</small>
<div class="my-3">
<form action="create.php" method="POST">
<input type="hidden" name="title" value="<?php echo $title; ?>">
<input type="hidden" name="description" value="<?php echo $description; ?>">
<input type="hidden" name="category" value="<?php echo $category; ?>">
<input type="hidden" name="timer" value="<?php echo $timer; ?>">
<button class="btn btn-success" type="submit" id="postSubmitBtn" name="post_submit" value="submit">Proceed to create quiz</button>
</form>
</div>
<div>
<div ng-repeat = "quiz in similarQuizzesJson" class="card w-50 my-3">
<!-- card links to quiz page -->
<a href="quiz.php?id={{quiz.id}}" class="text-decoration-none text-reset">
<div class="card-body">
<!-- quiz title -->
<div class="card-title h5">
<span class="display-6">{{quiz.title}}</span>
</div>
<!-- quiz category -->
<div>
<p>Category: {{ quiz.category }}</p>
</div>
<!-- quiz description -->
<blockquote class="blockquote">
<p>{{ quiz.description }}</p>
<!-- question no and timer -->
<footer class="blockquote-footer mt-3" ng-show="quiz.timer/60 != 1"> {{quiz.question_nums}} questions, {{quiz.timer/60}} mins</footer>
<footer class="blockquote-footer mt-3" ng-hide="quiz.timer/60 != 1"> {{quiz.question_nums}} questions, {{quiz.timer/60}} min</footer>
</blockquote>
</div>
<!-- created username -->
<div class="card-footer text-muted">
Created by {{ quiz.created_by }}
</div>
</a>
</div>
</div>
</div>
<?php endif; ?>
</div>
<script>
catglueQuiz.controller('quizCreateController', function($scope){
$scope.titleReg = /^[^"]{3,220}$/;
$scope.descriptionReg = /^[^"]{3,500}$/;
$scope.questionStatementReg = /^[^"]{3,500}$/;
$scope.categories = ['Art and Literature', 'General Knowledge', 'Geography', 'Life and health', 'History', 'Politics', 'Music', 'Science and Nature', 'Sports',
'Technology', 'TV and Films', 'Others/Misc'];
$scope.timings = [1,3,5,10,20];
$scope.quiz = {category: $scope.categories[0], timer: $scope.timings[1]};
<?php if (isset($_POST['initial_submit'])): ?>
$scope.similarQuizzesJson = <?php echo $arr_json; ?>;
if ($scope.similarQuizzesJson.length == 0)
{
// simulate post form submit
document.getElementById('postSubmitBtn').click();
}
<?php endif; ?>
})
</script>
<?php
require('./templates/footer.php')
?>