public function testShufflingShuffle() { $identifiers1 = new IdentifierCollection(array('id1', 'id2', 'id3', 'id4', 'id5')); $identifiers2 = new IdentifierCollection(array('id6', 'id7', 'id8', 'id9', 'id10')); $group1 = new ShufflingGroup($identifiers1); $group2 = new ShufflingGroup($identifiers2); $group2->setFixedIdentifiers(new IdentifierCollection(array('id7', 'id8', 'id9'))); $shuffling = new Shuffling('RESPONSE', new ShufflingGroupCollection(array($group1, $group2))); $shuffled = $shuffling->shuffle(); // Shuffling::shuffle makes a deep cloning... $this->assertNotSame($shuffling, $shuffled); $originalGroups = $shuffling->getShufflingGroups(); $shuffledGroups = $shuffled->getShufflingGroups(); $this->assertNotSame($originalGroups, $shuffledGroups); $this->assertNotSame($originalGroups[0], $shuffledGroups[0]); $this->assertNotSame($originalGroups[1], $shuffledGroups[1]); $this->assertNotSame($originalGroups[0]->getIdentifiers(), $shuffledGroups[0]->getIdentifiers()); $this->assertNotSame($originalGroups[1]->getIdentifiers(), $shuffledGroups[1]->getIdentifiers()); // We can check 1 thing: // Each identifier of original group is contained in shuffled group. for ($i = 0; $i < count($originalGroups); $i++) { $originalIdentifiers = $originalGroups[$i]->getIdentifiers(); for ($j = 0; $j < count($originalIdentifiers); $j++) { $this->assertTrue($shuffledGroups[$i]->getIdentifiers()->contains($originalIdentifiers[$j])); } } }
/** * Unmarshall a DOMElement object corresponding to a QTI ShufflingGroup element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A ShufflingGroup object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($identifiers = self::getDOMElementAttributeAs($element, 'identifiers')) !== null) { $identifiers = explode(" ", $identifiers); $component = new ShufflingGroup(new IdentifierCollection($identifiers)); if (($fixedIdentifiers = self::getDOMElementAttributeAs($element, 'fixedIdentifiers')) !== null) { $fixedIdentifiers = explode(" ", $fixedIdentifiers); $component->setFixedIdentifiers(new IdentifierCollection($fixedIdentifiers)); } return $component; } else { $msg = "The mandatory attribute 'identifiers' is missing from element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } }
public function testWriteShufflingState() { $shufflingGroup = new ShufflingGroup(new IdentifierCollection(array('id1', 'id2', 'id3'))); $shufflingGroup->setFixedIdentifiers(new IdentifierCollection(array('id2'))); $shufflingGroups = new ShufflingGroupCollection(array($shufflingGroup)); $shuffling = new Shuffling('RESPONSE', $shufflingGroups); $stream = new MemoryStream(); $stream->open(); $access = new QtiBinaryStreamAccess($stream, new FileSystemFileManager()); $access->writeShufflingState($shuffling); $stream->rewind(); $shufflingState = $access->readShufflingState(); $this->assertEquals('RESPONSE', $shufflingState->getResponseIdentifier()); $shufflingGroups = $shufflingState->getShufflingGroups(); $this->assertEquals(array('id1', 'id2', 'id3'), $shufflingGroups[0]->getIdentifiers()->getArrayCopy()); $this->assertEquals(array('id2'), $shufflingGroups[0]->getFixedIdentifiers()->getArrayCopy()); }
/** * Write a ShufflingGroup in the current binary stream. * * @param \qtism\data\state\ShufflingGroup $shufflingGroup * @throws QtiBinaryStreamAccessException */ public function writeShufflingGroup(ShufflingGroup $shufflingGroup) { try { $identifiers = $shufflingGroup->getIdentifiers(); $this->writeTinyInt(count($identifiers)); foreach ($identifiers as $identifier) { $this->writeString($identifier); } $fixedIdentifiers = $shufflingGroup->getFixedIdentifiers(); $this->writeTinyInt(count($fixedIdentifiers)); foreach ($fixedIdentifiers as $identifier) { $this->writeString($identifier); } } catch (BinaryStreamAccessException $e) { $msg = "An error occured while writing a shufflingGroup."; throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::SHUFFLING_GROUP, $e); } }
/** * Create a Shuffling component from a given Interaction object. * * A Shuffling object will be created depending on the $interaction object given. * If $interaction is an interaction type subject to shuffling e.g. choiceInteraction, * orderInteraction, associateInteraction, matchInteraction, gapMatchInteraction, * inlineChoiceInteraction, a Shuffling object is returned. * * Otherwise, the method returns false to indicate that no Shuffling component * can be built from the given $interaction object. * * @param \qtism\data\content\interactions\Interaction $interaction * @return \qtism\data\state\Shuffling|boolean */ public static function createShufflingFromInteraction(Interaction $interaction) { $className = $interaction->getQtiClassName(); $groups = array(); $shufflableInteractions = array('choiceInteraction', 'orderInteraction', 'associateInteraction', 'matchInteraction', 'gapMatchInteraction', 'inlineChoiceInteraction'); $returnValue = false; if (in_array($className, $shufflableInteractions) === true && $interaction->mustShuffle() === true) { if ($className === 'choiceInteraction' || $className === 'orderInteraction') { $choices = $interaction->getComponentsByClassName('simpleChoice'); $groups[] = array('identifiers' => array(), 'fixed' => array()); foreach ($choices as $choice) { $groups[0]['identifiers'][] = $choice->getIdentifier(); if ($choice->isFixed() === true) { $groups[0]['fixed'][] = $choice->getIdentifier(); } } } elseif ($className === 'associateInteraction') { $choices = $interaction->getComponentsByClassName('simpleAssociableChoice'); $groups[] = array('identifiers' => array(), 'fixed' => array()); foreach ($choices as $choice) { $groups[0]['identifiers'][] = $choice->getIdentifier(); if ($choice->isFixed() === true) { $groups[0]['fixed'][] = $choice->getIdentifier(); } } } elseif ($className === 'matchInteraction') { $matchSets = $interaction->getComponentsByClassName('simpleMatchSet'); $groups[] = array('identifiers' => array(), 'fixed' => array()); $groups[] = array('identifiers' => array(), 'fixed' => array()); for ($i = 0; $i < count($matchSets); $i++) { foreach ($matchSets[$i]->getComponentsByClassName('simpleAssociableChoice') as $choice) { $groups[$i]['identifiers'][] = $choice->getIdentifier(); if ($choice->isFixed() === true) { $groups[0]['fixed'][] = $choice->getIdentifier(); } } } } elseif ($className === 'gapMatchInteraction') { $choices = $interaction->getComponentsByClassName(array('gapText', 'gapImg')); $groups[] = array('identifiers' => array(), 'fixed' => array()); foreach ($choices as $choice) { $groups[0]['identifiers'][] = $choice->getIdentifier(); if ($choice->isFixed() === true) { $groups[0]['fixed'][] = $choice->getIdentifier(); } } } elseif ($className === 'inlineChoiceInteraction') { $choices = $interaction->getComponentsByClassName('inlineChoice'); $groups[] = array('identifiers' => array(), 'fixed' => array()); foreach ($choices as $choice) { $groups[0]['identifiers'][] = $choice->getIdentifier(); if ($choice->isFixed() === true) { $groups[0]['fixed'][] = $choice->getIdentifier(); } } } $responseIdentifier = $interaction->getResponseIdentifier(); $shufflingGroups = new ShufflingGroupCollection(); foreach ($groups as $group) { $shufflingGroup = new ShufflingGroup(new IdentifierCollection($group['identifiers'])); $shufflingGroup->setFixedIdentifiers(new IdentifierCollection($group['fixed'])); $shufflingGroups[] = $shufflingGroup; } $returnValue = new Shuffling($responseIdentifier, $shufflingGroups); } return $returnValue; }