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; }
public function testMarshallMaximal() { $identifier = 'Q01'; $title = 'Test Item'; $timeDependent = true; $adaptive = true; $lang = 'en-YO'; // Yoda English ;) $responseDeclarations = new ResponseDeclarationCollection(); $responseDeclarations[] = new ResponseDeclaration('resp1', BaseType::INTEGER, Cardinality::SINGLE); $responseDeclarations[] = new ResponseDeclaration('resp2', BaseType::FLOAT, Cardinality::SINGLE); $outcomeDeclarations = new OutcomeDeclarationCollection(); $outcomeDeclarations[] = new OutcomeDeclaration('out1', BaseType::BOOLEAN, Cardinality::MULTIPLE); $outcomeDeclarations[] = new OutcomeDeclaration('out2', BaseType::IDENTIFIER, Cardinality::SINGLE); $item = new AssessmentItem($identifier, $title, $timeDependent, $lang); $item->setAdaptive($adaptive); $item->setResponseDeclarations($responseDeclarations); $item->setOutcomeDeclarations($outcomeDeclarations); $marshaller = $this->getMarshallerFactory()->createMarshaller($item); $element = $marshaller->marshall($item); $this->assertInstanceOf('\\DOMElement', $element); $this->assertEquals('assessmentItem', $element->nodeName); // adaptive, timeDependent, identifier, lang, title $this->assertEquals($element->attributes->length, 5); $this->assertEquals($identifier, $element->getAttribute('identifier')); $this->assertEquals($title, $element->getAttribute('title')); $this->assertEquals('true', $element->getAttribute('timeDependent')); $this->assertEquals('true', $element->getAttribute('adaptive')); $this->assertEquals($lang, $element->getAttribute('lang')); $responseDeclarationElts = $element->getElementsByTagName('responseDeclaration'); $this->assertEquals(2, $responseDeclarationElts->length); $this->assertEquals('resp1', $responseDeclarationElts->item(0)->getAttribute('identifier')); $this->assertEquals('resp2', $responseDeclarationElts->item(1)->getAttribute('identifier')); $outcomeDeclarationElts = $element->getElementsByTagName('outcomeDeclaration'); $this->assertEquals(2, $outcomeDeclarationElts->length); $this->assertEquals('out1', $outcomeDeclarationElts->item(0)->getAttribute('identifier')); $this->assertEquals('out2', $outcomeDeclarationElts->item(1)->getAttribute('identifier')); }
/** * @dataProvider fillVariableProvider * * @param IAssessmentItem $itemRef The reference item. * @param string $variableIdentifier The identifier of the variable. * @param array $data Client-side data. * @param Variable $expectedVariable */ public function testFillVariable(array $data, Variable $expectedVariable) { // Non-time dependent basic item in 'Yoda English'. $itemRef = new AssessmentItem('Q01', 'Question 01', false, 'en-YO'); $outcomeDeclarations = new OutcomeDeclarationCollection(); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME1', BaseType::FLOAT, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME2', BaseType::INTEGER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME3', BaseType::BOOLEAN, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME4', BaseType::STRING, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME5', BaseType::POINT, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME6', BaseType::PAIR, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME7', BaseType::DIRECTED_PAIR, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME8', BaseType::DURATION, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME9', BaseType::URI, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME10', BaseType::IDENTIFIER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME11', BaseType::INT_OR_IDENTIFIER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME12', BaseType::INTEGER, Cardinality::SINGLE); $responseDeclarations = new ResponseDeclarationCollection(); $responseDeclarations[] = new ResponseDeclaration('RESPONSE1', BaseType::IDENTIFIER, Cardinality::SINGLE); $responseDeclarations[] = new ResponseDeclaration('RESPONSE2', BaseType::IDENTIFIER, Cardinality::MULTIPLE); $responseDeclarations[] = new ResponseDeclaration('RESPONSE3', BaseType::DIRECTED_PAIR, Cardinality::ORDERED); $responseDeclarations[] = new ResponseDeclaration('RESPONSE4', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE5', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE6', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE7', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE8', BaseType::BOOLEAN, Cardinality::MULTIPLE); $itemRef->setOutcomeDeclarations($outcomeDeclarations); $itemRef->setResponseDeclarations($responseDeclarations); $filler = new taoQtiCommon_helpers_PciVariableFiller($itemRef); $variable = $filler->fill($expectedVariable->getIdentifier(), $data); $this->assertEquals($expectedVariable->getIdentifier(), $variable->getIdentifier()); $this->assertEquals($expectedVariable->getBaseType(), $variable->getBaseType()); $this->assertEquals($expectedVariable->getCardinality(), $variable->getCardinality()); $this->assertEquals(get_class($expectedVariable), get_class($variable)); if ($expectedVariable->getValue() === null) { $this->assertSame($expectedVariable->getValue(), $variable->getValue()); } else { $this->assertTrue($expectedVariable->getValue()->equals($variable->getValue())); } }