*/ class MultipleDeliveriesResultsExporter implements ResultsExporterInterface { use OntologyAwareTrait; use ServiceLocatorAwareTrait; use FilesystemAwareTrait; /** * @var \core_kernel_classes_Resource|string */ private $deliveryClass; private $columnsToExport = []; private $variableToExport = ResultsService::VARIABLES_FILTER_LAST_SUBMITTED; private $storageOptions = []; /** * @var ResultsService */ private $resultsService; /** * Temporary directory to store the generated csv files. * * @var string */ private $tmpDir; /** * @var array */ private $filters = []; /** * @var DeliveryResultsExporterFactoryInterface */ private $deliveryResultExporterFactory; /** * MultipleDeliveriesResultsExporter constructor. * * @param string|\core_kernel_classes_Class $deliveryClass * @param ResultsService $resultsService * @param DeliveryResultsExporterFactoryInterface $deliveryResultExporterFactory * @throws \common_exception_NotFound */ public function __construct($deliveryClass, ResultsService $resultsService, DeliveryResultsExporterFactoryInterface $deliveryResultExporterFactory) { $this->deliveryClass = $this->getClass($deliveryClass); $this->deliveryResultExporterFactory = $deliveryResultExporterFactory; if (!$this->deliveryClass->exists()) { throw new \common_exception_NotFound('Results Exporter: delivery class "' . $this->deliveryClass->getUri() . '" does not exist.'); } $this->resultsService = $resultsService; $this->tmpDir = \tao_helpers_File::createTempDir(); } /** * @inheritdoc */ public function getResourceToExport() { return $this->deliveryClass; } /** * @inheritdoc */ public function setColumnsToExport($columnsToExport) { $this->columnsToExport = $columnsToExport; return $this; } /** * Empty array means all columns need to be used for export. * * @return array */ public function getColumnsToExport() { return $this->columnsToExport; } /** * @inheritdoc */ public function setVariableToExport($variableToExport) { $allowedFilters = [ ResultsService::VARIABLES_FILTER_ALL, ResultsService::VARIABLES_FILTER_FIRST_SUBMITTED, ResultsService::VARIABLES_FILTER_LAST_SUBMITTED, ]; if (!in_array($variableToExport, $allowedFilters)) { throw new \InvalidArgumentException('Results Exporter: wrong submitted variable "' . $variableToExport . '"'); } $this->variableToExport = $variableToExport; return $this; } /** * Always exports the last submitted variables. * * @return string */ public function getVariableToExport() { return $this->variableToExport; } public function setFiltersToExport($filters) { $this->filters = $filters; } /** * @return array */ public function getFiltersToExport() { return $this->filters; } /** * @inheritdoc */ public function setStorageOptions(array $storageOptions) { $this->storageOptions = $storageOptions; return $this; } /** * @return array * @throws \common_exception_NotFound */ public function getData() { $data = []; /** @var \core_kernel_classes_Resource $delivery */ foreach ($this->deliveryClass->getInstances(true) as $delivery) { $data[$delivery->getUri()] = $this->deliveryResultExporterFactory->getDeliveryResultsExporter( $delivery, $this->resultsService ) ->setServiceLocator($this->getServiceLocator()) ->getData(); } return $data; } /** * @param null|string $destination * @return string * @throws \common_Exception */ public function export($destination = null) { $this->exportIntoFolder($this->deliveryClass, $this->tmpDir); $tmpZipPath = \tao_helpers_File::createZip($this->tmpDir, true); if (file_exists($tmpZipPath)) { $finaleName = is_null($destination) ? $this->saveFileToStorage($tmpZipPath, $this->getFileName()) : $this->saveToLocal($tmpZipPath, $destination); // empty the temp dir if (\helpers_File::emptyDirectory($this->tmpDir)) { rmdir($this->tmpDir); } return $finaleName; } return ''; } public function getResultFormat() { return $this->deliveryResultExporterFactory->getFormat(); } /** * @param string $tmpZipPath * @param string $destination * @return string */ private function saveToLocal($tmpZipPath, $destination) { $fullPath = realpath($destination) . DIRECTORY_SEPARATOR . $this->getFileName(); \tao_helpers_File::copy($tmpZipPath, $fullPath, false); return $fullPath; } /** * @return string */ private function getFileName() { return 'results_export_' . strtolower(\tao_helpers_Display::textCleaner($this->deliveryClass->getLabel(), '*')) . '_' . \tao_helpers_Uri::getUniqueId($this->deliveryClass->getUri()) . '_' . date('YmdHis') . rand(10, 99) //more unique name . '.zip'; } /** * Recursively exports all results under a class into a folder. * * @param \core_kernel_classes_Class $deliveryClass * @param string $destination */ private function exportIntoFolder(\core_kernel_classes_Class $deliveryClass, $destination) { /** @var \core_kernel_classes_Resource $delivery */ foreach ($deliveryClass->getInstances(false) as $delivery) { $this->deliveryResultExporterFactory->getDeliveryResultsExporter( $delivery, $this->resultsService )->setServiceLocator($this->getServiceLocator()) ->export($destination); } if ($subClasses = $deliveryClass->getSubClasses(false)) { /** @var \core_kernel_classes_Class $subClass */ foreach ($subClasses as $subClass) { $newDestination = $destination . DIRECTORY_SEPARATOR . \tao_helpers_Display::textCleaner($subClass->getLabel(), '*'); mkdir($newDestination); $this->exportIntoFolder($subClass, $newDestination); } } } /** * @see FilesystemAwareTrait::getFileSystemService() */ protected function getFileSystemService() { return $this->getServiceLocator() ->get(FileSystemService::SERVICE_ID); } }