Issues (67)

src/User/Functions.php (13 issues)

1
<?php
2
3
namespace Alfs18\User;
4
5
// use Psr\Container\ContainerInterface;
6
7
/**
8
 * Functions.
9
 */
10
class Functions
11
{
12
    /**
13
     * Get user information.
14
     *
15
     * @param string $acronym current user that's logged in.
16
     * @param object $di.
17
     *
18
     * @return array
19
     */
20
    public function getProfileInfo($acronym, $di, $table, $rows)
21
    {
22
        $db = $di->get("dbqb");
23
        $db->connect();
24
        // $user = $db->select("id, question, tags")
25
        //            ->from("Question")
26
        $user = $db->select($rows)
27
                   ->from($table)
28
                   ->where("acronym = ?")
29
                   ->execute([$acronym])
30
                   ->fetchAll();
31
32
        // var_dump($user);
33
        return $user;
34
    }
35
36
37
    /**
38
     * Get one post.
39
     *
40
     * @param object $di.
41
     * @param int $id       the id of the post.
42
     * @param string $table the table that contains the post.
43
     * @param string $rows  the rows to be fetch.
44
     *
45
     * @return array
46
     */
47
    public function getOnePost($di, $id, $table, $rows)
48
    {
49
        $db = $di->get("dbqb");
50
        $db->connect();
51
        $post = $db->select($rows)
52
                   ->from($table)
53
                   ->where("id = ?")
54
                   ->execute([$id])
55
                   ->fetchAll();
56
57
        // var_dump($post);
58
        return $post;
59
    }
60
61
62
    /**
63
     * Get one post.
64
     *
65
     * @param object $di.
66
     * @param int $id       the id of the post.
67
     * @param string $table the table that contains the post.
68
     * @param string $rows  the rows to be updated.
69
     * @param string $res   the updated text.
70
     *
71
     * @return array
72
     */
73
    public function updatePost($di, $id, $table, $rows, $res)
74
    {
75
        $db = $di->get("dbqb");
76
        $db->connect()
77
            ->update($table, [$rows])
78
            ->where("id = ?")
79
            ->execute([$res, $id]);
80
81
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
82
    }
83
84
85
    /**
86
     * Get one post.
87
     *
88
     * @param object $di.
89
     * @param int $id       the id of the post.
90
     * @param string $table the table that contains the post.
91
     *
92
     * @return array
93
     */
94
    public function deletePost($di, $id, $table)
95
    {
96
        $db = $di->get("dbqb");
97
        $db->connect();
98
        $sql = "DELETE FROM $table WHERE id = ?;";
99
        $db->execute($sql, [$id]);
100
        return "Deleted";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Deleted' returns the type string which is incompatible with the documented return type array.
Loading history...
101
    }
102
103
104
    /**
105
     * Get one post.
106
     *
107
     * @param object $di.
108
     * @param int $id       the id of the post.
109
     * @param string $table the table that contains the post.
110
     *
111
     * @return array
112
     */
113
    public function deleteCommentOrAnswer($di, $id, $table)
114
    {
115
        $db = $di->get("dbqb");
116
        $db->connect();
117
        $sql = "DELETE FROM $table WHERE questionId = ?;";
118
        $db->execute($sql, [$id]);
119
120
        return "Deleted";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Deleted' returns the type string which is incompatible with the documented return type array.
Loading history...
121
    }
122
123
124
    /**
125
     * Get one post.
126
     *
127
     * @param object $di.
128
     * @param int $id       the id of the post.
129
     * @param string $table the table that contains the post.
130
     *
131
     * @return array
132
     */
133
    public function deleteAnswerComments($di, $id)
134
    {
135
        $db = $di->get("dbqb");
136
        $db->connect();
137
        $sql = "DELETE FROM AnswerComments WHERE answerId = ?;";
138
        $db->execute($sql, [$id]);
139
140
        return "Deleted";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Deleted' returns the type string which is incompatible with the documented return type array.
Loading history...
141
    }
142
143
144
145
    /**
146
     * Get all users.
147
     *
148
     * @param object $di.
149
     *
150
     * @return array
151
     */
152
    public function getAllUsers($di)
153
    {
154
        $db = $di->get("dbqb");
155
        $db->connect();
156
        $users = $db->select("acronym")
157
                   ->from("User")
158
                   ->execute()
159
                   ->fetchAll();
160
161
        return $users;
162
    }
163
164
165
166
    /**
167
     * Get information about which users are the most active.
168
     *
169
     * @param object $di.
170
     * @param array $users. List of all users.
171
     *
172
     * @return array
173
     */
174
    public function getUserStatus($di, $users)
175
    {
176
        // get all answers made by $acronym
177
        $answers = [];
178
        foreach ($users as $acronym) {
179
            $db = $di->get("dbqb");
180
            $db->connect();
181
            $answers = $db->select("answer")
182
                        ->from("Answers")
183
                        ->where("acronym = ?")
184
                        ->execute([$acronym])
185
                        ->fetchAll();
186
        }
187
188
        return $answers;
189
    }
190
191
192
193
    /**
194
     * Get all questions.
195
     *
196
     * @param object $di.
197
     *
198
     * @return array
199
     */
200
    public function getAllQuestions($di)
201
    {
202
        $db = $di->get("dbqb");
203
        $db->connect();
204
205
        $questions = $db->select("*")
206
                        ->from("Question")
207
                        ->execute()
208
                        ->fetchAll();
209
210
        return $questions;
211
    }
212
213
214
    /**
215
     * Get some questions.
216
     *
217
     * @param object $di.
218
     *
219
     * @return array
220
     */
221
    public function getSomeQuestions($di, $tag)
222
    {
223
        $db = $di->get("dbqb");
224
        $db->connect();
225
        $questions = $db->select("*")
226
                        ->from("Question")
227
                        ->where("tags LIKE ?")
228
                        ->execute(["%$tag%"])
229
                        ->fetchAll();
230
231
        return $questions;
232
    }
233
234
235
    /**
236
     * Get all tags.
237
     *
238
     * @param object $di.
239
     *
240
     * @return array
241
     */
242
    public function getAllTags($di)
243
    {
244
        $db = $di->get("dbqb");
245
        $db->connect();
246
247
        $res = $db->select("tags")
248
                        ->from("Question")
249
                        ->execute()
250
                        ->fetchAll();
251
252
        // Add all tags to one string.
253
        $tagString = "";
254
        foreach ($res as $question) {
255
            $tagString .= "; " . $question->tags;
256
            // var_dump($tagString);
257
        }
258
259
        // Remove '; ' from the beginning of string.
260
        $tagString = trim($tagString, "; ");
261
262
        // Create an array.
263
        $tagsArray = explode("; ", $tagString);
264
        // var_dump($tagsArray);
265
266
        // // Make sure a tag only occur once in the array.
267
        // $tags = [];
268
        // foreach ($tagsArray as $val) {
269
        //     if (!in_array($val, $tags)) {
270
        //         array_push($tags, $val);
271
        //     }
272
        // }
273
274
        return $tagsArray;
275
    }
276
277
278
    /**
279
     * Get tags once. Makes sure a tag
280
     * in the array only occurs once.
281
     *
282
     * @param array $tagsArray     Array with tags.
283
     *
284
     * @return array
285
     */
286
    public function getTagsOnce($tagsArray)
287
    {
288
        $tags = [];
289
        foreach ($tagsArray as $val) {
290
            if (!in_array($val, $tags)) {
291
                array_push($tags, $val);
292
            }
293
        }
294
295
        return $tags;
296
    }
297
298
299
    /**
300
     * Count the amount of time a tag occur in the array.
301
     *
302
     * @param array $tagsArray     Array with tags.
303
     *
304
     * @return array
305
     */
306
    public function countTagsFrequency($tagsArray)
307
    {
308
        // var_dump($tagsArray);
309
        $tags = [];
310
        foreach ($tagsArray as $val) {
311
            if (!array_key_exists($val, $tags)) {
312
                $tags[$val] = 0;
313
            }
314
            $tags[$val] += 1;
315
        }
316
317
        return $tags;
318
    }
319
320
321
    /**
322
     * Change character to ÅÄÖ if needed.
323
     *
324
     * @param string $text     The string/text to be checked.
325
     *
326
     * @return array
327
     */
328
    public function changeCharacter($text)
329
    {
330
        $characters = [
331
            "/&Aring;/" => "Ã…",
332
            "/&aring;/" => "Ã¥",
333
            "/&Auml;/" => "Ä",
334
            "/&auml;/" => "ä",
335
            "/&Ouml;/" => "Ö",
336
            "/&ouml;/" => "ö"
337
        ];
338
339
        foreach ($characters as $key => $val) {
340
            $text = preg_replace($key, $val, $text);
341
        }
342
        return $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $text returns the type string which is incompatible with the documented return type array.
Loading history...
343
    }
344
345
346
    /**
347
     * Get some tags.
348
     *
349
     * @param object $di.
350
     *
351
     * @return array
352
     */
353
    public function getSomeQuestionsTags($di, $res)
354
    {
355
        $db = $di->get("dbqb");
356
        $db->connect();
357
        $questions = $db->select("id, acronym, question, tags")
358
                   ->from("Question")
359
                   ->where("tags LIKE ?")
360
                   ->execute(["%$res%"])
361
                   ->fetchAll();
362
363
        // var_dump($questions);
364
        return $questions;
365
    }
366
367
368
    /**
369
     * Get one question.
370
     *
371
     * @param object $di.
372
     *
373
     * @return array
374
     */
375
    public function getOneQuestion($di, $res)
376
    {
377
        $db = $di->get("dbqb");
378
        $db->connect();
379
        $questions = $db->select("*")
380
                   ->from("Question")
381
                   ->where("id = ?")
382
                   ->execute([$res])
383
                   ->fetchAll();
384
385
        // var_dump($questions);
386
        return $questions;
387
    }
388
389
390
    /**
391
     * Get question information.
392
     *
393
     * @param string $acronym current user that's logged in.
394
     * @param object $di.
395
     *
396
     * @return array
397
     */
398
    public function getQuestionComments($di, $questionId)
399
    {
400
        $db = $di->get("dbqb");
401
        $db->connect();
402
        $res = $db->select("*")
403
                   ->from("Comments")
404
                   ->where("questionId = ?")
405
                   ->execute([$questionId])
406
                   ->fetchAll();
407
408
        // var_dump($res);
409
        return $res;
410
    }
411
412
413
    /**
414
     * Get one question comment.
415
     *
416
     * @param object $di.
417
     * @param int $id Id of the comment.
418
     *
419
     * @return array
420
     */
421
    public function getOneQuestionComment($di, $id)
422
    {
423
        $db = $di->get("dbqb");
424
        $db->connect();
425
        $res = $db->select("*")
426
                   ->from("Comments")
427
                   ->where("id = ?")
428
                   ->execute([$id])
429
                   ->fetchAll();
430
431
        return $res;
432
    }
433
434
435
    /**
436
     * Get all comments connected to answer.
437
     *
438
     * @param int $answerId the answer the comment is connected to.
439
     * @param object $di.
440
     *
441
     * @return array
442
     */
443
    public function getAnswerComments($di, $answerId)
444
    {
445
        $db = $di->get("dbqb");
446
        $db->connect();
447
        $res = $db->select("*")
448
                   ->from("AnswerComments")
449
                   ->where("answerId = ?")
450
                   ->execute([$answerId])
451
                   ->fetchAll();
452
453
        // var_dump($res);
454
        return $res;
455
    }
456
457
458
    /**
459
     * Get all comments connected to answer.
460
     *
461
     * @param object $di.
462
     * @param int $questionId the question the answer is connected to.
463
     * @param string $column the column to order by.
464
     *
465
     * @return array
466
     */
467
    public function getAnswersOrdered($di, $questionId, $column)
468
    {
469
        $db = $di->get("dbqb");
470
        $db->connect();
471
        $sql = "SELECT * FROM Answers WHERE questionId = ? ORDER BY $column;";
472
        $res = $db->executeFetchAll($sql, [$questionId]);
473
474
        return $res;
475
    }
476
477
478
    /**
479
     * Check if a question has been answered.
480
     *
481
     * @param int $questionId the question the answer is connected to.
482
     * @param object $di.
483
     *
484
     * @return string   either Ja or Nej.
485
     */
486
    public function checkIfAnswered($di, $questionId)
487
    {
488
        $db = $di->get("dbqb");
489
        $db->connect();
490
        $res = $db->select("answer")
491
                   ->from("Answers")
492
                   ->where("questionId = ?")
493
                   ->execute([$questionId])
494
                   ->fetch();
495
496
        $answered = "Nej";
497
        if ($res ?? null) {
498
            $answered = "Ja";
499
        }
500
        return $answered;
501
    }
502
503
504
    /**
505
     * Get question information.
506
     *
507
     * @param object $di.
508
     * @param int $questionId Id of the question.
509
     *
510
     * @return array
511
     */
512
    public function getQuestionAnswers($di, $questionId)
513
    {
514
        $db = $di->get("dbqb");
515
        $db->connect();
516
        $res = $db->select("*")
517
                   ->from("Answers")
518
                   ->where("questionId = ?")
519
                   ->execute([$questionId])
520
                   ->fetchAll();
521
522
        // var_dump($res);
523
        return $res;
524
    }
525
526
527
    /**
528
     * Check if logged in user is the same as acronym.
529
     *
530
     * @param string $acronym the username to check.
531
     *
532
     * @return array
533
     */
534
    public function userCheck($acronym)
535
    {
536
        $user = $_SESSION["acronym"];
537
        if ($user == $acronym) {
538
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type array.
Loading history...
539
        }
540
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
541
    }
542
543
544
    /**
545
     * Get question information.
546
     *
547
     * @param object $di.
548
     * @param int $questionId Id of the question.
549
     *
550
     * @return array
551
     */
552
    public function getPoints($di, $table, $id)
553
    {
554
        $db = $di->get("dbqb");
555
        $db->connect();
556
        $res = $db->select("points")
557
                   ->from($table)
558
                   ->where("id = ?")
559
                   ->execute([$id])
560
                   ->fetch();
561
562
        return $res;
563
    }
564
565
566
    /**
567
     * Check if logged in user is the same as acronym.
568
     *
569
     * @param object $di
570
     * @param string $table the table to update.
571
     * @param int $id       the id being updated.
572
     * @param int $points   the points to be added.
573
     *
574
     * @return array
575
     */
576
    public function setPoints($di, $table, $id, $points)
577
    {
578
        // var_dump($points);
579
        $db = $di->get("dbqb");
580
        $db->connect()
581
            ->update($table, ["points"])
582
            ->where("id = ?")
583
            ->execute([$points, $id]);
584
585
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
586
    }
587
588
589
    /**
590
     * Change profile picture.
591
     *
592
     * @param object $di
593
     * @param string $acronym the user's picture to update.
594
     * @param string $picture the picture to change to.
595
     *
596
     * @return array
597
     */
598
    public function setProfilePicture($di, $acronym, $picture)
599
    {
600
        // var_dump($points);
601
        $db = $di->get("dbqb");
602
        $db->connect()
603
            ->update("User", ["picture"])
604
            ->where("acronym = ?")
605
            ->execute([$picture, $acronym]);
606
607
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
608
    }
609
610
611
    /**
612
     * Change profile info.
613
     *
614
     * @param object $di
615
     * @param string $acronym   the user's picture to update.
616
     * @param string $info      the info text to change to.
617
     *
618
     * @return array
619
     */
620
    public function setProfileInfo($di, $acronym, $info)
621
    {
622
        $db = $di->get("dbqb");
623
        $db->connect()
624
            ->update("User", ["info"])
625
            ->where("acronym = ?")
626
            ->execute([$info, $acronym]);
627
628
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
629
    }
630
631
632
    /**
633
     * Add uploaded profile picture to table Pictures.
634
     *
635
     * @param object $di
636
     * @param string $acronym the user that uploads the picture.
637
     * @param string $name    the name of the picture.
638
     *
639
     * @return array
640
     */
641
    public function addProfilePicture($di, $acronym, $name)
642
    {
643
        // var_dump($points);
644
        $db = $di->get("dbqb");
645
        $db->connect()
646
            ->insert("Pictures", ["acronym", "name"])
647
            ->execute([$acronym, $name]);
648
649
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
650
    }
651
652
653
    /**
654
     * Get all profile pictures from table Pictures.
655
     *
656
     * @param object $di
657
     * @param string $acronym the user that uploaded the picture.
658
     * @param string $name    the name of the picture.
659
     *
660
     * @return array
661
     */
662
    public function getAllProfilePictures($di, $acronym)
663
    {
664
        $db = $di->get("dbqb");
665
        $db->connect();
666
        $res = $db->select("name")
667
                   ->from("Pictures")
668
                   ->where("acronym = ? OR acronym = ?")
669
                   ->execute(["default", $acronym])
670
                   ->fetchAll();
671
672
        return $res;
673
    }
674
675
676
    /**
677
     * Save answer.
678
     *
679
     * @param object $di.
680
     * @param int $questionId Id of the question.
681
     * @param string $acronym The user who made the comment/answer.
682
     * @param string $answer The comment/answer.
683
     *
684
     * @return array
685
     */
686
    public function saveAnswer($di, $questionId, $acronym, $answer, $created)
687
    {
688
        $points = 0;
689
690
        $db = $di->get("dbqb");
691
        $db->connect()
692
            ->insert("Answers", ["questionId", "acronym", "answer", "points", "created"])
693
            ->execute([$questionId, $acronym, $answer, $points, $created]);
694
695
        return "Saved";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Saved' returns the type string which is incompatible with the documented return type array.
Loading history...
696
    }
697
698
699
    /**
700
     * Delete comment.
701
     *
702
     * @param object $di.
703
     * @param int $id Id of the comment to be removed.
704
     *
705
     * @return array
706
     */
707
    public function deleteComment($di, $id)
708
    {
709
        $db = $di->get("dbqb");
710
        $db->connect();
711
        $sql = "DELETE FROM Comments WHERE id = ?;";
712
        $db->execute($sql, [$id]);
713
    }
714
715
716
717
    /**
718
     * Destroy a session, the session must be started.
719
     *
720
     * @return void
721
     */
722
    function sessionDestroy()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
723
    {
724
        $style = $_SESSION['AnaxStyleChooser'] ?? "css/mine.css";
725
        // Unset all of the session variables.
726
        $_SESSION = [];
727
728
        // If it's desired to kill the session, also delete the session cookie.
729
        // Note: This will destroy the session, and not just the session data!
730
        if (ini_get("session.use_cookies")) {
731
            $params = session_get_cookie_params();
732
            setcookie(
733
                session_name(),
734
                '',
735
                time() - 42000,
736
                $params["path"],
737
                $params["domain"],
738
                $params["secure"],
739
                $params["httponly"]
740
            );
741
        }
742
743
        // Finally, destroy the session.
744
        session_destroy();
745
746
        $_SESSION['AnaxStyleChooser'] = $style;
747
748
        // $_SESSION["status"] = null;
749
        // $_SESSION["acronym"] = null;
750
        // $_SESSION["message"] = null;
751
        // var_dump($_SESSION);
752
    }
753
}
754