Passed
Push — master ( 2d9a22...0085e5 )
by Julito
10:53 queued 02:39
created

aiken_parse_file()   F

Complexity

Conditions 23
Paths 361

Size

Total Lines 122
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 67
nc 361
nop 4
dl 0
loc 122
rs 1.3208
c 0
b 0
f 0

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
 * Library for the import of Aiken format.
6
 *
7
 * @author claro team <[email protected]>
8
 * @author Guillaume Lederer <[email protected]>
9
 * @author César Perales <[email protected]> Parse function for Aiken format
10
 */
11
12
/**
13
 * This function displays the form for import of the zip file with qti2.
14
 *
15
 * @param   string  Report message to show in case of error
16
 */
17
function aiken_display_form()
18
{
19
    $name_tools = get_lang('Import Aiken quiz');
20
    $form = '<div class="actions">';
21
    $form .= '<a href="exercise.php?show=test&'.api_get_cidreq().'">'.
22
        Display::return_icon(
23
            'back.png',
24
            get_lang('Back to Tests tool'),
25
            '',
26
            ICON_SIZE_MEDIUM
27
        ).'</a>';
28
    $form .= '</div>';
29
    $form_validator = new FormValidator(
30
        'aiken_upload',
31
        'post',
32
        api_get_self().'?'.api_get_cidreq(),
33
        null,
34
        ['enctype' => 'multipart/form-data']
35
    );
36
    $form_validator->addElement('header', $name_tools);
37
    $form_validator->addElement('text', 'total_weight', get_lang('Total weight'));
38
    $form_validator->addElement('file', 'userFile', get_lang('File'));
39
    $form_validator->addButtonUpload(get_lang('Upload'), 'submit');
40
    $form .= $form_validator->returnForm();
41
    $form .= '<blockquote>'.get_lang('Import Aiken quizExplanation').'<br /><pre>'.get_lang('Import Aiken quizExplanationExample').'</pre></blockquote>';
42
    echo $form;
43
}
44
45
/**
46
 * Gets the uploaded file (from $_FILES) and unzip it to the given directory.
47
 *
48
 * @param string The directory where to do the work
49
 * @param string The path of the temporary directory where the exercise was uploaded and unzipped
50
 * @param string $baseWorkDir
51
 * @param string $uploadPath
52
 *
53
 * @return bool True on success, false on failure
54
 */
55
function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)
56
{
57
    $_course = api_get_course_info();
58
    $_user = api_get_user_info();
59
60
    // Check if the file is valid (not to big and exists)
61
    if (!isset($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
62
        // upload failed
63
        return false;
64
    }
65
66
    if (preg_match('/.zip$/i', $_FILES['userFile']['name']) &&
67
        handle_uploaded_document(
68
            $_course,
69
            $_FILES['userFile'],
70
            $baseWorkDir,
71
            $uploadPath,
72
            $_user['user_id'],
73
            0,
74
            null,
75
            1,
76
            'overwrite',
77
            false
78
        )
79
    ) {
80
        if (!function_exists('gzopen')) {
81
            return false;
82
        }
83
        // upload successful
84
        return true;
85
    } elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) &&
86
        handle_uploaded_document(
87
            $_course,
88
            $_FILES['userFile'],
89
            $baseWorkDir,
90
            $uploadPath,
91
            $_user['user_id'],
92
            0,
93
            null,
94
            0,
95
            'overwrite',
96
            false
97
        )
98
    ) {
99
        return true;
100
    }
101
102
    return false;
103
}
104
105
/**
106
 * Main function to import the Aiken exercise.
107
 *
108
 * @param string $file
109
 *
110
 * @return mixed True on success, error message on failure
111
 */
112
function aiken_import_exercise($file)
113
{
114
    $archive_path = api_get_path(SYS_ARCHIVE_PATH).'aiken/';
115
    $baseWorkDir = $archive_path;
116
117
    if (!is_dir($baseWorkDir)) {
118
        mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true);
119
    }
