Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 107 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| QtiRunnerConfig | |
0.00% |
0 / 107 |
|
0.00% |
0 / 8 |
756 | |
0.00% |
0 / 1 |
| buildConfig | |
0.00% |
0 / 67 |
|
0.00% |
0 / 1 |
182 | |||
| getConfig | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getConfigValue | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| buildOptions | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
| getTestOptions | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getCategories | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSectionPauseService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOverriddenOptionsRepository | |
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) 2016 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com> |
| 23 | */ |
| 24 | |
| 25 | namespace oat\taoQtiTest\models\runner\config; |
| 26 | |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\taoQtiTest\models\runner\config\Business\Contract\OverriddenOptionsRepositoryInterface; |
| 29 | use oat\taoQtiTest\models\runner\RunnerServiceContext; |
| 30 | use oat\taoQtiTest\models\SectionPauseService; |
| 31 | |
| 32 | /** |
| 33 | * Class QtiRunnerOptions |
| 34 | * @package oat\taoQtiTest\models\runner\options |
| 35 | */ |
| 36 | class QtiRunnerConfig extends ConfigurableService implements RunnerConfig |
| 37 | { |
| 38 | public const SERVICE_ID = 'taoQtiTest/QtiRunnerConfig'; |
| 39 | |
| 40 | public const OPTION_CONFIG = 'config'; |
| 41 | |
| 42 | public const CATEGORY_OPTION_PREFIX = 'x-tao-option-'; |
| 43 | |
| 44 | /** |
| 45 | * @deprecated since version 29.5.0, to be removed in 30.0.0. Use QtiRunnerService::TOOL_ITEM_THEME_SWITCHER instead |
| 46 | */ |
| 47 | public const TOOL_ITEM_THEME_SWITCHER = 'itemThemeSwitcher'; |
| 48 | |
| 49 | /** |
| 50 | * @deprecated since version 29.5.0, to be removed in 30.0.0. |
| 51 | * Use QtiRunnerService::TOOL_ITEM_THEME_SWITCHER_KEY instead |
| 52 | */ |
| 53 | public const TOOL_ITEM_THEME_SWITCHER_KEY = 'taoQtiTest/runner/plugins/tools/itemThemeSwitcher/itemThemeSwitcher'; |
| 54 | |
| 55 | public const TARGET_CLIENT = 'client'; |
| 56 | |
| 57 | /** |
| 58 | * The test runner config |
| 59 | * @var array |
| 60 | */ |
| 61 | protected $config; |
| 62 | |
| 63 | /** |
| 64 | * The test runner currently activated options |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $options; |
| 68 | |
| 69 | /** |
| 70 | * Returns the config of the test runner |
| 71 | * @return array|mixed |
| 72 | * @throws \common_ext_ExtensionException |
| 73 | */ |
| 74 | protected function buildConfig() |
| 75 | { |
| 76 | if ($this->hasOption(self::OPTION_CONFIG)) { |
| 77 | // load the configuration from service |
| 78 | $config = $this->getOption(self::OPTION_CONFIG); |
| 79 | } else { |
| 80 | // fallback to get the raw server config, using the old notation |
| 81 | $rawConfig = \common_ext_ExtensionsManager::singleton() |
| 82 | ->getExtensionById('taoQtiTest') |
| 83 | ->getConfig('testRunner'); |
| 84 | // build the test config using the new notation |
| 85 | $target = isset($rawConfig['timer'], $rawConfig['timer']['target']) ? $rawConfig['timer']['target'] : null; |
| 86 | $config = [ |
| 87 | 'timerWarning' => isset($rawConfig['timerWarning']) ? $rawConfig['timerWarning'] : null, |
| 88 | 'timerWarningForScreenreader' => $rawConfig['timerWarningForScreenreader'] ?? null, |
| 89 | 'catEngineWarning' => isset($rawConfig['catEngineWarning']) ? $rawConfig['catEngineWarning'] : null, |
| 90 | 'progressIndicator' => [ |
| 91 | 'type' => isset($rawConfig['progress-indicator']) ? $rawConfig['progress-indicator'] : null, |
| 92 | 'renderer' => $rawConfig['progress-indicator-renderer'] ?? null, |
| 93 | 'scope' => $rawConfig['progress-indicator-scope'] ?? null, |
| 94 | 'forced' => $rawConfig['progress-indicator-forced'] ?? false, |
| 95 | 'showLabel' => !empty($rawConfig['progress-indicator-show-label']), |
| 96 | 'showTotal' => !empty($rawConfig['progress-indicator-show-total']), |
| 97 | 'categories' => isset($rawConfig['progress-categories']) ? $rawConfig['progress-categories'] : [], |
| 98 | ], |
| 99 | 'review' => [ |
| 100 | 'enabled' => !empty($rawConfig['test-taker-review']), |
| 101 | 'scope' => $rawConfig['test-taker-review-scope'] ?? null, |
| 102 | 'useTitle' => !empty($rawConfig['test-taker-review-use-title']), |
| 103 | 'forceTitle' => !empty($rawConfig['test-taker-review-force-title']), |
| 104 | 'forceInformationalTitle' => !empty($rawConfig['test-taker-review-force-informational-title']), |
| 105 | 'showLegend' => !empty($rawConfig['test-taker-review-show-legend']), |
| 106 | 'defaultOpen' => !empty($rawConfig['test-taker-review-default-open']), |
| 107 | 'itemTitle' => $rawConfig['test-taker-review-item-title'] ?? null, |
| 108 | 'informationalItemTitle' => $rawConfig['test-taker-review-informational-item-title'] ?? null, |
| 109 | 'preventsUnseen' => !empty($rawConfig['test-taker-review-prevents-unseen']), |
| 110 | 'canCollapse' => !empty($rawConfig['test-taker-review-can-collapse']), |
| 111 | 'displaySubsectionTitle' => !empty($rawConfig['test-taker-review-display-subsection-title']), |
| 112 | 'allowSkipahead' => $rawConfig['test-taker-review-skipahead'] ?? false, |
| 113 | // phpcs:disable Generic.Files.LineLength |
| 114 | 'partiallyAnsweredIsAnswered' => $rawConfig['test-taker-review-partially-answered-is-answered'] ?? true, |
| 115 | // phpcs:enable Generic.Files.LineLength |
| 116 | ], |
| 117 | 'exitButton' => !empty($rawConfig['exitButton']), |
| 118 | 'nextSection' => !empty($rawConfig['next-section']), |
| 119 | 'plugins' => isset($rawConfig['plugins']) ? $rawConfig['plugins'] : null, |
| 120 | 'security' => [ |
| 121 | 'csrfToken' => isset($rawConfig['csrf-token']) ? $rawConfig['csrf-token'] : false, |
| 122 | ], |
| 123 | 'timer' => [ |
| 124 | 'target' => $target, |
| 125 | 'resetAfterResume' => !empty($rawConfig['reset-timer-after-resume']), |
| 126 | 'keepUpToTimeout' => !empty($rawConfig['keep-timer-up-to-timeout']), |
| 127 | 'restoreTimerFromClient' => $target === self::TARGET_CLIENT, |
| 128 | ], |
| 129 | 'enableAllowSkipping' => $rawConfig['enable-allow-skipping'] ?? false, |
| 130 | 'enableValidateResponses' => $rawConfig['enable-validate-responses'] ?? false, |
| 131 | 'checkInformational' => $rawConfig['check-informational'] ?? false, |
| 132 | 'enableUnansweredItemsWarning' => $rawConfig['test-taker-unanswered-items-message'] ?? true, |
| 133 | 'allowShortcuts' => !empty($rawConfig['allow-shortcuts']), |
| 134 | 'shortcuts' => isset($rawConfig['shortcuts']) ? $rawConfig['shortcuts'] : [], |
| 135 | 'itemCaching' => [ |
| 136 | 'enabled' => $rawConfig['allow-browse-next-item'] ?? false, |
| 137 | 'amount' => isset($rawConfig['item-cache-size']) ? intval($rawConfig['item-cache-size']) : 3, |
| 138 | 'itemStoreTTL' => isset($rawConfig['item-store-ttl']) |
| 139 | ? intval($rawConfig['item-store-ttl']) |
| 140 | : 15 * 60, |
| 141 | ], |
| 142 | 'guidedNavigation' => isset($rawConfig['guidedNavigation']) ? $rawConfig['guidedNavigation'] : false, |
| 143 | 'toolStateServerStorage' => $rawConfig['tool-state-server-storage'] ?? [], |
| 144 | 'forceEnableLinearNextItemWarning' => $rawConfig['force-enable-linear-next-item-warning'] ?? false, |
| 145 | 'enableLinearNextItemWarningCheckbox' => $rawConfig['enable-linear-next-item-warning-checkbox'] ?? true, |
| 146 | 'skipPausedAssessmentDialog' => $rawConfig['skip-paused-assessment-dialog'] ?? false, |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | return $config; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the config of the test runner |
| 155 | * @return mixed |
| 156 | */ |
| 157 | public function getConfig() |
| 158 | { |
| 159 | if (is_null($this->config)) { |
| 160 | // build the test config using the new notation |
| 161 | $this->config = $this->buildConfig(); |
| 162 | } |
| 163 | return $this->config; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the value of a config entry. |
| 168 | * The name can be a namespace, each name being separated by a dot, like: 'itemCaching.enabled' |
| 169 | * @param string $name |
| 170 | * @return mixed |
| 171 | */ |
| 172 | public function getConfigValue($name) |
| 173 | { |
| 174 | $config = $this->getConfig(); |
| 175 | |
| 176 | $path = explode('.', (string)$name); |
| 177 | foreach ($path as $entry) { |
| 178 | if (isset($config[$entry])) { |
| 179 | $config =& $config[$entry]; |
| 180 | } else { |
| 181 | return null; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return $config; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the options related to the current test context |
| 190 | * @param RunnerServiceContext $context The test context |
| 191 | * @return mixed |
| 192 | */ |
| 193 | protected function buildOptions(RunnerServiceContext $context) |
| 194 | { |
| 195 | $session = $context->getTestSession(); |
| 196 | |
| 197 | // Comment allowed? Skipping allowed? Logout or Exit allowed ? |
| 198 | $options = [ |
| 199 | 'allowComment' => \taoQtiTest_helpers_TestRunnerUtils::doesAllowComment($session), |
| 200 | 'allowSkipping' => \taoQtiTest_helpers_TestRunnerUtils::doesAllowSkipping($session), |
| 201 | 'exitButton' => \taoQtiTest_helpers_TestRunnerUtils::doesAllowExit($session, $context), |
| 202 | 'logoutButton' => \taoQtiTest_helpers_TestRunnerUtils::doesAllowLogout($session), |
| 203 | 'validateResponses' => \taoQtiTest_helpers_TestRunnerUtils::doesValidateResponses($session), |
| 204 | 'sectionPause' => $this->getSectionPauseService()->couldBePaused($session) |
| 205 | ]; |
| 206 | |
| 207 | // get the options from the categories owned by the current item |
| 208 | $categories = $this->getCategories($context); |
| 209 | $prefixCategoryLen = strlen(self::CATEGORY_OPTION_PREFIX); |
| 210 | foreach ($categories as $category) { |
| 211 | if (!strncmp($category, self::CATEGORY_OPTION_PREFIX, $prefixCategoryLen)) { |
| 212 | // extract the option name from the category, transform to camelCase if needed |
| 213 | $optionName = lcfirst( |
| 214 | str_replace( |
| 215 | ' ', |
| 216 | '', |
| 217 | ucwords(strtr(substr($category, $prefixCategoryLen), ['-' => ' ', '_' => ' '])) |
| 218 | ) |
| 219 | ); |
| 220 | |
| 221 | // the options added by the categories are just flags |
| 222 | $options[$optionName] = true; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | foreach ($this->getOverriddenOptionsRepository()->findAll() as $option) { |
| 227 | $options[$option->getId()] = $option->isEnabled(); |
| 228 | } |
| 229 | |
| 230 | return $options; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns the options related to the current test context |
| 235 | * @param RunnerServiceContext $context The test context |
| 236 | * @return mixed |
| 237 | */ |
| 238 | public function getTestOptions(RunnerServiceContext $context) |
| 239 | { |
| 240 | if (is_null($this->options)) { |
| 241 | // build the test config using the new notation |
| 242 | $this->options = $this->buildOptions($context); |
| 243 | } |
| 244 | return $this->options; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get Categories. |
| 249 | * |
| 250 | * Get the categories of the current AssessmentItemRef in the route depending on a given $context. |
| 251 | * |
| 252 | * @param RunnerServiceContext $context |
| 253 | * @return array An array of strings. |
| 254 | */ |
| 255 | protected function getCategories(RunnerServiceContext $context) |
| 256 | { |
| 257 | return $context->getCurrentAssessmentItemRef()->getCategories()->getArrayCopy(); |
| 258 | } |
| 259 | |
| 260 | private function getSectionPauseService(): SectionPauseService |
| 261 | { |
| 262 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 263 | return $this->getServiceLocator()->get(SectionPauseService::SERVICE_ID); |
| 264 | } |
| 265 | |
| 266 | private function getOverriddenOptionsRepository(): OverriddenOptionsRepositoryInterface |
| 267 | { |
| 268 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 269 | return $this->getServiceLocator()->get(OverriddenOptionsRepositoryInterface::SERVICE_ID); |
| 270 | } |
| 271 | } |