public function testMarshall()
 {
     $choice1 = new SimpleChoice('choice_1');
     $choice1->setContent(new FlowStaticCollection(array(new TextRun('Choice #1'))));
     $choice2 = new SimpleChoice('choice_2');
     $choice2->setContent(new FlowStaticCollection(array(new TextRun('Choice #2'))));
     $choices = new SimpleChoiceCollection(array($choice1, $choice2));
     $component = new ChoiceInteraction('RESPONSE', $choices);
     $prompt = new Prompt();
     $prompt->setContent(new FlowStaticCollection(array(new TextRun('Prompt...'))));
     $component->setPrompt($prompt);
     $marshaller = $this->getMarshallerFactory()->createMarshaller($component);
     $element = $marshaller->marshall($component);
     $dom = new DOMDocument('1.0', 'UTF-8');
     $element = $dom->importNode($element, true);
     $this->assertEquals('<choiceInteraction responseIdentifier="RESPONSE"><prompt>Prompt...</prompt><simpleChoice identifier="choice_1">Choice #1</simpleChoice><simpleChoice identifier="choice_2">Choice #2</simpleChoice></choiceInteraction>', $dom->saveXML($element));
 }
 public function convert(BaseQuestionType $questionType, $interactionIdentifier, $interactionLabel)
 {
     /** @var mcq $question */
     $question = $questionType;
     // Build <choiceInteraction>
     $valueIdentifierMap = [];
     $simpleChoiceCollection = new SimpleChoiceCollection();
     foreach ($question->get_options() as $index => $option) {
         /** @var mcq_options_item $option */
         $choiceContent = new FlowStaticCollection();
         foreach (QtiMarshallerUtil::unmarshallElement($option->get_label()) as $component) {
             $choiceContent->attach($component);
         }
         // Use option['value'] as choice `identifier` if it has the correct format,
         // Otherwise, generate a valid using index such `CHOICE_1`, `CHOICE_2`, etc
         $originalOptionValue = $option->get_value();
         $choiceIdentifier = Format::isIdentifier($originalOptionValue, false) ? $originalOptionValue : 'CHOICE_' . $index;
         // Store this reference in a map
         $valueIdentifierMap[$originalOptionValue] = $choiceIdentifier;
         $choice = new SimpleChoice($choiceIdentifier);
         $choice->setContent($choiceContent);
         $simpleChoiceCollection->attach($choice);
     }
     // Build final interaction and its corresponding <responseDeclaration>, and its <responseProcessingTemplate>
     $interaction = new ChoiceInteraction($interactionIdentifier, $simpleChoiceCollection);
     $interaction->setLabel($interactionLabel);
     $interaction->setMinChoices(1);
     $interaction->setMaxChoices($question->get_multiple_responses() ? $simpleChoiceCollection->count() : 1);
     // Build the prompt
     $interaction->setPrompt($this->convertStimulusForPrompt($question->get_stimulus()));
     // Set shuffle options
     $interaction->setShuffle($question->get_shuffle_options() ? true : false);
     // Set the layout
     if ($question->get_ui_style() instanceof mcq_ui_style && $question->get_ui_style()->get_type() === 'horizontal' && intval($question->get_ui_style()->get_columns()) === count($question->get_options())) {
         $interaction->setOrientation(Orientation::HORIZONTAL);
     } else {
         $interaction->setOrientation(Orientation::VERTICAL);
         LogService::log('ui_style` is ignored and `choiceInteraction` is assumed and set as `vertical`');
     }
     if (empty($question->get_validation())) {
         return [$interaction, null, null];
     }
     $builder = new McqValidationBuilder($question->get_multiple_responses(), $valueIdentifierMap);
     list($responseDeclaration, $responseProcessing) = $builder->buildValidation($interactionIdentifier, $question->get_validation());
     return [$interaction, $responseDeclaration, $responseProcessing];
 }
 public function testMarshallOrientationNoOutput20()
 {
     // Aims at testing that orientation is not output in a QTI 2.0 context.
     $choice1 = new SimpleChoice('choice_1');
     $choice1->setContent(new FlowStaticCollection(array(new TextRun('Choice #1'))));
     $choices = new SimpleChoiceCollection(array($choice1));
     $component = new ChoiceInteraction('RESPONSE', $choices);
     // Set non-default value for orientation...
     $component->setOrientation(Orientation::HORIZONTAL);
     $marshaller = $this->getMarshallerFactory('2.0.0')->createMarshaller($component);
     $element = $marshaller->marshall($component);
     $dom = new DOMDocument('1.0', 'UTF-8');
     $element = $dom->importNode($element, true);
     $this->assertEquals('<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="0"><simpleChoice identifier="choice_1">Choice #1</simpleChoice></choiceInteraction>', $dom->saveXML($element));
 }
