Passed
Push — master ( 08314e...a4ad40 )
by Julito
10:14
created

aiken_import_exercise()   D

Complexity

Conditions 18
Paths 4

Size

Total Lines 117
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 76
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 117
rs 4.8666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Library for the import of Aiken format.
7
 *
8
 * @author claro team <[email protected]>
9
 * @author Guillaume Lederer <[email protected]>
10
 * @author César Perales <[email protected]> Parse function for Aiken format
11
 */
12
13
/**
14
 * This function displays the form for import of the zip file with qti2.
15
 *
16
 * @param   string  Report message to show in case of error
17
 */
18
function aiken_display_form()
19
{
20
    $name_tools = get_lang('Import Aiken quiz');
21
    $form = '<div class="actions">';
22
    $form .= '<a href="exercise.php?show=test&'.api_get_cidreq().'">'.
23
        Display::return_icon(
24
            'back.png',
25
            get_lang('Back to Tests tool'),
26
            '',
27
            ICON_SIZE_MEDIUM
28
        ).'</a>';
29
    $form .= '</div>';
30
    $form_validator = new FormValidator(
31
        'aiken_upload',
32
        'post',
33
        api_get_self().'?'.api_get_cidreq(),
34
        null,
35
        ['enctype' => 'multipart/form-data']
36
    );
37
    $form_validator->addElement('header', $name_tools);
38
    $form_validator->addElement('text', 'total_weight', get_lang('Total weight'));
39
    $form_validator->addElement('file', 'userFile', get_lang('File'));
40
    $form_validator->addButtonUpload(get_lang('Upload'), 'submit');
41
    $form .= $form_validator->returnForm();
42
    $form .= '<blockquote>'.get_lang('Import Aiken quizExplanation').'<br /><pre>'.get_lang('Import Aiken quizExplanationExample').'</pre></blockquote>';
43
    echo $form;
44
}
45
46
/**
47
 * Main function to import the Aiken exercise.
48
 *
49
 * @param string $file
50
 *
51
 * @return mixed True on success, error message on failure
52
 */
53
function aiken_import_exercise($file)
54
{
55
    // set some default values for the new exercise
56
    $exercise_info = [];
57
    $exercise_info['name'] = preg_replace('/.(txt)$/i', '', $file);
58
    $exercise_info['question'] = [];
59
60
    // if file is not a .zip, then we cancel all
61
    if (!preg_match('/.(zip)$/i', $file)) {
62
        return 'You must upload a .txt file';
63
    }
64
65
    $file_found = false;
66
    $operation = false;
67
    $result = aiken_parse_file($exercise_info,   $file);
68
69
    if (true !== $result) {
70
        return $result;
71
    }
72
73
    // 1. Create exercise
74
    $exercise = new Exercise();
75
    $exercise->exercise = $exercise_info['name'];
76
    $exercise->save();
77
    $last_exercise_id = $exercise->getId();
78
    $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
79
    $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
80
    if (!empty($last_exercise_id)) {
81
        $courseId = api_get_course_int_id();
82
        foreach ($exercise_info['question'] as $key => $question_array) {
83
            // 2. Create question.
84
            $question = new Aiken2Question();
85
            $question->type = $question_array['type'];
86
            $question->setAnswer();
87
            $question->updateTitle($question_array['title']);
88
89
            if (isset($question_array['description'])) {
90
                $question->updateDescription($question_array['description']);
91
            }
92
            $type = $question->selectType();
93
            $question->type = constant($type);
94
            $question->save($exercise);
95
            $last_question_id = $question->getId();
96
97
            // 3. Create answer
98
            $answer = new Answer($last_question_id, $courseId, $exercise, false);
99
            $answer->new_nbrAnswers = count($question_array['answer']);
100
            $max_score = 0;
101
102
            $scoreFromFile = 0;
103
            if (isset($question_array['score']) && !empty($question_array['score'])) {
104
                $scoreFromFile = $question_array['score'];
105
            }
106
107
            foreach ($question_array['answer'] as $key => $answers) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
108
                $key++;
109
                $answer->new_answer[$key] = $answers['value'];
110
                $answer->new_position[$key] = $key;
111
                $answer->new_comment[$key] = '';
112
                // Correct answers ...
113
                if (isset($question_array['correct_answers']) &&
114
                    in_array($key, $question_array['correct_answers'])
115
                ) {
116
                    $answer->new_correct[$key] = 1;
117
                    if (isset($question_array['feedback'])) {
118
                        $answer->new_comment[$key] = $question_array['feedback'];
119
                    }
120
                } else {
121
                    $answer->new_correct[$key] = 0;
122
                }
123
124
                if (isset($question_array['weighting'][$key - 1])) {
125
                    $answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
126
                    $max_score += $question_array['weighting'][$key - 1];
127
                }
128
129
                if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
130
                    $answer->new_weighting[$key] = $scoreFromFile;
131
                }
132
133
                $params = [
134
                    'c_id' => $courseId,
135
                    'question_id' => $last_question_id,
136
                    'answer' => $answer->new_answer[$key],
137
                    'correct' => $answer->new_correct[$key],
138
                    'comment' => $answer->new_comment[$key],
139
                    'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '',
140
                    'position' => $answer->new_position[$key],
141
                    'hotspot_coordinates' => '',
142
                    'hotspot_type' => '',
143
                ];
144
145
                $answerId = Database::insert($tableAnswer, $params);
146
                if ($answerId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $answerId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
147
                    $params = [
148
                        'iid' => $answerId,
149
                    ];
150
                    Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]);
151
                }
