/** * @depends testVeryBasic */ public function testTemplateConstraintImpossibleWithTemplateVariableOnly() { $component = $this->createComponentFromXml(' <templateProcessing> <setTemplateValue identifier="TEMPLATE"> <baseValue baseType="integer">0</baseValue> </setTemplateValue> <templateConstraint> <gt> <variable identifier="TEMPLATE"/> <baseValue baseType="integer">0</baseValue> </gt> </templateConstraint> </templateProcessing> '); $var = new TemplateVariable('TEMPLATE', Cardinality::SINGLE, BaseType::INTEGER); $var->setDefaultValue(new QtiInteger(-1)); $state = new State(array($var)); // The <templateConstraint> will never be satisfied. // We should then find the default value in TEMPLATE. $engine = new TemplateProcessingEngine($component, $state); $engine->process(); $this->assertEquals(-1, $state['TEMPLATE']->getValue()); }
public function testCreateFromDataModel() { $decl = $this->createComponentFromXml(' <templateDeclaration identifier="mytpl1" cardinality="single" baseType="identifier" paramVariable="true" mathVariable="false"> <defaultValue> <value>default</value> </defaultValue> </templateDeclaration> '); $var = TemplateVariable::createFromDataModel($decl); $this->assertInstanceOf('qtism\\runtime\\common\\TemplateVariable', $var); $this->assertEquals('mytpl1', $var->getIdentifier()); $this->assertEquals(Cardinality::SINGLE, $var->getCardinality()); $this->assertEquals(BaseType::IDENTIFIER, $var->getCardinality()); $this->assertEquals('default', $var->getDefaultValue()->getValue()); $this->assertSame(null, $var->getValue()); $this->assertTrue($var->isParamVariable()); $this->assertFalse($var->isMathVariable()); }
/** * Create a new AssessmentItemSession object. * * * The built-in response variables 'numAttempts' and 'duration' will be created and set up with appropriate default values, respectively Integer(0) and Duration('PT0S'). * * The built-in outcome variable 'completionStatus' will be created and set up with an appropriate default value of String('not_attempted'). * * The item session is set up with a default ItemSessionControl object. If you want a specific ItemSessionControl object to rule the session, use the setItemSessionControl() method. * * The item session is set up with no TimeLimits object. If you want to set a a specfici TimeLimits object to rule the session, use the setTimeLimits() method. * * @param \qtism\data\IAssessmentItem $assessmentItem The description of the item that the session handles. * @param integer $navigationMode (optional) A value from the NavigationMode enumeration. * @param integer $submissionMode (optional) A value from the SubmissionMode enumeration. * @throws \InvalidArgumentException If $navigationMode or $submission is not a value from the NavigationMode/SubmissionMode enumeration. * @see \qtism\runtime\tests\AssessmentItemSession::setItemSessionControl() The setItemSessionControl() method. * @see \qtism\runtime\tests\AssessmentItemSession::setTimeLimits() The setTimeLimits() method. */ public function __construct(IAssessmentItem $assessmentItem, $navigationMode = NavigationMode::LINEAR, $submissionMode = SubmissionMode::INDIVIDUAL) { parent::__construct(); $this->setAssessmentItem($assessmentItem); $this->setItemSessionControl(new ItemSessionControl()); $this->setNavigationMode($navigationMode); $this->setSubmissionMode($submissionMode); // -- Create the built-in response variables. $this->setVariable(new ResponseVariable('numAttempts', Cardinality::SINGLE, BaseType::INTEGER, new QtiInteger(0))); $this->setVariable(new ResponseVariable('duration', Cardinality::SINGLE, BaseType::DURATION, new QtiDuration('PT0S'))); // -- Create the built-in outcome variables. $this->setVariable(new OutcomeVariable('completionStatus', Cardinality::SINGLE, BaseType::IDENTIFIER, new QtiIdentifier(self::COMPLETION_STATUS_NOT_ATTEMPTED))); // -- Create item specific outcome, response and template variables. foreach ($assessmentItem->getOutcomeDeclarations() as $outcomeDeclaration) { $outcomeVariable = OutcomeVariable::createFromDataModel($outcomeDeclaration); $this->setVariable($outcomeVariable); } foreach ($this->getAssessmentItem()->getResponseDeclarations() as $responseDeclaration) { $responseVariable = ResponseVariable::createFromDataModel($responseDeclaration); $this->setVariable($responseVariable); } foreach ($assessmentItem->getTemplateDeclarations() as $templateDeclaration) { $templateVariable = TemplateVariable::createFromDataModel($templateDeclaration); $this->setVariable($templateVariable); } // -- Perform choice shuffling for interactions by creating the Shuffling States for this item session. $shufflingStates = new ShufflingCollection(); foreach ($assessmentItem->getShufflings() as $shuffling) { $shufflingStates[] = $shuffling->shuffle(); } $this->setShufflingStates($shufflingStates); }
use qtism\runtime\common\State; use qtism\runtime\rendering\markup\AbstractMarkupRenderingEngine; use qtism\common\enums\BaseType; use qtism\common\enums\Cardinality; use qtism\runtime\common\TemplateVariable; use qtism\data\storage\xml\XmlDocument; use qtism\runtime\rendering\markup\xhtml\XhtmlRenderingEngine; require_once dirname(__FILE__) . '/../../qtism/qtism.php'; $doc = new XmlDocument(); $doc->load('../samples/rendering/choiceinteraction_1.xml'); $renderer = new XhtmlRenderingEngine(); $shuffle = false; if (isset($argv[1]) && $argv[1] === 'shuffle') { $renderer->setShuffle(true); $shuffle = true; } if (isset($argv[1]) && $shuffle === true && isset($argv[2]) || isset($argv[1]) && $shuffle === false) { $templateVariable = new TemplateVariable('SHOWBLACK', Cardinality::SINGLE, BaseType::IDENTIFIER); if ($shuffle === true) { $templateVariable->setValue($argv[2]); } else { $templateVariable->setValue($argv[1]); } $renderer->setChoiceShowHidePolicy(AbstractMarkupRenderingEngine::CONTEXT_AWARE); $state = new State(array($templateVariable)); $renderer->setState($state); } $rendering = $renderer->render($doc->getDocumentComponent()); $rendering->formatOutput = true; echo $rendering->saveXML();
use qtism\common\datatypes\Identifier; use qtism\runtime\common\State; use qtism\runtime\rendering\markup\AbstractMarkupRenderingEngine; use qtism\common\enums\BaseType; use qtism\common\enums\Cardinality; use qtism\runtime\common\TemplateVariable; use qtism\data\storage\xml\XmlDocument; use qtism\runtime\rendering\markup\xhtml\XhtmlRenderingEngine; require_once dirname(__FILE__) . '/../../vendor/autoload.php'; $doc = new XmlDocument(); $doc->load(dirname(__FILE__) . '/../samples/rendering/choiceinteraction_1.xml'); $renderer = new XhtmlRenderingEngine(); $shuffle = false; if (isset($argv[1]) && $argv[1] === 'shuffle') { $renderer->setShufflingPolicy(XhtmlRenderingEngine::CONTEXT_AWARE); $shuffle = true; } if (isset($argv[1]) && $shuffle === true && isset($argv[2]) || isset($argv[1]) && $shuffle === false) { $templateVariable = new TemplateVariable('SHOWBLACK', Cardinality::SINGLE, BaseType::IDENTIFIER); if ($shuffle === true) { $templateVariable->setValue(new Identifier($argv[2])); } else { $templateVariable->setValue(new Identifier($argv[1])); } $renderer->setChoiceShowHidePolicy(AbstractMarkupRenderingEngine::CONTEXT_AWARE); $state = new State(array($templateVariable)); $renderer->setState($state); } $rendering = $renderer->render($doc->getDocumentComponent()); $rendering->formatOutput = true; echo $rendering->saveXML();
/** * Write an AssessmetnItemSession from the current binary stream. * * @param \qtism\runtime\storage\common\AssessmentTestSeeker $seeker The AssessmentTestSeeker object from where the position of components will be pulled out. * @param \qtism\runtime\tests\AssessmentItemSession $session An AssessmentItemSession object. * @throws \qtism\runtime\storage\binary\QtiBinaryStreamAccessException */ public function writeAssessmentItemSession(AssessmentTestSeeker $seeker, AssessmentItemSession $session) { try { $this->writeShort($seeker->seekPosition($session->getAssessmentItem())); $this->writeTinyInt($session->getState()); $this->writeTinyInt($session->getNavigationMode()); $this->writeTinyInt($session->getSubmissionMode()); $this->writeBoolean($session->isAttempting()); $isItemSessionControlDefault = $session->getItemSessionControl()->isDefault(); if ($isItemSessionControlDefault === true) { $this->writeBoolean(false); } else { $this->writeBoolean(true); $this->writeShort($seeker->seekPosition($session->getItemSessionControl())); } $this->writeTinyInt($session['numAttempts']->getValue()); $this->writeDuration($session['duration']); $this->writeString($session['completionStatus']->getValue()); $timeReference = $session->getTimeReference(); if (is_null($timeReference) === true) { // Describe that we have no time reference for the session. $this->writeBoolean(false); } else { // Describe that we have a time reference for the session. $this->writeBoolean(true); // Write the time reference. $this->writeDateTime($timeReference); } // Write the session variables. // (minus the 3 built-in variables) $varCount = count($session) - 3; $this->writeTinyInt($varCount); $itemOutcomes = $session->getAssessmentItem()->getOutcomeDeclarations(); $itemResponses = $session->getAssessmentItem()->getResponseDeclarations(); $itemTemplates = $session->getAssessmentItem()->getTemplateDeclarations(); foreach ($session->getKeys() as $varId) { if (in_array($varId, array('numAttempts', 'duration', 'completionStatus')) === false) { $var = $session->getVariable($varId); if ($var instanceof OutcomeVariable) { $variableDeclaration = $itemOutcomes[$varId]; $variable = OutcomeVariable::createFromDataModel($variableDeclaration); $varNature = 0; } elseif ($var instanceof ResponseVariable) { $variableDeclaration = $itemResponses[$varId]; $variable = ResponseVariable::createFromDataModel($variableDeclaration); $varNature = 1; } elseif ($var instanceof TemplateVariable) { $variableDeclaration = $itemTemplates[$varId]; $variable = TemplateVariable::createFromDataModel($variableDeclaration); $varNature = 2; } try { $this->writeShort($varNature); $this->writeShort($seeker->seekPosition($variableDeclaration)); // If defaultValue or correct response is different from what's inside // the variable declaration, just write it. $hasDefaultValue = !Utils::equals($variable->getDefaultValue(), $var->getDefaultValue()); $hasCorrectResponse = false; if ($varNature === 1 && !Utils::equals($variable->getCorrectResponse(), $var->getCorrectResponse())) { $hasCorrectResponse = true; } $this->writeBoolean($hasDefaultValue); $this->writeBoolean($hasCorrectResponse); $this->writeVariableValue($var, self::RW_VALUE); if ($hasDefaultValue === true) { $this->writeVariableValue($var, self::RW_DEFAULTVALUE); } if ($hasCorrectResponse === true) { $this->writeVariableValue($var, self::RW_CORRECTRESPONSE); } } catch (OutOfBoundsException $e) { $msg = "No variable found in the assessmentTest tree structure."; throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::ITEM_SESSION, $e); } } } // Write shuffling states if any. $shufflingStates = $session->getShufflingStates(); $this->writeTinyInt(count($shufflingStates)); foreach ($shufflingStates as $shufflingState) { $this->writeShufflingState($shufflingState); } } catch (BinaryStreamAccessException $e) { $msg = "An error occured while writing an assessment item session."; throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::ITEM_SESSION, $e); } catch (OutOfBoundsException $e) { $msg = "No assessmentItemRef found in the assessmentTest tree structure."; throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::ITEM_SESSION, $e); } }