Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| QtiItemRunner | |
0.00% |
0 / 63 |
|
0.00% |
0 / 10 |
650 | |
0.00% |
0 / 1 |
| selectView | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getResultServerEndpoint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getItemUri | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getTestUri | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getServiceCallId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getPostedTraces | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| submitResponses | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| processResponses | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
72 | |||
| transmitResults | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| buildOutcomeResponse | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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\helpers\QtiFile; |
| 26 | use oat\taoQtiItem\controller\AbstractQtiItemRunner; |
| 27 | use core_kernel_classes_Resource; |
| 28 | use common_Exception; |
| 29 | use taoQtiCommon_helpers_PciVariableFiller; |
| 30 | use taoQtiCommon_helpers_PciStateOutput; |
| 31 | use taoQtiCommon_helpers_Utils; |
| 32 | use common_Logger; |
| 33 | use taoQtiCommon_helpers_ResultTransmissionException; |
| 34 | use taoQtiCommon_helpers_ResultTransmitter; |
| 35 | use taoResultServer_models_classes_ResultServerStateFull; |
| 36 | use qtism\runtime\common\State; |
| 37 | use qtism\runtime\tests\SessionManager; |
| 38 | use qtism\runtime\tests\AssessmentItemSession; |
| 39 | use qtism\runtime\tests\AssessmentItemSessionException; |
| 40 | use qtism\data\storage\StorageException; |
| 41 | use qtism\data\storage\xml\XmlDocument; |
| 42 | |
| 43 | /** |
| 44 | * Qti Item Runner Controller |
| 45 | * |
| 46 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
| 47 | * @package taoQTI |
| 48 | |
| 49 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 50 | */ |
| 51 | class QtiItemRunner extends AbstractQtiItemRunner |
| 52 | { |
| 53 | /** |
| 54 | * The implementation of this method calls ItemRunner::setView in order to |
| 55 | * select the view to be displayed. |
| 56 | */ |
| 57 | protected function selectView() |
| 58 | { |
| 59 | $this->setInitialVariableElements(); |
| 60 | |
| 61 | $this->setData('itemDataPath', $this->getRequestParameter('itemDataPath')); |
| 62 | $this->setView('runtime/qti_item_runner.tpl', 'taoQtiItem'); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * The endpoint specific to QTI Items |
| 67 | * @return string |
| 68 | */ |
| 69 | protected function getResultServerEndpoint() |
| 70 | { |
| 71 | return _url('', 'QtiItemRunner', 'taoQtiItem'); |
| 72 | } |
| 73 | |
| 74 | protected function getItemUri() |
| 75 | { |
| 76 | return $this->hasRequestParameter("itemId") ? $this->getRequestParameter("itemId") : ''; |
| 77 | } |
| 78 | |
| 79 | protected function getTestUri() |
| 80 | { |
| 81 | return $this->hasRequestParameter("testId") ? $this->getRequestParameter("testId") : ''; |
| 82 | } |
| 83 | |
| 84 | protected function getServiceCallId() |
| 85 | { |
| 86 | return $this->hasRequestParameter("serviceCallId") ? $this->getRequestParameter("serviceCallId") : ''; |
| 87 | } |
| 88 | |
| 89 | protected function getPostedTraces() |
| 90 | { |
| 91 | return $this->hasRequestParameter("traceVariables") ? $this->getRequestParameter("traceVariables") : []; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * The main entry poin for respon evaluation |
| 96 | * |
| 97 | * @throws common_Exception |
| 98 | */ |
| 99 | public function submitResponses() |
| 100 | { |
| 101 | |
| 102 | $success = false; |
| 103 | $itemUri = $this->getItemUri(); |
| 104 | |
| 105 | if (!empty($itemUri)) { |
| 106 | $this->processResponses(new core_kernel_classes_Resource($itemUri)); |
| 107 | $success = true; |
| 108 | } else { |
| 109 | throw new common_Exception('missing required itemId'); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Item's ResponseProcessing. |
| 115 | * |
| 116 | * @throws RuntimeException If an error occurs while processing responses or transmitting results |
| 117 | */ |
| 118 | protected function processResponses(core_kernel_classes_Resource $item) |
| 119 | { |
| 120 | |
| 121 | $jsonPayload = taoQtiCommon_helpers_Utils::readJsonPayload(); |
| 122 | |
| 123 | try { |
| 124 | $qtiXmlFileContent = QtiFile::getQtiFileContent($item); |
| 125 | $qtiXmlDoc = new XmlDocument(); |
| 126 | $qtiXmlDoc->loadFromString($qtiXmlFileContent); |
| 127 | } catch (StorageException $e) { |
| 128 | $msg = "An error occurred while loading QTI-XML file."; |
| 129 | throw new \RuntimeException($msg, 0, $e); |
| 130 | } |
| 131 | |
| 132 | $itemSession = new AssessmentItemSession($qtiXmlDoc->getDocumentComponent(), new SessionManager()); |
| 133 | $itemSession->beginItemSession(); |
| 134 | |
| 135 | $variables = []; |
| 136 | |
| 137 | // Convert client-side data as QtiSm Runtime Variables. |
| 138 | foreach ($jsonPayload as $identifier => $response) { |
| 139 | $filler = new taoQtiCommon_helpers_PciVariableFiller($qtiXmlDoc->getDocumentComponent()); |
| 140 | |
| 141 | try { |
| 142 | $var = $filler->fill($identifier, $response); |
| 143 | // Do not take into account QTI File placeholders. |
| 144 | if (taoQtiCommon_helpers_Utils::isQtiFilePlaceHolder($var) === false) { |
| 145 | $variables[] = $var; |
| 146 | } |
| 147 | } catch (\OutOfRangeException $e) { |
| 148 | // A variable value could not be converted, ignore it. |
| 149 | // Developer's note: QTI Pairs with a single identifier (missing second identifier of the pair) |
| 150 | // are transmitted as an array of length 1, this might cause problem. Such "broken" pairs are simply |
| 151 | // ignored. |
| 152 | common_Logger::d("Client-side value for variable '${identifier}' is ignored due to data malformation."); |
| 153 | } catch (\OutOfBoundsException $e) { |
| 154 | // The response identifier does not match any response declaration. |
| 155 | common_Logger::d("Uknown item variable declaration '${identifier}."); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | try { |
| 160 | $itemSession->beginAttempt(); |
| 161 | $itemSession->endAttempt(new State($variables)); |
| 162 | |
| 163 | // Transmit results to the Result Server. |
| 164 | $this->transmitResults($item, $itemSession); |
| 165 | |
| 166 | // Return the item session state to the client-side. |
| 167 | echo json_encode([ |
| 168 | 'success' => true, |
| 169 | 'displayFeedback' => true, |
| 170 | 'itemSession' => self::buildOutcomeResponse($itemSession), |
| 171 | 'feedbacks' => $this->getFeedbacks($itemSession) |
| 172 | ]); |
| 173 | } catch (AssessmentItemSessionException $e) { |
| 174 | $msg = "An error occured while processing the responses."; |
| 175 | throw new \RuntimeException($msg, 0, $e); |
| 176 | } catch (taoQtiCommon_helpers_ResultTransmissionException $e) { |
| 177 | $msg = "An error occured while transmitting variable '${identifier}' to the target Result Server."; |
| 178 | throw new \RuntimeException($msg, 0, $e); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Transmit the variables contained in the AssessmentTestSession $itemSession as |
| 184 | * item results to the Result Server. |
| 185 | * |
| 186 | * @param core_kernel_classes_Resource $item The item definition in database. |
| 187 | * @param AssessmentItemSession $itemSession The AssessmentItemSession objects from where the results must be |
| 188 | * extracted. |
| 189 | * @throws taoQtiCommon_helpers_ResultTransmissionException If an error occurs while transmitting results |
| 190 | * to the ResultServer. |
| 191 | */ |
| 192 | protected function transmitResults(core_kernel_classes_Resource $item, AssessmentItemSession $itemSession) |
| 193 | { |
| 194 | $resultTransmitter = new taoQtiCommon_helpers_ResultTransmitter( |
| 195 | taoResultServer_models_classes_ResultServerStateFull::singleton() |
| 196 | ); |
| 197 | |
| 198 | foreach ($itemSession->getKeys() as $identifier) { |
| 199 | // QTI built-in variables not suitable for this standalone QTI item execution case. |
| 200 | if (!in_array($identifier, ['completionStatus', 'numAttempts', 'duration'])) { |
| 201 | // Transmit to Result Server. |
| 202 | $resultTransmitter->transmitItemVariable( |
| 203 | $itemSession->getVariable($identifier), |
| 204 | $this->getServiceCallId(), |
| 205 | $item->getUri() |
| 206 | ); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | protected static function buildOutcomeResponse(AssessmentItemSession $itemSession) |
| 212 | { |
| 213 | |
| 214 | $stateOutput = new taoQtiCommon_helpers_PciStateOutput(); |
| 215 | |
| 216 | foreach ($itemSession->getAllVariables() as $var) { |
| 217 | $stateOutput->addVariable($var); |
| 218 | } |
| 219 | |
| 220 | $output = $stateOutput->getOutput(); |
| 221 | return $output; |
| 222 | } |
| 223 | } |