Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| CreatorRegistry | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 1 |
| getBaseDevDir | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getBaseDevUrl | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getHookFileName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getEntryPointFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDevImplementations | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| getDevImplementation | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getDevImplementationDirectory | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getImplementatioByTypeIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addRequiredResources | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model; |
| 23 | |
| 24 | use core_kernel_classes_Resource; |
| 25 | use common_exception_Error; |
| 26 | use oat\taoQtiItem\model\qti\Service; |
| 27 | use oat\taoQtiItem\helpers\Authoring; |
| 28 | use oat\tao\model\ClientLibRegistry; |
| 29 | |
| 30 | /** |
| 31 | * CreatorRegistry stores reference to |
| 32 | * |
| 33 | * @package taoQtiItem |
| 34 | */ |
| 35 | abstract class CreatorRegistry |
| 36 | { |
| 37 | /** |
| 38 | * @return string - e.g. DIR_VIEWS/js/pciCreator/dev/ |
| 39 | */ |
| 40 | abstract protected function getBaseDevDir(); |
| 41 | |
| 42 | /** |
| 43 | * @return string - e.g. base_www/js/pciCreator/dev/ |
| 44 | */ |
| 45 | abstract protected function getBaseDevUrl(); |
| 46 | |
| 47 | /** |
| 48 | * get the hook file name to distinguish various implementation |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | abstract protected function getHookFileName(); |
| 53 | |
| 54 | /** |
| 55 | * Get the entry point file path from the baseUrl |
| 56 | * |
| 57 | * @param string $baseUrl |
| 58 | * @return string |
| 59 | */ |
| 60 | protected function getEntryPointFile($baseUrl) |
| 61 | { |
| 62 | return $baseUrl . '/' . $this->getHookFileName(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get PCI Creator hooks directly located at views/js/pciCreator/myCustomInteraction: |
| 67 | * |
| 68 | * @return array |
| 69 | */ |
| 70 | public function getDevImplementations() |
| 71 | { |
| 72 | |
| 73 | $returnValue = []; |
| 74 | |
| 75 | $hookFileName = $this->getHookFileName(); |
| 76 | |
| 77 | foreach (glob($this->getBaseDevDir() . '*/' . $hookFileName . '.js') as $file) { |
| 78 | $dir = str_replace($hookFileName . '.js', '', $file); |
| 79 | $manifestFile = $dir . $hookFileName . '.json'; |
| 80 | |
| 81 | if (file_exists($manifestFile)) { |
| 82 | $typeIdentifier = basename($dir); |
| 83 | $baseUrl = $this->getBaseDevUrl() . $typeIdentifier . '/'; |
| 84 | $manifest = json_decode(file_get_contents($manifestFile), true); |
| 85 | |
| 86 | $returnValue[] = [ |
| 87 | 'typeIdentifier' => $typeIdentifier, |
| 88 | 'label' => $manifest['label'], |
| 89 | 'directory' => $dir, |
| 90 | 'baseUrl' => $baseUrl, |
| 91 | 'file' => $this->getEntryPointFile($typeIdentifier), |
| 92 | 'manifest' => $manifest, |
| 93 | 'dev' => true, |
| 94 | 'debug' => (isset($manifest['debug']) && $manifest['debug']), |
| 95 | 'registry' => get_class($this) |
| 96 | ]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $returnValue; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get PCI Creator hook located at views/js/{{hookFileName}}/$typeIdentifier |
| 105 | * |
| 106 | * @param string $typeIdentifier |
| 107 | * @return array |
| 108 | */ |
| 109 | public function getDevImplementation($typeIdentifier) |
| 110 | { |
| 111 | |
| 112 | //@todo : re-implement it to be more optimal |
| 113 | $devImplementations = $this->getDevImplementations(); |
| 114 | foreach ($devImplementations as $impl) { |
| 115 | if ($impl['typeIdentifier'] == $typeIdentifier) { |
| 116 | return $impl; |
| 117 | } |
| 118 | } |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the path to the directory of a the Creator located at views/js/{{hookFileName}}/ |
| 124 | * |
| 125 | * @param string $typeIdentifier |
| 126 | * @return string |
| 127 | * @throws \common_Exception |
| 128 | */ |
| 129 | public function getDevImplementationDirectory($typeIdentifier) |
| 130 | { |
| 131 | $dir = $this->getBaseDevDir() . $typeIdentifier; |
| 132 | if (file_exists($dir)) { |
| 133 | return $dir; |
| 134 | } else { |
| 135 | throw new \common_Exception('the type identifier cannot be found'); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the data of the implementation by its typeIdentifier |
| 141 | * |
| 142 | * @param string $typeIdentifier |
| 143 | * @return array |
| 144 | */ |
| 145 | protected function getImplementatioByTypeIdentifier($typeIdentifier) |
| 146 | { |
| 147 | return $this->getDevImplementation($typeIdentifier); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add required resources for a custom interaction (css, js) in the item directory |
| 152 | * |
| 153 | * @param string $typeIdentifier |
| 154 | * @param \core_kernel_classes_Resource $item |
| 155 | * @throws common_exception_Error |
| 156 | */ |
| 157 | public function addRequiredResources($typeIdentifier, core_kernel_classes_Resource $item) |
| 158 | { |
| 159 | |
| 160 | //find the interaction in the registry |
| 161 | $implementationData = $this->getImplementatioByTypeIdentifier($typeIdentifier); |
| 162 | if (is_null($implementationData)) { |
| 163 | throw new common_exception_Error('no implementation found with the type identifier ' . $typeIdentifier); |
| 164 | } |
| 165 | |
| 166 | //get the root directory of the interaction |
| 167 | $directory = $implementationData['directory']; |
| 168 | |
| 169 | //get the lists of all required resources |
| 170 | $manifest = $implementationData['manifest']; |
| 171 | $required = [$manifest['entryPoint']]; |
| 172 | |
| 173 | //include libraries remotely only, so this block is temporarily disabled |
| 174 | foreach ($manifest['libraries'] as $lib) { |
| 175 | if (!ClientLibRegistry::getRegistry()->isRegistered($lib)) { |
| 176 | $lib = preg_replace('/^.\//', '', $lib); |
| 177 | $lib .= '.js'; //add js extension |
| 178 | $required[] = $lib; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | //include custom interaction specific css in the item |
| 183 | if (isset($manifest['css'])) { |
| 184 | $required = array_merge($required, array_values($manifest['css'])); |
| 185 | } |
| 186 | |
| 187 | //include media in the item |
| 188 | if (isset($manifest['media'])) { |
| 189 | $required = array_merge($required, array_values($manifest['media'])); |
| 190 | } |
| 191 | |
| 192 | //add them to the rdf item |
| 193 | $resources = Authoring::addRequiredResources($directory, $required, $typeIdentifier, $item, ''); |
| 194 | |
| 195 | return $resources; |
| 196 | } |
| 197 | } |