Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 97 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| QtiPreview | |
0.00% |
0 / 97 |
|
0.00% |
0 / 10 |
702 | |
0.00% |
0 / 1 |
| getPreviewUrl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| submitResponses | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| processResponses | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
90 | |||
| getResultServer | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| buildOutcomeResponse | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getRenderedItem | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
6 | |||
| getRequestView | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getRubricBlocks | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getModalFeedbacks | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getTemplateElements | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\controller; |
| 24 | |
| 25 | //use oat\taoQtiItem\controller\QtiPreview; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\tao\model\asset\AssetService; |
| 28 | use oat\taoQtiItem\helpers\QtiFile; |
| 29 | use oat\taoQtiItem\model\qti\Service; |
| 30 | use oat\taoQtiItem\model\qti\Item; |
| 31 | use OutOfBoundsException; |
| 32 | use OutOfRangeException; |
| 33 | use taoItems_actions_ItemPreview; |
| 34 | use tao_helpers_Uri; |
| 35 | use core_kernel_classes_Resource; |
| 36 | use common_Exception; |
| 37 | use taoQtiCommon_helpers_PciVariableFiller; |
| 38 | use common_Logger; |
| 39 | use taoQtiCommon_helpers_ResultTransmissionException; |
| 40 | use taoQtiCommon_helpers_PciStateOutput; |
| 41 | use taoQtiCommon_helpers_Utils; |
| 42 | use common_ext_ExtensionsManager; |
| 43 | use qtism\common\datatypes\files\FileSystemFileManager; |
| 44 | use qtism\runtime\common\State; |
| 45 | use qtism\runtime\tests\SessionManager; |
| 46 | use qtism\runtime\tests\AssessmentItemSession; |
| 47 | use qtism\runtime\tests\AssessmentItemSessionException; |
| 48 | use qtism\data\storage\xml\XmlDocument; |
| 49 | use qtism\data\storage\StorageException; |
| 50 | |
| 51 | /** |
| 52 | * Qti Item Runner Controller |
| 53 | * |
| 54 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
| 55 | * @package taoQTI |
| 56 | |
| 57 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 58 | */ |
| 59 | class QtiPreview extends taoItems_actions_ItemPreview |
| 60 | { |
| 61 | public function getPreviewUrl($item, $options = []) |
| 62 | { |
| 63 | $code = base64_encode($item->getUri()); |
| 64 | return _url('render/' . $code . '/index', 'QtiPreview', 'taoQtiItem', $options); |
| 65 | } |
| 66 | |
| 67 | public function submitResponses() |
| 68 | { |
| 69 | |
| 70 | $itemUri = tao_helpers_Uri::decode($this->getRequestParameter('itemUri')); |
| 71 | |
| 72 | if (!empty($itemUri)) { |
| 73 | $this->processResponses(new core_kernel_classes_Resource($itemUri)); |
| 74 | } else { |
| 75 | throw new common_Exception('missing required itemUri'); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Item's ResponseProcessing. |
| 81 | * |
| 82 | * @param core_kernel_classes_Resource $item The Item you want to apply ResponseProcessing. |
| 83 | * @throws \RuntimeException If an error occurs while processing responses or transmitting results |
| 84 | */ |
| 85 | protected function processResponses(core_kernel_classes_Resource $item) |
| 86 | { |
| 87 | |
| 88 | $jsonPayload = taoQtiCommon_helpers_Utils::readJsonPayload(); |
| 89 | |
| 90 | try { |
| 91 | $qtiXmlFileContent = QtiFile::getQtiFileContent($item); |
| 92 | $qtiXmlDoc = new XmlDocument(); |
| 93 | $qtiXmlDoc->loadFromString($qtiXmlFileContent); |
| 94 | } catch (StorageException $e) { |
| 95 | $msg = "An error occurred while loading QTI-XML file."; |
| 96 | common_Logger::e(($e->getPrevious() !== null) ? $e->getPrevious()->getMessage() : $e->getMessage()); |
| 97 | throw new \RuntimeException($msg, 0, $e); |
| 98 | } |
| 99 | |
| 100 | $itemSession = new AssessmentItemSession($qtiXmlDoc->getDocumentComponent(), new SessionManager()); |
| 101 | $itemSession->beginItemSession(); |
| 102 | |
| 103 | $variables = []; |
| 104 | $filler = new taoQtiCommon_helpers_PciVariableFiller($qtiXmlDoc->getDocumentComponent()); |
| 105 | |
| 106 | // Convert client-side data as QtiSm Runtime Variables. |
| 107 | foreach ($jsonPayload as $id => $response) { |
| 108 | try { |
| 109 | $var = $filler->fill($id, $response); |
| 110 | // Do not take into account QTI Files at preview time. |
| 111 | // Simply delete the created file. |
| 112 | if (taoQtiCommon_helpers_Utils::isQtiFile($var, false) === true) { |
| 113 | $fileManager = new FileSystemFileManager(); |
| 114 | $fileManager->delete($var->getValue()); |
| 115 | } else { |
| 116 | $variables[] = $var; |
| 117 | } |
| 118 | } catch (OutOfRangeException $e) { |
| 119 | // A variable value could not be converted, ignore it. |
| 120 | // Developer's note: QTI Pairs with a single identifier (missing second identifier of the pair) |
| 121 | // are transmitted as an array of length 1, this might cause problem. Such "broken" pairs are simply |
| 122 | // ignored. |
| 123 | common_Logger::d("Client-side value for variable '${id}' is ignored due to data malformation."); |
| 124 | } catch (OutOfBoundsException $e) { |
| 125 | // No such identifier found in item. |
| 126 | common_Logger::d("The variable with identifier '${id}' is not declared in the item definition."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | $itemSession->beginAttempt(); |
| 132 | $itemSession->endAttempt(new State($variables)); |
| 133 | |
| 134 | // Return the item session state to the client-side. |
| 135 | $this->returnJson( |
| 136 | [ |
| 137 | 'success' => true, |
| 138 | 'displayFeedback' => true, |
| 139 | 'itemSession' => self::buildOutcomeResponse($itemSession) |
| 140 | ] |
| 141 | ); |
| 142 | } catch (AssessmentItemSessionException $e) { |
| 143 | $msg = "An error occurred while processing the responses."; |
| 144 | throw new \RuntimeException($msg, 0, $e); |
| 145 | } catch (taoQtiCommon_helpers_ResultTransmissionException $e) { |
| 146 | $msg = "An error occurred while transmitting a result to the target Result Server."; |
| 147 | throw new \RuntimeException($msg, 0, $e); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the ResultServer API call to be used by the item. |
| 153 | * |
| 154 | * @return string A string representing JavaScript instructions. |
| 155 | */ |
| 156 | protected function getResultServer() |
| 157 | { |
| 158 | $itemUri = tao_helpers_Uri::decode($this->getRequestParameter('uri')); |
| 159 | return [ |
| 160 | 'module' => 'taoQtiItem/QtiPreviewResultServerApi', |
| 161 | 'endpoint' => ROOT_URL . 'taoQtiItem/QtiPreview/', |
| 162 | 'params' => $itemUri |
| 163 | ]; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | protected static function buildOutcomeResponse(AssessmentItemSession $itemSession) |
| 168 | { |
| 169 | $stateOutput = new taoQtiCommon_helpers_PciStateOutput(); |
| 170 | |
| 171 | foreach ($itemSession->getOutcomeVariables(false) as $var) { |
| 172 | $stateOutput->addVariable($var); |
| 173 | } |
| 174 | |
| 175 | $output = $stateOutput->getOutput(); |
| 176 | return $output; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * (non-PHPdoc) |
| 181 | * @see taoItems_actions_ItemPreview::getRenderedItem() |
| 182 | */ |
| 183 | protected function getRenderedItem($item) |
| 184 | { |
| 185 | |
| 186 | //@todo make getRenderedItem language dependent |
| 187 | $lang = \common_session_SessionManager::getSession()->getDataLanguage(); |
| 188 | $qtiItem = Service::singleton()->getDataItemByRdfItem($item, $lang, true); |
| 189 | if ($qtiItem) { |
| 190 | $contentVariableElements = array_merge( |
| 191 | $this->getModalFeedbacks($qtiItem), |
| 192 | $this->getRubricBlocks($qtiItem) |
| 193 | ); |
| 194 | |
| 195 | /** @var AssetService $assetService */ |
| 196 | $assetService = ServiceManager::getServiceManager()->get(AssetService::SERVICE_ID); |
| 197 | |
| 198 | $taoBaseUrl = $assetService->getJsBaseWww('tao'); |
| 199 | $qtiBaseUrl = $assetService->getJsBaseWww('taoQtiItem'); |
| 200 | |
| 201 | $taoLibUrl = $taoBaseUrl . 'js/'; |
| 202 | $taoQtiItemLibUrl = $qtiBaseUrl . 'js/'; |
| 203 | |
| 204 | $xhtml = $qtiItem->toXHTML([ |
| 205 | 'contentVariableElements' => $contentVariableElements, |
| 206 | // 'js' => array($qtiBaseUrl.'js/preview/qtiViewSelector.js'), |
| 207 | 'js_var' => ['view' => $this->getRequestView()], |
| 208 | // 'css' => array($qtiBaseUrl.'css/preview/qtiViewSelector.css'), |
| 209 | 'path' => [ |
| 210 | 'tao' => $taoLibUrl, |
| 211 | 'taoQtiItem' => $taoQtiItemLibUrl |
| 212 | ], |
| 213 | 'client_config_url' => $this->getClientConfigUrl() |
| 214 | |
| 215 | ]); |
| 216 | } else { |
| 217 | \common_Logger::i('Try to preview an empty item', [$item->getUri()]); |
| 218 | $xhtml = ''; |
| 219 | } |
| 220 | |
| 221 | return $xhtml; |
| 222 | } |
| 223 | |
| 224 | protected function getRequestView() |
| 225 | { |
| 226 | $returnValue = 'candidate'; |
| 227 | |
| 228 | if ($this->hasRequestParameter('view')) { |
| 229 | $returnValue = tao_helpers_Uri::decode($this->getRequestParameter('view')); |
| 230 | } |
| 231 | |
| 232 | return $returnValue; |
| 233 | } |
| 234 | |
| 235 | protected function getRubricBlocks(Item $qtiItem) |
| 236 | { |
| 237 | |
| 238 | $returnValue = []; |
| 239 | |
| 240 | $currentView = $this->getRequestView(); |
| 241 | $rubricBlocks = $qtiItem->getRubricBlocks(); |
| 242 | |
| 243 | foreach ($rubricBlocks as $rubricBlock) { |
| 244 | $view = $rubricBlock->attr('view'); |
| 245 | |
| 246 | if (!empty($view) && in_array($currentView, $view)) { |
| 247 | $returnValue[$rubricBlock->getSerial()] = $rubricBlock->toArray(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return $returnValue; |
| 252 | } |
| 253 | |
| 254 | protected function getModalFeedbacks(Item $qtiItem) |
| 255 | { |
| 256 | |
| 257 | $returnValue = []; |
| 258 | |
| 259 | $feedbacks = $qtiItem->getModalFeedbacks(); |
| 260 | foreach ($feedbacks as $feedback) { |
| 261 | $returnValue[$feedback->getSerial()] = $feedback->toArray(); |
| 262 | } |
| 263 | |
| 264 | return $returnValue; |
| 265 | } |
| 266 | |
| 267 | public function getTemplateElements(Item $qtiItem) |
| 268 | { |
| 269 | |
| 270 | throw new common_Exception('qti template elments, to be implemented'); |
| 271 | //1. process templateRules |
| 272 | //2. return the template values |
| 273 | } |
| 274 | } |