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

QuestionAddModel::addAnswers()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 14
nc 6
nop 4
1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class QuestionAddModel extends AbstractModel
8
{
9
    public function add($examID, $post)
10
    {
11
        if (!isset($post['question']['correct'])) {
12
            $post['question']['correct'] = 0;
13
        }
14
        $qid = $this->addQuestion($examID, $post['question']);
15
        $cid = $this->addAnswers($examID, $qid, $post['question']['correct'], $post['answers']);
16
        $this->addCorrectAnswerToQuestion($qid, $cid);
17
18
        return $qid;
19
    }
20
21
    /**
22
     * Add question for exam.
23
     *
24
     * @param int   $examID
25
     * @param array $question
26
     *
27
     * @return book
28
     */
29 View Code Duplication
    private function addQuestion($examID, $question)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $stmt = $this->db->prepare('INSERT INTO questions (exam_id, content)
32
            VALUES (:exam_id, :content)');
33
        $stmt->bindValue(':exam_id', $examID, PDO::PARAM_INT);
34
        $stmt->bindValue(':content', trim($question['content']), PDO::PARAM_STR);
35
        $stmt->execute();
36
37
        return $this->db->lastInsertId();
38
    }
39
40
    /**
41
     * Add correct answer to question.
42
     *
43
     * @param int $questionID Question id
44
     * @param int $correct    Correct anwer id
45
     */
46 View Code Duplication
    public function addCorrectAnswerToQuestion($questionID, $correct)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $stmt = $this->db->prepare('UPDATE questions SET correct = :correct
49
            WHERE id = :id');
50
        $stmt->bindValue(':correct', $correct, PDO::PARAM_INT);
51
        $stmt->bindValue(':id', $questionID, PDO::PARAM_INT);
52
53
        return $stmt->execute();
54
    }
55
56
    /**
57
     * Add answers for exam.
58
     *
59
     * @param int   $examID
60
     * @param int   $questionID
61
     * @param int   $correct
62
     * @param array $answers
63
     *
64
     * @return bool
65
     */
66
    private function addAnswers($examID, $questionID, $correct, $answers)
67
    {
68
        $stmt = $this->db->prepare('INSERT INTO answers (exam_id, question_id, content)
69
            VALUES (:exam_id, :question_id, :content)
70
        ');
71
        $this->db->beginTransaction();
72
73
        foreach ($answers as $key => $value) {
74
            $stmt->bindValue(':exam_id', $examID, PDO::PARAM_INT);
75
            $stmt->bindValue(':question_id', $questionID, PDO::PARAM_INT);
76
            $stmt->bindValue(':content', trim($value), PDO::PARAM_STR);
77
            $stmt->execute();
78
79
            if ($correct == $key) {
80
                $correctId = $this->db->lastInsertId();
81
            }
82
        }
83
84
        if (!isset($correctId)) {
85
            $correctId = 0;
86
        }
87
88
        $this->db->commit();
89
90
        return $correctId;
91
    }
92
}
93