Completed
Push — master ( ac011d...56c397 )
by Mikołaj
03:19
created

QuestionEditModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 5 2
B editQuestion() 0 26 4
A editAnswers() 0 14 2
1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class QuestionEditModel extends AbstractModel
8
{
9
    /**
10
     * Edit question.
11
     *
12
     * @param int $questionID
13
     * @param array $post
14
     * @param array $thumbUpload
15
     * @param string $rootDir
16
     *
17
     * @return bool
18
     */
19
    public function edit($questionID, $post, $thumbUpload, $rootDir)
20
    {
21
        return $this->editQuestion($questionID, $post['question'], $thumbUpload, $rootDir)
22
        && $this->editAnswers($post['answers']);
23
    }
24
25
    /**
26
     * Edit question in exam.
27
     *
28
     * @param int    $questionID
29
     * @param array  $question
30
     * @param array  $thumbUpload
31
     * @param string $rootDir
32
     *
33
     * @return book
34
     */
35
    private function editQuestion($questionID, $question, $thumbUpload, $rootDir)
36
    {
37
        $stmt = $this->db->prepare('UPDATE questions SET content = :content,
38
            correct = :correct WHERE id = :id');
39
        $stmt->bindValue(':content', trim($question['content']), PDO::PARAM_STR);
40
        $stmt->bindValue(':correct', $question['correct'], PDO::PARAM_INT);
41
        $stmt->bindValue(':id', $questionID, PDO::PARAM_INT);
42
        $status = $stmt->execute();
43
44
        if (isset($question['delete-img'])) {
45
            array_map('unlink', glob($rootDir.'/public/storage/'.$questionID.'_*'));
46
            $stmt = $this->db->prepare('UPDATE questions SET image = \'\' WHERE id = ?');
47
            $stmt->execute([$questionID]);
48
        } elseif (!empty($thumbUpload['name'])) {
49
            if (is_uploaded_file($thumbUpload['tmp_name'])) {
50
                array_map('unlink', glob($rootDir.'/public/storage/'.$questionID.'_*'));
51
                $file = $rootDir.'/public/storage/'.$questionID.'_'.$thumbUpload['name'];
52
                move_uploaded_file($thumbUpload['tmp_name'], $file);
53
            }
54
55
            $stmt = $this->db->prepare('UPDATE questions SET image = ? WHERE id = ?');
56
            $stmt->execute([$thumbUpload['name'], $questionID]);
57
        }
58
59
        return $status;
60
    }
61
62
    /**
63
     * Edit answers in exam.
64
     *
65
     * @param array $answers
66
     *
67
     * @return bool
68
     */
69
    private function editAnswers($answers)
70
    {
71
        $stmt = $this->db->prepare('UPDATE answers SET content = :content
72
            WHERE id = :id');
73
        $this->db->beginTransaction();
74
75
        foreach ($answers as $key => $value) {
76
            $stmt->bindValue(':content', trim($value), PDO::PARAM_STR);
77
            $stmt->bindValue(':id', $key, PDO::PARAM_INT);
78
            $stmt->execute();
79
        }
80
81
        return $this->db->commit();
82
    }
83
}
84