public function convert(BaseQuestionType $questionType, $interactionIdentifier, $interactionLabel)
 {
     //TODO: Need validation a question shall have at least 1 {{response}} and 1 item in `possible_responses`
     /** @var clozeassociation $question */
     $question = $questionType;
     // Replace {{ response }} with `gap` elements
     $index = 0;
     $template = preg_replace_callback('/{{response}}/', function ($match) use(&$index) {
         $gapIdentifier = self::GAP_IDENTIFIER_PREFIX . $index;
         $replacement = '<gap identifier="' . $gapIdentifier . '"/>';
         $index++;
         return $replacement;
     }, $question->get_template());
     $content = ContentCollectionBuilder::buildBlockStaticCollectionContent(QtiMarshallerUtil::unmarshallElement($template));
     // Map `possible_responses` to gaps
     // TODO: Detect `img`
     $gapChoices = new GapChoiceCollection();
     $possibleResponses = $question->get_possible_responses();
     $matchMax = $question->get_duplicate_responses() ? count($possibleResponses) : 1;
     foreach ($possibleResponses as $index => $possibleResponse) {
         $gapChoice = new GapText(self::GAPCHOICE_IDENTIFIER_PREFIX . $index, $matchMax);
         $gapChoiceContent = new TextOrVariableCollection();
         $gapChoiceContent->attach(new TextRun($possibleResponse));
         $gapChoice->setContent($gapChoiceContent);
         $gapChoices->attach($gapChoice);
     }
     $interaction = new GapMatchInteraction($interactionIdentifier, $gapChoices, $content);
     $interaction->setLabel($interactionLabel);
     $interaction->setPrompt($this->convertStimulusForPrompt($question->get_stimulus()));
     $validationBuilder = new ClozeassociationValidationBuilder($possibleResponses);
     list($responseDeclaration, $responseProcessing) = $validationBuilder->buildValidation($interaction->getResponseIdentifier(), $question->get_validation());
     return [$interaction, $responseDeclaration, $responseProcessing];
 }
 private function buildTemplate(QtiGapMatchInteraction $interaction)
 {
     $templateCollection = new QtiComponentCollection();
     foreach ($interaction->getComponents() as $component) {
         // Ignore `prompt` and the `gapChoice` since they are going to be mapped somewhere else :)
         if (!$component instanceof Prompt && !$component instanceof GapChoice) {
             $templateCollection->attach($component);
         }
     }
     $gapIdentifiers = [];
     $content = QtiMarshallerUtil::marshallCollection($templateCollection);
     foreach ($interaction->getComponentsByClassName('gap', true) as $gap) {
         /** @var Gap $gap */
         $gapIdentifiers[] = $gap->getIdentifier();
         $gapString = QtiMarshallerUtil::marshall($gap);
         $content = str_replace($gapString, '{{response}}', $content);
     }
     return [$content, $gapIdentifiers];
 }
Exemple #3
0
 public function testCreateShufflingFromGapMatchInteraction()
 {
     $choiceCollection = new GapChoiceCollection();
     $choiceCollection[] = new GapText('id1', 1);
     $choiceCollection[] = new GapText('id2', 1);
     $choiceCollection[] = new GapText('id3', 1);
     $blockCollection = new BlockStaticCollection(array(new Div()));
     $gapMatchInteraction = new GapMatchInteraction('RESPONSE', $choiceCollection, $blockCollection);
     $gapMatchInteraction->setShuffle(true);
     $shuffling = StateUtils::createShufflingFromInteraction($gapMatchInteraction);
     $this->assertEquals('RESPONSE', $shuffling->getResponseIdentifier());
     $shufflingGroups = $shuffling->getShufflingGroups();
     $this->assertEquals(1, count($shufflingGroups));
     $this->assertEquals(array('id1', 'id2', 'id3'), $shufflingGroups[0]->getIdentifiers()->getArrayCopy());
 }