Completed
Push — master ( 293319...13cdbf )
by Mikołaj
01:32
created

AnswersModel::getAnswersByQuestionsById()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 14
nc 9
nop 1
1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class AnswersModel extends AbstractModel
8
{
9
    public function getAnswersByQuestionsById(array $questions): array
10
    {
11
        if (empty($questions)) {
12
            return [];
13
        }
14
15
        $where = '';
16
        foreach ($questions as $key => $value) {
17
            $where .= ' OR question_id = :question_id_'.md5($value['id']);
18
        }
19
20
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE '.trim($where, ' OR '));
21
        foreach ($questions as $key => $value) {
22
            $stmt->bindValue(':question_id_'.md5($value['id']), $value['id'], PDO::PARAM_INT);
23
        }
24
25
        $stmt->execute();
26
27
        $array = [];
28
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $key => $value) {
29
            $array[$value['question_id']][] = $value;
30
        }
31
32
        return $array;
33
    }
34
35
    public function getAnswersByOneQuestionId(int $questionID): array
36
    {
37
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE question_id = :qid');
38
        $stmt->bindValue(':qid', $questionID, PDO::PARAM_INT);
39
        $stmt->execute();
40
41
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
42
    }
43
}
44