-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
160 lines (139 loc) · 6.32 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
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
<?PHP
session_start();
if(isset($_SESSION['disableQuizPage']))
unset($_SESSION['disableQuizPage']);
?>
<html>
<head>
<title> IKwizU </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Tajawal" rel="stylesheet">
<link href="CSS/styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<header>
<nav class="navBar">
<nav class="menuwrapper">
<div class="logo">IKwizU</div>
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" class="label-toggle"></label>
<ul>
<li><a href="about/" >About</a></li>
<li><a href="standings/" >Check Standings </a></li>
<li><a href="analytics/">Analyze</a></li>
<li><a href="feedback/">Feedback</a></li>
</ul>
</nav>
</nav>
</header>
<div id="main">
<br/><br/>
<span class="mainHeading">Welcome to the TRIVIA Quiz by IKwizU.</span><br/><br/>
Here you can select the category the questions that would be based on, and the level of difficulty and type of questions as well.<br/>
Once submitting the inputs, you have an option to take the quiz to test yourself and see where are at in your fav genre.<br/><br/>
Once you take the quiz, you can challenge your friends by sharing the same quiz to them via Facebook, Twitter, and other platforms, and see if they can beat you.<br/><br/>
<div class="formBox">
<form action="" method="post" name="inputsForm" onSubmit="return quizInpValidation();">
Name:<br/>
<input name="inpName" type="text" value="" placeholder="Enter your name here.." class = "inpText" style="background-color:#FFFFF" required/> <br/><br/>
Email:<br/>
<input name="inpEmail" type="email" value="" placeholder="Enter your email here.." class ="inpText" style="background-color:#FFFFF" required/><br/><br/>
Select Category:<br/>
<select id="inpCategory" name="inpCategory" class="inpDD">
<option value="any">Any category</option>
<?PHP
$catFetch = file_get_contents("https://opentdb.com/api_category.php", true);
$categoriesJson = json_decode($catFetch, true); // decode the JSON feed
foreach ($categoriesJson as $categories) {
if(is_array($categories) || is_object($categories)){
foreach($categories as $category){
print "<option value='".$category['id']."'>".$category['name']."</option>";
}
}
}
?>
</select><br/><br/>
Select Difficulty:<br/>
<select id="inpDifficulty" name="inpDifficulty" class="inpDD">
<option value="any">Any Difficulty</option>
<option value="easy">Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</select><br/><br/>
Select Type:<br/>
<select id="inpQtype" name="inpQtype" class="inpDD">
<option value="any">Any Type</option>
<option value="multiple">Multiple Choice</option>
<option value="boolean">True / False</option>
</select><br/><br/><br/>
<button class="formButton" name="proceed" type="submit"><i class="fa fa-angle-double-right"></i> Click here to proceed</button>
<div class="error" id="errContainer"></div>
</form>
</div>
</div>
<?PHP
require "db.inc";
if(isset($_POST["proceed"])){
$inpCategory = $_POST["inpCategory"];
$inpDifficulty = $_POST["inpDifficulty"];
$inpQtype = $_POST["inpQtype"];
$inpName = mysqli_real_escape_string($connection, $_POST["inpName"]);
$inpEmail = mysqli_real_escape_string($connection, $_POST["inpEmail"]);
$url = 'https://opentdb.com/api.php?amount=15'; //Set it to 2 for testing flexibility, Change this back to 15
if($inpCategory == 30)
$url = 'https://opentdb.com/api.php?amount=15';
else{
if($inpCategory != "any")
$url.= '&category='.$inpCategory;
if($inpDifficulty != "any")
$url.= '&difficulty='.$inpDifficulty;
if($inpQtype != "any")
$url.= '&type='.$inpQtype;
}
$data = file_get_contents($url, true); // put the contents of the file into a variable
$apiQuizDetails = json_decode($data, true); // decode the JSON feed
$i = 0;
$quesArr = [];
$corrAns = [];
if (!($connection = @ mysqli_connect("localhost", $username, $password)))//Connecting to localhost
die("Could not connect to database");
if (!mysqli_select_db($connection, $databaseName)) //connecting to Database using "db.inc"
showerror($connection);
$nextRecId_row = mysqli_query($connection, "select max(id)+1 from tokens");
$nextRecId = mysqli_fetch_row($nextRecId_row); //Fetching the next token value, this acts as a sequence
$insToken = "insert into tokens(id, name, email_id) values({$nextRecId[0]}, '{$inpName}', '{$inpEmail}');";
$_SESSION['currToken'] = $nextRecId[0];
$_SESSION['inpName'] = $inpName;
$_SESSION['inpEmail'] = $inpEmail;
if(!@mysqli_query ($connection, $insToken)){
print '<br><b style="color:red">Exception:</b> '; //Exception raised if the token insertion fails.
throw new Exception(showerror($connection));
} else {
foreach ($apiQuizDetails as $quizDetails) {
if(is_array($quizDetails) || is_object($quizDetails)){
foreach($quizDetails as $quesDetails){
$i += 1;
$choicesArr = $quesDetails['incorrect_answers'];
array_push($choicesArr, $quesDetails['correct_answer']);
shuffle($choicesArr); //This shuffles the choices. Same will be used when other users try to take test using this token
if($quesDetails['type'] == "multiple")
$insQues = "INSERT INTO quizbytoken (token, qNo, question, quesType, optA, optB, optC, optD, optKey) VALUES ({$nextRecId[0]}, $i, '{$quesDetails['question']}', '{$quesDetails['type']}' ,'{$choicesArr[0]}', '{$choicesArr[1]}', '{$choicesArr[2]}', '{$choicesArr[3]}', '{$quesDetails['correct_answer']}')";
else
$insQues = "INSERT INTO quizbytoken (token, qNo, question, quesType, optA, optB, optKey) VALUES ({$nextRecId[0]}, $i, '{$quesDetails['question']}', '{$quesDetails['type']}', '{$choicesArr[0]}', '{$choicesArr[1]}', '{$quesDetails['correct_answer']}')";
if(!@mysqli_query ($connection, $insQues)){
print '<br><b style="color:red">Exception:</b> '; //Exception raised if the token insertion fails.
throw new Exception(showerror($connection));
}
}
}
}
}
if(isset($_SESSION['quizAttempted']))
unset($_SESSION['quizAttempted']);
header("Location: http://localhost/IKwizU/quizshere/");
exit;
}
?>
</body>
</html>