| Conditions | 58 |
| Paths | 272 |
| Total Lines | 276 |
| Code Lines | 192 |
| 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 |
||
| 344 | function parseQti2($xmlData) |
||
| 345 | { |
||
| 346 | global $exerciseInfo; |
||
| 347 | global $questionTempDir; |
||
| 348 | global $resourcesLinks; |
||
| 349 | |||
| 350 | $crawler = new Crawler($xmlData); |
||
| 351 | $nodes = $crawler->filter('*'); |
||
| 352 | |||
| 353 | $currentQuestionIdent = ''; |
||
| 354 | $currentAnswerId = ''; |
||
| 355 | $currentQuestionItemBody = ''; |
||
| 356 | $cardinality = ''; |
||
| 357 | $nonHTMLTagToAvoid = [ |
||
| 358 | "simpleChoice", |
||
| 359 | "choiceInteraction", |
||
| 360 | "inlineChoiceInteraction", |
||
| 361 | "inlineChoice", |
||
| 362 | "soMPLEMATCHSET", |
||
| 363 | "simpleAssociableChoice", |
||
| 364 | "textEntryInteraction", |
||
| 365 | "feedbackInline", |
||
| 366 | "matchInteraction", |
||
| 367 | 'extendedTextInteraction', |
||
| 368 | "itemBody", |
||
| 369 | "br", |
||
| 370 | "img", |
||
| 371 | ]; |
||
| 372 | $currentMatchSet = null; |
||
| 373 | |||
| 374 | /** @var DOMElement $node */ |
||
| 375 | foreach ($nodes as $node) { |
||
| 376 | if ('#text' === $node->nodeName) { |
||
| 377 | continue; |
||
| 378 | } |
||
| 379 | |||
| 380 | switch ($node->nodeName) { |
||
| 381 | case 'assessmentItem': |
||
| 382 | $currentQuestionIdent = $node->getAttribute('identifier'); |
||
| 383 | |||
| 384 | $exerciseInfo['question'][$currentQuestionIdent] = [ |
||
| 385 | 'answer' => [], |
||
| 386 | 'correct_answers' => [], |
||
| 387 | 'title' => $node->getAttribute('title'), |
||
| 388 | 'category' => $node->getAttribute('category'), |
||
| 389 | 'type' => '', |
||
| 390 | 'tempdir' => $questionTempDir, |
||
| 391 | ]; |
||
| 392 | break; |
||
| 393 | case 'section': |
||
| 394 | $title = $node->getAttribute('title'); |
||
| 395 | |||
| 396 | if (!empty($title)) { |
||
| 397 | $exerciseInfo['name'] = $title; |
||
| 398 | } |
||
| 399 | break; |
||
| 400 | case 'responseDeclaration': |
||
| 401 | if ('multiple' === $node->getAttribute('cardinality')) { |
||
| 402 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = MCMA; |
||
| 403 | $cardinality = 'multiple'; |
||
| 404 | } |
||
| 405 | |||
| 406 | if ('single' === $node->getAttribute('cardinality')) { |
||
| 407 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = MCUA; |
||
| 408 | $cardinality = 'single'; |
||
| 409 | } |
||
| 410 | |||
| 411 | $currentAnswerId = $node->getAttribute('identifier'); |
||
| 412 | break; |
||
| 413 | case 'inlineChoiceInteraction': |
||
| 414 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = FIB; |
||
| 415 | $exerciseInfo['question'][$currentQuestionIdent]['subtype'] = 'LISTBOX_FILL'; |
||
| 416 | $currentAnswerId = $node->getAttribute('responseIdentifier'); |
||
| 417 | break; |
||
| 418 | case 'inlineChoice': |
||
| 419 | $answerIdentifier = $exerciseInfo['question'][$currentQuestionIdent]['correct_answers'][$currentAnswerId]; |
||
| 420 | |||
| 421 | if ($node->getAttribute('identifier') == $answerIdentifier) { |
||
| 422 | $currentQuestionItemBody = str_replace( |
||
| 423 | "**claroline_start**".$currentAnswerId."**claroline_end**", |
||
| 424 | "[".$node->nodeValue."]", |
||
| 425 | $currentQuestionItemBody |
||
| 426 | ); |
||
| 427 | } else { |
||
| 428 | if (!isset($exerciseInfo['question'][$currentQuestionIdent]['wrong_answers'])) { |
||
| 429 | $exerciseInfo['question'][$currentQuestionIdent]['wrong_answers'] = []; |
||
| 430 | } |
||
| 431 | |||
| 432 | $exerciseInfo['question'][$currentQuestionIdent]['wrong_answers'][] = $node->nodeValue; |
||
| 433 | } |
||
| 434 | break; |
||
| 435 | case 'textEntryInteraction': |
||
| 436 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = FIB; |
||
| 437 | $exerciseInfo['question'][$currentQuestionIdent]['subtype'] = 'TEXTFIELD_FILL'; |
||
| 438 | $exerciseInfo['question'][$currentQuestionIdent]['response_text'] = $currentQuestionItemBody; |
||
| 439 | break; |
||
| 440 | case 'matchInteraction': |
||
| 441 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = MATCHING; |
||
| 442 | break; |
||
| 443 | case 'extendedTextInteraction': |
||
| 444 | $exerciseInfo['question'][$currentQuestionIdent]['type'] = FREE_ANSWER; |
||
| 445 | $exerciseInfo['question'][$currentQuestionIdent]['description'] = $node->nodeValue; |
||
| 446 | break; |
||
| 447 | case 'simpleMatchSet': |
||
| 448 | if (!isset($currentMatchSet)) { |
||
| 449 | $currentMatchSet = 1; |
||
| 450 | } else { |
||
| 451 | $currentMatchSet++; |
||
| 452 | } |
||
| 453 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentMatchSet] = []; |
||
| 454 | break; |
||
| 455 | case 'simpleAssociableChoice': |
||
| 456 | $currentAssociableChoice = $node->getAttribute('identifier'); |
||
| 457 | |||
| 458 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentMatchSet][$currentAssociableChoice] = trim($node->nodeValue); |
||
| 459 | break; |
||
| 460 | case 'simpleChoice': |
||
| 461 | $currentAnswerId = $node->getAttribute('identifier'); |
||
| 462 | if (!isset($exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId])) { |
||
| 463 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId] = []; |
||
| 464 | } |
||
| 465 | |||
| 466 | if (!isset($exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId]['value'])) { |
||
| 467 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId]['value'] = trim( |
||
| 468 | $node->nodeValue |
||
| 469 | ); |
||
| 470 | } else { |
||
| 471 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId]['value'] .= '' |
||
| 472 | .trim($node->nodeValue); |
||
| 473 | } |
||
| 474 | break; |
||
| 475 | case 'mapEntry': |
||
| 476 | if (in_array($node->parentNode->nodeName, ['mapping', 'mapEntry'])) { |
||
| 477 | $answer_id = $node->getAttribute('mapKey'); |
||
| 478 | |||
| 479 | if (!isset($exerciseInfo['question'][$currentQuestionIdent]['weighting'])) { |
||
| 480 | $exerciseInfo['question'][$currentQuestionIdent]['weighting'] = []; |
||
| 481 | } |
||
| 482 | |||
| 483 | $exerciseInfo['question'][$currentQuestionIdent]['weighting'][$answer_id] = $node->getAttribute( |
||
| 484 | 'mappedValue' |
||
| 485 | ); |
||
| 486 | } |
||
| 487 | break; |
||
| 488 | case 'mapping': |
||
| 489 | $defaultValue = $node->getAttribute('defaultValue'); |
||
| 490 | |||
| 491 | if (!empty($defaultValue)) { |
||
| 492 | $exerciseInfo['question'][$currentQuestionIdent]['default_weighting'] = $defaultValue; |
||
| 493 | } |
||
| 494 | // no break ? |
||
| 495 | case 'itemBody': |
||
| 496 | $nodeValue = $node->nodeValue; |
||
| 497 | |||
| 498 | $currentQuestionItemBody = ''; |
||
| 499 | |||
| 500 | /** @var DOMElement $childNode */ |
||
| 501 | foreach ($node->childNodes as $childNode) { |
||
| 502 | if ('#text' === $childNode->nodeName) { |
||
| 503 | continue; |
||
| 504 | } |
||
| 505 | |||
| 506 | if (!in_array($childNode->nodeName, $nonHTMLTagToAvoid)) { |
||
| 507 | $currentQuestionItemBody .= '<'.$childNode->nodeName; |
||
| 508 | |||
| 509 | if ($childNode->attributes) { |
||
| 510 | foreach ($childNode->attributes as $attribute) { |
||
| 511 | $currentQuestionItemBody .= ' '.$attribute->nodeName.'="'.$attribute->nodeValue.'"'; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | $currentQuestionItemBody .= '>' |
||
| 516 | .$childNode->nodeValue |
||
| 517 | .'</'.$node->nodeName.'>'; |
||
| 518 | |||
| 519 | continue; |
||
| 520 | } |
||
| 521 | |||
| 522 | if ('inlineChoiceInteraction' === $childNode->nodeName) { |
||
| 523 | $currentQuestionItemBody .= "**claroline_start**" |
||
| 524 | .$childNode->attr('responseIdentifier') |
||
| 525 | ."**claroline_end**"; |
||
| 526 | |||
| 527 | continue; |
||
| 528 | } |
||
| 529 | |||
| 530 | if ('textEntryInteraction' === $childNode->nodeName) { |
||
| 531 | $correct_answer_value = $exerciseInfo['question'][$currentQuestionIdent]['correct_answers'][$currentAnswerId]; |
||
| 532 | $currentQuestionItemBody .= "[".$correct_answer_value."]"; |
||
| 533 | |||
| 534 | continue; |
||
| 535 | } |
||
| 536 | |||
| 537 | if ('br' === $childNode->nodeName) { |
||
| 538 | $currentQuestionItemBody .= '<br>'; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | // Replace relative links by links to the documents in the course |
||
| 543 | // $resourcesLinks is only defined by qtiProcessManifest() |
||
| 544 | if (isset($resourcesLinks) && isset($resourcesLinks['manifest']) && isset($resourcesLinks['web'])) { |
||
| 545 | foreach ($resourcesLinks['manifest'] as $key => $value) { |
||
| 546 | $nodeValue = preg_replace('|'.$value.'|', $resourcesLinks['web'][$key], $nodeValue); |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | $currentQuestionItemBody .= $node->firstChild->nodeValue; |
||
| 551 | |||
| 552 | if ($exerciseInfo['question'][$currentQuestionIdent]['type'] == FIB) { |
||
| 553 | $exerciseInfo['question'][$currentQuestionIdent]['response_text'] = $currentQuestionItemBody; |
||
| 554 | } else { |
||
| 555 | if ($exerciseInfo['question'][$currentQuestionIdent]['type'] == FREE_ANSWER) { |
||
| 556 | $currentQuestionItemBody = trim($currentQuestionItemBody); |
||
| 557 | |||
| 558 | if (!empty($currentQuestionItemBody)) { |
||
| 559 | $exerciseInfo['question'][$currentQuestionIdent]['description'] = $currentQuestionItemBody; |
||
| 560 | } |
||
| 561 | } else { |
||
| 562 | $exerciseInfo['question'][$currentQuestionIdent]['statement'] = $currentQuestionItemBody; |
||
| 563 | } |
||
| 564 | } |
||
| 565 | break; |
||
| 566 | case 'img': |
||
| 567 | $exerciseInfo['question'][$currentQuestionIdent]['attached_file_url'] = $node->getAttribute('src'); |
||
| 568 | break; |
||
| 569 | case 'order': |
||
| 570 | $orderType = $node->getAttribute('order_type'); |
||
| 571 | |||
| 572 | if (!empty($orderType)) { |
||
| 573 | $exerciseInfo['order_type'] = $orderType; |
||
| 574 | } |
||
| 575 | break; |
||
| 576 | case 'feedbackInline': |
||
| 577 | if (!isset($exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId]['feedback'])) { |
||
| 578 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId] = trim( |
||
| 579 | $node->nodeValue |
||
| 580 | ); |
||
| 581 | } else { |
||
| 582 | $exerciseInfo['question'][$currentQuestionIdent]['answer'][$currentAnswerId]['feedback'] .= '' |
||
| 583 | .trim( |
||
| 584 | $node->nodeValue |
||
| 585 | ); |
||
| 586 | } |
||
| 587 | break; |
||
| 588 | case 'value': |
||
| 589 | if ('correctResponse' === $node->parentNode->nodeName) { |
||
| 590 | $nodeValue = trim($node->nodeValue); |
||
| 591 | |||
| 592 | if ('single' === $cardinality) { |
||
| 593 | $exerciseInfo['question'][$currentQuestionIdent]['correct_answers'][$nodeValue] = $nodeValue; |
||
| 594 | } else { |
||
| 595 | $exerciseInfo['question'][$currentQuestionIdent]['correct_answers'][] = $nodeValue; |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | if ('outcomeDeclaration' === $node->parentNode->parentNode->nodeName) { |
||
| 600 | $nodeValue = trim($node->nodeValue); |
||
| 601 | |||
| 602 | if (!empty($nodeValue)) { |
||
| 603 | $exerciseInfo['question'][$currentQuestionIdent]['weighting'][0] = $nodeValue; |
||
| 604 | } |
||
| 605 | } |
||
| 606 | break; |
||
| 607 | case 'mattext': |
||
| 608 | if ('flow_mat' === $node->parentNode->parentNode->nodeName && |
||
| 609 | ('presentation_material' === $node->parentNode->parentNode->parentNode->nodeName || |
||
| 610 | 'section' === $node->parentNode->parentNode->parentNode->nodeName |
||
| 611 | ) |
||
| 612 | ) { |
||
| 613 | $nodeValue = trim($node->nodeValue); |
||
| 614 | |||
| 615 | if (!empty($nodeValue)) { |
||
| 616 | $exerciseInfo['description'] = $node->nodeValue; |
||
| 617 | } |
||
| 618 | } |
||
| 619 | break; |
||
| 620 | } |
||
| 725 |