49 self::IDENTIFIER_TYPE_SOLUTION => [],
50 self::IDENTIFIER_TYPE_RANDOM => []
71 $this->objectInstanceId = ++self::$objectInstanceCounter;
82 $this->objectInstanceId = ++self::$objectInstanceCounter;
86 foreach ($this as $key => $element) {
120 return count($this->elements);
130 return $position == 0;
143 $elementList =
new self();
146 foreach ($this as $element) {
147 if ($element->getPosition() == $currentPosition) {
148 $elementList->addElement($dodgingElement);
152 if ($element->getPosition() == $targetPosition) {
153 $elementList->addElement($movingElement);
157 $elementList->addElement($element);
160 $dodgingElement->setPosition($currentPosition);
161 $movingElement->setPosition($targetPosition);
168 $elementList =
new self();
171 $positionCounter = 0;
173 foreach ($this as $element) {
174 if ($element->isSameElement($removeElement)) {
178 $element->setPosition($positionCounter++);
179 $elementList->addElement($element);
188 $this->elements = [];
250 foreach ($this as $element) {
266 $this->elements[] = $element;
271 if (isset($this->elements[$position])) {
272 return $this->elements[$position];
285 foreach ($this as $element) {
286 if ($element->getRandomIdentifier() === intval($random_identifier)) {
309 foreach ($this as $element) {
310 if ($element->getSolutionIdentifier() != $solutionIdentifier) {
350 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
354 return self::$identifierRegistry[$identifierType][$this->
getQuestionId()];
363 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_SOLUTION);
369 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_RANDOM);
420 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
421 self::$identifierRegistry[$identifierType][$this->
getQuestionId()] = [];
426 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
427 self::$identifierRegistry[$identifierType][$this->
getQuestionId()][] = $identifier;
439 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
445 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
457 if ($identifierType == self::IDENTIFIER_TYPE_SOLUTION) {
474 switch ($identifierType) {
486 switch ($identifierType) {
487 case self::IDENTIFIER_TYPE_SOLUTION:
488 return self::isValidSolutionIdentifier($identifier);
490 case self::IDENTIFIER_TYPE_RANDOM:
491 return self::isValidRandomIdentifier($identifier);
499 switch ($identifierType) {
500 case self::IDENTIFIER_TYPE_SOLUTION:
503 case self::IDENTIFIER_TYPE_RANDOM:
517 "unknown identifier type given (type: $identifierType)" 528 "could not build random identifier (max tries: $maxTries)" 539 "cannot reorder element due to missing position (random identifier: $randomIdentifier)" 550 'cannot reorder element due to one or more unknown random identifiers ' .
551 '(' . implode(
', ', $randomIdentifiers) .
')' 560 $lastSolutionIdentifier =
null;
563 if ($lastSolutionIdentifier > $registeredIdentifier) {
567 $lastSolutionIdentifier = $registeredIdentifier;
570 return $lastSolutionIdentifier;
580 if ($lastSolutionIdentifier ===
null) {
584 $nextSolutionIdentifier = $lastSolutionIdentifier + self::SOLUTION_IDENTIFIER_VALUE_INTERVAL;
586 return $nextSolutionIdentifier;
595 $usedTriesCounter = 0;
598 if ($usedTriesCounter >= self::RANDOM_IDENTIFIER_BUILD_MAX_TRIES) {
604 $lowerBound = self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND;
605 $upperBound = self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND;
606 $randomIdentifier = mt_rand($lowerBound, $upperBound);
609 $testElement->setRandomIdentifier($randomIdentifier);
612 return $randomIdentifier;
617 return is_numeric($identifier)
618 && $identifier == (
int) $identifier
619 && (
int) $identifier >= 0;
624 return is_numeric($identifier)
625 && $identifier == (
int) $identifier
626 && (
int) $identifier >= self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND
627 && (
int) $identifier <= self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND;
632 return self::isValidSolutionIdentifier($position);
637 return self::isValidPosition($indentation);
645 foreach ($this as $element) {
655 $numIntersectingElements = count(array_intersect(
656 $otherList->getRandomIdentifierIndex(),
661 && $numIntersectingElements == $otherList->countElements();
670 $parityTrueElementList =
new self();
671 $parityTrueElementList->setQuestionId($this->
getQuestionId());
673 foreach ($this as $thisElement) {
674 $otherElement = $otherList->getElementByRandomIdentifier(
675 $thisElement->getRandomIdentifier()
678 if ($otherElement->getPosition() != $thisElement->getPosition()) {
682 if ($otherElement->getIndentation() != $thisElement->getIndentation()) {
686 $parityTrueElementList->addElement($thisElement);
689 return $parityTrueElementList;
698 $positionsMap = array_flip(array_values($randomIdentifiers));
700 $orderedElements = [];
702 foreach ($this as $element) {
703 if (!isset($positionsMap[$element->getRandomIdentifier()])) {
707 $position = $positionsMap[$element->getRandomIdentifier()];
708 unset($positionsMap[$element->getRandomIdentifier()]);
710 $element->setPosition($position);
711 $orderedElements[$position] = $element;
714 if (count($positionsMap)) {
718 ksort($orderedElements);
720 $this->
setElements(array_values($orderedElements));
728 foreach ($this as $element) {
729 $element->setIndentation(0);
741 $differenceElementList =
new self();
742 $differenceElementList->setQuestionId($this->
getQuestionId());
744 foreach ($differenceRandomIdentifierIndex as $randomIdentifier) {
746 $differenceElementList->addElement($element);
749 return $differenceElementList;
758 $differenceRandomIdentifierIndex = array_diff(
760 $otherElementList->getRandomIdentifierIndex()
763 return $differenceRandomIdentifierIndex;
771 foreach ($this as $thisElement) {
772 if (!$otherList->elementExistByRandomIdentifier($thisElement->getRandomIdentifier())) {
776 $otherElement = $otherList->getElementByRandomIdentifier(
777 $thisElement->getRandomIdentifier()
780 $thisElement->setContent($otherElement->getContent());
789 return current($this->elements);
797 next($this->elements);
805 return key($this->elements);
813 return ($this->
key() !==
null);
818 reset($this->elements);
827 $element->setRandomIdentifier(self::FALLBACK_DEFAULT_ELEMENT_RANDOM_IDENTIFIER);
835 $elementList =
new self();
847 foreach ($this as $element) {
848 $items[] = implode(
':', [
849 $element->getSolutionIdentifier(),
850 $element->getRandomIdentifier(),
851 $element->getIndentation()
855 return md5(serialize($items));
863 $clone = clone $this;
870 $clone = clone $this;
getElementByRandomIdentifier(int $random_identifier)
__clone()
clone list by additionally cloning the element objects
reorderByRandomIdentifiers($randomIdentifiers)
fetchIdentifier(ilAssOrderingElement $element, string $identifierType)
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)
PhpInconsistentReturnPointsInspection
getRegisteredSolutionIdentifiers()
getLastSolutionIdentifier()
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)
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)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
distributeNewRandomIdentifiers()
buildIdentifier($identifierType)
throwUnknownRandomIdentifiersException($randomIdentifiers)
getElementBySolutionIdentifier($solutionIdentifier)
ensureValidIdentifier(ilAssOrderingElement $element, $identifierType)
throwUnknownIdentifierTypeException($identifierType)
addElement(ilAssOrderingElement $element)
getDifferenceRandomIdentifierIndex(self $otherElementList)
elementExistByPosition(int $position)
getIndexedElements($identifierType)
isIdentifierRegistered(ilAssOrderingElement $element, $identifierType)
getRandomIdentifierIndexedElements()
static getFallbackDefaultElement()
getRandomIdentifierIndex()
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()
setSolutionIdentifier(int $solution_identifier)
getParityTrueElementList(self $otherList)
getElementByPosition(int $position)
static $objectInstanceCounter
getSolutionIdentifierIndexedElements()
withQuestionId(int $question_id)
static isValidIndentation($indentation)
hasValidIdentifiers(ilAssOrderingElement $element)
isLastElementPosition($position)
ensureValidIdentifiers(ilAssOrderingElement $element)
const IDENTIFIER_TYPE_RANDOM
registerIdentifier(ilAssOrderingElement $element, $identifierType)
__construct(?int $question_id=null, array $elements=[])
ilAssOrderingElementList constructor.
const SOLUTION_IDENTIFIER_BUILD_MAX_TRIES
getDifferenceElementList(self $otherElementList)