public function buildItemBody(array $interactions, $content = '')
 {
     // Try to build the <itemBody> according to items` content if exists
     if (empty($content)) {
         return $this->buildItemBodySimple($interactions);
     }
     try {
         return $this->buildItemBodyWithItemContent($interactions, $content);
         // If anything fails, <itemBody> can't be mapped due to whatever reasons
         // Probably simply due to its being wrapped in a tag which only accept inline content
         // Simply build it without considering items` content and put the content on the top
     } catch (\Exception $e) {
         $itemBody = $this->buildItemBodySimple($interactions);
         $itemBodyContent = new BlockCollection();
         // Build the div bundle that contains all the item`s content
         // minus those questions and features `span`
         $html = new SimpleHtmlDom();
         $html->load($content);
         foreach ($html->find('span.learnosity-response') as &$span) {
             $span->outertext = '';
         }
         $div = new Div();
         $contentCollection = QtiMarshallerUtil::unmarshallElement($html->save());
         $div->setContent(ContentCollectionBuilder::buildFlowCollectionContent($contentCollection));
         $itemBodyContent->attach($div);
         $itemBodyContent->merge($itemBody->getComponents());
         $itemBody->setContent($itemBodyContent);
         LogService::log('Interactions are failed to be mapped with `item` content: ' . $e->getMessage() . '. Thus, interactions are separated from its actual `item` content and appended in the bottom');
         return $itemBody;
     }
 }
 public function processAssessmentItem(AssessmentItem $assessmentItem)
 {
     // TODO: Yea, we ignore rubric but what happen if the rubric is deep inside nested
     $newCollection = new BlockCollection();
     $itemBodyNew = new ItemBody();
     /** @var QtiComponent $component */
     foreach ($assessmentItem->getItemBody()->getContent() as $key => $component) {
         if (!$component instanceof RubricBlock) {
             $newCollection->attach($component);
         } else {
             LogService::log('Does not support <rubricBlock>. Ignoring <rubricBlock>');
         }
     }
     $itemBodyNew->setContent($newCollection);
     $assessmentItem->setItemBody($itemBodyNew);
     return $assessmentItem;
 }
 public static function buildItemBody(QtiComponentCollection $componentCollection)
 {
     $itemBody = new ItemBody('testItemBody');
     $blockCollection = new BlockCollection();
     $p = new P();
     $pCollection = new InlineCollection();
     $pCollection->attach(new TextRun('The Matrix movie is starring '));
     // Build the <itemBody>
     $blockCollection->attach($p);
     foreach ($componentCollection as $c) {
         if ($c instanceof InlineInteraction) {
             $pCollection->attach($c);
         } else {
             $blockCollection->attach($c);
         }
     }
     $p->setContent($pCollection);
     $itemBody->setContent($blockCollection);
     return $itemBody;
 }
 public function buildContentCollection(QtiComponentCollection $contentCollection)
 {
     $areBlockComponents = array_reduce($contentCollection->getArrayCopy(), function ($initial, $component) {
         return $initial && $component instanceof Block;
     }, true);
     // Check whether the content could all be attached as is
     if ($areBlockComponents) {
         $blockCollection = new BlockCollection();
         foreach ($contentCollection as $component) {
             $blockCollection->attach($component);
         }
         return $blockCollection;
     }
     // Otherwise, build a `div` wrapper around it
     $divCollection = new FlowCollection();
     foreach ($contentCollection as $component) {
         $divCollection->attach($component);
     }
     $div = new Div();
     $div->setContent($divCollection);
     $blockCollection = new BlockCollection();
     $blockCollection->attach($div);
     return $blockCollection;
 }
 protected function buildAssessmentItem(array $interactions, $responseProcessingTemplate = '')
 {
     $assessmentItem = new AssessmentItem('testItemID', 'testItemTitle', false);
     $responseProcessing = new ResponseProcessing();
     $responseProcessing->setTemplate($responseProcessingTemplate);
     $assessmentItem->setResponseProcessing($responseProcessing);
     $itemBody = new ItemBody();
     $p = new P();
     $pCollection = new InlineCollection();
     $pCollection->attach(new TextRun('The Matrix movie is starring '));
     $pCollection->attach(new TextRun('.'));
     foreach ($interactions as $interaction) {
         $pCollection->attach($interaction);
     }
     $p->setContent($pCollection);
     $collection = new BlockCollection();
     $collection->attach($p);
     $itemBody->setContent($collection);
     $assessmentItem->setItemBody($itemBody);
     return $assessmentItem;
 }
 private function buildItemBodyWithSingleInteraction(TextEntryInteraction $interactionOne = null)
 {
     $interactionOne = empty($interactionOne) ? new TextEntryInteraction('testIdentifierOne') : $interactionOne;
     $itemBody = new ItemBody();
     $itemBodyCollection = new BlockCollection();
     // Build `<p>The Matrix .... <inlineChoiceInteraction...></inlineChoiceInteraction>.</p>`
     $p = new P();
     $pCollection = new InlineCollection();
     $pCollection->attach(new TextRun('The Matrix movie is starring '));
     $pCollection->attach($interactionOne);
     $pCollection->attach(new TextRun('.'));
     $p->setContent($pCollection);
     // Build the <itemBody>
     $itemBodyCollection->attach($p);
     $itemBody->setContent($itemBodyCollection);
     return $itemBody;
 }
 private function buildItemBodyWithTwoInteractions()
 {
     $interactionOne = InlineChoiceInteractionBuilder::buildSimple('testIdentifierOne', ['sydney' => 'Sydney', 'melbourne' => 'Melbourne', 'canberra' => 'Canberra']);
     $interactionTwo = InlineChoiceInteractionBuilder::buildSimple('testIdentifierTwo', ['hugh' => 'Hugh Jackman', 'keanu' => 'Keanu Reeves', 'gloria' => 'Gloria Foster']);
     $itemBody = new ItemBody();
     $itemBodyCollection = new BlockCollection();
     // Build `<p>The Matrix .... <inlineChoiceInteraction...></inlineChoiceInteraction>.</p>`
     $p = new P();
     $pCollection = new InlineCollection();
     $pCollection->attach(new TextRun('The Matrix movie is filmed at '));
     $pCollection->attach($interactionOne);
     $pCollection->attach(new TextRun(', and starring '));
     $pCollection->attach($interactionTwo);
     $p->setContent($pCollection);
     // Build the <itemBody>
     $itemBodyCollection->attach($p);
     $itemBody->setContent($itemBodyCollection);
     return $itemBody;
 }