4 require_once
'Modules/TestQuestionPool/classes/questions/class.ilAssOrderingElement.php';
36 self::IDENTIFIER_TYPE_SOLUTION =>
array(),
37 self::IDENTIFIER_TYPE_RANDOM =>
array()
55 $this->objectInstanceId = ++self::$objectInstanceCounter;
57 $this->questionId = null;
66 $this->objectInstanceId = ++self::$objectInstanceCounter;
70 foreach ($this as
$key => $element) {
110 "SELECT * FROM qpl_a_ordering WHERE question_fi = %s ORDER BY position ASC",
118 $element->setRandomIdentifier(
$row[
'random_id']);
119 $element->setSolutionIdentifier(
$row[
'solution_key']);
121 $element->setPosition(
$row[
'position']);
122 $element->setIndentation(
$row[
"depth"]);
124 $element->setContent(
$row[
'answertext']);
134 public function saveToDb()
140 "DELETE FROM qpl_a_ordering WHERE question_fi = %s",
145 foreach ($this as $orderElement) {
149 'answer_id' =>
array(
'integer',
$ilDB->nextId(
'qpl_a_ordering') ),
151 'answertext' =>
array(
'text', $orderElement->getContent()),
152 'solution_key' =>
array(
'integer', $orderElement->getSolutionIdentifier() ),
153 'random_id' =>
array(
'integer', $orderElement->getRandomIdentifier() ),
154 'position' =>
array(
'integer', $orderElement->getPosition() ),
155 'depth' =>
array(
'integer', $orderElement->getIndentation() ),
166 foreach ($this as $orderingElement) {
167 $orderingElement->setContent(
'');
173 return count($this->elements);
178 return $position == 0;
191 $elementList =
new self();
194 foreach ($this as $element) {
195 if ($element->getPosition() == $currentPosition) {
196 $elementList->addElement($dodgingElement);
200 if ($element->getPosition() == $targetPosition) {
201 $elementList->addElement($movingElement);
205 $elementList->addElement($element);
208 $dodgingElement->setPosition($currentPosition);
209 $movingElement->setPosition($targetPosition);
216 $elementList =
new self();
219 $positionCounter = 0;
221 foreach ($this as $element) {
222 if ($element->isSameElement($removeElement)) {
226 $element->setPosition($positionCounter++);
227 $elementList->addElement($element);
236 $this->elements =
array();
298 foreach ($this as $element) {
314 $this->elements[] = $element;
323 if (isset($this->elements[$position])) {
324 return $this->elements[$position];
345 foreach ($this as $element) {
346 if ($element->getRandomIdentifier() != $randomIdentifier) {
371 foreach ($this as $element) {
372 if ($element->getSolutionIdentifier() != $solutionIdentifier) {
412 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
416 return self::$identifierRegistry[$identifierType][$this->
getQuestionId()];
425 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_SOLUTION);
431 $identifier = $this->
fetchIdentifier($element, self::IDENTIFIER_TYPE_RANDOM);
480 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
486 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
487 self::$identifierRegistry[$identifierType][$this->
getQuestionId()][] = $identifier;
499 if (!isset(self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
505 if (!in_array($identifier, self::$identifierRegistry[$identifierType][$this->
getQuestionId()])) {
520 switch ($identifierType) {
536 switch ($identifierType) {
551 switch ($identifierType) {
552 case self::IDENTIFIER_TYPE_SOLUTION:
553 return self::isValidSolutionIdentifier($identifier);
555 case self::IDENTIFIER_TYPE_RANDOM:
556 return self::isValidRandomIdentifier($identifier);
569 switch ($identifierType) {
584 "unknown identifier type given (type: $identifierType)" 595 "could not build random identifier (max tries: $maxTries)" 606 "cannot reorder element due to missing position (random identifier: $randomIdentifier)" 617 'cannot reorder element due to one or more unknown random identifiers ' .
618 '(' . implode(
', ', $randomIdentifiers) .
')' 627 $lastSolutionIdentifier = null;
630 if ($lastSolutionIdentifier > $registeredIdentifier) {
634 $lastSolutionIdentifier = $registeredIdentifier;
637 return $lastSolutionIdentifier;
647 if ($lastSolutionIdentifier === null) {
651 $nextSolutionIdentifier = $lastSolutionIdentifier + self::SOLUTION_IDENTIFIER_VALUE_INTERVAL;
653 return $nextSolutionIdentifier;
662 $usedTriesCounter = 0;
665 if ($usedTriesCounter >= self::RANDOM_IDENTIFIER_BUILD_MAX_TRIES) {
671 $lowerBound = self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND;
672 $upperBound = self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND;
673 $randomIdentifier = mt_rand($lowerBound, $upperBound);
676 $testElement->setRandomIdentifier($randomIdentifier);
679 return $randomIdentifier;
684 if (!is_numeric($identifier)) {
688 if ($identifier != (
int) $identifier) {
692 if ($identifier < 0) {
701 if (!is_numeric($identifier)) {
705 if ($identifier != (
int) $identifier) {
709 if ($identifier < self::RANDOM_IDENTIFIER_RANGE_LOWER_BOUND) {
713 if ($identifier > self::RANDOM_IDENTIFIER_RANGE_UPPER_BOUND) {
722 return self::isValidSolutionIdentifier($position);
727 return self::isValidPosition($indentation);
735 foreach ($this as $element) {
746 $numIntersectingElements = count(array_intersect(
747 $otherList->getRandomIdentifierIndex(),
755 if ($numIntersectingElements != $otherList->countElements()) {
761 $otherListRandomIdentifierIndex = $otherList->getRandomIdentifierIndex();
763 foreach ($this as $orderingElement) {
764 if (!in_array($orderingElement->getRandomIdentifier(), $otherListRandomIdentifierIndex)) {
768 $randomIdentifierIndexMatchingsCount = count(array_keys(
769 $otherListRandomIdentifierIndex,
770 $orderingElement->getRandomIdentifier(),
774 if ($randomIdentifierIndexMatchingsCount != 1) {
779 return $this->
countElements() == $otherList->countElements();
788 $parityTrueElementList =
new self();
789 $parityTrueElementList->setQuestionId($this->
getQuestionId());
791 foreach ($this as $thisElement) {
792 $otherElement = $otherList->getElementByRandomIdentifier(
793 $thisElement->getRandomIdentifier()
796 if ($otherElement->getPosition() != $thisElement->getPosition()) {
800 if ($otherElement->getIndentation() != $thisElement->getIndentation()) {
804 $parityTrueElementList->addElement($thisElement);
807 return $parityTrueElementList;
817 $positionsMap = array_flip(array_values($randomIdentifiers));
819 $orderedElements =
array();
821 foreach ($this as $element) {
822 if (!isset($positionsMap[$element->getRandomIdentifier()])) {
826 $position = $positionsMap[$element->getRandomIdentifier()];
827 unset($positionsMap[$element->getRandomIdentifier()]);
829 $element->setPosition($position);
830 $orderedElements[$position] = $element;
833 if (count($positionsMap)) {
837 ksort($orderedElements);
839 $this->
setElements(array_values($orderedElements));
847 foreach ($this as $element) {
848 $element->setIndentation(0);
860 $differenceElementList =
new self();
861 $differenceElementList->setQuestionId($this->
getQuestionId());
863 foreach ($differenceRandomIdentifierIndex as $randomIdentifier) {
865 $differenceElementList->addElement($element);
868 return $differenceElementList;
877 $differenceRandomIdentifierIndex = array_diff(
879 $otherElementList->getRandomIdentifierIndex()
882 return $differenceRandomIdentifierIndex;
890 foreach ($this as $thisElement) {
891 if (!$otherList->elementExistByRandomIdentifier($thisElement->getRandomIdentifier())) {
895 $otherElement = $otherList->getElementByRandomIdentifier(
896 $thisElement->getRandomIdentifier()
899 $thisElement->setContent($otherElement->getContent());
908 return current($this->elements);
916 return next($this->elements);
924 return key($this->elements);
932 return ($this->
key() !== null);
940 return reset($this->elements);
949 $element->setRandomIdentifier(self::FALLBACK_DEFAULT_ELEMENT_RANDOM_IDENTIFIER);
961 $elementList =
new self();
964 $elementList->setElements($orderingElements);
__clone()
clone list by additionally cloning the element objects
reorderByRandomIdentifiers($randomIdentifiers)
registerIdentifiers(ilAssOrderingElement $element)
elementExistBySolutionIdentifier($solutionIdentifier)
static isValidPosition($position)
const IDENTIFIER_TYPE_SOLUTION
const JS_ADDED_ELEMENTS_RANDOM_IDENTIFIER_START_VALUE
static $identifierRegistry
isValidIdentifier($identifierType, $identifier)
getRegisteredSolutionIdentifiers()
getLastSolutionIdentifier()
__construct()
ilAssOrderingElementList constructor.
const SOLUTION_IDENTIFIER_START_VALUE
populateIdentifier(ilAssOrderingElement $element, $identifierType, $identifier)
removeElement(ilAssOrderingElement $removeElement)
const RANDOM_IDENTIFIER_RANGE_UPPER_BOUND
setSolutionIdentifier($solutionIdentifier)
isFirstElementPosition($position)
completeContentsFromElementList(self $otherList)
elementExistByPosition($position)
moveElementByPositions($currentPosition, $targetPosition)
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
hasSameElementSetByRandomIdentifiers(self $otherList)
static buildInstance($questionId, $orderingElements=array())
setQuestionId($questionId)
elementExistByRandomIdentifier($randomIdentifier)
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)
loadFromDb()
load elements from database
throwUnknownRandomIdentifiersException($randomIdentifiers)
getElementBySolutionIdentifier($solutionIdentifier)
ensureValidIdentifier(ilAssOrderingElement $element, $identifierType)
throwUnknownIdentifierTypeException($identifierType)
addElement(ilAssOrderingElement $element)
setRandomIdentifier($randomIdentifier)
getDifferenceRandomIdentifierIndex(self $otherElementList)
getIndexedElements($identifierType)
isIdentifierRegistered(ilAssOrderingElement $element, $identifierType)
getRandomIdentifierIndexedElements()
static getFallbackDefaultElement()
getRandomIdentifierIndex()
Create styles array
The data for the language used.
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)
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static $objectInstanceCounter
getSolutionIdentifierIndexedElements()
static isValidIndentation($indentation)
hasValidIdentifiers(ilAssOrderingElement $element)
isLastElementPosition($position)
ensureValidIdentifiers(ilAssOrderingElement $element)
clearElementContents()
clears the contents of all elements
const IDENTIFIER_TYPE_RANDOM
registerIdentifier(ilAssOrderingElement $element, $identifierType)
const SOLUTION_IDENTIFIER_BUILD_MAX_TRIES
getDifferenceElementList(self $otherElementList)