Exemple #4
0
 public function testCreateShufflingWithShuffleFalse()
 {
     $choiceCollection = new SimpleChoiceCollection();
     $choiceCollection[] = new SimpleChoice('id1');
     $choiceCollection[] = new SimpleChoice('id2');
     $choiceCollection[] = new SimpleChoice('id3');
     $choiceInteraction = new ChoiceInteraction('RESPONSE', $choiceCollection);
     $choiceInteraction->setShuffle(false);
     $shuffling = StateUtils::createShufflingFromInteraction($choiceInteraction);
     $this->assertFalse($shuffling);
 }
 private function assertInteractionTwo(ChoiceInteraction $interaction, ResponseDeclaration $responseDeclaration)
 {
     // And its prompt is mapped correctly
     $promptString = QtiMarshallerUtil::marshallCollection($interaction->getPrompt()->getComponents());
     $this->assertEquals('Pick the odd one out', $promptString);
     // All the choices also mapped properly
     /** @var SimpleChoice[] $simpleChoices */
     $simpleChoices = $interaction->getSimpleChoices()->getArrayCopy(true);
     $this->assertEquals($simpleChoices[0]->getIdentifier(), 'CHOICE_0');
     $this->assertEquals(QtiMarshallerUtil::marshallCollection($simpleChoices[0]->getContent()), 'Tomato');
     $this->assertEquals($simpleChoices[1]->getIdentifier(), 'CHOICE_1');
     $this->assertEquals(QtiMarshallerUtil::marshallCollection($simpleChoices[1]->getContent()), 'Orange');
     $this->assertEquals($simpleChoices[2]->getIdentifier(), 'CHOICE_2');
     $this->assertEquals(QtiMarshallerUtil::marshallCollection($simpleChoices[2]->getContent()), 'Celery');
     $this->assertEquals($simpleChoices[3]->getIdentifier(), 'CHOICE_3');
     $this->assertEquals(QtiMarshallerUtil::marshallCollection($simpleChoices[3]->getContent()), 'Pear');
     // Check usual stuff
     $this->assertEquals(1, $interaction->getMinChoices());
     $this->assertEquals(1, $interaction->getMaxChoices());
     $this->assertFalse($interaction->mustShuffle());
     $this->assertEquals(Orientation::HORIZONTAL, $interaction->getOrientation());
     // Check the response declaration fine for mcq with multiple responses
     $this->assertEquals(BaseType::IDENTIFIER, $responseDeclaration->getBaseType());
     $this->assertEquals(Cardinality::SINGLE, $responseDeclaration->getCardinality());
     $correctResponse = $responseDeclaration->getCorrectResponse();
     $this->assertTrue($correctResponse instanceof CorrectResponse);
     /** @var Value[] $values */
     $values = $correctResponse->getValues()->getArrayCopy(true);
     $this->assertEquals($values[0]->getValue(), 'CHOICE_2');
     $this->assertNull($responseDeclaration->getMapping());
 }