| Total Complexity | 137 |
| Total Lines | 837 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Cc13Quiz often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cc13Quiz, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Cc13Quiz extends Cc13Entities |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Extracts quiz data (non-question-bank) from QTI assessments. |
||
| 28 | */ |
||
| 29 | public function generateData() |
||
| 30 | { |
||
| 31 | $data = []; |
||
| 32 | $instances = $this->generateInstances(); |
||
| 33 | if (!empty($instances)) { |
||
| 34 | foreach ($instances as $instance) { |
||
| 35 | if (0 === (int) ($instance['is_question_bank'] ?? 0)) { |
||
| 36 | $data[] = $this->getQuizData($instance); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | return $data; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Creates a Chamilo quiz (Exercise) and inserts questions/answers. |
||
| 46 | * |
||
| 47 | * @param array $quiz |
||
| 48 | */ |
||
| 49 | public function storeQuiz($quiz): void |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | public function storeQuizzes($quizzes): void |
||
| 190 | { |
||
| 191 | if (!empty($quizzes)) { |
||
| 192 | foreach ($quizzes as $quiz) { |
||
| 193 | $this->storeQuiz($quiz); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getQuizData($instance) |
||
| 199 | { |
||
| 200 | $values = []; |
||
| 201 | if (!empty($instance)) { |
||
| 202 | $questions = []; |
||
| 203 | if (!empty($instance['questions'])) { |
||
| 204 | foreach ($instance['questions'] as $question) { |
||
| 205 | $questions[$question['id']] = [ |
||
| 206 | 'title' => $question['title'], |
||
| 207 | 'type' => $question['qtype'], |
||
| 208 | 'ponderation' => $question['defaultgrade'], |
||
| 209 | 'answers' => $question['answers'], |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | $values = [ |
||
| 214 | 'id' => $instance['id'], |
||
| 215 | 'title' => $instance['title'], |
||
| 216 | 'description' => $instance['description'], |
||
| 217 | 'timelimit' => $instance['options']['timelimit'], |
||
| 218 | 'max_attempts' => $instance['options']['max_attempts'], |
||
| 219 | 'questions' => $questions, |
||
| 220 | ]; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $values; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Build instances from Cc1p3Convert::$instances for both assessments and banks. |
||
| 228 | * Uses string keys as produced by the converter ('quiz', 'question_bank'). |
||
| 229 | */ |
||
| 230 | private function generateInstances() |
||
| 231 | { |
||
| 232 | $lastInstanceId = 0; |
||
| 233 | $lastQuestionId = 0; |
||
| 234 | $lastAnswerId = 0; |
||
| 235 | |||
| 236 | $instances = []; |
||
| 237 | |||
| 238 | // Keys as filled by Cc1p3Convert::createInstances() |
||
| 239 | $types = ['quiz', 'question_bank']; |
||
| 240 | |||
| 241 | foreach ($types as $type) { |
||
| 242 | if (empty(Cc1p3Convert::$instances['instances'][$type])) { |
||
| 243 | continue; |
||
| 244 | } |
||
| 245 | foreach (Cc1p3Convert::$instances['instances'][$type] as $instance) { |
||
| 246 | $is_question_bank = ('quiz' === $type) ? 0 : 1; |
||
| 247 | |||
| 248 | // Path to assessment.xml |
||
| 249 | $assessmentFile = $this->getExternalXml($instance['resource_identifier']); |
||
| 250 | |||
| 251 | if (empty($assessmentFile)) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | $assessment = $this->loadXmlResource( |
||
| 256 | Cc1p3Convert::$pathToManifestFolder.DIRECTORY_SEPARATOR.$assessmentFile |
||
| 257 | ); |
||
| 258 | |||
| 259 | if (empty($assessment)) { |
||
| 260 | continue; |
||
| 261 | } |
||
| 262 | |||
| 263 | Cc1p3Convert::logAction( |
||
| 264 | 'QTI loaded', |
||
| 265 | [ |
||
| 266 | 'resource' => $assessmentFile, |
||
| 267 | 'rootNS' => (string) ($assessment->documentElement?->namespaceURI ?? ''), |
||
| 268 | ] |
||
| 269 | ); |
||
| 270 | |||
| 271 | $replaceValues = ['unlimited' => 0]; |
||
| 272 | |||
| 273 | $questions = $this->getQuestions($assessment, $lastQuestionId, $lastAnswerId, \dirname($assessmentFile), $is_question_bank); |
||
| 274 | $questionCount = \is_array($questions) ? \count($questions) : 0; |
||
| 275 | |||
| 276 | Cc1p3Convert::logAction( |
||
| 277 | 'QTI questions detected', |
||
| 278 | [ |
||
| 279 | 'resource' => $assessmentFile, |
||
| 280 | 'count' => (int) $questionCount, |
||
| 281 | ] |
||
| 282 | ); |
||
| 283 | |||
| 284 | if ($questionCount > 0) { |
||
| 285 | $lastInstanceId++; |
||
| 286 | |||
| 287 | $instances[$instance['resource_identifier']]['questions'] = $questions; |
||
| 288 | $instances[$instance['resource_identifier']]['id'] = $lastInstanceId; |
||
| 289 | $instances[$instance['resource_identifier']]['title'] = $instance['title']; |
||
| 290 | $instances[$instance['resource_identifier']]['description'] = $this->getQuizDescription($assessment); |
||
| 291 | $instances[$instance['resource_identifier']]['is_question_bank'] = $is_question_bank; |
||
| 292 | $instances[$instance['resource_identifier']]['options']['timelimit'] = $this->getGlobalConfig($assessment, 'qmd_timelimit', 0); |
||
| 293 | $instances[$instance['resource_identifier']]['options']['max_attempts'] = $this->getGlobalConfig($assessment, 'cc_maxattempts', 0, $replaceValues); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | return $instances; |
||
| 299 | } |
||
| 300 | |||
| 301 | private function getGlobalConfig($assessment, $option, $defaultValue, $replaceValues = '') |
||
| 302 | { |
||
| 303 | $xp = $this->xp($assessment); |
||
| 304 | $nodes = $xp->query( |
||
| 305 | '/*[local-name()="questestinterop"]/*[local-name()="assessment"]' |
||
| 306 | .'/*[local-name()="qtimetadata"]/*[local-name()="qtimetadatafield"]' |
||
| 307 | ); |
||
| 308 | |||
| 309 | $response = ''; |
||
| 310 | foreach ($nodes as $field) { |
||
| 311 | $label = $xp->query('*[local-name()="fieldlabel"]/text()', $field)->item(0)?->nodeValue ?? ''; |
||
| 312 | if (0 === strcasecmp((string) $label, (string) $option)) { |
||
| 313 | $response = $xp->query('*[local-name()="fieldentry"]/text()', $field)->item(0)?->nodeValue ?? ''; |
||
| 314 | |||
| 315 | break; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | $response = trim((string) $response); |
||
| 320 | |||
| 321 | if (!empty($replaceValues)) { |
||
| 322 | foreach ($replaceValues as $key => $value) { |
||
| 323 | $response = ($key == $response) ? (string) $value : $response; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | return ('' === $response) ? $defaultValue : $response; |
||
| 328 | } |
||
| 329 | |||
| 330 | private function getQuizDescription(DOMDocument $assessment): string |
||
| 331 | { |
||
| 332 | $xp = $this->xp($assessment); |
||
| 333 | $n = $xp->query( |
||
| 334 | '/*[local-name()="questestinterop"]/*[local-name()="assessment"]' |
||
| 335 | .'/*[local-name()="rubric"]/*[local-name()="material"]/*[local-name()="mattext"]/text()' |
||
| 336 | ); |
||
| 337 | |||
| 338 | return $n && $n->length > 0 ? (string) $n->item(0)->nodeValue : ''; |
||
| 339 | } |
||
| 340 | |||
| 341 | private function getQuestions($assessment, &$lastQuestionId, &$last_answer_id, $rootPath, $is_question_bank) |
||
| 342 | { |
||
| 343 | $questions = []; |
||
| 344 | $xp = $this->xp($assessment); |
||
| 345 | |||
| 346 | $itemPath = $is_question_bank |
||
| 347 | ? '/*[local-name()="questestinterop"]/*[local-name()="objectbank"]/*[local-name()="item"]' |
||
| 348 | : '/*[local-name()="questestinterop"]/*[local-name()="assessment"]/*[local-name()="section"]/*[local-name()="item"]'; |
||
| 349 | |||
| 350 | $items = $xp->query($itemPath); |
||
| 351 | |||
| 352 | foreach ($items as $item) { |
||
| 353 | $questionIdentifier = trim((string) ($item->getAttribute('ident') ?? '')); |
||
| 354 | if ('' === $questionIdentifier) { |
||
| 355 | continue; |
||
| 356 | } |
||
| 357 | |||
| 358 | // Title inside <presentation> (with/without <flow>) |
||
| 359 | $titleNode = $xp->query( |
||
| 360 | '(*[local-name()="presentation"]/*[local-name()="flow"]/*[local-name()="material"]/*[local-name()="mattext"]' |
||
| 361 | .' | *[local-name()="presentation"]/*[local-name()="material"]/*[local-name()="mattext"])[1]/text()', |
||
| 362 | $item |
||
| 363 | ); |
||
| 364 | $questionTitle = $titleNode->item(0)?->nodeValue ?? ''; |
||
| 365 | |||
| 366 | $qTypeInfo = $this->getQuestionType($questionIdentifier, $assessment); |
||
| 367 | if (empty($qTypeInfo['qtype'])) { |
||
| 368 | continue; |
||
| 369 | } |
||
| 370 | |||
| 371 | $lastQuestionId++; |
||
| 372 | $questions[$questionIdentifier]['id'] = $lastQuestionId; |
||
| 373 | |||
| 374 | $questionTitle = $this->updateSources($questionTitle, $rootPath); |
||
| 375 | $questionTitle = '' !== $questionTitle ? str_replace('%24', '$', $this->includeTitles($questionTitle)) : ''; |
||
| 376 | |||
| 377 | $questionname = $item->getAttribute('title') ?? ''; |
||
| 378 | |||
| 379 | $questions[$questionIdentifier]['title'] = $questionTitle; |
||
| 380 | $questions[$questionIdentifier]['name'] = $questionname; |
||
| 381 | $questions[$questionIdentifier]['identifier'] = $questionIdentifier; |
||
| 382 | $questions[$questionIdentifier]['qtype'] = $qTypeInfo['qtype']; // 'unique_answer', 'multiple_answer', 'fib', 'essay' |
||
| 383 | $questions[$questionIdentifier]['cc_type'] = $qTypeInfo['cc']; // raw CC type string |
||
| 384 | $questions[$questionIdentifier]['feedback'] = $this->getGeneralFeedback($assessment, $questionIdentifier); |
||
| 385 | $questions[$questionIdentifier]['defaultgrade'] = $this->getDefaultgrade($assessment, $questionIdentifier); |
||
| 386 | $questions[$questionIdentifier]['answers'] = $this->getAnswers($questionIdentifier, $assessment, $last_answer_id); |
||
| 387 | } |
||
| 388 | |||
| 389 | return !empty($questions) ? $questions : ''; |
||
| 390 | } |
||
| 391 | |||
| 392 | private function getDefaultgrade($assessment, $questionIdentifier) |
||
| 393 | { |
||
| 394 | $xp = $this->xp($assessment); |
||
| 395 | $n = $xp->query( |
||
| 396 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 397 | .'//*[local-name()="qtimetadatafield"][*[local-name()="fieldlabel" and text()="cc_weighting"]]' |
||
| 398 | .'/*[local-name()="fieldentry"]/text()' |
||
| 399 | ); |
||
| 400 | $result = 1; |
||
| 401 | if ($n && $n->length > 0) { |
||
| 402 | $resp = (int) $n->item(0)->nodeValue; |
||
| 403 | if ($resp >= 0 && $resp <= 99) { |
||
| 404 | $result = $resp; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | return $result; |
||
| 409 | } |
||
| 410 | |||
| 411 | private function getGeneralFeedback($assessment, $questionIdentifier) |
||
| 412 | { |
||
| 413 | $xp = $this->xp($assessment); |
||
| 414 | |||
| 415 | $respconditions = $xp->query( |
||
| 416 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 417 | .'/*[local-name()="resprocessing"]/*[local-name()="respcondition"]' |
||
| 418 | ); |
||
| 419 | |||
| 420 | $feedbackIds = []; |
||
| 421 | foreach ($respconditions as $rc) { |
||
| 422 | $cont = strtolower((string) ($rc->getAttribute('continue') ?? '')); |
||
| 423 | if ('yes' === $cont) { |
||
| 424 | $dfs = $xp->query('*[local-name()="displayfeedback"]', $rc); |
||
| 425 | foreach ($dfs as $df) { |
||
| 426 | $link = $df->getAttribute('linkrefid') ?? ''; |
||
| 427 | if ('' !== $link) { |
||
| 428 | $feedbackIds[] = $link; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | $feedback = ''; |
||
| 435 | foreach ($feedbackIds as $fid) { |
||
| 436 | $texts = $xp->query( |
||
| 437 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 438 | .'/*[local-name()="itemfeedback" and @ident="'.$fid.'"]' |
||
| 439 | .'/*[local-name()="flow_mat"]/*[local-name()="material"]/*[local-name()="mattext"]/text()' |
||
| 440 | ); |
||
| 441 | if ($texts && $texts->length > 0) { |
||
| 442 | $feedback .= $texts->item(0)->nodeValue.' '; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | return trim($feedback); |
||
| 447 | } |
||
| 448 | |||
| 449 | private function getFeedback($assessment, $identifier, $itemIdentifier, $questionType) |
||
| 450 | { |
||
| 451 | $xp = $this->xp($assessment); |
||
| 452 | |||
| 453 | $rcs = $xp->query( |
||
| 454 | '//*[local-name()="item" and @ident="'.$itemIdentifier.'"]' |
||
| 455 | .'/*[local-name()="resprocessing"]/*[local-name()="respcondition"]' |
||
| 456 | ); |
||
| 457 | |||
| 458 | $ids = []; |
||
| 459 | foreach ($rcs as $rc) { |
||
| 460 | $ve = $xp->query('*[local-name()="conditionvar"]/*[local-name()="varequal"]/text()', $rc) |
||
| 461 | ->item(0)?->nodeValue ?? '' |
||
| 462 | ; |
||
| 463 | if (0 === strcasecmp((string) $ve, (string) $identifier) || ('essay' === $questionType)) { |
||
| 464 | $dfs = $xp->query('*[local-name()="displayfeedback"]', $rc); |
||
| 465 | foreach ($dfs as $df) { |
||
| 466 | $link = $df->getAttribute('linkrefid') ?? ''; |
||
| 467 | if ('' !== $link) { |
||
| 468 | $ids[] = $link; |
||
| 469 | } |
||
| 470 | } |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | $feedback = ''; |
||
| 475 | foreach ($ids as $fid) { |
||
| 476 | $texts = $xp->query( |
||
| 477 | '//*[local-name()="item" and @ident="'.$itemIdentifier.'"]' |
||
| 478 | .'/*[local-name()="itemfeedback" and @ident="'.$fid.'"]' |
||
| 479 | .'/*[local-name()="flow_mat"]/*[local-name()="material"]/*[local-name()="mattext"]/text()' |
||
| 480 | ); |
||
| 481 | if ($texts && $texts->length > 0) { |
||
| 482 | $feedback .= $texts->item(0)->nodeValue.' '; |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | return trim($feedback); |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Namespace-agnostic FIB answers (Fill-in-the-blank). |
||
| 491 | * |
||
| 492 | * @param mixed $questionIdentifier |
||
| 493 | * @param mixed $identifier |
||
| 494 | * @param mixed $assessment |
||
| 495 | * @param mixed $lastAnswerId |
||
| 496 | */ |
||
| 497 | private function getAnswersFib($questionIdentifier, $identifier, $assessment, &$lastAnswerId) |
||
| 498 | { |
||
| 499 | $xp = $this->xp($assessment); |
||
| 500 | |||
| 501 | $correct = []; |
||
| 502 | $incorrect = []; |
||
| 503 | |||
| 504 | // All respconditions for this item |
||
| 505 | $responseItems = $xp->query( |
||
| 506 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 507 | .'/*[local-name()="resprocessing"]/*[local-name()="respcondition"]' |
||
| 508 | ); |
||
| 509 | |||
| 510 | // Find the condition with setvar=100 (correct) and collect varequal values |
||
| 511 | $correctResp = $xp->query( |
||
| 512 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 513 | .'/*[local-name()="resprocessing"]/*[local-name()="respcondition"][*[local-name()="setvar" and normalize-space(text())="100"]]' |
||
| 514 | ); |
||
| 515 | |||
| 516 | if ($correctResp && $correctResp->length > 0) { |
||
| 517 | $canswers = $xp->query('*[local-name()="conditionvar"]/*[local-name()="varequal"]/text()', $correctResp->item(0)); |
||
| 518 | foreach ($canswers as $cnode) { |
||
| 519 | $answertitle = trim((string) $cnode->nodeValue); |
||
| 520 | if ('' === $answertitle) { |
||
| 521 | continue; |
||
| 522 | } |
||
| 523 | $lastAnswerId++; |
||
| 524 | $correct[$answertitle] = [ |
||
| 525 | 'id' => $lastAnswerId, |
||
| 526 | 'title' => $answertitle, |
||
| 527 | 'score' => 1, |
||
| 528 | 'feedback' => '', |
||
| 529 | 'case' => 0, |
||
| 530 | ]; |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | // Iterate through all respconditions to attach feedback and collect incorrects |
||
| 535 | foreach ($responseItems as $rc) { |
||
| 536 | // Skip the correct one (setvar=100) here |
||
| 537 | $sv = $xp->query('*[local-name()="setvar"]/text()', $rc)->item(0)?->nodeValue ?? null; |
||
| 538 | if (null !== $sv && '100' === trim($sv)) { |
||
| 539 | continue; |
||
| 540 | } |
||
| 541 | |||
| 542 | $ve = $xp->query('*[local-name()="conditionvar"]/*[local-name()="varequal"]/text()', $rc); |
||
| 543 | if (!$ve || 0 === $ve->length) { |
||
| 544 | continue; |
||
| 545 | } |
||
| 546 | $answerTitle = trim((string) $ve->item(0)->nodeValue); |
||
| 547 | |||
| 548 | // Gather feedback ids |
||
| 549 | $dfs = $xp->query('*[local-name()="displayfeedback"]', $rc); |
||
| 550 | $fbids = []; |
||
| 551 | foreach ($dfs as $df) { |
||
| 552 | $link = $df->getAttribute('linkrefid') ?? ''; |
||
| 553 | if ('' !== $link) { |
||
| 554 | $fbids[] = $link; |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | // Resolve feedback text(s) |
||
| 559 | $feedback = ''; |
||
| 560 | foreach ($fbids as $fid) { |
||
| 561 | $fbt = $xp->query( |
||
| 562 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 563 | .'/*[local-name()="itemfeedback" and @ident="'.$fid.'"]' |
||
| 564 | .'/*[local-name()="flow_mat"]/*[local-name()="material"]/*[local-name()="mattext"]/text()' |
||
| 565 | ); |
||
| 566 | if ($fbt && $fbt->length > 0) { |
||
| 567 | $feedback .= $fbt->item(0)->nodeValue.' '; |
||
| 568 | } |
||
| 569 | } |
||
| 570 | $feedback = trim($feedback); |
||
| 571 | |||
| 572 | if (\array_key_exists($answerTitle, $correct)) { |
||
| 573 | $correct[$answerTitle]['feedback'] = $feedback; |
||
| 574 | } else { |
||
| 575 | $lastAnswerId++; |
||
| 576 | $incorrect[] = [ |
||
| 577 | 'id' => $lastAnswerId, |
||
| 578 | 'title' => $answerTitle, |
||
| 579 | 'score' => 0, |
||
| 580 | 'feedback' => $feedback, |
||
| 581 | 'case' => 0, |
||
| 582 | ]; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | $answers = array_merge($correct, $incorrect); |
||
| 587 | |||
| 588 | return empty($answers) ? '' : $answers; |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Namespace-agnostic Pattern Match answers. |
||
| 593 | * |
||
| 594 | * @param mixed $questionIdentifier |
||
| 595 | * @param mixed $identifier |
||
| 596 | * @param mixed $assessment |
||
| 597 | * @param mixed $lastAnswerId |
||
| 598 | */ |
||
| 599 | private function getAnswersPatternMatch($questionIdentifier, $identifier, $assessment, &$lastAnswerId) |
||
| 664 | } |
||
| 665 | |||
| 666 | private function getAnswers($identifier, $assessment, &$lastAnswerId) |
||
| 748 | } |
||
| 749 | |||
| 750 | private function getScore($assessment, $identifier, $questionIdentifier) |
||
| 751 | { |
||
| 752 | $xp = $this->xp($assessment); |
||
| 753 | |||
| 754 | $rcs = $xp->query( |
||
| 755 | '//*[local-name()="item" and @ident="'.$questionIdentifier.'"]' |
||
| 756 | .'/*[local-name()="resprocessing"]/*[local-name()="respcondition"]' |
||
| 757 | ); |
||
| 758 | |||
| 759 | $scoreValue = null; |
||
| 760 | foreach ($rcs as $rc) { |
||
| 761 | $ve = $xp->query('*[local-name()="conditionvar"]/*[local-name()="varequal"]/text()', $rc) |
||
| 762 | ->item(0)?->nodeValue ?? '' |
||
| 763 | ; |
||
| 764 | if (0 === strcasecmp((string) $ve, (string) $identifier)) { |
||
| 765 | $sv = $xp->query('*[local-name()="setvar"]/text()', $rc)->item(0)?->nodeValue ?? null; |
||
| 766 | if (null !== $sv) { |
||
| 767 | $scoreValue = trim($sv); |
||
| 768 | |||
| 769 | break; |
||
| 770 | } |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | // Normalize to [0,1] granularity expected later. |
||
| 775 | if (null === $scoreValue) { |
||
| 776 | return '0.0000000'; |
||
| 777 | } |
||
| 778 | |||
| 779 | return ((float) $scoreValue > 0) ? '1.0000000' : '0.0000000'; |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Reads cc_profile and maps to our internal qtypes. |
||
| 784 | * Returns: |
||
| 785 | * - 'qtype' => one of: unique_answer, multiple_answer, fib, essay |
||
| 786 | * - 'cc' => the normalized cc_profile string. |
||
| 787 | * |
||
| 788 | * @param mixed $identifier |
||
| 789 | * @param mixed $assessment |
||
| 790 | */ |
||
| 791 | private function getQuestionType($identifier, $assessment) |
||
| 792 | { |
||
| 793 | // Namespace-agnostic XPath |
||
| 794 | $x = new DOMXPath($assessment); |
||
| 795 | $metadata = $x->query( |
||
| 796 | '//*[local-name()="item" and @ident="'.$identifier.'"]'. |
||
| 797 | '/*[local-name()="itemmetadata"]/*[local-name()="qtimetadata"]/*[local-name()="qtimetadatafield"]' |
||
| 798 | ); |
||
| 799 | |||
| 800 | $type = ''; |
||
| 801 | foreach ($metadata as $field) { |
||
| 802 | $label = $x->query('./*[local-name()="fieldlabel"]/text()', $field); |
||
| 803 | $lab = ($label && $label->length > 0) ? trim((string) $label->item(0)->nodeValue) : ''; |
||
| 804 | if (0 === strcasecmp($lab, 'cc_profile')) { |
||
| 805 | $entry = $x->query('./*[local-name()="fieldentry"]/text()', $field); |
||
| 806 | $type = ($entry && $entry->length > 0) ? trim((string) $entry->item(0)->nodeValue) : ''; |
||
| 807 | |||
| 808 | break; |
||
| 809 | } |
||
| 810 | } |
||
| 811 | |||
| 812 | // Normalize patterns like "cc.multiple_choice.v0p1" -> "multiple_choice" |
||
| 813 | $raw = $type; |
||
| 814 | $type = preg_replace('~^cc\.~i', '', (string) $type); |
||
| 815 | $type = preg_replace('~\.v\d+p\d+$~i', '', (string) $type); |
||
| 816 | |||
| 817 | // Map to internal set |
||
| 818 | $qtype = ''; |
||
| 819 | |||
| 820 | switch ($type) { |
||
| 821 | case 'multiple_choice': |
||
| 822 | case 'true_false': |
||
| 823 | $qtype = 'unique_answer'; |
||
| 824 | |||
| 825 | break; |
||
| 826 | |||
| 827 | case 'multiple_response': |
||
| 828 | $qtype = 'multiple_answer'; |
||
| 829 | |||
| 830 | break; |
||
| 831 | |||
| 832 | case 'fib': |
||
| 833 | case 'pattern_match': |
||
| 834 | $qtype = 'fib'; |
||
| 835 | |||
| 836 | break; |
||
| 837 | |||
| 838 | case 'essay': |
||
| 839 | $qtype = 'essay'; |
||
| 840 | |||
| 841 | break; |
||
| 842 | |||
| 843 | default: |
||
| 844 | $qtype = ''; |
||
| 845 | } |
||
| 846 | |||
| 847 | Cc1p3Convert::logAction( |
||
| 848 | 'QTI cc_profile mapped', |
||
| 849 | ['identifier' => $identifier, 'raw' => $raw, 'norm' => $type, 'qtype' => $qtype] |
||
| 850 | ); |
||
| 851 | |||
| 852 | return ['qtype' => $qtype, 'cc' => $type]; |
||
| 853 | } |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Build an NS-agnostic XPath (we'll use local-name() in expressions). |
||
| 857 | */ |
||
| 858 | private function xp(DOMDocument $doc): DOMXPath |
||
| 861 | } |
||
| 862 | } |
||
| 863 |