Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| taoResultServer_actions_QtiRestResults | |
0.00% |
0 / 57 |
|
0.00% |
0 / 7 |
306 | |
0.00% |
0 / 1 |
| getQtiResultXml | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| getQtiResultService | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLatest | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| byDeliveryExecution | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| checkMethod | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| validateParams | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| returnValidXml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | //http://tao.dev/taoResultServer/QtiRestResults?testtaker=http%3A%2F%2Ftao.local%2Fmytao.rdf%23i1460560178726251&delivery=http%3A%2F%2Ftao.local%2Fmytao.rdf%23i14607116346750186 |
| 23 | |
| 24 | use oat\taoResultServer\models\classes\ResultService; |
| 25 | |
| 26 | class taoResultServer_actions_QtiRestResults extends tao_actions_RestController |
| 27 | { |
| 28 | public const TESTTAKER = 'testtaker'; |
| 29 | public const DELIVERY = 'delivery'; |
| 30 | public const LAST_RESULT = 'last_result'; |
| 31 | public const DELIVERY_EXECUTION = 'deliveryExecution'; |
| 32 | public const RESULT = 'result'; |
| 33 | |
| 34 | protected $service; |
| 35 | |
| 36 | public function getQtiResultXml() |
| 37 | { |
| 38 | try { |
| 39 | $this->checkMethod(); |
| 40 | |
| 41 | $this->validateParams([self::DELIVERY, self::RESULT]); |
| 42 | |
| 43 | $deliveryId = $this->getRequestParameter(self::DELIVERY); |
| 44 | $resultId = $this->getRequestParameter(self::RESULT); |
| 45 | $fetchOnlyLastAttemptResult = filter_var( |
| 46 | $this->getRequestParameter(self::LAST_RESULT), |
| 47 | FILTER_VALIDATE_BOOLEAN |
| 48 | ); |
| 49 | |
| 50 | $this->returnValidXml( |
| 51 | $this->getQtiResultService()->getQtiResultXml( |
| 52 | $deliveryId, |
| 53 | $resultId, |
| 54 | $fetchOnlyLastAttemptResult |
| 55 | ) |
| 56 | ); |
| 57 | } catch (Exception $e) { |
| 58 | $this->returnFailure($e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Return the service for Result |
| 64 | * |
| 65 | * @return ResultService |
| 66 | */ |
| 67 | protected function getQtiResultService() |
| 68 | { |
| 69 | if (!$this->service) { |
| 70 | $this->service = $this->getServiceManager()->get(ResultService::SERVICE_ID); |
| 71 | } |
| 72 | return $this->service; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * It's based on TestTaker and Delivery. Parameters are in URI format. |
| 77 | * It fetches the latest Delivery Execution. |
| 78 | * |
| 79 | * There are two endpoints because of swagger. In this way, it's possible to describe the parameters properly. |
| 80 | * |
| 81 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 82 | */ |
| 83 | public function getLatest() |
| 84 | { |
| 85 | try { |
| 86 | $this->checkMethod(); |
| 87 | |
| 88 | $this->validateParams([self::TESTTAKER, self::DELIVERY]); |
| 89 | |
| 90 | $deliveryExecution = $this->getQtiResultService()->getDeliveryExecutionByTestTakerAndDelivery( |
| 91 | $this->getRequestParameter(self::DELIVERY), |
| 92 | $this->getRequestParameter(self::TESTTAKER) |
| 93 | ); |
| 94 | |
| 95 | $this->returnValidXml($this->getQtiResultService()->getDeliveryExecutionXml($deliveryExecution)); |
| 96 | } catch (Exception $e) { |
| 97 | $this->returnFailure($e); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * It requires only Delivery Execution in URI format. |
| 103 | * |
| 104 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 105 | */ |
| 106 | public function byDeliveryExecution() |
| 107 | { |
| 108 | try { |
| 109 | $this->checkMethod(); |
| 110 | |
| 111 | $this->validateParams([self::DELIVERY_EXECUTION]); |
| 112 | |
| 113 | $deliveryExecution = $this->getQtiResultService()->getDeliveryExecutionById( |
| 114 | $this->getRequestParameter(self::DELIVERY_EXECUTION) |
| 115 | ); |
| 116 | |
| 117 | $this->returnValidXml($this->getQtiResultService()->getDeliveryExecutionXml($deliveryExecution)); |
| 118 | } catch (Exception $e) { |
| 119 | $this->returnFailure($e); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Checks the required request method. |
| 125 | * |
| 126 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 127 | * @throws common_exception_MethodNotAllowed |
| 128 | */ |
| 129 | protected function checkMethod() |
| 130 | { |
| 131 | if ($this->getRequestMethod() != 'GET') { |
| 132 | throw new common_exception_MethodNotAllowed($this->getRequestURI()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Validates the given parameters. |
| 138 | * |
| 139 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 140 | * @param array $params |
| 141 | * @throws common_exception_MissingParameter |
| 142 | * @throws common_exception_ValidationFailed |
| 143 | */ |
| 144 | protected function validateParams(array $params) |
| 145 | { |
| 146 | foreach ($params as $param) { |
| 147 | if (!$this->hasRequestParameter($param)) { |
| 148 | throw new common_exception_MissingParameter( |
| 149 | $param . ' is missing from the request.', |
| 150 | $this->getRequestURI() |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | if (empty($this->getRequestParameter($param))) { |
| 155 | throw new common_exception_ValidationFailed($param, $param . ' cannot be empty'); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Return XML response if it is valid. |
| 162 | * |
| 163 | * @param string $data |
| 164 | * @throws Exception |
| 165 | * @throws common_exception_NotFound |
| 166 | */ |
| 167 | protected function returnValidXml($data) |
| 168 | { |
| 169 | if (empty($data)) { |
| 170 | throw new common_exception_NotFound('Delivery execution not found.'); |
| 171 | } |
| 172 | |
| 173 | // delete control characters |
| 174 | $data = preg_replace('/[\x00-\x1F\x7F]/', '', $data); |
| 175 | $doc = @simplexml_load_string($data); |
| 176 | if (!$doc) { |
| 177 | common_Logger::i('invalid xml result'); |
| 178 | throw new Exception('Xml output is malformed.'); |
| 179 | } |
| 180 | |
| 181 | // force XML content type header |
| 182 | header('Content-Type: application/xml'); |
| 183 | |
| 184 | echo $data; |
| 185 | exit(0); |
| 186 | } |
| 187 | } |