120
121
    $uploadPath = 'aiken_'.api_get_unique_id().'/';
122
123
    // set some default values for the new exercise
124
    $exercise_info = [];
125
    $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file);
126
    $exercise_info['question'] = [];
127
128
    // if file is not a .zip, then we cancel all
129
    if (!preg_match('/.(zip|txt)$/i', $file)) {
130
        return 'YouMustUploadAZipOrTxtFile';
131
    }
132
133
    // unzip the uploaded file in a tmp directory
134
    if (preg_match('/.(zip|txt)$/i', $file)) {
135
        if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
136
            return 'ThereWasAProblemWithYourFile';
137
        }
138
    }
139
140
    // find the different manifests for each question and parse them
141
    $exerciseHandle = opendir($baseWorkDir.$uploadPath);
142
    $file_found = false;
143
    $operation = false;
144
    $result = false;
145
146
    // Parse every subdirectory to search txt question files
147
    while (false !== ($file = readdir($exerciseHandle))) {
148
        if (is_dir($baseWorkDir.'/'.$uploadPath.$file) && '.' != $file && '..' != $file) {
149
            //find each manifest for each question repository found
150
            $questionHandle = opendir($baseWorkDir.'/'.$uploadPath.$file);
151
            while (false !== ($questionFile = readdir($questionHandle))) {
152
                if (preg_match('/.txt$/i', $questionFile)) {
153
                    $result = aiken_parse_file(
154
                        $exercise_info,
155
                        $baseWorkDir,
156
                        $file,
157
                        $questionFile
158
                    );
159
                    $file_found = true;
160
                }
161
            }
162
        } elseif (preg_match('/.txt$/i', $file)) {
163
            $result = aiken_parse_file($exercise_info, $baseWorkDir.$uploadPath, '', $file);
164
            $file_found = true;
165
        }
166
    }
167
168
    if (!$file_found) {
169
        $result = 'NoTxtFileFoundInTheZip';
170
    }
171
172
    if (true !== $result) {
173
        return $result;
174
    }
175
176
    // 1. Create exercise
177
    $exercise = new Exercise();
178
    $exercise->exercise = $exercise_info['name'];
179
    $exercise->save();
180
    $last_exercise_id = $exercise->selectId();
181
    $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
182
    $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
183
    if (!empty($last_exercise_id)) {
184
        // For each question found...
185
        $courseId = api_get_course_int_id();
186
        foreach ($exercise_info['question'] as $key => $question_array) {
187
            // 2.create question
188
            $question = new Aiken2Question();
189
            $question->type = $question_array['type'];
190
            $question->setAnswer();
191
            $question->updateTitle($question_array['title']);
192
193
            if (isset($question_array['description'])) {
194
                $question->updateDescription($question_array['description']);
195
            }
196
            $type = $question->selectType();
197
            $question->type = constant($type);
198
            $question->save($exercise);
199
            $last_question_id = $question->selectId();
200
201
            // 3. Create answer
202
            $answer = new Answer($last_question_id, $courseId, $exercise, false);
203
            $answer->new_nbrAnswers = count($question_array['answer']);
204
            $max_score = 0;
205
206
            $scoreFromFile = 0;
207
            if (isset($question_array['score']) && !empty($question_array['score'])) {
208
                $scoreFromFile = $question_array['score'];
209
            }
210
211
            foreach ($question_array['answer'] as $key => $answers) {
212
                $key++;
213
                $answer->new_answer[$key] = $answers['value'];
214
                $answer->new_position[$key] = $key;
215
                $answer->new_comment[$key] = '';
216
                // Correct answers ...
217
                if (isset($question_array['correct_answers']) &&
218
                    in_array($key, $question_array['correct_answers'])
219
                ) {
220
                    $answer->new_correct[$key] = 1;
221
                    if (isset($question_array['feedback'])) {
222
                        $answer->new_comment[$key] = $question_array['feedback'];
223
                    }
224
                } else {
225
                    $answer->new_correct[$key] = 0;
226
                }
227
228
                if (isset($question_array['weighting'][$key - 1])) {
229
                    $answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
230
                    $max_score += $question_array['weighting'][$key - 1];
231
                }
232
233
                if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
234
                    $answer->new_weighting[$key] = $scoreFromFile;
235
                }
236
237
                $params = [
238
                    'c_id' => $courseId,
239
                    'question_id' => $last_question_id,
240
                    'answer' => $answer->new_answer[$key],
241
                    'correct' => $answer->new_correct[$key],
242
                    'comment' => $answer->new_comment[$key],
243
                    'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '',
244
                    'position' => $answer->new_position[$key],
245
                    'hotspot_coordinates' => '',
246
                    'hotspot_type' => '',
247
                ];
248
249
                $answerId = Database::insert($tableAnswer, $params);
250
                if ($answerId) {
251
                    $params = [
252
                        'id_auto' => $answerId,
253
                        'id' => $answerId,
254
                    ];
255
                    Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]);
256
                }
