Completed
Push — master ( 067f55...e1cb48 )
by Julito
09:39
created

getQuestionsFromCourse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 3
dl 0
loc 26
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/* For license terms, see /license.txt */
3
/**
4
 * Functions.
5
 *
6
 * @package chamilo.plugin.test2pdf
7
 */
8
$letters = [
9
    'a',
10
    'b',
11
    'c',
12
    'd',
13
    'e',
14
    'f',
15
    'g',
16
    'h',
17
    'i',
18
    'j',
19
    'k',
20
    'l',
21
    'm',
22
    'n',
23
    'o',
24
    'p',
25
    'q',
26
    'r',
27
    's',
28
    't',
29
    'u',
30
    'v',
31
    'w',
32
    'x',
33
    'y',
34
    'z',
35
];
36
37
/**
38
 * List exercises.
39
 *
40
 * @param int $courseId  Course ID
41
 * @param int $sessionId Session ID
42
 *
43
 * @throws Exception
44
 *
45
 * @return array Results (list of exercise details)
46
 */
47
function showExerciseCourse($courseId, $sessionId = 0)
48
{
49
    $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
50
    $tableLpItem = Database::get_course_table(TABLE_LP_ITEM);
51
    $courseId = (int) $courseId;
52
    $sessionId = (int) $sessionId;
53
    $conditionSession = api_get_session_condition($sessionId, true, true, 'a.session_id');
54
    $sql = "SELECT a.* 
55
            FROM $tableQuiz a 
56
            LEFT JOIN $tableLpItem b ON a.iid = b.path AND a.c_id = b.c_id 
57
            WHERE a.c_id = $courseId
58
            AND (a.active = 1 OR (item_type = 'quiz' AND b.c_id = $courseId)) 
59
            $conditionSession
60
            ORDER BY a.title ASC;";
61
    $res = Database::query($sql);
62
    $aux = [];
63
    while ($row = Database::fetch_assoc($res)) {
64
        $aux[] = $row;
65
    }
66
67
    return $aux;
68
}
69
70
/**
71
 * List quiz details.
72
 *
73
 * @throws Exception
74
 *
75
 * @return array Results (list of quiz details)
76
 */
77
function getInfoQuiz($courseId, $id)
78
{
79
    $courseId = (int) $courseId;
80
    $id = (int) $id;
81
    $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
82
    $sql = "SELECT * FROM $tableQuiz WHERE c_id = $courseId AND iid = $id";
83
    $res = Database::query($sql);
84
    $row = Database::fetch_assoc($res);
85
86
    return $row;
87
}
88
89
/**
90
 * List question_id.
91
 *
92
 * @throws Exception
93
 *
94
 * @return array Results (list question ID)
95
 */
96
function getQuestionsFromCourse($courseId, $quizId, $sessionId = 0)
97
{
98
    $courseId = (int) $courseId;
99
    $quizId = (int) $quizId;
100
    $sessionId = (int) $sessionId;
101
102
    $tableQuizQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
103
    $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
104
    $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
105
    $conditionSession = api_get_session_condition($sessionId, true, true, 'q.session_id');
106
107
    $sql = "SELECT a.question_id AS question_id 
108
            FROM $tableQuizQuestion a 
109
            INNER JOIN $tableQuestion b ON a.question_id = b.iid 
110
            INNER JOIN $tableQuiz q ON q.iid = a.exercice_id 
111
            WHERE a.c_id = $courseId AND b.c_id = a.c_id AND a.exercice_id = $quizId 
112
            AND (b.type IN (1, 2, 9, 10, 11, 12, 14)) 
113
            $conditionSession 
114
            ORDER BY question_order ASC;";
115
    $res = Database::query($sql);
116
    $aux = [];
117
    while ($row = Database::fetch_assoc($res)) {
118
        $aux[] = $row['question_id'];
119
    }
120
121
    return $aux;
122
}
123
124
/**
125
 * List question details.
126
 *
127
 * @throws Exception
128
 *
129
 * @return array Results (list of question details)
130
 */
131
function getInfoQuestion($courseId, $id)
132
{
133
    $courseId = (int) $courseId;
134
    $id = (int) $id;
135
    $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
136
    $sql = "SELECT * FROM $tableQuestion 
137
            WHERE c_id = $courseId 
138
            AND iid = $id
139
            AND (type IN (1, 2, 9, 10, 11, 12, 14))";
140
    $res = Database::query($sql);
141
    $row = Database::fetch_assoc($res);
142
143
    return $row;
144
}
145
146
/**
147
 * List answer details.
148
 *
149
 * @throws Exception
150
 *
151
 * @return array Results (list of answer by question_id)
152
 */
153
function getAnswers($courseId, $id)
154
{
155
    $courseId = (int) $courseId;
156
    $id = (int) $id;
157
    $tableQuizAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
158
    $sql = "SELECT * FROM $tableQuizAnswer
159
    	    WHERE c_id = $courseId AND question_id = $id
160
    	    ORDER BY position ASC;";
161
    $res = Database::query($sql);
162
    $aux = [];
163
    while ($row = Database::fetch_assoc($res)) {
164
        $aux[] = $row;
165
    }
166
167
    return $aux;
168
}
169
170
/**
171
 * Remove all html tag.
172
 *
173
 * @param string $string The string to be stripped of HTML
174
 *
175
 * @return string clean of html tag
176
 */
