-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
291 lines (283 loc) · 10.6 KB
/
User.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
280
281
282
283
284
285
286
287
288
289
290
291
<?php
class User{
public $firstName;
public $lastName;
public $userName;
public $gender;
public $role;
function __construct($firstName1, $lastName1 , $userName1 , $gender1 , $role1) {
$this->firstName=$firstName1;
$this->lastName=$lastName1;
$this->userName=$userName1;
$this->gender=$gender1;
$this->role=$role1;
}
public function getFirstName() {
return $this->firstName;
}
public function getLastName() {
return $this->lastName;
}
public function getUserName() {
return $this->userName;
}
public function getGender() {
return $this->gender;
}
public function getRole() {
return $this->role;
}
}
class Applicant extends User{
private $presentLimit;
private $grantMoneyLeft;
private $numberOfGrantsRequested;
function __construct($firstName1 , $lastName1 , $userName1 , $gender1 , $role1 , $presentLimit1 , $grantMoneyLeft1 , $numberOfGrantsRequested1) {
parent::__construct($firstName1 , $lastName1 , $userName1 , $gender1 , $role1 );
$this->presentLimit = $presentLimit1;
$this->grantMoneyLeft = $grantMoneyLeft1;
$this->numberOfGrantsRequested = $numberOfGrantsRequested1;
}
public function getPresentLimit() {
return $this->presentLimit;
}
public function setPresentLimit($presentLimit1) {
$this->presentLimit = $presentLimit1;
}
public function getGrantMoneyLeft() {
return $this->grantMoneyLeft;
}
public function setGrantMoneyLeft($grantMoneyLeft1) {
$this->grantMoneyLeft = $grantMoneyLeft1;
}
public function getNumberOfGrantsRequested() {
return $this->numberOfGrantsRequested;
}
public function setNumberOfGrantsRequested($factor) {
$this->numberOfGrantsRequested = $this->numberOfGrantsRequested+$factor;
//return $this->numberOfGrantsRequested;
}
public function viewGrants($status) {
require_once('dbConnect.php');
$userName=$this->getUserName();
$sql = "SELECT * FROM grantdata WHERE username = '$userName' AND grantStatus = '$status'";
$result = $_SESSION['databaseObj']->retrieveQuery($con,$sql);
return $result;
}
public function requestGrant($grantType , $grantMoney , $size){
require_once('dbConnect.php');
session_start();
date_default_timezone_set('Asia/Kolkata');
$time = date('H:i d/m/Y');
$userName = $this->getUserName();
$grantId = $this->getUserName();
$this->setNumberOfGrantsRequested(1);
$len = strlen($this->getNumberOfGrantsRequested());
for( $i = 0; $i<4-$len; $i++ ) {
$grantId .= "0";
}
$grantId .= $this->getNumberOfGrantsRequested();
$sql = "INSERT INTO grantdata (grantId,userName,grantType,grantMoney,grantFileSize,grantStatus,requestTime) VALUES ('$grantId','$userName','$grantType','$grantMoney','$size','Pending','$time')";
if($_SESSION['databaseObj']->updateQuery($con,$sql)){
$this->setPresentLimit($this->getPresentLimit() - $grantMoney);
$grantLimit = $this->getPresentLimit();
$number = $this->getNumberOfGrantsRequested();
$sql1 = "UPDATE users SET presentLimit = '$grantLimit' , numberOfGrantsRequested = '$number' WHERE userName = '$userName' ";
if( $_SESSION['databaseObj']->updateQuery($con,$sql1) ){
$_SESSION['result'] = "Grant Successfully Requested Grant Id ".$grantId;
return $grantId;
}
else{
$this->setPresentLimit($this->getPresentLimit() + $grantMoney);
$_SESSION['result'] = "Sorry your grant is not Requested";
$this->setNumberOfGrantsRequested(-1);
$sql2 = "DELETE FROM grantdata WHERE grantId = '$grantId' ";
$success = $_SESSION['databaseObj']->updateQuery($con,$sql2);
return "Unsuccess";
}
}
else{
$_SESSION['result'] = "Sorry your grant is not Requested";
$this->setNumberOfGrantsRequested(-1);
return "Unsuccess";
}
mysqli_close($con);
}
public function deleteGrant($grantId){
require_once('dbConnect.php');
session_start();
$sql1 = "SELECT * FROM grantdata WHERE grantId='$grantId'";
$grant = $_SESSION['databaseObj']->retrieveQuery($con,$sql1);
if($grant->num_rows==1){
$rowGrant = $grant->fetch_assoc();
$grantMoney = $rowGrant['grantMoney'];
$userName = $this->getUserName();
$grantType = $rowGrant['grantType'];
$size = $rowGrant['grantFileSize'];
$time = $rowGrant['requestTime'];
$sql2 = "DELETE FROM grantdata WHERE grantId = '$grantId' ";
if($_SESSION['databaseObj']->updateQuery($con,$sql2)){
$this->setPresentLimit($this->getPresentLimit() + $grantMoney);
$grantLimit = $this->getPresentLimit();
$sql3 = "UPDATE users SET presentLimit = '$grantLimit' WHERE userName = '$userName' ";
if($_SESSION['databaseObj']->updateQuery($con,$sql3)){
$_SESSION['result'] = "Grant Successfully Deleted";
$fileName = "uploads/".$grantId.".pdf";
unlink($fileName);
return true;
}
else{
$this->setPresentLimit($this->getPresentLimit() + $grantMoney);
$sql4 = "INSERT INTO grantdata (grantId,userName,grantType,grantMoney,grantFileSize,grantStatus,requestTime) VALUES ('$grantId','$userName','$grantType','$grantMoney','$size','Pending','$time')";
$success = $_SESSION['databaseObj']->updateQuery($con,$sql4);
$_SESSION['result'] = " Grant not Deleted";
return false;
}
}
else{
$_SESSION['result'] = "Grant not deleted";
return false;
}
}
else{
$_SESSION['result'] = "Please Try Again";
return false;
}
}
}
class Admin extends User{
function __construct($firstName1 , $lastName1 , $userName1 , $gender1 , $role1) {
parent::__construct($firstName1 , $lastName1 , $userName1 , $gender1 , $role1 );
}
public function viewPendingGrants($pending){
require_once('dbConnect.php');
$sql = "SELECT * FROM grantdata WHERE grantStatus='Pending'";
$result = $_SESSION['databaseObj']->retrieveQuery($con,$sql);
mysqli_close($con);
return $result;
}
public function viewOldGrants(){
require_once('dbConnect.php');
$sql = "SELECT * FROM grantdata WHERE grantStatus!='Pending'";
$result = $_SESSION['databaseObj']->retrieveQuery($con,$sql);
mysqli_close($con);
return $result;
}
public function validateGrant($userName,$grantId,$result){
require_once('dbConnect.php');
$sql1 = "SELECT * FROM grantdata WHERE grantId='$grantId'";
$grant = $_SESSION['databaseObj']->retrieveQuery($con,$sql1);
$sql2 = "SELECT * FROM users WHERE userName='$userName'";
$user = $_SESSION['databaseObj']->retrieveQuery($con,$sql2);
if($grant->num_rows==1 and $user->num_rows==1){
$rowGrant = $grant->fetch_assoc();
$rowUser = $user->fetch_assoc();
if($result=="Approved"){
date_default_timezone_set('Asia/Kolkata');
$time = date('H:i d/m/Y');
$sql3 = "UPDATE grantdata SET grantStatus = '$result' , decisionTime = '$time' WHERE grantId='$grantId' ";
$updatedGrantMoneyLeft = $rowUser['grantMoneyLeft'] - $rowGrant['grantMoney'];
$sql4 = "UPDATE users SET grantMoneyLeft = '$updatedGrantMoneyLeft' WHERE userName='$userName' ";
if( $_SESSION['databaseObj']->updateQuery($con,$sql3) and $_SESSION['databaseObj']->updateQuery($con,$sql4) ){
$_SESSION['result'] = "Grant is approved successfully";
return true;
}
}
else{
date_default_timezone_set('Asia/Kolkata');
$time = date('H:i d/m/Y');
$sql3 = "UPDATE grantdata SET grantStatus = '$result' , decisionTime = '$time' WHERE grantId='$grantId' ";
$updatedPresentLimit = $rowUser['presentLimit'] + $rowGrant['grantMoney'];
$sql4 = "UPDATE users SET presentLimit = '$updatedPresentLimit' WHERE userName='$userName' ";
if( $_SESSION['databaseObj']->updateQuery($con,$sql3) and $_SESSION['databaseObj']->updateQuery($con,$sql4) ){
$_SESSION['result'] = "Grant is disapproved successfully";
return true;
}
else if(!($_SESSION['databaseObj']->updateQuery($con,$sql4)) and $_SESSION['databaseObj']->updateQuery($con,$sql3)){
$sql5 = "UPDATE grantdata SET grantStatus = 'Pending' , decisionTime = 'No Time' WHERE grantId='$grantId' ";
$success = $_SESSION['databaseObj']->updateQuery($con,$sql5);
$_SESSION['result'] = "Error occured";
return false;
}
else if(!($_SESSION['databaseObj']->updateQuery($con,$sql3)) and $_SESSION['databaseObj']->updateQuery($con,$sql4)){
$updatedPresentLimit = $rowUser['presentLimit'] - $rowGrant['grantMoney'];
$sql6 = "UPDATE users SET presentLimit = '$updatedPresentLimit' WHERE userName='$userName' ";
$success = $_SESSION['databaseObj']->updateQuery($con,$sql6);
$_SESSION['result'] = "Error occured";
return false;
}
else{
$_SESSION['result'] = "Error occured";
return false;
}
}
}
else{
$_SESSION['result'] = "Error occured";
return false;
}
}
public function removeApplicant($userName){
if($userName=="admin"){
$_SESSION['result'] = "You can't remove yourself";
}
else{
require_once('dbConnect.php');
$sql1 = "SELECT * FROM users WHERE userName='$userName'";
$result1 = $_SESSION['databaseObj']->retrieveQuery($con,$sql1);
//echo $result1->num_rows;
if($result1->num_rows==1){
$sql2 = "SELECT * FROM grantdata WHERE userName='$userName'";
$result2 = $_SESSION['databaseObj']->retrieveQuery($con,$sql2);
while($rows = $result2->fetch_assoc()){
$fileName = "uploads/".$rows['grantId'].".pdf";
unlink($fileName);
}
$sql3 = " DELETE FROM users WHERE userName='$userName' ";
$sql4 = " DELETE FROM grantdata WHERE userName='$userName' ";
if($_SESSION['databaseObj']->updateQuery($con,$sql3) && $_SESSION['databaseObj']->updateQuery($con,$sql4)){
$_SESSION['result'] = "User Successfully removed";
return true;
}
else{
$_SESSION['result'] .= "User is not removed";
return false;
}
}
else{
$_SESSION['result'] = "User does not exist";
return false;
}
mysqli_close($con);
}
}
public function giveGrantMoney($grantId){
require_once('dbConnect.php');
$sql1 = "SELECT * FROM grantdata WHERE grantId='$grantId'";
$result1 = $_SESSION['databaseObj']->retrieveQuery($con,$sql1);
if($result1->num_rows==1){
$grant = $result1->fetch_assoc();
if($grant['moneyGiven']==0 && $grant['grantStatus']=="Approved"){
$sql2 = "UPDATE grantdata SET moneyGiven = '1' WHERE grantId='$grantId' ";
if($_SESSION['databaseObj']->updateQuery($con,$sql2)){
$_SESSION['result'] = "Success! Please give money to applicant";
return true;
}
}
else if($grant['moneyGiven']!=0 ) {
$_SESSION['result'] = "Grant Money is given before";
return false;
}
else{
$_SESSION['result'] = "Grant is not approved";
return false;
}
}
else{
$_SESSION['result'] = "Grant does not exists";
return false;
}
mysqli_close($con);
}
}
?>