257
            }
258
259
            if (!empty($scoreFromFile)) {
260
                $max_score = $scoreFromFile;
261
            }
262
263
            //$answer->save();
264
265
            $params = ['ponderation' => $max_score];
266
            Database::update(
267
                $tableQuestion,
268
                $params,
269
                ['iid = ?' => [$last_question_id]]
270
            );
271
        }
272
273
        // Delete the temp dir where the exercise was unzipped
274
        my_delete($baseWorkDir.$uploadPath);
275
        $operation = $last_exercise_id;
276
    }
277
278
    return $operation;
279
}
280
281
/**
282
 * Parses an Aiken file and builds an array of exercise + questions to be
283
 * imported by the import_exercise() function.
284
 *
285
 * @param array The reference to the array in which to store the questions
286
 * @param string Path to the directory with the file to be parsed (without final /)
287
 * @param string Name of the last directory part for the file (without /)
288
 * @param string Name of the file to be parsed (including extension)
289
 * @param string $exercisePath
290
 * @param string $file
291
 * @param string $questionFile
292
 *
293
 * @return string|bool True on success, error message on error
294
 * @assert ('','','') === false
295
 */
296
function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile)
297
{
298
    $questionTempDir = $exercisePath.'/'.$file.'/';
299
    $questionFilePath = $questionTempDir.$questionFile;
300
301
    if (!is_file($questionFilePath)) {
302
        return 'FileNotFound';
303
    }
304
    $text = file_get_contents($questionFilePath);
305
    $detect = mb_detect_encoding($text, 'ASCII', true);
306
    if ('ASCII' === $detect) {
307
        $data = explode("\n", $text);
308
    } else {
309
        $text = str_ireplace(["\x0D", "\r\n"], "\n", $text); // Removes ^M char from win files.
310
        $data = explode("\n\n", $text);
311
    }
312
313
    $question_index = 0;
314
    $answers_array = [];
315
    foreach ($data as $line => $info) {
316
        $info = trim($info);
317
        if (empty($info)) {
318
            // double empty line
319
            continue;
320
        }
321
        //make sure it is transformed from iso-8859-1 to utf-8 if in that form
322
        if (!mb_check_encoding($info, 'utf-8') && mb_check_encoding($info, 'iso-8859-1')) {
323
            $info = utf8_encode($info);
324
        }
325
        $exercise_info['question'][$question_index]['type'] = 'MCUA';
326
        if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
327
            //adding one of the possible answers
328
            $exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
329
            $answers_array[] = $matches[1];
330
        } elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
331
            //the correct answers
332
            $correct_answer_index = array_search($matches[1], $answers_array);
333
            $exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
334
            //weight for correct answer
335
            $exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
336
            $next = $line + 1;
337
338
            if (false !== strpos($data[$next], 'ANSWER_EXPLANATION:')) {
339
                continue;
340
            }
341
342
            // Check if next has score, otherwise loop too next question.
343
            if (false === strpos($data[$next], 'SCORE:')) {
344
                $answers_array = [];
345
                $question_index++;
346
                continue;
347
            }
348
        } elseif (preg_match('/^SCORE:\s?(.*)/', $info, $matches)) {
349
            $exercise_info['question'][$question_index]['score'] = (float) $matches[1];
350
            $answers_array = [];
351
            $question_index++;
352
            continue;
353
        } elseif (preg_match('/^DESCRIPTION:\s?(.*)/', $info, $matches)) {
354
            $exercise_info['question'][$question_index]['description'] = $matches[1];
355
        } elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
