Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| taoItems_actions_ItemPreview | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
272 | |
0.00% |
0 / 1 |
| forwardMe | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| index | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getPreviewUrl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getRenderedItem | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| renderItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| renderResource | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getResultServer | |
0.00% |
0 / 3 |
|
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013-2018(update and modification) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | use oat\tao\helpers\Base64; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\taoItems\model\media\ItemMediaResolver; |
| 30 | use oat\tao\model\media\sourceStrategy\HttpSource; |
| 31 | use oat\taoItems\model\preview\OntologyItemNotFoundException; |
| 32 | |
| 33 | use function GuzzleHttp\Psr7\stream_for; |
| 34 | |
| 35 | /** |
| 36 | * Preview API |
| 37 | * |
| 38 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
| 39 | * @package taoItems |
| 40 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 41 | */ |
| 42 | class taoItems_actions_ItemPreview extends tao_actions_CommonModule |
| 43 | { |
| 44 | use OntologyAwareTrait; |
| 45 | |
| 46 | public function forwardMe() |
| 47 | { |
| 48 | $item = $this->getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); |
| 49 | $lang = DEFAULT_LANG; |
| 50 | $previewUrl = taoItems_models_classes_ItemsService::singleton()->getPreviewUrl($item, $lang); |
| 51 | |
| 52 | if (null === $previewUrl) { |
| 53 | throw new OntologyItemNotFoundException(); |
| 54 | } |
| 55 | |
| 56 | $this->forwardUrl($previewUrl); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @requiresRight uri READ |
| 61 | */ |
| 62 | public function index() |
| 63 | { |
| 64 | $item = $this->getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); |
| 65 | |
| 66 | $itemService = taoItems_models_classes_ItemsService::singleton(); |
| 67 | if ($itemService->hasItemContent($item) && $itemService->isItemModelDefined($item)) { |
| 68 | //this is this url that will contains the preview |
| 69 | //@see taoItems_actions_LegacyPreviewApi |
| 70 | $previewUrl = $this->getPreviewUrl($item); |
| 71 | |
| 72 | $this->setData('previewUrl', $previewUrl); |
| 73 | $this->setData('client_config_url', $this->getClientConfigUrl()); |
| 74 | $this->setData('resultServer', $this->getResultServer()); |
| 75 | } |
| 76 | |
| 77 | $this->setData('state', html_entity_decode($this->getRequestParameter('state'))); |
| 78 | |
| 79 | $this->setView('ItemPreview/index.tpl', 'taoItems'); |
| 80 | } |
| 81 | |
| 82 | protected function getPreviewUrl($item, $options = []) |
| 83 | { |
| 84 | $code = base64_encode($item->getUri()); |
| 85 | return _url('render/' . $code . '/index', 'ItemPreview', 'taoItems', $options); |
| 86 | } |
| 87 | |
| 88 | public function render() |
| 89 | { |
| 90 | $relPath = tao_helpers_Request::getRelativeUrl(); |
| 91 | [$extension, $module, $action, $codedUri, $path] = explode('/', $relPath, 5); |
| 92 | |
| 93 | $path = rawurldecode($path); |
| 94 | $uri = base64_decode($codedUri); |
| 95 | if (!common_Utils::isUri($uri)) { |
| 96 | throw new common_exception_BadRequest('"' . $codedUri . '" does not decode to a valid item URI'); |
| 97 | } |
| 98 | $item = $this->getResource($uri); |
| 99 | if ($path === 'index') { |
| 100 | $this->renderItem($item); |
| 101 | } else { |
| 102 | $this->renderResource($item, $path); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | protected function getRenderedItem($item) |
| 107 | { |
| 108 | $itemModel = $item->getOnePropertyValue( |
| 109 | $this->getProperty(taoItems_models_classes_ItemsService::PROPERTY_ITEM_MODEL) |
| 110 | ); |
| 111 | $impl = taoItems_models_classes_ItemsService::singleton()->getItemModelImplementation($itemModel); |
| 112 | if (is_null($impl)) { |
| 113 | throw new common_Exception('preview not supported for this item type ' . $itemModel->getUri()); |
| 114 | } |
| 115 | return $impl->render($item, ''); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Add the rendered item to psr7 response |
| 120 | * |
| 121 | * @param $item |
| 122 | * @throws common_Exception |
| 123 | */ |
| 124 | private function renderItem($item) |
| 125 | { |
| 126 | $this->response = $this->response->withBody(stream_for($this->getRenderedItem($item))); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param $item |
| 131 | * @param $path |
| 132 | * |
| 133 | * @throws common_Exception |
| 134 | * @throws common_exception_Error |
| 135 | * @throws tao_models_classes_FileNotFoundException |
| 136 | */ |
| 137 | private function renderResource($item, $path) |
| 138 | { |
| 139 | $lang = $this->getSession()->getDataLanguage(); |
| 140 | $resolver = new ItemMediaResolver($item, $lang); |
| 141 | $asset = $resolver->resolve($path); |
| 142 | $mediaSource = $asset->getMediaSource(); |
| 143 | $mediaIdentifier = $asset->getMediaIdentifier(); |
| 144 | |
| 145 | if ($mediaSource instanceof HttpSource || Base64::isEncodedImage($mediaIdentifier)) { |
| 146 | throw new common_Exception('Only tao files available for rendering through item preview'); |
| 147 | } |
| 148 | |
| 149 | $info = $mediaSource->getFileInfo($mediaIdentifier); |
| 150 | $stream = $mediaSource->getFileStream($mediaIdentifier); |
| 151 | \tao_helpers_Http::returnStream($stream, $info['mime']); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the ResultServer API call to be used by the item. |
| 156 | * |
| 157 | * @return string A string representing JavaScript instructions. |
| 158 | */ |
| 159 | protected function getResultServer() |
| 160 | { |
| 161 | return [ |
| 162 | 'module' => 'taoItems/runtime/ConsoleResultServer' |
| 163 | ]; |
| 164 | } |
| 165 | } |