Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: selection of required and not required items #308

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/qtism/runtime/tests/BasicSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function select()

if ($withReplacement === false) {
unset($selectionBag[$baseIndex]);
$selectionBag = array_values($selectionBag);
Copy link
Member

@wazelin wazelin Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 95 has the same issue, I believe.

WDYT of reversing the logic here, and populating the $selectionBag values in a foreach loop, while iterating over $selectableRoutes, instead of setting all the values there, and then unsetting the unwanted ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Line 95 doesn't have this issue, because the index to access the reindexed array is calculated on each iteration, and not fixed by a foreach.
I would keep the overall implementation the same, for me it makes sense to pick out the required items and then randomly pick as much as needed from the leftovers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, true. I wasn't paying enough attention.
Thanks for elaborating!

$selectionBagCount--;
$select--;
}
}
}
// reset indexes for remaining selections
$selectionBag = array_values($selectionBag);

for ($i = 0; $i < $select; $i++) {
$selectedIndex = mt_rand(0, $selectionBagCount - 1);
Expand Down
34 changes: 34 additions & 0 deletions test/qtismtest/runtime/tests/BasicSelectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace qtismtest\runtime\tests;

use qtism\data\AssessmentItemRef;
use qtism\data\SectionPart;
use qtism\data\storage\xml\XmlDocument;
use qtism\runtime\tests\BasicSelection;
use qtism\runtime\tests\SelectableRoute;
Expand Down Expand Up @@ -55,6 +57,38 @@ public function testBasicSelection()

$this::assertFalse($routeCheck1 === true && $routeCheck2 === true);
$this::assertTrue($routeCheck1 === true || $routeCheck2 === true);

}

public function testSelectRequired()
{
$doc = new XmlDocument();
$doc->load(self::samplesDir() . 'custom/runtime/selection_ordering/selection_and_ordering.xml');

$testPart = $doc->getDocumentComponent()->getComponentByIdentifier('testPart');

$s04 = $doc->getDocumentComponent()->getComponentByIdentifier('S04', true);
$this::assertEquals('S04', $s04->getIdentifier());

$s04RouteCollection = new SelectableRouteCollection();

/** @var SectionPart $sectionPart */
foreach ($s04->getSectionParts() as $sectionPart) {
$route = new SelectableRoute();
$route->addRouteItem($sectionPart, $s04, $testPart, $doc->getDocumentComponent());
$route->setRequired($sectionPart->isRequired());
$s04RouteCollection->attach($route);
}

$selection = new BasicSelection($s04, $s04RouteCollection);
$selectedRoutes = $selection->select();
$itemRefs = [];
foreach ($selectedRoutes as $r) {
foreach ($r->getAssessmentItemRefs() as $itemRef) {
$itemRefs[] = $itemRef->getIdentifier();
}
}
$this::assertEquals(5, count(array_unique($itemRefs)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@
<assessmentItemRef identifier="Q11" href="./Q11.xml" fixed="false" timeDependent="false"/>
<assessmentItemRef identifier="Q12" href="./Q12.xml" fixed="false" timeDependent="false"/>
</assessmentSection>
<assessmentSection identifier="S04" fixed="false" title="Section S04" visible="true">
<selection select="5"/>
<assessmentItemRef identifier="Q1" required="false" href="./Q1.xml" fixed="false" timeDependent="false" />
<assessmentItemRef identifier="Q2" required="true" href="./Q2.xml" fixed="false" timeDependent="false" />
<assessmentItemRef identifier="Q3" required="false" href="./Q3.xml" fixed="false" timeDependent="false" />
<assessmentItemRef identifier="Q7" required="true" href="./Q7.xml" fixed="false" timeDependent="false" />
<assessmentItemRef identifier="Q8" required="false" href="./Q8.xml" fixed="false" timeDependent="false" />
</assessmentSection>
</testPart>
</assessmentTest>
</assessmentTest>