| Conditions | 23 |
| Paths | 361 |
| Total Lines | 122 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 449 |