-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question.php
394 lines (346 loc) · 12.8 KB
/
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
include_once 'Classes/Connection.php';
include_once 'functions.php';
/**
* Used to manage questions in the system
*
* @author Alhassan Kamil
*/
class Question {
private $id, $question, $description, $asked_by, $tags = array(),$tag, $date_;
private $upvotes, $downvotes;
function __construct($q=null){
$this->question = $q;
if($this->select()){
$qry = $this->select();
$row = $qry->fetch_object();
$this->id = $row->id;
$this->description = $row->description;
$this->asked_by = $row->asked_by;
$this->tags[] = $row->tags;
$this->date_ = $row->added_on;
$this->upvotes = $row->upvotes;
$this->downvotes = $row->downvotes;
}else{
$this->select();
}
}
function set_question($q){
$this->question = $q;
}
function set_asker($asker){
$this->asked_by = $asker;
}
function set_tags(...$tags){
foreach($tags as $val=>$tag)
$this->tags[$val] = $tag;
}
function set_date($d){
$this->date_ = $d;
}
function set_id($id){
$this->id = $id;
}
/**
* Returns the id number of the current question
* @return integer The id of this question
*/
function get_id(){
$conn = get_connection_handle();
$res = $conn->query("select id from questions where question = "
. "'$this->question'");
if($res && $res->num_rows > 0){
$row = $res->fetch_object();
$this->id = $row->id;
return $row->id;
}
}
function get_question(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select question from questions where id = $id");
if($res && $res->num_rows > 0){
$row = $res->fetch_object();
$this->question = $row->question;
return $row->question;
}
return false;
}
/**
* Gets the tags of the current question. This returns the tags as a string
* array
* @return array All tags of this question
*/
function get_tags(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select tags from questions"
. " where id = '$id'");
if($res && $res->num_rows > 0){
$rows = fetch_item($res);
return $tags = explode(",", $rows->tags);
}
return false;
}
function get_description(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select description from questions"
. " where id = '$id'");
if($res && $res->num_rows > 0){
$desc = fetch_item($res);
return $desc->description;
}
return false;
}
/**
* Returns the date the current question was asked
* @return string The date this question was assked
*/
function get_date(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select date from questions where id = '$id'");
if($res && $res->num_rows > 0){
$date_ = fetch_item($res);
return $date_;
}
}
/**
* Gets the comments of the current question
* @return mixed An associative array of a comment and its other details
*/
function get_comments(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select comment, user, commented_on from comments"
. " where question_id = '$id'");
if($res && $res->num_rows > 0){
$comment = array();//initialise an associative array comment
while ($rows = $res->fetch_object()){
//assign values to $comment array
$comment['comment'] = $rows->comment;
$comment['user'] = $rows->user;
$comment['commented_on'] = $rows->commented_on;
}
return $comment;
}
return false;
}
/**
* Returns the number of page visits of the current question
* @return integer
*/
function get_views(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select sum(view) from views"
. " where type = questions and type_id = '$id'");
if($res && $res->num_rows > 0){
$count = fetch_item($res);
return $count;
}
return 0;
}
/**
*
* @return integer The total votes for the current question
*/
function get_upvotes(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select sum(votes) as total from votes where
type = 'question' and type_id = '$id' and vote_type = 'upvote'");
if($res && $res->num_rows > 0){
$count = $res->fetch_object();
return $count->total;
}
return 0;
}
function get_downvotes(){
$conn = get_connection_handle();
$id = $this->get_id();
$res = $conn->query("select sum(votes) as total from votes where
type = 'question' and type_id = '$id' and vote_type = 'downvote'");
if($res && $res->num_rows > 0){
$count = $res->fetch_object();
return $count->total;
}
return 0;
}
/**
* Saves a question details into the database. Note that this depends on the
* constructor to store a question. Thus, the class must be fully initialized
* with non-null values before save() is called. Failure to initialize the
* constructor will insert null values in some/all fields.
* @return mixed Returns the number of rows affected by query fi successful,
* False otherwise
*/
function save(){
$conn = get_connection_handle();
$stmt = $conn->prepare("insert into questions(question, description,
asked_by, tags, upvotes, downvotes, added_on) values(?,?,?,?,?,?,?)");
$stmt->bind_param("ssssiis", $this->question,$this->description,
$this->asked_by,$this->tags,$this->upvotes,$this->downvotes,
$this->date_);
$stmt->execute();
if($conn->affected_rows == 1){
return $conn->affected_rows;
}
return FALSE;
}
public function insert_votes($user,int $upvote = null, int $downvote = null){
$conn = get_connection_handle();
$qry = $conn->query("select id from votes where type = 'question'
and username = '$user' and type_id = $this->id");
if($qry->num_rows == 0){
if(!is_null($upvote) || !empty($upvote)){
$query = $conn->query("insert into votes(type_id,username,votes,type,
vote_type, added_on) values($this->id,'$user',$upvote,'question',
'upvote',CURRENT_DATE)");
if($conn->affected_rows == 1){
echo $conn->affected_rows;
}else{
echo $conn->error;
}
}
if(!is_null($downvote) || !empty($downvote)){
$query = $conn->query("insert into votes(type_id,username,votes,type,
vote_type, added_on) values($this->id,'$user',$downvote,'question',
'downvote',CURRENT_DATE)");
if($conn->affected_rows == 1){
echo $conn->affected_rows;
}else{
echo $conn->error;
}
}
}
}
/**
* Updates this question with a new question @param $question.
* @param string $question The new question to update to.
* @param string $tags The new tags you want to assign to the question
* @return mixed If both @param $question and @param $tags are null, exits
* the method. If at least one @param is not null, updates and returns the
* number of rows affected. Returns FALSE if update is not successful.
*/
function update($question=null, $tags=null, $desc=null){
$conn = get_connection_handle();
$id = $this->get_id();
$first = is_first($question, $desc, $tags);
$second = is_second($question,$desc,$tags);
$third = is_third($question, $desc, $tags);
if($first && $second && $third){
$stmt = $conn->prepare("update questions set question = ?, description = ?,
tags = ? where id = ?");
$stmt->bind_param("ssi",$question, $desc, $tags,$id);
}elseif($first && $second){
$stmt = $conn->prepare("update questions set question = ?, description = ?
where id = ?");
$stmt->bind_param("ssi",$question, $desc, $id);
}elseif($second && $third){
$stmt = $conn->prepare("update questions set description = ?, tags = ?
where id = ?");
$stmt->bind_param("ssi", $desc, $tags,$id);
}elseif($first && $third){
$stmt = $conn->prepare("update questions set question = ?, tags = ?
where id = ?");
$stmt->bind_param("ssi",$question,$tags,$id);
}elseif($first){
$stmt = $conn->prepare("update questions set question = ? where id = ?");
$stmt->bind_param("si", $question, $id);
}elseif($second){
$stmt = $conn->prepare("update questions set description = ? where id = ?");
$stmt->bind_param("si", $desc, $id);
}elseif($third){
$stmt = $conn->prepare("update questions set tags = ? where id = ?");
$stmt->bind_param("si", $tags, $id);
}
$stmt->execute();
if($conn->affected_rows == 1){
return $conn->affected_rows;
}
return FALSE;
}
function select($date_=null,$tag=null){
$conn = get_connection_handle();
$q = $this->question;
if(are_not_null($q,$tag, $date_)){
$query = $conn->query("select * from questions
where question = '$q' and tags LIKE '%$tag%' and
added_on = '$date_' order by added_on LIMIT 25");
}elseif(is_first($q, $tag,$date_)){
$query = $conn->query("select * from questions where
question = '$q' order by added_on LIMIT 25");
}elseif(is_second($q, $tag,$date_)){
$query = $conn->query("select * from questions where
tags like '%$tag%' order by added_on LIMIT 25");
}elseif(is_third($q, $tag,$date_)){
$query = $conn->query("select * from questions where
added_on = '$date_' order by added_on LIMIT 25");
}else{
$query = $conn->query("select * from questions order by added_on LIMIT 25");
}
if($query && $query->num_rows > 0){
return $query;
}
return false;
}
function get_all($limit=null){
$conn = get_connection_handle();
if(!is_null($limit)){
$query = $conn->query("select * from questions order by added_on DESC LIMIT $limit");
}else{
$query = $conn->query("select * from questions order by added_on DESC LIMIT 25");
}
if($query && $query->num_rows > 0){
return $query;
}
return false;
}
function get_all_by_tag($tag, $limit=null){
$conn = get_connection_handle();
if(!is_null($limit)){
$query = $conn->query("select distinct id, question from questions where
tags like '%$tag%' order by added_on DESC LIMIT $limit");
}else{
$query = $conn->query("select distinct id, question from questions where
tags like '%$tag%' order by added_on DESC LIMIT 25");
}
if($query && $query->num_rows > 0){
return $query;
}
return false;
}
function get_questions($limit=null){
$conn = get_connection_handle();
if(!is_null($limit)){
$query = $conn->query("select question from questions order by added_on DESC LIMIT $limit");
}else{
$query = $conn->query("select question from questions order by added_on DESC LIMIT 25");
}
if($query && $query->num_rows > 0){
return $query;
}
return false;
}
function get_answers(){
$conn = get_connection_handle();
$query = $conn->query("select * from answers where
question = '$this->question'");
if($query && $query->num_rows > 0){
return $query;
}
return false;
}
function get_num_answers(){
$conn = get_connection_handle();
$query = $conn->query("select count(id) as num from answers where
question = '$this->question'");
if($query && $query->num_rows > 0){
$count = $query->fetch_object();
return $count->num;
}
return 0;
}
}