356
            //Comment of correct answer
357
            $correct_answer_index = array_search($matches[1], $answers_array);
358
            $exercise_info['question'][$question_index]['feedback'] = $matches[1];
359
            $next = $line + 1;
360
            // Check if next has score, otherwise loop too next question.
361
            if (false === strpos($data[$next], 'SCORE:')) {
362
                $answers_array = [];
363
                $question_index++;
364
                continue;
365
            }
366
        } elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
367
            //Comment of correct answer (Spanish e-ducativa format)
368
            $correct_answer_index = array_search($matches[1], $answers_array);
369
            $exercise_info['question'][$question_index]['feedback'] = $matches[1];
370
        } elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
371
            //Question Title
372
            $correct_answer_index = array_search($matches[1], $answers_array);
373
            $exercise_info['question'][$question_index]['title'] = $matches[1];
374
        } elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
375
            //TAGS for chamilo >= 1.10
376
            $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
377
        } elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
378
            //TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
379
            $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
380
        } elseif (empty($info)) {
381
            /*if (empty($exercise_info['question'][$question_index]['title'])) {
382
                $exercise_info['question'][$question_index]['title'] = $info;
383
            }
384
            //moving to next question (tolerate \r\n or just \n)
385
            if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
386
                error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
387
388
                return 'ExerciseAikenErrorNoCorrectAnswerDefined';
389
            }
390
            if (empty($exercise_info['question'][$question_index]['answer'])) {
391
                error_log('Aiken: Error in question index '.$question_index.': no answer option given');
392
393
                return 'ExerciseAikenErrorNoAnswerOptionGiven';
394
            }
395
            $question_index++;
396
            //emptying answers array when moving to next question
397
            $answers_array = [];
398
        } else {
399
            if (empty($exercise_info['question'][$question_index]['title'])) {
400
                $exercise_info['question'][$question_index]['title'] = $info;
401
            }
402
            /*$question_index++;
403
            //emptying answers array when moving to next question
404
            $answers_array = [];
405
            $new_question = true;*/
406
        }
407
    }
408
    $total_questions = count($exercise_info['question']);
409
    $total_weight = !empty($_POST['total_weight']) ? (int) ($_POST['total_weight']) : 20;
410
    foreach ($exercise_info['question'] as $key => $question) {
411
        if (!isset($exercise_info['question'][$key]['weighting'])) {
412
            continue;
413
        }
414
        $exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
415
    }
416
417
    return true;
418
}
419
420
/**
421
 * Imports the zip file.
422
 *
423
 * @param array $array_file ($_FILES)
424
 *
425
 * @return bool
426
 */
427
function aiken_import_file($array_file)
428
{
429
    $unzip = 0;
430
    $process = process_uploaded_file($array_file, false);
431
    if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
432
        // if it's a zip, allow zip upload
433
        $unzip = 1;
434
    }
435
436
    if ($process && 1 == $unzip) {
437
        $imported = aiken_import_exercise($array_file['name']);
438
        if (is_numeric($imported) && !empty($imported)) {
439
            Display::addFlash(Display::return_message(get_lang('Uploaded.')));
440
441
            return $imported;
442
        } else {
443
            Display::addFlash(Display::return_message(get_lang($imported), 'error'));
444
445
            return false;
446
        }
447
    }
448
}
449