*/ namespace oat\taoQtiTest\scripts\cli; use oat\oatbox\action\Action; use qtism\data\storage\xml\marshalling\MarshallerFactory; use qtism\data\rules\BranchRule; use qtism\data\rules\BranchRuleCollection; /** * An BranchRule injection script. * * This script aims at providing the mechanics to inject dynamically some QTI-XML representing a QTI branchRule * into a QTI-XML test definition. * * Parameter 1 is the URI of the test you want to inject the branchRule. * Parameter 2 is the QTI identifier of the assessmentItemRef you want the branchRule to be appended to. * stdin must contain the QTI-XML representing the branchRule to inject. * * Example usage: * sudo -u www-data php index.php "oat\taoQtiTest\scripts\cli\InjectBranchRule" "http://taoplatform300/300.rdf#i1475242228154935" "item-1" < /home/jerome/Documents/input.txt */ class InjectBranchRule implements Action { public function __invoke($params) { // load constants... \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest')->load(); if (empty($params[0]) === true) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'Test URI not provided as parameter 1.' ); } if (empty($params[1]) === true) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'AssessmentItemRefIdentifier not provided as parameter 2.' ); } $testResource = new \core_kernel_classes_Resource($params[0]); if ($testResource->exists() === false) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'No RDFS Resource found for URI ' . $params[0] . '.' ); } $qtiService = \taoQtiTest_models_classes_QtiTestService::singleton(); $testDoc = $qtiService->getDoc($testResource); $test = $testDoc->getDocumentComponent(); $assessmentItemRef = $test->getComponentByIdentifier($params[1]); if (!$assessmentItemRef) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'No QTI assessmentItemRef with identifier ' . $params[1] . ' found in the QTI Test definition.' ); } $input = file_get_contents('php://stdin'); if (empty($input) === true) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'No QTI-XML input provided in stdin.' ); } else { $dom = new \DOMDocument('1.0', 'UTF-8'); if (@$dom->loadXML($input)) { $element = $dom->documentElement; $marshallerFactory = new MarshallerFactory(); $marshaller = $marshallerFactory->createMarshaller($element); $component = $marshaller->unmarshall($element); if (!$component instanceof BranchRule) { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'No QTI branchRule component found in stdin.' ); } $assessmentItemRef->setBranchRules(new BranchRuleCollection([$component])); $qtiService->getQtiTestFile($testResource)->update($testDoc->saveToString()); } else { return new \common_report_Report( \common_report_Report::TYPE_ERROR, 'Invalid QTI-XML input provided in stdin.' ); } } return new \common_report_Report(\common_report_Report::TYPE_SUCCESS, "BranchRule found in stdin successfully appended to assessmentItemRef '" . $params[1] . "' part of QTI Test with URI '" . $params[0] . "'."); } }