* @author Jérôme Bogaerts * @package taoQtiItem * * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php */ class QtiRunner { /** * Get the intrinsic values of a given QTI $variable. * * @param Variable $variable * @return array */ public static function getVariableValues(Variable $variable) { $returnValue = []; $baseType = $variable->getBaseType(); $cardinalityType = $variable->getCardinality(); $value = $variable->getValue(); // This only works if the variable has a value ;) if ($value !== null) { if ($baseType === BaseType::IDENTIFIER) { if ($cardinalityType === Cardinality::SINGLE) { $returnValue[] = $value->getValue(); } elseif ($cardinalityType === Cardinality::MULTIPLE) { foreach ($variable->getValue() as $value) { $returnValue[] = $value->getValue(); } } } } return $returnValue; } /** * Get the flysystem path to the compilation folder described by $directory. * * @param tao_models_classes_service_StorageDirectory $directory The root directory resource where the item is stored. * @return string The flysystem path to the private folder with a trailing directory separator. */ public static function getPrivatePathByLanguage(tao_models_classes_service_StorageDirectory $directory) { $lang = \common_session_SessionManager::getSession()->getDataLanguage(); if (!$directory->has($lang) && $directory->has(DEFAULT_LANG)) { $lang = DEFAULT_LANG; } return $lang . DIRECTORY_SEPARATOR; } /** * Get the JSON QTI Model representing the elements (A.K.A. components) that vary over time for * the item stored in $directory. * * @param tao_models_classes_service_StorageDirectory $directory * @return array A JSON decoded array. */ public static function getContentVariableElements(tao_models_classes_service_StorageDirectory $directory) { $jsonFile = self::getPrivatePathByLanguage($directory) . 'variableElements.json'; $data = $directory->read($jsonFile); return json_decode($data, true); } /** * Get rubric block visible by the given "view" * * @param tao_models_classes_service_StorageDirectory $directory * @param type $view * @return array */ public static function getRubricBlocks(tao_models_classes_service_StorageDirectory $directory, $view) { $returnValue = []; $elements = self::getContentVariableElements($directory); foreach ($elements as $serial => $data) { if (isset($data['qtiClass']) && $data['qtiClass'] == 'rubricBlock') { if (!empty($data['attributes']) && is_array($data['attributes']['view']) && in_array($view, $data['attributes']['view'])) { $returnValue[$serial] = $data; } } } return $returnValue; } /** * Get the feedback to be displayed on an AssessmentItemSession * * @param tao_models_classes_service_StorageDirectory $directory * @param \qtism\runtime\tests\AssessmentItemSession $itemSession * @return array */ public static function getFeedbacks(tao_models_classes_service_StorageDirectory $directory, AssessmentItemSession $itemSession) { $returnValue = []; $feedbackClasses = ['modalFeedback', 'feedbackInline', 'feedbackBlock']; $elements = self::getContentVariableElements($directory); $outcomes = []; foreach ($elements as $data) { if (empty($data['qtiClass']) === false && in_array($data['qtiClass'], $feedbackClasses)) { $feedbackIdentifier = $data['attributes']['identifier']; $outcomeIdentifier = $data['attributes']['outcomeIdentifier']; if (!isset($outcomes[$outcomeIdentifier])) { $outcomes[$outcomeIdentifier] = []; } $outcomes[$outcomeIdentifier][$feedbackIdentifier] = $data; } } foreach ($itemSession->getAllVariables() as $var) { $identifier = $var->getIdentifier(); if (isset($outcomes[$identifier])) { $feedbacks = $outcomes[$identifier]; $feedbackIds = QtiRunner::getVariableValues($var); foreach ($feedbackIds as $feedbackId) { if (isset($feedbacks[$feedbackId])) { $data = $feedbacks[$feedbackId]; $returnValue[$data['serial']] = $data; } } } } return $returnValue; } }