177
function removeHtml($string)
178
{
179
    $txt = str_replace("<html>", "", $string);
180
    $txt = str_replace("<head>", "", $txt);
181
    $txt = str_replace("<title>", "", $txt);
182
    $txt = str_replace("</title>", "", $txt);
183
    $txt = str_replace("</head>", "", $txt);
184
    $txt = str_replace("<body>", "", $txt);
185
    $txt = str_replace("</body>", "", $txt);
186
    $txt = str_replace("</html>", "", $txt);
187
    $txt = strip_tags($txt);
188
    $txt = str_replace(chr(13).chr(10), "", $txt);
189
    $txt = str_replace("&nbsp;", " ", $txt);
190
    $txt = str_replace("&Aacute;", "Á", $txt);
191
    $txt = str_replace("&aacute;", "á", $txt);
192
    $txt = str_replace("&Eacute;", "É", $txt);
193
    $txt = str_replace("&eacute;", "é", $txt);
194
    $txt = str_replace("&Iacute;", "Í", $txt);
195
    $txt = str_replace("&iacute;", "í", $txt);
196
    $txt = str_replace("&Oacute;", "Ó", $txt);
197
    $txt = str_replace("&oacute;", "ó", $txt);
198
    $txt = str_replace("&Uacute;", "Ú", $txt);
199
    $txt = str_replace("&uacute;", "ú", $txt);
200
    $txt = str_replace("&Ntilde;", "Ñ", $txt);
201
    $txt = str_replace("&ntilde;", "ñ", $txt);
202
    $txt = str_replace("&agrave;", "à", $txt);
203
    $txt = str_replace("&Agrave;", "À", $txt);
204
    $txt = str_replace("&iexcl;", "¡", $txt);
205
    $txt = str_replace("&middot;", "·", $txt);
206
    $txt = str_replace("&Ccedil;", "Ç", $txt);
207
    $txt = str_replace("&ccedil;", "ç", $txt);
208
    $txt = str_replace("&quot;", '"', $txt);
209
    $txt = str_replace("&ordf;", 'ª', $txt);
210
    $txt = str_replace("&ordm;", 'º', $txt);
211
    $txt = str_replace("&amp;", '&', $txt);
212
    $txt = str_replace("&bull;", '•', $txt);
213
    $txt = str_replace("&iquest;", '¿', $txt);
214
    $txt = str_replace("&euro;", 'EUR', $txt);
215
    $txt = str_replace("&uuml;", 'ü', $txt);
216
    $txt = str_replace("&Uuml;", 'Ü', $txt);
217
    $txt = str_replace("&uml;", '¨', $txt);
218
219
    return $txt;
220
}
221
222
/**
223
 * Remove all html tag.
224
 *
225
 * @param string $string The string to be stripped of accents
226
 *
227
 * @return string clean of html tag
228
 */
229
function removeQuotes($string)
230
{
231
    $txt = str_replace("&nbsp;", " ", $string);
232
    $txt = str_replace("&Aacute;", "Á", $txt);
233
    $txt = str_replace("&aacute;", "á", $txt);
234
    $txt = str_replace("&Eacute;", "É", $txt);
235
    $txt = str_replace("&eacute;", "é", $txt);
236
    $txt = str_replace("&Iacute;", "Í", $txt);
237
    $txt = str_replace("&iacute;", "í", $txt);
238
    $txt = str_replace("&Oacute;", "Ó", $txt);
239
    $txt = str_replace("&oacute;", "ó", $txt);
240
    $txt = str_replace("&Uacute;", "Ú", $txt);
241
    $txt = str_replace("&uacute;", "ú", $txt);
242
    $txt = str_replace("&Ntilde;", "Ñ", $txt);
243
    $txt = str_replace("&ntilde;", "ñ", $txt);
244
    $txt = str_replace("&quot;", '"', $txt);
245
    $txt = str_replace("&ordf;", 'ª', $txt);
246
    $txt = str_replace("&ordm;", 'º', $txt);
247
    $txt = str_replace("&amp;", '&', $txt);
248
    $txt = str_replace("&bull;", '•', $txt);
249
    $txt = str_replace("&iquest; &", '¿', $txt);
250
    $txt = str_replace("&agrave;", "à", $txt);
251
    $txt = str_replace("&Agrave;", "À", $txt);
252
    $txt = str_replace("&iexcl;", "¡", $txt);
253
    $txt = str_replace("&middot;", "·", $txt);
254
    $txt = str_replace("&Ccedil;", "Ç", $txt);
255
    $txt = str_replace("&ccedil;", "ç", $txt);
256
    $txt = str_replace("&euro;", 'EUR', $txt);
257
    $txt = str_replace("&uuml;", 'ü', $txt);
258
    $txt = str_replace("&Uuml;", 'Ü', $txt);
259
    $txt = str_replace("uml;", '¨', $txt);
260
261
    return $txt;
262
}
263