4 require_once
'Modules/TestQuestionPool/classes/questions/class.ilAssOrderingElement.php';
36 self::IDENTIFIER_TYPE_SOLUTION => array(),
37 self::IDENTIFIER_TYPE_RANDOM => array()
58 $this->objectInstanceId = ++self::$objectInstanceCounter;
69 $this->objectInstanceId = ++self::$objectInstanceCounter;
73 foreach ($this as $key => $element) {
107 return count($this->elements);
117 return $position == 0;
130 $elementList =
new self();
133 foreach ($this as $element) {
134 if ($element->getPosition() == $currentPosition) {
135 $elementList->addElement($dodgingElement);
139 if ($element->getPosition() == $targetPosition) {
140 $elementList->addElement($movingElement);
144 $elementList->addElement($element);
147 $dodgingElement->setPosition($currentPosition);
148 $movingElement->setPosition($targetPosition);
155 $elementList =
new self();
158 $positionCounter = 0;
160 foreach ($this as $element) {
161 if ($element->isSameElement($removeElement)) {
165 $element->setPosition($positionCounter++);
166 $elementList->addElement($element);
175 $this->elements = array();
237 foreach ($this as $element) {
253 $this->elements[] = $element;
262 if (isset($this->elements[$position])) {
263 return $this->elements[$position];
284 foreach ($this as $element) {
285 if ($element->getRandomIdentifier() === intval($randomIdentifier)) {
307 foreach ($this as $element) {
308 if ($element->getSolutionIdentifier() != $solutionIdentifier) {
348 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
352 return self::$identifierRegistry[$identifierType][$this->
getQuestionId()];
361 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_SOLUTION);
367 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_RANDOM);
418 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
419 self::$identifierRegistry[$identifierType][$this->
getQuestionId()] = array();
424 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
425 self::$identifierRegistry[$identifierType][$this->
getQuestionId()][] = $identifier;
437 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
443 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
458 switch ($identifierType) {
474 switch ($identifierType) {
489 switch ($identifierType) {
490 case self::IDENTIFIER_TYPE_SOLUTION:
491 return self::isValidSolutionIdentifier($identifier);
493 case self::IDENTIFIER_TYPE_RANDOM:
494 return self::isValidRandomIdentifier($identifier);
507 switch ($identifierType) {
522 "unknown identifier type given (type: $identifierType)" 533 "could not build random identifier (max tries: $maxTries)" 544 "cannot reorder element due to missing position (random identifier: $randomIdentifier)" 555 'cannot reorder element due to one or more unknown random identifiers ' .
556 '(' . implode(
', ', $randomIdentifiers) .
')' 565 $lastSolutionIdentifier = null;
568 if ($lastSolutionIdentifier > $registeredIdentifier) {
572 $lastSolutionIdentifier = $registeredIdentifier;
575 return $lastSolutionIdentifier;
585 if ($lastSolutionIdentifier === null) {
589 $nextSolutionIdentifier = $lastSolutionIdentifier + self::SOLUTION_IDENTIFIER_VALUE_INTERVAL;
591 return $nextSolutionIdentifier;
600 $usedTriesCounter = 0;
603 if ($usedTriesCounter >= self::RANDOM_IDENTIFIER_BUILD_MAX_TRIES) {
609 $lowerBound = self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND;
610 $upperBound = self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND;
611 $randomIdentifier = mt_rand($lowerBound, $upperBound);
614 $testElement->setRandomIdentifier($randomIdentifier);
617 return $randomIdentifier;
622 return is_numeric($identifier)
623 && $identifier == (int) $identifier
624 && (
int) $identifier >= 0;
629 return is_numeric($identifier)
630 && $identifier == (int) $identifier
631 && (
int) $identifier >= self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND
632 && (int) $identifier <= self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND;
637 return self::isValidSolutionIdentifier($position);
642 return self::isValidPosition($indentation);
650 foreach ($this as $element) {
660 $numIntersectingElements = count(array_intersect(
661 $otherList->getRandomIdentifierIndex(),
666 && $numIntersectingElements == $otherList->countElements();
675 $parityTrueElementList =
new self();
676 $parityTrueElementList->setQuestionId($this->
getQuestionId());
678 foreach ($this as $thisElement) {
679 $otherElement = $otherList->getElementByRandomIdentifier(
680 $thisElement->getRandomIdentifier()
683 if ($otherElement->getPosition() != $thisElement->getPosition()) {
687 if ($otherElement->getIndentation() != $thisElement->getIndentation()) {
691 $parityTrueElementList->addElement($thisElement);
694 return $parityTrueElementList;
704 $positionsMap = array_flip(array_values($randomIdentifiers));
706 $orderedElements = array();
708 foreach ($this as $element) {
709 if (!isset($positionsMap[$element->getRandomIdentifier()])) {
713 $position = $positionsMap[$element->getRandomIdentifier()];
714 unset($positionsMap[$element->getRandomIdentifier()]);
716 $element->setPosition($position);
717 $orderedElements[$position] = $element;
720 if (count($positionsMap)) {
724 ksort($orderedElements);
726 $this->
setElements(array_values($orderedElements));
734 foreach ($this as $element) {
735 $element->setIndentation(0);
747 $differenceElementList =
new self();
748 $differenceElementList->setQuestionId($this->
getQuestionId());
750 foreach ($differenceRandomIdentifierIndex as $randomIdentifier) {
752 $differenceElementList->addElement($element);
755 return $differenceElementList;
764 $differenceRandomIdentifierIndex = array_diff(
766 $otherElementList->getRandomIdentifierIndex()
769 return $differenceRandomIdentifierIndex;
777 foreach ($this as $thisElement) {
778 if (!$otherList->elementExistByRandomIdentifier($thisElement->getRandomIdentifier())) {
782 $otherElement = $otherList->getElementByRandomIdentifier(
783 $thisElement->getRandomIdentifier()
786 $thisElement->setContent($otherElement->getContent());
795 return current($this->elements);
803 return next($this->elements);
811 return key($this->elements);
819 return ($this->
key() !== null);
827 return reset($this->elements);
836 $element->setRandomIdentifier(self::FALLBACK_DEFAULT_ELEMENT_RANDOM_IDENTIFIER);
844 $elementList =
new self();
856 foreach ($this as $element) {
857 $items[] = implode(
':', array(
858 $element->getSolutionIdentifier(),
859 $element->getRandomIdentifier(),
860 $element->getIndentation()
864 return md5(serialize($items));
872 $clone = clone $this;
879 $clone = clone $this;
__clone()
clone list by additionally cloning the element objects
reorderByRandomIdentifiers($randomIdentifiers)
registerIdentifiers(ilAssOrderingElement $element)
elementExistBySolutionIdentifier($solutionIdentifier)
static isValidPosition($position)
const IDENTIFIER_TYPE_SOLUTION
setRandomIdentifier(int $random_identifier)
const JS_ADDED_ELEMENTS_RANDOM_IDENTIFIER_START_VALUE
static $identifierRegistry
isValidIdentifier($identifierType, $identifier)
getRegisteredSolutionIdentifiers()
getLastSolutionIdentifier()
setSolutionIdentifier($solution_identifier)
const SOLUTION_IDENTIFIER_START_VALUE
populateIdentifier(ilAssOrderingElement $element, $identifierType, $identifier)
removeElement(ilAssOrderingElement $removeElement)
const RANDOM_IDENTIFIER_RANGE_UPPER_BOUND
withElements(array $elements)
isFirstElementPosition($position)
completeContentsFromElementList(self $otherList)
elementExistByPosition($position)
moveElementByPositions($currentPosition, $targetPosition)
setQuestionId($question_id)
hasSameElementSetByRandomIdentifiers(self $otherList)
elementExistByRandomIdentifier($randomIdentifier)
static buildInstance(int $question_id, array $elements=[])
getSolutionIdentifierIndex()
getRegisteredRandomIdentifiers()
throwMissingReorderPositionException($randomIdentifier)
resetElements()
resets elements
const JS_ADDED_ELEMENTS_RANDOM_IDENTIFIER_VALUE_INTERVAL
static isValidRandomIdentifier($identifier)
const SOLUTION_IDENTIFIER_VALUE_INTERVAL
throwCouldNotBuildRandomIdentifierException($maxTries)
getRegisteredIdentifiers($identifierType)
distributeNewRandomIdentifiers()
getElementByRandomIdentifier($randomIdentifier)
buildIdentifier($identifierType)
throwUnknownRandomIdentifiersException($randomIdentifiers)
getElementBySolutionIdentifier($solutionIdentifier)
ensureValidIdentifier(ilAssOrderingElement $element, $identifierType)
throwUnknownIdentifierTypeException($identifierType)
addElement(ilAssOrderingElement $element)
getDifferenceRandomIdentifierIndex(self $otherElementList)
getIndexedElements($identifierType)
isIdentifierRegistered(ilAssOrderingElement $element, $identifierType)
getRandomIdentifierIndexedElements()
static getFallbackDefaultElement()
getRandomIdentifierIndex()
fetchIdentifier(ilAssOrderingElement $element, $identifierType)
const FALLBACK_DEFAULT_ELEMENT_RANDOM_IDENTIFIER
static isValidSolutionIdentifier($identifier)
const RANDOM_IDENTIFIER_RANGE_LOWER_BOUND
const RANDOM_IDENTIFIER_BUILD_MAX_TRIES
resetElementsIndentations()
resets the indentation to level 0 for all elements in list
buildSolutionIdentifier()
getElementByPosition($position)
getParityTrueElementList(self $otherList)
static $objectInstanceCounter
getSolutionIdentifierIndexedElements()
withQuestionId(int $question_id)
__construct(int $question_id=null, array $elements=[])
ilAssOrderingElementList constructor.
static isValidIndentation($indentation)
hasValidIdentifiers(ilAssOrderingElement $element)
isLastElementPosition($position)
ensureValidIdentifiers(ilAssOrderingElement $element)
const IDENTIFIER_TYPE_RANDOM
registerIdentifier(ilAssOrderingElement $element, $identifierType)
const SOLUTION_IDENTIFIER_BUILD_MAX_TRIES
getDifferenceElementList(self $otherElementList)