protected function buildResponseDeclaration($responseIdentifier, $validation) { /** @var clozetext_validation $validation */ // Since we split {{response}} to multiple interactions, so we would have multiple <responseDeclaration> as needed as well $responseDeclarationCollection = new ResponseDeclarationCollection(); // Process `valid_response` foreach ($validation->get_valid_response()->get_value() as $index => $value) { // We make assumption about interaction identifier shall always be the appended with index, ie. `_0` $responseDeclaration = new ResponseDeclaration($responseIdentifier . '_' . $index); $responseDeclaration->setCardinality(Cardinality::SINGLE); $responseDeclaration->setBaseType(BaseType::STRING); $valueCollection = new ValueCollection(); $valueCollection->attach(new Value($value)); $validResponseScore = floatval($validation->get_valid_response()->get_score()); $mapEntriesCollection = new MapEntryCollection(); $mapEntriesCollection->attach(new MapEntry($value, $validResponseScore, $this->isCaseSensitive)); if (count($validation->get_alt_responses()) > 0) { /** @var clozetext_validation_alt_responses_item $alt */ foreach ($validation->get_alt_responses() as $alt) { // Assuming if (!is_null($alt->get_value()) && isset($alt->get_value()[$index])) { $alternativeValue = $alt->get_value()[$index]; $alternativeScore = floatval($alt->get_score()); $valueCollection->attach(new Value($alternativeValue)); $mapEntriesCollection->attach(new MapEntry($alternativeValue, $alternativeScore, $this->isCaseSensitive)); } } } $responseDeclaration->setCorrectResponse(new CorrectResponse($valueCollection)); $responseDeclaration->setMapping(new Mapping($mapEntriesCollection)); $responseDeclarationCollection->attach($responseDeclaration); } return $responseDeclarationCollection; }
public function build($itemIdentifier, $itemLabel, array $questions, $content = '') { // Initialise our <assessmentItem> $assessmentItem = new AssessmentItem($itemIdentifier, $itemIdentifier, false); $assessmentItem->setLabel($itemLabel); $assessmentItem->setOutcomeDeclarations($this->buildOutcomeDeclarations()); $assessmentItem->setToolName('Learnosity'); // Store interactions on this array to later be placed on <itemBody> $interactions = []; $responseDeclarationCollection = new ResponseDeclarationCollection(); $responseProcessingTemplates = []; foreach ($questions as $question) { /** @var Question $question */ // Map the `questions` and its validation objects to be placed at <itemBody> // The extraContent usually comes from `stimulus` of item that mapped to inline interaction and has no `prompt` list($interaction, $responseDeclaration, $responseProcessing, $extraContent) = $this->map($question); if (!empty($responseDeclaration)) { // TODO: Need to tidy this up // Well sometimes we can have multiple response declarations, ie. clozetext if ($responseDeclaration instanceof ResponseDeclarationCollection) { $responseDeclarationCollection->merge($responseDeclaration); } else { $responseDeclarationCollection->attach($responseDeclaration); } } if (!empty($responseProcessing)) { /** @var ResponseProcessing $responseProcessing */ $responseProcessingTemplates[] = $responseProcessing->getTemplate(); } $interactions[$question->get_reference()]['interaction'] = $interaction; if (!empty($extraContent)) { $interactions[$question->get_reference()]['extraContent'] = $extraContent; } } // Build <itemBody> $assessmentItem->setItemBody($this->itemBodyBuilder->buildItemBody($interactions, $content)); // Map <responseDeclaration> if (!empty($responseDeclarationCollection)) { $assessmentItem->setResponseDeclarations($responseDeclarationCollection); } // Map <responseProcessing> - combine response processing from questions // TODO: Tidy up this stuff if (!empty($responseProcessingTemplates)) { $templates = array_unique($responseProcessingTemplates); $isOnlyMatchCorrect = count($templates) === 1 && $templates[0] === Constants::RESPONSE_PROCESSING_TEMPLATE_MATCH_CORRECT; $responseProcessing = new ResponseProcessing(); $responseProcessing->setTemplate($isOnlyMatchCorrect ? Constants::RESPONSE_PROCESSING_TEMPLATE_MATCH_CORRECT : Constants::RESPONSE_PROCESSING_TEMPLATE_MAP_RESPONSE); $assessmentItem->setResponseProcessing($responseProcessing); } return $assessmentItem; }
protected function buildResponseDeclaration($responseIdentifier, $validation) { /** @var clozedropdown_validation $validation */ // Since we split {{response}} to multiple interactions, so we would have multiple <responseDeclaration> as needed as well $responseDeclarationCollection = new ResponseDeclarationCollection(); foreach ($validation->get_valid_response()->get_value() as $index => $value) { $valueIdentifierMap = $this->valueIdentifierMapPerInlineChoices[$index]; $valueCollection = new ValueCollection(); $valueCollection->attach(new Value($valueIdentifierMap[$value])); // We make assumption about interaction identifier shall always be the appended with index, ie. `_0` $responseDeclaration = new ResponseDeclaration($responseIdentifier . '_' . $index); $responseDeclaration->setBaseType(BaseType::IDENTIFIER); $responseDeclaration->setCardinality(Cardinality::SINGLE); $responseDeclaration->setCorrectResponse(new CorrectResponse($valueCollection)); $responseDeclarationCollection->attach($responseDeclaration); } return $responseDeclarationCollection; }