Passed
Push — master ( d0b226...f5358a )
by Julito
10:41
created

aiken_import_exercise()   F

Complexity

Conditions 28
Paths 100

Size

Total Lines 165
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

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