diff --git a/bin/qtisdk b/bin/qtisdk index 0f15f24b4..5620f6fa2 100755 --- a/bin/qtisdk +++ b/bin/qtisdk @@ -1,7 +1,9 @@ #!/usr/bin/env php getArguments(); + + try { + $compactDoc = new XmlCompactDocument(); + $compactDoc->load($arguments['xml']); + + /** @var AssessmentTest $test */ + $test = $compactDoc->getDocumentComponent(); + $sessionManager = new SessionManager(new FileSystemFileManager()); + + $sessionId = 'dump-qtism-session-id'; + $storage = new LocalQtiBinaryStorage($sessionManager, $test); + $stream = new MemoryStream(base64_decode($arguments['session'])); + $storage->setStream($stream, $sessionId); + $session = $storage->retrieve($sessionId); + + $this->out("Assessment Test Session Dump"); + $this->out("============================"); + $this->out(''); + + $this->out("Test Identifier: " . $session->getAssessmentTest()->getIdentifier()); + $this->out("Test Title: " . $session->getAssessmentTest()->getTitle()); + $this->out("Test Tool Name: " . $session->getAssessmentTest()->getToolName()); + $this->out("Test Tool Version: " . $session->getAssessmentTest()->getToolVersion()); + $this->out("Session State: " . AssessmentTestSessionState::getNameByConstant($session->getState())); + $this->out(''); + + $this->out("Assessment Item Sessions"); + $this->out('========================'); + + /** @var AssessmentItemSession $itemSession */ + foreach ($session->getAssessmentItemSessionStore()->getAllAssessmentItemSessions() as $itemSession) { + $this->out("Assessment Item Identifier: " . $itemSession->getAssessmentItem()->getIdentifier()); + $this->out("Assessment Item Title: " . $itemSession->getAssessmentItem()->getTitle()); + $this->out("Assessment Item Label: " . $itemSession->getAssessmentItem()->getLabel()); + $this->out("Assessment Item Session State: " . AssessmentItemSessionState::getNameByConstant($itemSession->getState())); + + /** @var Variable $variable */ + foreach ($itemSession as $variable) { + $lastPart = $variable->getIdentifier() . ': ' . $variable->getValue(); + $firstPart = 'Response Variable'; + + if ($variable instanceof OutcomeVariable) { + $firstPart = 'Outcome Variable'; + } elseif ($variable instanceof TemplateVariable) { + $firstPart = 'TemplateVariable'; + } + + $this->out("${firstPart} ${lastPart}"); + } + + $this->out(''); + } + + $this->out("Duration by Location"); + $this->out("===================="); + + $durationStore = $session->getDurationStore(); + foreach ($durationStore as $variable) { + echo 'Identifier: ' . $variable->getIdentifier() . ' / Time: ' . $variable->getValue() . "\n"; + } + + } catch (XmlStorageException $e) { + $this->fail('XML Compact Test document could not be read.'); + } catch (StorageException $e) { + $this->fail('The session could not be rebuilt.'); + } + + } + + /** + * @inheritDoc + */ + protected function setupArguments(): Arguments + { + $arguments = new Arguments(['strict' => false]); + + // -- Options + // Session option. + $arguments->addOption( + ['session'], + [ + 'description' => 'The base64 session string to dump.', + ] + ); + + // XML option. + $arguments->addOption( + ['xml'], + [ + 'description' => 'The path to the related XML Compact Test file.', + ] + ); + + return $arguments; + } + + /** + * @inheritDoc + */ + protected function checkArguments(): void + { + $arguments = $this->getArguments(); + + if ($arguments['xml'] === null) { + $this->fail('Please provide the --xml argument.'); + } else if ($arguments['session'] === null) { + $this->fail('Please provide the --session argument.'); + } + } +} diff --git a/src/qtism/cli/CompactTest.php b/src/qtism/cli/CompactTest.php new file mode 100644 index 000000000..8e8ab66d0 --- /dev/null +++ b/src/qtism/cli/CompactTest.php @@ -0,0 +1,65 @@ +getArguments(); + + $doc = new XmlDocument(); + try { + $doc->load($arguments['input']); + $compactDoc = XmlCompactDocument::createFromXmlAssessmentTestDocument($doc); + $compactDoc->save($arguments['output']); + + } catch (XmlStorageException $e) { + $this->fail('Input XML Test document could not be read.'); + } catch (ReflectionException $e) { + $this->fail('An unexpected error occurred.'); + } + } + + protected function setupArguments(): Arguments + { + $arguments = new Arguments(['strict' => false]); + + // -- Options + // Session option. + $arguments->addOption( + ['input'], + [ + 'description' => 'The input QTI test file.', + ] + ); + + // XML option. + $arguments->addOption( + ['output'], + [ + 'description' => 'The output QTI compact test file.', + ] + ); + + return $arguments; + } + + protected function checkArguments() + { + $arguments = $this->getArguments(); + + if ($arguments['input'] === null) { + $this->fail('Please provide the --input argument.'); + } else if ($arguments['output'] === null) { + $this->fail('Please provide the --output argument.'); + } + } +} \ No newline at end of file diff --git a/src/qtism/cli/Render.php b/src/qtism/cli/Render.php index 259e9711f..983d020e5 100644 --- a/src/qtism/cli/Render.php +++ b/src/qtism/cli/Render.php @@ -280,14 +280,14 @@ private function runGoldilocks(XmlDocument $doc, GoldilocksRenderingEngine $rend $body = substr($body, 0, strlen('') * -1); $body = "{$nl}"; } else { - $body = $xml->saveXml($xml->documentElement) . {$nl}; + $body = $xml->saveXml($xml->documentElement) . $nl; } if ($arguments['document'] === true) { $footer = "\n"; } } else { - $body = $xml->saveXml($xml->documentElement) . {$nl}; + $body = $xml->saveXml($xml->documentElement) . $nl; } // Indent body... @@ -358,7 +358,7 @@ private function runXhtml(XmlDocument $doc, XhtmlRenderingEngine $renderer): str $footer .= "\n"; } - $body = $xml->saveXml($xml->documentElement) . {$nl}; + $body = $xml->saveXml($xml->documentElement) . $nl; // Indent body... $indentBody = ''; diff --git a/src/qtism/runtime/storage/binary/LocalQtiBinaryStorage.php b/src/qtism/runtime/storage/binary/LocalQtiBinaryStorage.php index 59b651bea..2806b4290 100644 --- a/src/qtism/runtime/storage/binary/LocalQtiBinaryStorage.php +++ b/src/qtism/runtime/storage/binary/LocalQtiBinaryStorage.php @@ -170,4 +170,17 @@ public function delete(AssessmentTestSession $assessmentTestSession): bool return @unlink($this->getPath() . DIRECTORY_SEPARATOR . md5($assessmentTestSession->getSessionId()) . '.bin'); } + + /** + * Set the $stream value manually for a given $sessionId. + * + * @param MemoryStream $stream + * @param string $sessionId + * @return void + */ + public function setStream(MemoryStream $stream, string $sessionId): void + { + $path = $this->getPath() . DIRECTORY_SEPARATOR . md5($sessionId) . '.bin'; + @file_put_contents($path, $stream->getBinary()); + } }