Passed
Pull Request — 1.11.x (#7042)
by
unknown
11:02
created

getQuestionAnswers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 17
rs 9.8333
cc 4
nc 4
nop 1
1
<?php
2
/* For license terms, see /license.txt */
3
4
/**
5
 * Additional functions for the Extended Question Pool plugin.
6
 *
7
 * @package chamilo.plugin.extendedquestionpool
8
 *
9
 * @author Nosolored <[email protected]>
10
 */
11
12
/**
13
 * Get the number of occurrences of a question in quizzes
14
 * @param int $questionId
15
 * @return int
16
 */
17
function getQuestionOcurrences($questionId) {
18
    $questionQuizTable = 'c_quiz_rel_question';
19
    if (empty($questionId)) {
20
        return false;
21
    }
22
    $query = "SELECT count(iid) as nbr FROM $questionQuizTable 
23
                WHERE question_id = '$questionId'";
24
    $q = Database::query($query);
25
    $row = Database::fetch_assoc($q);
26
    $res = $row['nbr'];
27
28
    return $res;
29
}
30
/**
31
 * Get the number of failures answers of a question in quizzes
32
 * @param int $questionId
33
 * @param int $c_id  course id
34
 * @return int
35
 */
36
function getQuestionFailures($questionId, $c_id = 0) 
37
{
38
    $attemptTable = 'track_e_attempt';
39
    if (empty($questionId)) {
40
        return false;
41
    }
42
    $query = "SELECT count(id) as nbr FROM $attemptTable 
43
                WHERE question_id = '$questionId'
44
                AND marks <= 0";
45
    if (!empty($c_id)) {
46
        $query .= " AND c_id='$c_id'";
47
    }
48
    $q = Database::query($query);
49
    $row = Database::fetch_assoc($q);
50
    $res = $row['nbr'];
51
52
    return $res;
53
}
54
/**
55
 * Get the number of successes answers of a question in quizzes
56
 * @param int $questionId
57
 * @param int $c_id  course id
58
 * @return int
59
 */
60
function getQuestionSuccesses($questionId, $c_id = 0) 
61
{
62
    $attemptTable = 'track_e_attempt';
63
    if (empty($questionId)) {
64
        return false;
65
    }
66
    $query = "SELECT count(id) as nbr FROM $attemptTable 
67
                WHERE question_id = '$questionId'
68
                AND marks > 0";
69
    if (!empty($c_id)) {
70
        $query .= " AND c_id='$c_id'";
71
    }
72
    $q = Database::query($query);
73
    $row = Database::fetch_assoc($q);
74
    $res = $row['nbr'];
75
76
    return $res;
77
}
78
/**
79
 * Sort array by column
80
 * @param array $data
81
 * @param string $col
82
 * @param bool $asc
83
 * @return array
84
 */
85
function sortByCol(array $data, string $col, bool $asc = true): array {
86
    usort($data, function ($a, $b) use ($col, $asc) {
87
        if (!isset($a[$col]) || !isset($b[$col])) {
88
            return 0; // column doesn't exist, nothing to sort
89
        }
90
91
        if ($a[$col] == $b[$col]) {
92
            return 0;
93
        }
94
95
        if ($asc) {
96
            return (strip_tags($a[$col]) < strip_tags($b[$col])) ? -1 : 1;
97
        } else {
98
            return (strip_tags($a[$col]) > strip_tags($b[$col])) ? -1 : 1;
99
        }
100
    });
101
102
    return $data;
103
}
104
/**
105
 * Get answer options for a question
106
 * @param int $questionId
107
 * @return string
108
 */
109
function getQuestionAnswers(int $questionId) {
110
    $table = Database::get_course_table(TABLE_QUIZ_ANSWER);
111
    if (empty($questionId)) {
112
        return false;
113
    }
114
    $res = '';
115
    $query = "SELECT * FROM $table WHERE question_id=$questionId ORDER BY position";
116
    $result = Database::query($query);
117
    while ($row = Database::fetch_assoc($result)) {
118
        $c = '- ';
119
        if ($row['correct'] == 1) {
120
            $c = '# ';
121
        }
122
        $answer = html_entity_decode(strip_tags($row['answer']));
123
        $res .= $c.$answer.chr(13).chr(10);
124
    }
125
    return $res;
126
}