Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AbstractIrregularityReport | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| getFormatedDateForFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIrregularities | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| __invoke | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
| getFileName | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getIrregularitiesTable | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoProctoring\model\service; |
| 23 | |
| 24 | use common_report_Report as Report; |
| 25 | use oat\generis\model\OntologyAwareTrait; |
| 26 | use oat\oatbox\action\Action; |
| 27 | use oat\oatbox\filesystem\FileSystemService; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\tao\model\export\implementation\CsvExporter; |
| 30 | use oat\tao\model\taskQueue\QueueDispatcher; |
| 31 | use oat\tao\model\taskQueue\Task\FilesystemAwareTrait; |
| 32 | use oat\tao\model\taskQueue\Task\TaskInterface; |
| 33 | |
| 34 | abstract class AbstractIrregularityReport extends ConfigurableService implements Action |
| 35 | { |
| 36 | use OntologyAwareTrait; |
| 37 | use FilesystemAwareTrait; |
| 38 | |
| 39 | public const SERVICE_ID = 'taoProctoring/irregularity'; |
| 40 | |
| 41 | /** |
| 42 | * return formated string for export file name |
| 43 | * |
| 44 | * @param $time |
| 45 | * @return false|string |
| 46 | */ |
| 47 | protected function getFormatedDateForFileName($time) |
| 48 | { |
| 49 | return date('Y-m-d.H.i.s', $time); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param \core_kernel_classes_Resource $delivery |
| 54 | * @param string $from |
| 55 | * @param string $to |
| 56 | * @return TaskInterface |
| 57 | */ |
| 58 | public function getIrregularities(\core_kernel_classes_Resource $delivery, $from = '', $to = '') |
| 59 | { |
| 60 | /** @var QueueDispatcher $queueDispatcher */ |
| 61 | $queueDispatcher = $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID); |
| 62 | |
| 63 | $action = $this->getServiceLocator()->get(self::SERVICE_ID); |
| 64 | $parameters = [ |
| 65 | 'deliveryId' => $delivery->getUri(), |
| 66 | 'from' => $from, |
| 67 | 'to' => $to, |
| 68 | ]; |
| 69 | |
| 70 | return $queueDispatcher->createTask( |
| 71 | $action, |
| 72 | $parameters, |
| 73 | __( |
| 74 | 'CSV irregularities export for delivery "%s" from %s to %s', |
| 75 | $delivery->getLabel(), |
| 76 | $this->getFormatedDateForFileName($from), |
| 77 | $this->getFormatedDateForFileName($to) |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | public function __invoke($params) |
| 83 | { |
| 84 | $this->getServiceLocator() |
| 85 | ->get(\common_ext_ExtensionsManager::SERVICE_ID) |
| 86 | ->getExtensionById('taoResultServer'); |
| 87 | |
| 88 | if (!isset($params['deliveryId'])) { |
| 89 | throw new \InvalidArgumentException('Delivery uri is missing for irregularity export'); |
| 90 | } |
| 91 | |
| 92 | if (!isset($params['from'])) { |
| 93 | throw new \InvalidArgumentException('From date is missing for irregularity export'); |
| 94 | } |
| 95 | |
| 96 | if (!isset($params['to'])) { |
| 97 | throw new \InvalidArgumentException('To date is missing for irregularity export'); |
| 98 | } |
| 99 | |
| 100 | $delivery = $this->getResource($params['deliveryId']); |
| 101 | $data = $this->getIrregularitiesTable($delivery, $params['from'], $params['to']); |
| 102 | $exporter = new CsvExporter($data); |
| 103 | |
| 104 | $filePrefix = $this->saveStringToStorage($exporter->export(), $this->getFileName($delivery, $params)); |
| 105 | |
| 106 | return $filePrefix === false |
| 107 | ? Report::createFailure(__('Unable to create irregularities export for %s', $delivery->getLabel())) |
| 108 | : Report::createSuccess( |
| 109 | __('Irregularities for "%s" successfully exported', $delivery->getLabel()), |
| 110 | $filePrefix |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | protected function getFileName(\core_kernel_classes_Resource $delivery, array $params) |
| 115 | { |
| 116 | return strtolower( |
| 117 | 'irregularities_' |
| 118 | . \tao_helpers_File::getSafeFileName($delivery->getLabel()) . '_' |
| 119 | . $this->getFormatedDateForFileName($params['from']) . '_' |
| 120 | . $this->getFormatedDateForFileName($params['to']) . '_' |
| 121 | . date('YmdHis') . rand(10, 99) //more unique name |
| 122 | . '.csv' |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @param \core_kernel_classes_Resource $delivery |
| 128 | * @param string $from |
| 129 | * @param string $to |
| 130 | * @return array |
| 131 | */ |
| 132 | abstract public function getIrregularitiesTable(\core_kernel_classes_Resource $delivery, $from = '', $to = ''); |
| 133 | |
| 134 | /** |
| 135 | * @see FilesystemAwareTrait::getFileSystemService() |
| 136 | */ |
| 137 | protected function getFileSystemService() |
| 138 | { |
| 139 | return $this->getServiceLocator() |
| 140 | ->get(FileSystemService::SERVICE_ID); |
| 141 | } |
| 142 | } |