public function convert(BaseQuestionType $question, $interactionIdentifier, $interactionLabel) { /** @var tokenhighlight $question */ // Grab those `template` and convert those highlights to <hottext> $html = new SimpleHtmlDom(); $html->load($question->get_template()); $tokens = $html->find('span.lrn_token'); $indexIdentifierMap = []; foreach ($tokens as $key => &$span) { $span->outertext = '<hottext identifier="TOKEN_' . intval($key) . '">' . $span->innertext . '</hottext>'; $indexIdentifierMap[$key] = 'TOKEN_' . intval($key); } $htmlContent = $html->save(); $contentComponents = QtiMarshallerUtil::unmarshallElement($htmlContent); $contentCollection = ContentCollectionBuilder::buildBlockStaticCollectionContent($contentComponents); // Build the interaction $interaction = new HottextInteraction($interactionIdentifier, $contentCollection); $interaction->setLabel($interactionLabel); $interaction->setPrompt($this->convertStimulusForPrompt($question->get_stimulus())); // Learnosity does not enforce number of choices, thus using default such the min choice would be 1 // and max would be the `max_selection` if set, otherwise use token count $interaction->setMinChoices(1); $interaction->setMaxChoices(is_int($question->get_max_selection()) ? $question->get_max_selection() : count($tokens)); // Build validation $builder = new TokenhighlightValidationBuilder($indexIdentifierMap); list($responseDeclaration, $responseProcessing) = $builder->buildValidation($interaction->getResponseIdentifier(), $question->get_validation()); return [$interaction, $responseDeclaration, $responseProcessing]; }
private function getFeatureReplacementString($node) { // Process inline feature if (isset($node->attr['data-type']) && isset($node->attr['data-src'])) { $src = trim($node->attr['data-src']); $type = trim($node->attr['data-type']); if ($type === 'audioplayer' || $type === 'audioplayer') { return QtiMarshallerUtil::marshallValidQti(new Object($src, MimeUtil::guessMimeType(basename($src)))); } // Process regular question feature } else { $nodeClassAttribute = $node->attr['class']; $featureReference = $this->getFeatureReferenceFromClassName($nodeClassAttribute); $feature = $this->widgets[$featureReference]; $type = $feature['data']['type']; if ($type === 'audioplayer' || $type === 'audioplayer') { $src = $feature['data']['src']; $object = new Object($src, MimeUtil::guessMimeType(basename($src))); $object->setLabel($featureReference); return QtiMarshallerUtil::marshallValidQti($object); } else { if ($type === 'sharedpassage') { $content = $feature['data']['content']; $object = new Object('', 'text/html'); $object->setContent(ContentCollectionBuilder::buildObjectFlowCollectionContent(QtiMarshallerUtil::unmarshallElement($content))); $object->setLabel($featureReference); return QtiMarshallerUtil::marshallValidQti($object); } } } throw new MappingException($type . ' not supported'); }
public function convert(BaseQuestionType $questionType, $interactionIdentifier, $interactionLabel) { /** @var clozetext $question */ $question = $questionType; // Extra text that can't be mapped since we are in textEntryInteraction which does not have prompt $this->extraContent = $question->get_stimulus(); // Replace {{ response }} with `textEntryInteraction` elements $maxLength = !is_null($question->get_max_length()) ? intval($question->get_max_length()) : 15; // Set default to `15` if not set $index = 0; $template = preg_replace_callback('/{{response}}/', function ($match) use(&$index, $interactionIdentifier, $interactionLabel, $maxLength) { $interaction = new TextEntryInteraction($interactionIdentifier . '_' . $index); $interaction->setLabel($interactionLabel); $interaction->setExpectedLength($maxLength); $index++; $replacement = QtiMarshallerUtil::marshall($interaction); return $replacement; }, $question->get_template()); // Wrap this interaction in a block since our `clozetext` `template` meant to be blocky and not inline $div = new Div(); $div->setClass('lrn-template'); $div->setContent(ContentCollectionBuilder::buildFlowCollectionContent(QtiMarshallerUtil::unmarshallElement($template))); // Build validation $isCaseSensitive = is_null($question->get_case_sensitive()) ? true : $question->get_case_sensitive(); $validationBuilder = new ClozetextValidationBuilder($isCaseSensitive); list($responseDeclaration, $responseProcessing) = $validationBuilder->buildValidation($interactionIdentifier, $question->get_validation(), $isCaseSensitive); return [$div, $responseDeclaration, $responseProcessing]; }
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]; }
protected function convertStimulusForPrompt($stimulusString) { $stimulusComponents = QtiMarshallerUtil::unmarshallElement($stimulusString); $prompt = new Prompt(); $prompt->setContent(ContentCollectionBuilder::buildFlowStaticCollectionContent($stimulusComponents)); return $prompt; }
public function convert(BaseQuestionType $questionType, $interactionIdentifier, $interactionLabel) { /** @var orderlist $question */ $question = $questionType; $simpleChoiceCollection = new SimpleChoiceCollection(); $indexIdentifiersMap = []; foreach ($question->get_list() as $key => $item) { $simpleChoice = new SimpleChoice('CHOICE_' . $key); $choiceContent = new FlowStaticCollection(); foreach (QtiMarshallerUtil::unmarshallElement($item) as $component) { $choiceContent->attach($component); } $simpleChoice->setContent($choiceContent); $simpleChoiceCollection->attach($simpleChoice); $indexIdentifiersMap[$key] = $simpleChoice->getIdentifier(); } $interaction = new OrderInteraction($interactionIdentifier, $simpleChoiceCollection); $interaction->setLabel($interactionLabel); $interaction->setPrompt($this->convertStimulusForPrompt($question->get_stimulus())); $interaction->setShuffle(false); $interaction->setOrientation(Orientation::VERTICAL); $builder = new OrderlistValidationBuilder($indexIdentifiersMap); list($responseDeclaration, $responseProcessing) = $builder->buildValidation($interactionIdentifier, $question->get_validation()); return [$interaction, $responseDeclaration, $responseProcessing]; }
private function buildOptionCollection(choicematrix $question, $stemCount) { $optionIndexIdentifierMap = []; $optionCollection = new SimpleAssociableChoiceCollection(); foreach ($question->get_options() as $key => $optionValue) { // Learnosity's `choicematrix` always have its options to have any number of associable choice, thus setting to stems count // Same as above, won't validate upon empty response, thus setting match min to 1 $optionChoice = new SimpleAssociableChoice('OPTION_' . $key, $stemCount); $optionChoice->setMatchMin(1); $optionChoice->setContent(ContentCollectionBuilder::buildFlowStaticCollectionContent(QtiMarshallerUtil::unmarshallElement($optionValue))); $optionCollection->attach($optionChoice); $optionIndexIdentifierMap[$key] = $optionChoice->getIdentifier(); } return [$optionCollection, $optionIndexIdentifierMap]; }
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 convert(BaseQuestionType $questionType, $interactionIdentifier, $interactionLabel) { /** @var clozedropdown $question */ $question = $questionType; // Extra text that can't be mapped since we are in textEntryInteraction which does not have prompt $this->extraContent = $question->get_stimulus(); // Replace {{ response }} with `textEntryInteraction` elements $valueIdentifierMapPerInlineChoices = []; $index = 0; $possibleResponses = $question->get_possible_responses(); $template = preg_replace_callback('/{{response}}/', function ($match) use(&$index, &$valueIdentifierMapPerInlineChoices, $possibleResponses, $interactionIdentifier, $interactionLabel) { $inlineChoiceCollection = new InlineChoiceCollection(); if (!isset($possibleResponses[$index])) { throw new MappingException('Invalid `possible_responses`, missing entries'); } foreach ($possibleResponses[$index] as $choiceIndex => $choiceValue) { $inlineChoiceIdentifier = 'INLINECHOICE_' . $choiceIndex; $valueIdentifierMapPerInlineChoices[$index][$choiceValue] = $inlineChoiceIdentifier; // Update this map so can be used later upon building responseDeclaration objects $inlineChoice = new InlineChoice($inlineChoiceIdentifier); $inlineChoiceContent = new TextOrVariableCollection(); $inlineChoiceContent->attach(new TextRun($choiceValue)); $inlineChoice->setContent($inlineChoiceContent); $inlineChoiceCollection->attach($inlineChoice); } $interaction = new InlineChoiceInteraction($interactionIdentifier . '_' . $index, $inlineChoiceCollection); $interaction->setLabel($interactionLabel); $index++; $replacement = QtiMarshallerUtil::marshall($interaction); return $replacement; }, $question->get_template()); // Wrap this interaction in a block since our `clozedropdown` `template` meant to be blocky and not inline $div = new Div(); $div->setClass('lrn-template'); $div->setContent(ContentCollectionBuilder::buildFlowCollectionContent(QtiMarshallerUtil::unmarshallElement($template))); // Build validation $validationBuilder = new ClozedropdownValidationBuilder($valueIdentifierMapPerInlineChoices); list($responseDeclaration, $responseProcessing) = $validationBuilder->buildValidation($interactionIdentifier, $question->get_validation()); return [$div, $responseDeclaration, $responseProcessing]; }
private function buildItemBodySimple(array $interactions) { $interactions = array_values($interactions); $contentCollection = new QtiComponentCollection(); // Append the extra contents belong to an interaction before the interaction itself foreach ($interactions as $data) { if (isset($data['extraContent'])) { $content = QtiMarshallerUtil::unmarshallElement($data['extraContent']); $contentCollection->merge($content); } $contentCollection->attach($data['interaction']); } $itemBody = new ItemBody(); $itemBody->setContent(ContentCollectionBuilder::buildBlockCollectionContent($contentCollection)); return $itemBody; }