Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 114 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| TaskQueueWebApi | |
0.00% |
0 / 114 |
|
0.00% |
0 / 8 |
812 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getAll | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| get | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
| stats | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| archive | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
42 | |||
| download | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| assertTaskIdExists | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| detectTaskIds | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 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 (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTaskQueue\controller; |
| 23 | |
| 24 | use common_session_SessionManager; |
| 25 | use oat\oatbox\filesystem\FileSystemService; |
| 26 | use oat\tao\model\routing\AnnotationReader\security; |
| 27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 28 | use oat\tao\model\taskQueue\TaskLog\Broker\TaskLogBrokerInterface; |
| 29 | use oat\tao\model\taskQueue\TaskLog\TaskLogFilter; |
| 30 | use oat\tao\model\taskQueue\TaskLogInterface; |
| 31 | use oat\taoTaskQueue\model\Entity\Decorator\CategoryEntityDecorator; |
| 32 | use oat\taoTaskQueue\model\Entity\Decorator\HasFileEntityDecorator; |
| 33 | use oat\taoTaskQueue\model\TaskLog\Decorator\SimpleManagementCollectionDecorator; |
| 34 | |
| 35 | /** |
| 36 | * API controller to get task queue data from the WEB. |
| 37 | * |
| 38 | * @deprecated Use \tao_actions_TaskQueueWebApi |
| 39 | * |
| 40 | * @author Gyula Szucs <gyula@taotesting.com> |
| 41 | */ |
| 42 | class TaskQueueWebApi extends \tao_actions_CommonModule |
| 43 | { |
| 44 | public const PARAMETER_TASK_ID = 'taskId'; |
| 45 | public const PARAMETER_LIMIT = 'limit'; |
| 46 | public const PARAMETER_OFFSET = 'offset'; |
| 47 | public const ARCHIVE_ALL = 'all'; |
| 48 | |
| 49 | /** @var string */ |
| 50 | private $userId; |
| 51 | |
| 52 | /** |
| 53 | * @inheritdoc |
| 54 | * @security("hide") |
| 55 | */ |
| 56 | public function __construct() |
| 57 | { |
| 58 | parent::__construct(); |
| 59 | |
| 60 | $this->userId = common_session_SessionManager::getSession()->getUserUri(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @throws \common_exception_NotImplemented |
| 65 | */ |
| 66 | public function getAll() |
| 67 | { |
| 68 | if (!\tao_helpers_Request::isAjax()) { |
| 69 | throw new \Exception('Only ajax call allowed.'); |
| 70 | } |
| 71 | |
| 72 | /** @var TaskLogInterface $taskLogService */ |
| 73 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 74 | $limit = $offset = null; |
| 75 | |
| 76 | if ($this->hasRequestParameter(self::PARAMETER_LIMIT)) { |
| 77 | $limit = (int) $this->getRequestParameter(self::PARAMETER_LIMIT); |
| 78 | } |
| 79 | |
| 80 | if ($this->hasRequestParameter(self::PARAMETER_OFFSET)) { |
| 81 | $offset = (int) $this->getRequestParameter(self::PARAMETER_OFFSET); |
| 82 | } |
| 83 | |
| 84 | /** @var FileSystemService $fs */ |
| 85 | $fs = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 86 | |
| 87 | $collection = new SimpleManagementCollectionDecorator( |
| 88 | $taskLogService->findAvailableByUser($this->userId, $limit, $offset), |
| 89 | $taskLogService, |
| 90 | $fs, |
| 91 | false |
| 92 | ); |
| 93 | |
| 94 | return $this->returnJson([ |
| 95 | 'success' => true, |
| 96 | 'data' => $collection->toArray() |
| 97 | ]); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @throws \common_exception_NotImplemented |
| 102 | */ |
| 103 | public function get() |
| 104 | { |
| 105 | if (!\tao_helpers_Request::isAjax()) { |
| 106 | throw new \Exception('Only ajax call allowed.'); |
| 107 | } |
| 108 | |
| 109 | /** @var TaskLogInterface $taskLogService */ |
| 110 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 111 | |
| 112 | /** @var FileSystemService $fs */ |
| 113 | $fs = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 114 | |
| 115 | try { |
| 116 | $this->assertTaskIdExists(); |
| 117 | |
| 118 | $entity = $taskLogService->getByIdAndUser( |
| 119 | $this->getRequestParameter(self::PARAMETER_TASK_ID), |
| 120 | $this->userId |
| 121 | ); |
| 122 | |
| 123 | return $this->returnJson([ |
| 124 | 'success' => true, |
| 125 | 'data' => (new HasFileEntityDecorator( |
| 126 | new CategoryEntityDecorator($entity, $taskLogService), |
| 127 | $fs |
| 128 | ))->toArray() |
| 129 | ]); |
| 130 | } catch (\Exception $e) { |
| 131 | return $this->returnJson([ |
| 132 | 'success' => false, |
| 133 | 'errorMsg' => $e instanceof \common_exception_UserReadableException |
| 134 | ? $e->getUserMessage() |
| 135 | : $e->getMessage(), |
| 136 | 'errorCode' => $e->getCode(), |
| 137 | ]); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @throws \common_exception_NotImplemented |
| 143 | */ |
| 144 | public function stats() |
| 145 | { |
| 146 | if (!\tao_helpers_Request::isAjax()) { |
| 147 | throw new \Exception('Only ajax call allowed.'); |
| 148 | } |
| 149 | |
| 150 | /** @var TaskLogInterface $taskLogService */ |
| 151 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 152 | |
| 153 | return $this->returnJson([ |
| 154 | 'success' => true, |
| 155 | 'data' => $taskLogService->getStats($this->userId)->toArray() |
| 156 | ]); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @throws \common_exception_NotImplemented |
| 161 | * @throws \Exception |
| 162 | */ |
| 163 | public function archive() |
| 164 | { |
| 165 | if (!\tao_helpers_Request::isAjax()) { |
| 166 | throw new \Exception('Only ajax call allowed.'); |
| 167 | } |
| 168 | |
| 169 | try { |
| 170 | $this->assertTaskIdExists(); |
| 171 | $taskIds = $this->detectTaskIds(); |
| 172 | |
| 173 | /** @var TaskLogInterface $taskLogService */ |
| 174 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 175 | |
| 176 | if ($taskIds === static::ARCHIVE_ALL) { |
| 177 | $filter = (new TaskLogFilter())->availableForArchived($this->userId); |
| 178 | $taskLogCollection = $taskLogService->search($filter); |
| 179 | } else { |
| 180 | $filter = (new TaskLogFilter()) |
| 181 | ->addAvailableFilters($this->userId) |
| 182 | ->in(TaskLogBrokerInterface::COLUMN_ID, $taskIds); |
| 183 | $taskLogCollection = $taskLogService->search($filter); |
| 184 | } |
| 185 | |
| 186 | $hasBeenArchive = $taskLogService->archiveCollection($taskLogCollection); |
| 187 | return $this->returnJson([ |
| 188 | 'success' => (bool)$hasBeenArchive |
| 189 | ]); |
| 190 | } catch (\Exception $e) { |
| 191 | return $this->returnJson([ |
| 192 | 'success' => false, |
| 193 | 'errorMsg' => $e instanceof \common_exception_UserReadableException |
| 194 | ? $e->getUserMessage() |
| 195 | : $e->getMessage(), |
| 196 | 'errorCode' => $e instanceof \common_exception_NotFound ? 404 : $e->getCode(), |
| 197 | ]); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Download the file created by task. |
| 203 | */ |
| 204 | public function download() |
| 205 | { |
| 206 | try { |
| 207 | $this->assertTaskIdExists(); |
| 208 | |
| 209 | /** @var TaskLogInterface $taskLogService */ |
| 210 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 211 | |
| 212 | $taskLogEntity = $taskLogService->getByIdAndUser( |
| 213 | $this->getRequestParameter(self::PARAMETER_TASK_ID), |
| 214 | $this->userId |
| 215 | ); |
| 216 | |
| 217 | if (!$taskLogEntity->getStatus()->isCompleted()) { |
| 218 | throw new \common_Exception('Task "' . $taskLogEntity->getId() . '" is not downloadable.'); |
| 219 | } |
| 220 | |
| 221 | $filename = $taskLogEntity->getFileNameFromReport(); |
| 222 | |
| 223 | if (empty($filename)) { |
| 224 | throw new \common_Exception('Filename not found in report.'); |
| 225 | } |
| 226 | |
| 227 | /** @var FileSystemService $fileSystem */ |
| 228 | $fileSystem = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 229 | $directory = $fileSystem->getDirectory(QueueDispatcherInterface::FILE_SYSTEM_ID); |
| 230 | $file = $directory->getFile($filename); |
| 231 | |
| 232 | if (!$file->exists()) { |
| 233 | throw new \common_exception_NotFound('File not found.'); |
| 234 | } |
| 235 | |
| 236 | header('Set-Cookie: fileDownload=true'); |
| 237 | setcookie('fileDownload', 'true', 0, '/'); |
| 238 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
| 239 | header('Content-Type: ' . $file->getMimeType()); |
| 240 | |
| 241 | \tao_helpers_Http::returnStream($file->readPsrStream()); |
| 242 | exit(); |
| 243 | } catch (\Exception $e) { |
| 244 | return $this->returnJson([ |
| 245 | 'success' => false, |
| 246 | 'errorMsg' => $e instanceof \common_exception_UserReadableException |
| 247 | ? $e->getUserMessage() |
| 248 | : $e->getMessage(), |
| 249 | 'errorCode' => $e->getCode(), |
| 250 | ]); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @throws \common_exception_MissingParameter |
| 256 | */ |
| 257 | protected function assertTaskIdExists() |
| 258 | { |
| 259 | if (!$this->hasRequestParameter(self::PARAMETER_TASK_ID)) { |
| 260 | throw new \common_exception_MissingParameter(self::PARAMETER_TASK_ID, $this->getRequestURI()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @return array|string |
| 266 | */ |
| 267 | protected function detectTaskIds() |
| 268 | { |
| 269 | $taskIdsParams = $this->getRequestParameter(self::PARAMETER_TASK_ID); |
| 270 | |
| 271 | if (is_array($taskIdsParams)) { |
| 272 | return $taskIdsParams; |
| 273 | } elseif ($taskIdsParams === static::ARCHIVE_ALL) { |
| 274 | return static::ARCHIVE_ALL; |
| 275 | } else { |
| 276 | return [$taskIdsParams]; |
| 277 | } |
| 278 | } |
| 279 | } |