Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AbstractPortableElementManager | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCreatorRegistry | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getImplementationDirectory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| renderFile | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| 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\controller; |
| 23 | |
| 24 | use core_kernel_classes_Resource; |
| 25 | use oat\tao\model\routing\AnnotationReader\security; |
| 26 | use oat\taoQtiItem\model\CreatorRegistry; |
| 27 | use tao_actions_CommonModule; |
| 28 | use common_exception_Error; |
| 29 | use tao_helpers_File; |
| 30 | use tao_helpers_Http; |
| 31 | |
| 32 | abstract class AbstractPortableElementManager extends tao_actions_CommonModule |
| 33 | { |
| 34 | /** |
| 35 | * @var CreatorRegistry |
| 36 | */ |
| 37 | protected $registry; |
| 38 | |
| 39 | /** |
| 40 | * Instanciate the controller |
| 41 | * @security("hide") |
| 42 | */ |
| 43 | public function __construct() |
| 44 | { |
| 45 | $this->registry = $this->getCreatorRegistry(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Return the registry used by this controller |
| 50 | * |
| 51 | * @return CreatorRegistry |
| 52 | */ |
| 53 | abstract protected function getCreatorRegistry(); |
| 54 | |
| 55 | /** |
| 56 | * Get a file of a custom interaction |
| 57 | */ |
| 58 | public function getFile() |
| 59 | { |
| 60 | if ($this->hasRequestParameter('file')) { |
| 61 | $file = urldecode($this->getRequestParameter('file')); |
| 62 | $filePathTokens = explode('/', $file); |
| 63 | $typeIdentifier = array_shift($filePathTokens); |
| 64 | $relPath = implode(DIRECTORY_SEPARATOR, $filePathTokens); |
| 65 | $this->renderFile($typeIdentifier, $relPath); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the directory where the implementation sits |
| 71 | * |
| 72 | * @param string $typeIdentifier |
| 73 | * @return string |
| 74 | */ |
| 75 | protected function getImplementationDirectory($typeIdentifier) |
| 76 | { |
| 77 | return $this->registry->getDevImplementationDirectory($typeIdentifier); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Render the file to the browser |
| 82 | * |
| 83 | * @param string $typeIdentifier |
| 84 | * @param string $relPath |
| 85 | * @throws common_exception_Error |
| 86 | */ |
| 87 | private function renderFile($typeIdentifier, $relPath) |
| 88 | { |
| 89 | if (tao_helpers_File::securityCheck($relPath, true)) { |
| 90 | $folder = $this->getImplementationDirectory($typeIdentifier); |
| 91 | $filename = $folder . $relPath; |
| 92 | |
| 93 | //@todo : find better way to to this |
| 94 | //load amd module |
| 95 | if (! file_exists($filename) && file_exists($filename . '.js')) { |
| 96 | $filename = $filename . '.js'; |
| 97 | } |
| 98 | tao_helpers_Http::returnFile($filename); |
| 99 | } else { |
| 100 | throw new common_exception_Error('invalid item preview file path'); |
| 101 | } |
| 102 | } |
| 103 | } |