Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 74 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| MultipleDeliveriesResultsExporter | |
0.00% |
0 / 74 |
|
0.00% |
0 / 16 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getResourceToExport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setColumnsToExport | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getColumnsToExport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setVariableToExport | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getVariableToExport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFiltersToExport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFiltersToExport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setStorageOptions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getData | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| export | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| getResultFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| saveToLocal | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getFileName | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| exportIntoFolder | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| getFileSystemService | |
0.00% |
0 / 2 |
|
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoOutcomeUi\model\export; |
| 23 | |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\oatbox\filesystem\Directory; |
| 26 | use oat\oatbox\filesystem\FileSystemService; |
| 27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 28 | use oat\tao\model\taskQueue\Task\FilesystemAwareTrait; |
| 29 | use oat\taoOutcomeUi\model\ResultsService; |
| 30 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 31 | |
| 32 | /** |
| 33 | * MultipleDeliveriesResultsExporter |
| 34 | * |
| 35 | * @author Gyula Szucs <gyula@taotesting.com> |
| 36 | */ |
| 37 | class MultipleDeliveriesResultsExporter implements ResultsExporterInterface |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | use ServiceLocatorAwareTrait; |
| 41 | use FilesystemAwareTrait; |
| 42 | |
| 43 | /** |
| 44 | * @var \core_kernel_classes_Resource|string |
| 45 | */ |
| 46 | private $deliveryClass; |
| 47 | |
| 48 | private $columnsToExport = []; |
| 49 | |
| 50 | private $variableToExport = ResultsService::VARIABLES_FILTER_LAST_SUBMITTED; |
| 51 | |
| 52 | private $storageOptions = []; |
| 53 | |
| 54 | /** |
| 55 | * @var ResultsService |
| 56 | */ |
| 57 | private $resultsService; |
| 58 | |
| 59 | /** |
| 60 | * Temporary directory to store the generated csv files. |
| 61 | * |
| 62 | * @var string |
| 63 | */ |
| 64 | private $tmpDir; |
| 65 | |
| 66 | /** |
| 67 | * @var array |
| 68 | */ |
| 69 | private $filters = []; |
| 70 | /** |
| 71 | * @var DeliveryResultsExporterFactoryInterface |
| 72 | */ |
| 73 | private $deliveryResultExporterFactory; |
| 74 | |
| 75 | /** |
| 76 | * MultipleDeliveriesResultsExporter constructor. |
| 77 | * |
| 78 | * @param string|\core_kernel_classes_Class $deliveryClass |
| 79 | * @param ResultsService $resultsService |
| 80 | * @param DeliveryResultsExporterFactoryInterface $deliveryResultExporterFactory |
| 81 | * @throws \common_exception_NotFound |
| 82 | */ |
| 83 | public function __construct( |
| 84 | $deliveryClass, |
| 85 | ResultsService $resultsService, |
| 86 | DeliveryResultsExporterFactoryInterface $deliveryResultExporterFactory |
| 87 | ) { |
| 88 | $this->deliveryClass = $this->getClass($deliveryClass); |
| 89 | |
| 90 | $this->deliveryResultExporterFactory = $deliveryResultExporterFactory; |
| 91 | |
| 92 | if (!$this->deliveryClass->exists()) { |
| 93 | throw new \common_exception_NotFound( |
| 94 | 'Results Exporter: delivery class "' . $this->deliveryClass->getUri() . '" does not exist.' |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | $this->resultsService = $resultsService; |
| 99 | |
| 100 | $this->tmpDir = \tao_helpers_File::createTempDir(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @inheritdoc |
| 105 | */ |
| 106 | public function getResourceToExport() |
| 107 | { |
| 108 | return $this->deliveryClass; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @inheritdoc |
| 113 | */ |
| 114 | public function setColumnsToExport($columnsToExport) |
| 115 | { |
| 116 | $this->columnsToExport = $columnsToExport; |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Empty array means all columns need to be used for export. |
| 123 | * |
| 124 | * @return array |
| 125 | */ |
| 126 | public function getColumnsToExport() |
| 127 | { |
| 128 | return $this->columnsToExport; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @inheritdoc |
| 133 | */ |
| 134 | public function setVariableToExport($variableToExport) |
| 135 | { |
| 136 | $allowedFilters = [ |
| 137 | ResultsService::VARIABLES_FILTER_ALL, |
| 138 | ResultsService::VARIABLES_FILTER_FIRST_SUBMITTED, |
| 139 | ResultsService::VARIABLES_FILTER_LAST_SUBMITTED, |
| 140 | ]; |
| 141 | if (!in_array($variableToExport, $allowedFilters)) { |
| 142 | throw new \InvalidArgumentException( |
| 143 | 'Results Exporter: wrong submitted variable "' . $variableToExport . '"' |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | $this->variableToExport = $variableToExport; |
| 148 | |
| 149 | return $this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Always exports the last submitted variables. |
| 154 | * |
| 155 | * @return string |
| 156 | */ |
| 157 | public function getVariableToExport() |
| 158 | { |
| 159 | return $this->variableToExport; |
| 160 | } |
| 161 | |
| 162 | public function setFiltersToExport($filters) |
| 163 | { |
| 164 | $this->filters = $filters; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return array |
| 169 | */ |
| 170 | public function getFiltersToExport() |
| 171 | { |
| 172 | return $this->filters; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @inheritdoc |
| 177 | */ |
| 178 | public function setStorageOptions(array $storageOptions) |
| 179 | { |
| 180 | $this->storageOptions = $storageOptions; |
| 181 | |
| 182 | return $this; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * @return array |
| 188 | * @throws \common_exception_NotFound |
| 189 | */ |
| 190 | public function getData() |
| 191 | { |
| 192 | $data = []; |
| 193 | |
| 194 | /** @var \core_kernel_classes_Resource $delivery */ |
| 195 | foreach ($this->deliveryClass->getInstances(true) as $delivery) { |
| 196 | $data[$delivery->getUri()] = |
| 197 | $this->deliveryResultExporterFactory->getDeliveryResultsExporter( |
| 198 | $delivery, |
| 199 | $this->resultsService |
| 200 | ) |
| 201 | ->setServiceLocator($this->getServiceLocator()) |
| 202 | ->getData(); |
| 203 | } |
| 204 | |
| 205 | return $data; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param null|string $destination |
| 210 | * @return string |
| 211 | * @throws \common_Exception |
| 212 | */ |
| 213 | public function export($destination = null) |
| 214 | { |
| 215 | $this->exportIntoFolder($this->deliveryClass, $this->tmpDir); |
| 216 | |
| 217 | $tmpZipPath = \tao_helpers_File::createZip($this->tmpDir, true); |
| 218 | |
| 219 | if (file_exists($tmpZipPath)) { |
| 220 | $finaleName = is_null($destination) |
| 221 | ? $this->saveFileToStorage($tmpZipPath, $this->getFileName()) |
| 222 | : $this->saveToLocal($tmpZipPath, $destination); |
| 223 | |
| 224 | // empty the temp dir |
| 225 | if (\helpers_File::emptyDirectory($this->tmpDir)) { |
| 226 | rmdir($this->tmpDir); |
| 227 | } |
| 228 | |
| 229 | return $finaleName; |
| 230 | } |
| 231 | |
| 232 | return ''; |
| 233 | } |
| 234 | |
| 235 | public function getResultFormat() |
| 236 | { |
| 237 | return $this->deliveryResultExporterFactory->getFormat(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @param string $tmpZipPath |
| 242 | * @param string $destination |
| 243 | * @return string |
| 244 | */ |
| 245 | private function saveToLocal($tmpZipPath, $destination) |
| 246 | { |
| 247 | $fullPath = realpath($destination) . DIRECTORY_SEPARATOR . $this->getFileName(); |
| 248 | |
| 249 | \tao_helpers_File::copy($tmpZipPath, $fullPath, false); |
| 250 | |
| 251 | return $fullPath; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @return string |
| 256 | */ |
| 257 | private function getFileName() |
| 258 | { |
| 259 | return 'results_export_' |
| 260 | . strtolower(\tao_helpers_Display::textCleaner($this->deliveryClass->getLabel(), '*')) |
| 261 | . '_' |
| 262 | . \tao_helpers_Uri::getUniqueId($this->deliveryClass->getUri()) |
| 263 | . '_' |
| 264 | . date('YmdHis') . rand(10, 99) //more unique name |
| 265 | . '.zip'; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Recursively exports all results under a class into a folder. |
| 270 | * |
| 271 | * @param \core_kernel_classes_Class $deliveryClass |
| 272 | * @param string $destination |
| 273 | */ |
| 274 | private function exportIntoFolder(\core_kernel_classes_Class $deliveryClass, $destination) |
| 275 | { |
| 276 | /** @var \core_kernel_classes_Resource $delivery */ |
| 277 | foreach ($deliveryClass->getInstances(false) as $delivery) { |
| 278 | $this->deliveryResultExporterFactory->getDeliveryResultsExporter( |
| 279 | $delivery, |
| 280 | $this->resultsService |
| 281 | )->setServiceLocator($this->getServiceLocator()) |
| 282 | ->setVariableToExport($this->getVariableToExport()) |
| 283 | ->export($destination); |
| 284 | } |
| 285 | |
| 286 | if ($subClasses = $deliveryClass->getSubClasses(false)) { |
| 287 | /** @var \core_kernel_classes_Class $subClass */ |
| 288 | foreach ($subClasses as $subClass) { |
| 289 | $newDestination = $destination . DIRECTORY_SEPARATOR |
| 290 | . \tao_helpers_Display::textCleaner($subClass->getLabel(), '*'); |
| 291 | mkdir($newDestination); |
| 292 | $this->exportIntoFolder($subClass, $newDestination); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * @see FilesystemAwareTrait::getFileSystemService() |
| 299 | */ |
| 300 | protected function getFileSystemService() |
| 301 | { |
| 302 | return $this->getServiceLocator() |
| 303 | ->get(FileSystemService::SERVICE_ID); |
| 304 | } |
| 305 | } |