-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreatepoll.php
223 lines (183 loc) · 4.75 KB
/
createpoll.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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Credentials: true');
error_reporting(0);
$_POST = json_decode(file_get_contents('php://input'), true);
/*******************
0. DOCUMENTATION
********************/
/*
Version: 1.0
Admin API: YES
Brief: To create a poll by the Teachers or Admin staff
Author: Shafeef
First Created: 19-09-2017
Last Modified: 05-12-2017 @Shafeef
*/
/**********************************
1.1 AUTHENTICATION STANDARD PART
***********************************/
//Encryption Credentials
define('SECURE_CHECK', true);
require 'secure.php';
//Encryption Validation
if(!isset($_POST['token'])){
$output = array(
"status" => false,
"error" => "Access Token Missing",
"errorCode" => 103,
"response" => ""
);
die(json_encode($output));
}
$token = $_POST['token'];
$decryptedtoken = openssl_decrypt($token, $encryptionMethod, $secretHash);
$tokenid = json_decode($decryptedtoken, true);
//Expiry Validation
date_default_timezone_set('Asia/Calcutta');
$dateStamp = date_create($tokenid['date']);
$today = date_create(date("Y-m-j"));
$interval = date_diff($dateStamp, $today);
$interval = $interval->format('%a');
if($interval > $tokenExpiryDays){
$output = array(
"status" => false,
"error" => "Login Expired",
"errorCode" => 401,
"response" => ""
);
die(json_encode($output));
}
/**********************************
1.2 AUTHENTICATION CUSTOM PART
***********************************/
//Check if the token is valid
if(!($tokenid['schoolCode'] == "")){
$schoolCode = $tokenid['schoolCode'];
$admin_mobile = $tokenid['mobile'];
$admin_role = $tokenid['role'];
}
else{
$output = array(
"status" => false,
"error" => "Invalid Token",
"errorCode" => 402,
"response" => ""
);
die(json_encode($output));
}
//Check if the user has permission to access this API
if($admin_role != "ADMIN" && $admin_role != "TEACHER"){
$output = array(
"status" => false,
"error" => "Access Restricted",
"errorCode" => 403,
"response" => ""
);
die(json_encode($output));
}
/**********************
2. PARAMETERS CHECK
***********************/
//Params passed or not
if(!isset($_POST['title'])){
$output = array(
"status" => false,
"error" => "title is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['brief'])){
$output = array(
"status" => false,
"error" => "brief is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['endDate'])){
$output = array(
"status" => false,
"error" => "endDate is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['pollContent'])){
$output = array(
"status" => false,
"error" => "Poll Content is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['target'])){
$output = array(
"status" => false,
"error" => "target is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
//REQUIRED PARAMETERS
date_default_timezone_set('Asia/Kolkata');
//assign the values to local varibles
$title=$_POST['title'];
$brief=$_POST['brief'];
$pollContent=$_POST['pollContent'];
$target=$_POST['target'];
$endDate=$_POST['endDate'];
$count = 0;
foreach ($pollContent as $key) {
$tmp[] = array(
"value" => $key,
"option" => $count,
"count" => ''
);
$count++;
}
$pollContent = json_encode($tmp);
/****************
3. MAIN LOGIC
*****************/
//3.1 CONNECTION TO CUSTOM DATABASE
define('INCLUDE_CHECK', true);
require 'connect_'.$schoolCode.'.php';
$targetCount = 0;
$temp = explode(", ", $target);
if($target == 0 || $target == ''){
$query = mysql_query("SELECT COUNT(`roll`) AS total FROM `d_studentsmasterlist` WHERE 1");
$row = mysql_fetch_assoc($query);
$targetCount = $row['total'];
}
else{
foreach ($temp as $key) {
$temp1 = explode("-", $key);
$class = $temp1[0];
$division = $temp1[1];
$query = mysql_query("SELECT COUNT(`roll`) AS total FROM `d_studentsmasterlist` WHERE `class`= '{$class}' && `division`='{$division}'");
$row = mysql_fetch_assoc($query);
$targetCount = $targetCount + $row['total'];
}
}
//3.2 MAIN OPERATION (INSERTION or DELETION or UPDATION)
mysql_query("INSERT INTO `d_polls` (`title`, `brief`, `pollContent`, `target`, `isActive`, `endDate`, `targetCount` ) VALUES ('{$title}','{$brief}','{$pollContent}','{$target}', 1, '{$endDate}', '{$targetCount}')");
$response = array(
"message" => "Poll was created sucessfully"
);
$output = array(
"status" => true,
"error" => "",
"errorCode" => "",
"response" => $response
);
echo json_encode($output);
?>