152
            }
153
154
            if (!empty($scoreFromFile)) {
155
                $max_score = $scoreFromFile;
156
            }
157
158
            $params = ['ponderation' => $max_score];
159
            Database::update(
160
                $tableQuestion,
161
                $params,
162
                ['iid = ?' => [$last_question_id]]
163
            );
164
        }
165
166
        $operation = $last_exercise_id;
167
    }
168
169
    return $operation;
170
}
171
172
/**
173
 * Parses an Aiken file and builds an array of exercise + questions to be
174
 * imported by the import_exercise() function.
175
 *
176
 * @param array The reference to the array in which to store the questions
177
 * @param string Path to the directory with the file to be parsed (without final /)
178
 * @param string Name of the last directory part for the file (without /)
179
 * @param string Name of the file to be parsed (including extension)
180
 * @param string $exercisePath
181
 * @param string $file
182
 * @param string $questionFile
183
 *
184
 * @return string|bool True on success, error message on error
185
 * @assert ('','','') === false
186
 */
187
function aiken_parse_file(&$exercise_info, $file)
188
{
189
    if (!is_file($file)) {
190
        return 'FileNotFound';
191
    }
192
193
    $text = file_get_contents($file);
194
    $detect = mb_detect_encoding($text, 'ASCII', true);
195
    if ('ASCII' === $detect) {
196
        $data = explode("\n", $text);
197
    } else {
198
        $text = str_ireplace(["\x0D", "\r\n"], "\n", $text); // Removes ^M char from win files.
199
        $data = explode("\n\n", $text);
200
    }
201
202
    $question_index = 0;
203
    $answers_array = [];
204
    foreach ($data as $line => $info) {
205
        $info = trim($info);
206
        if (empty($info)) {
207
            continue;
208
        }
209
        //make sure it is transformed from iso-8859-1 to utf-8 if in that form
210
        if (!mb_check_encoding($info, 'utf-8') && mb_check_encoding($info, 'iso-8859-1')) {
211
            $info = utf8_encode($info);
212
        }
213
        $exercise_info['question'][$question_index]['type'] = 'MCUA';
214
        if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
215
            //adding one of the possible answers
216
            $exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
217
            $answers_array[] = $matches[1];
218
        } elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
219
            //the correct answers
220
            $correct_answer_index = array_search($matches[1], $answers_array);
221
            $exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
222
            //weight for correct answer
223
            $exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
224
            $next = $line + 1;
225
226
            if (false !== strpos($data[$next], 'ANSWER_EXPLANATION:')) {
227
                continue;
228
            }
229
230
            if (false !== strpos($data[$next], 'DESCRIPTION:')) {
231
                continue;
232
            }
233
            // Check if next has score, otherwise loop too next question.
234
            if (false === strpos($data[$next], 'SCORE:')) {
235
                $answers_array = [];
236
                $question_index++;
237
                continue;
238
            }
239
        } elseif (preg_match('/^SCORE:\s?(.*)/', $info, $matches)) {
240
            $exercise_info['question'][$question_index]['score'] = (float) $matches[1];
241
            $answers_array = [];
242
            $question_index++;
243
            continue;
244
        } elseif (preg_match('/^DESCRIPTION:\s?(.*)/', $info, $matches)) {
245
            $exercise_info['question'][$question_index]['description'] = $matches[1];
246
            $next = $line + 1;
247
248
            if (false !== strpos($data[$next], 'ANSWER_EXPLANATION:')) {
249
                continue;
250
            }
251
            // Check if next has score, otherwise loop too next question.
252
            if (false === strpos($data[$next], 'SCORE:')) {
253
                $answers_array = [];
254
                $question_index++;
255
                continue;
256
            }
257
        } elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
