-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathupdate_question.php
267 lines (250 loc) · 14.9 KB
/
update_question.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
<!--Body Starts Here-->
<?php
if(isset($_GET['id']))
{
$question_id=$_GET['id'];
$tbl_name='tbl_question';
$where="question_id=$question_id";
$query=$obj->select_data($tbl_name,$where);
$res=$obj->execute_query($conn,$query);
$count_rows=$obj->num_rows($res);
if($count_rows==1)
{
$row=$obj->fetch_data($res);
$question=$row['question'];
$first_answer=$row['first_answer'];
$second_answer=$row['second_answer'];
$third_answer=$row['third_answer'];
$fourth_answer=$row['fourth_answer'];
$fifth_answer=$row['fifth_answer'];
$answer=$row['answer'];
$reason=$row['reason'];
$marks=$row['marks'];
$category=$row['category'];
$faculty_db=$row['faculty'];
$is_active=$row['is_active'];
$previous_image=$row['image_name'];
}
else
{
header('location:'.SITEURL.'admin/index.php?page=questions');
}
}
else
{
header('location:'.SITEURL.'admin/index.php?page=questions');
}
?>
<div class="main">
<div class="content">
<div class="report">
<form method="post" action="" class="forms" enctype="multipart/form-data">
<h2>Update Question</h2>
<?php
if(isset($_SESSION['validation']))
{
echo $_SESSION['validation'];
unset($_SESSION['validation']);
}
if(isset($_SESSION['update']))
{
echo $_SESSION['update'];
unset($_SESSION['update']);
}
?>
<span class="name">Question</span>
<textarea name="question" required="true"><?php echo $question; ?></textarea> <br />
<script>
CKEDITOR.replace( 'question' );
</script>
<?php
if($previous_image!="")
{
?>
<span class="name">Previous Image</span>
<img src="<?php echo SITEURL; ?>images/questions/<?php echo $previous_image; ?>" /> <br />
<?php
}
?>
<input type="hidden" name="previous_image" value="<?php echo $previous_image; ?>" />
<span class="name">New Image</span>
<input type="file" name="image" /><br />
<span class="name">First Answer</span>
<input type="text" name="first_answer" value="<?php echo $first_answer;; ?>" required="true" /><br />
<span class="name">Second Answer</span>
<input type="text" name="second_answer" value="<?php echo $second_answer; ?>" required="true" /><br />
<span class="name">Third Answer</span>
<input type="text" name="third_answer" value="<?php echo $third_answer; ?>" required="true" /><br />
<span class="name">Fourth Answer</span>
<input type="text" name="fourth_answer" value="<?php echo $fourth_answer; ?>" required="true" /><br />
<span class="name">Fifth Answer</span>
<input type="text" name="fifth_answer" value="<?php echo $fifth_answer; ?>" required="true" /><br />
<span class="name">Answer</span>
<select name="answer">
<option <?php if($answer==1){echo "selected='seleccted'";} ?> value="1">First Answer</option>
<option <?php if($answer==2){echo "selected='seleccted'";} ?> value="2">Second Answer</option>
<option <?php if($answer==3){echo "selected='seleccted'";} ?> value="3">Third Answer</option>
<option <?php if($answer==4){echo "selected='seleccted'";} ?> value="4">Fourth Answer</option>
<option <?php if($answer==5){echo "selected='seleccted'";} ?> value="5">Fifth Answer</option>
</select>
<br />
<span class="name">Reason</span>
<textarea name="reason" ><?php echo $reason; ?></textarea>
<br />
<span class="name">Marks</span>
<input type="text" name="marks" value="<?php echo $marks; ?>" />
<br />
<span class="name">Category</span>
<select name="category">
<option <?php if($category=="English"){echo "selected='seleccted'";} ?> value="English">English</option>
<option <?php if($category=="Math"){echo "selected='seleccted'";} ?> value="Math">Math</option>
</select>
<br />
<span class="name">Faculty</span>
<select name="faculty">
<?php
//Get Faculties from database
$tbl_name="tbl_faculty";
$query=$obj->select_data($tbl_name);
$res=$obj->execute_query($conn,$query);
$count_rows=$obj->num_rows($res);
if($count_rows>0)
{
while($row=$obj->fetch_data($res))
{
$faculty_id=$row['faculty_id'];
$faculty_name=$row['faculty_name'];
?>
<option <?php if($faculty_db==$faculty_id){echo"selected='selected'";} ?> value="<?php echo $faculty_id; ?>"><?php echo $faculty_name; ?></option>
<?php
}
}
else
{
?>
<option value="0">Uncategorized</option>
<?php
}
?>
</select>
<br />
<span class="name">Is Active?</span>
<input <?php if($is_active=='yes'){echo "checked='checked'";} ?> type="radio" name="is_active" value="yes" /> Yes
<input <?php if($is_active=='no'){echo "checked='checked'";} ?> type="radio" name="is_active" value="no" /> No
<br />
<input type="submit" name="submit" value="Update Question" class="btn-update" style="margin-left: 15%;" />
<a href="<?php echo SITEURL; ?>admin/index.php?page=questions"><button type="button" class="btn-delete">Cancel</button></a>
</form>
<?php
if(isset($_POST['submit']))
{
//echo "Clicked";
$question=$obj->sanitize($conn,$_POST['question']);
$first_answer=$obj->sanitize($conn,$_POST['first_answer']);
$second_answer=$obj->sanitize($conn,$_POST['second_answer']);
$third_answer=$obj->sanitize($conn,$_POST['third_answer']);
$fourth_answer=$obj->sanitize($conn,$_POST['fourth_answer']);
$fifth_answer=$obj->sanitize($conn,$_POST['fifth_answer']);
$answer=$obj->sanitize($conn,$_POST['answer']);
$reason=$obj->sanitize($conn,$_POST['reason']);
$marks=$obj->sanitize($conn,$_POST['marks']);
$category=$obj->sanitize($conn,$_POST['category']);
$faculty=$obj->sanitize($conn,$_POST['faculty']);
$previous_image=$_POST['previous_image'];
if(isset($_POST['is_active']))
{
$is_active=$_POST['is_active'];
}
else
{
$is_active="yes";
}
$updated_date=date('Y-m-d');
//Managing Question Images
if($_FILES['image']['name']!="")
{
//echo "Book Cover is Available";
//Getting File Extension
$ext=end(explode('.',$_FILES['image']['name']));
//Checking if the file type is valid or not
$valid_file=$obj->check_image_type($ext);
if($valid_file==false)
{
$_SESSION['invalid']="<div class='error'>Invalid Image type. Please use JPG or PNG or GIF file type.</div>";
header('location:'.SITEURL.'admin/index.php?page=update_question&id='.$question_id);
die();
}
//Removing Previous Image
if($previous_image!="")
{
$path="../images/questions/".$previous_image;
$remove=$obj->remove_file($path);
if($remove==false)
{
$_SESSION['remove_book']="Failed to remove previous Image. Try again.";
header('location:'.SITEURL.'admin/index.php?page=update_question&id='.$question_id);
die();
}
}
//Uploading if the file is valid
//first changing image name
$new_name='Beyond_Boundaries_Question_'.$obj->uniqid();
$image_name=$new_name.'.'.$ext;
//Adding Watermark to the image fie too
$source=$_FILES['image']['tmp_name'];
$destination="../images/questions/".$image_name;
$upload=$obj->upload_file($source,$destination);
if($upload==false)
{
$_SESSION['upload']="<div class='error'>Failed to upload question image. Try again.</div>";
header('location:'.SITEURL.'admin/index.php?page=update_question&id='.$question_id);
die();
}
}
else
{
$image_name=$previous_image;
}
//Normal PHP Validation
if(($question==null)or($first_answer==null)or($second_answer==null)or($third_answer==null)or($fourth_answer==null)or($answer==null))
{
$_SESSION['validation']="<div class='error'>Either Question or One of the Answers field is empty.</div>";
header('location:'.SITEURL.'admin/index.php?page=update_question&id='.$question_id);
}
//Updating Question
$tbl_name="tbl_question";
$data="
question='$question',
first_answer='$first_answer',
second_answer='$second_answer',
third_answer='$third_answer',
fourth_answer='$fourth_answer',
fifth_answer='$fifth_answer',
answer='$answer',
reason='$reason',
marks='$marks',
category='$category',
faculty='$faculty',
is_active='$is_active',
updated_date='$updated_date',
image_name='$image_name'
";
$where="question_id='$question_id'";
$query=$obj->update_data($tbl_name,$data,$where);
$res=$obj->execute_query($conn,$query);
if($res===true)
{
$_SESSION['update']="<div class='success'>Question successfully updated.</div>";
header('location:'.SITEURL.'admin/index.php?page=questions');
}
else
{
$_SESSION['update']="<div class='error'>Failed to update question.</div>";
header('location:'.SITEURL.'admin/index.php?page=update_question&id='.$question_id);
}
}
?>
</div>
</div>
</div>
<!--Body Ends Here-->