Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
7.69% |
2 / 26 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RunnerToolStates | |
7.69% |
2 / 26 |
|
0.00% |
0 / 3 |
58.34 | |
0.00% |
0 / 1 |
| hasRequestParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRequestParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRawRequestParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getServiceLocator | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRunnerService | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getServiceContext | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getToolStatesFromRequest | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
5.67 | |||
| saveToolStates | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| getToolStates | |
0.00% |
0 / 5 |
|
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) 2017 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiTest\models\runner; |
| 22 | |
| 23 | use oat\oatbox\service\ServiceManager; |
| 24 | |
| 25 | trait RunnerToolStates |
| 26 | { |
| 27 | /** |
| 28 | * @param string $name |
| 29 | * @return string |
| 30 | */ |
| 31 | abstract public function hasRequestParameter($name); |
| 32 | |
| 33 | /** |
| 34 | * @param string $name |
| 35 | * @return string mixed |
| 36 | */ |
| 37 | abstract public function getRequestParameter($name); |
| 38 | |
| 39 | /** |
| 40 | * @param string $name |
| 41 | * @return string |
| 42 | */ |
| 43 | abstract public function getRawRequestParameter($name); |
| 44 | |
| 45 | /** |
| 46 | * @return ServiceManager |
| 47 | */ |
| 48 | abstract public function getServiceLocator(); |
| 49 | |
| 50 | /** |
| 51 | * @return RunnerService |
| 52 | */ |
| 53 | abstract public function getRunnerService(); |
| 54 | |
| 55 | /** |
| 56 | * @return RunnerServiceContext |
| 57 | */ |
| 58 | abstract public function getServiceContext(); |
| 59 | |
| 60 | protected function getToolStatesFromRequest(): ?array |
| 61 | { |
| 62 | if (!$this->hasRequestParameter('toolStates')) { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | // since the parameter content is a JSON string |
| 67 | // we need to load it using the raw mode |
| 68 | $rawToolStates = $this->getRawRequestParameter('toolStates'); |
| 69 | |
| 70 | if (empty($rawToolStates)) { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | return (array)json_decode($rawToolStates, true); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Save the tool state if some are found in the current request |
| 79 | * |
| 80 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 81 | * @throws \common_Exception |
| 82 | * |
| 83 | * @return boolean true if some state was found and saved |
| 84 | */ |
| 85 | protected function saveToolStates() |
| 86 | { |
| 87 | $toolStateParameter = 'toolStates'; |
| 88 | if ($this->hasRequestParameter($toolStateParameter)) { |
| 89 | // since the parameter content is a JSON string |
| 90 | // we need to load it using the raw mode |
| 91 | $param = $this->getRawRequestParameter($toolStateParameter); |
| 92 | if ($param) { |
| 93 | $toolStates = json_decode($param, true); |
| 94 | |
| 95 | if (count($toolStates) > 0) { |
| 96 | array_walk($toolStates, function (&$toolState) { |
| 97 | $toolState = json_encode($toolState); |
| 98 | }); |
| 99 | $this->getRunnerService()->setToolsStates( |
| 100 | $this->getServiceContext(), |
| 101 | $toolStates |
| 102 | ); |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the current tools states |
| 112 | * |
| 113 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 114 | * @throws \common_Exception |
| 115 | * @throws \common_ext_ExtensionException |
| 116 | * |
| 117 | * @return array the tools states |
| 118 | */ |
| 119 | protected function getToolStates() |
| 120 | { |
| 121 | $toolStates = $this->getRunnerService()->getToolsStates($this->getServiceContext()); |
| 122 | array_walk($toolStates, function (&$toolState) { |
| 123 | $toolState = json_decode($toolState); |
| 124 | }); |
| 125 | return $toolStates; |
| 126 | } |
| 127 | } |