258
            //Comment of correct answer
259
            $correct_answer_index = array_search($matches[1], $answers_array);
260
            $exercise_info['question'][$question_index]['feedback'] = $matches[1];
261
            $next = $line + 1;
262
            // Check if next has score, otherwise loop too next question.
263
            if (false === strpos($data[$next], 'SCORE:')) {
264
                $answers_array = [];
265
                $question_index++;
266
                continue;
267
            }
268
        } elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
269
            //Comment of correct answer (Spanish e-ducativa format)
270
            $correct_answer_index = array_search($matches[1], $answers_array);
271
            $exercise_info['question'][$question_index]['feedback'] = $matches[1];
272
        } elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
273
            //Question Title
274
            $correct_answer_index = array_search($matches[1], $answers_array);
275
            $exercise_info['question'][$question_index]['title'] = $matches[1];
276
        } elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
277
            //TAGS for chamilo >= 1.10
278
            $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
279
        } elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
280
            //TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
281
            $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
282
        } elseif (empty($info)) {
283
            /*if (empty($exercise_info['question'][$question_index]['title'])) {
284
                $exercise_info['question'][$question_index]['title'] = $info;
285
            }
286
            //moving to next question (tolerate \r\n or just \n)
287
            if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
288
                error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
289
290
                return 'ExerciseAikenErrorNoCorrectAnswerDefined';
291
            }
292
            if (empty($exercise_info['question'][$question_index]['answer'])) {
293
                error_log('Aiken: Error in question index '.$question_index.': no answer option given');
294
295
                return 'ExerciseAikenErrorNoAnswerOptionGiven';
296
            }
297
            $question_index++;
298
            //emptying answers array when moving to next question
299
            $answers_array = [];
300
        } else {
301
            if (empty($exercise_info['question'][$question_index]['title'])) {
302
                $exercise_info['question'][$question_index]['title'] = $info;
303
            }
304
            /*$question_index++;
305
            //emptying answers array when moving to next question
306
            $answers_array = [];
307
            $new_question = true;*/
308
        }
309
    }
310
    $total_questions = count($exercise_info['question']);
311
    $total_weight = !empty($_POST['total_weight']) ? (int) ($_POST['total_weight']) : 20;
312
    foreach ($exercise_info['question'] as $key => $question) {
313
        if (!isset($exercise_info['question'][$key]['weighting'])) {
314
            continue;
315
        }
316
        $exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
317
    }
318
319
    return true;
320
}
321
322
/**
323
 * Imports the zip file.
324
 *
325
 * @param array $array_file ($_FILES)
326
 *
327
 * @return bool
328
 */
329
function aiken_import_file($array_file)
330
{
331
    $unzip = 0;
332
    $process = process_uploaded_file($array_file, false);
333
    if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
334
        // if it's a zip, allow zip upload
335
        $unzip = 1;
336
    }
337
338
    if ($process && 1 == $unzip) {
339
        $imported = aiken_import_exercise($array_file['name']);
340
        if (is_numeric($imported) && !empty($imported)) {
341
            Display::addFlash(Display::return_message(get_lang('Uploaded.')));
342
343
            return $imported;
344
        } else {
345
            Display::addFlash(Display::return_message(get_lang($imported), 'error'));
346
347
            return false;
348
        }
349
    }
350
}
351