Passed
Push — master ( f69a3f...2acc11 )
by Marcel
02:32
created

DatasetController::getOwnFavoriteDatasets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Db\DataloadMapper;
16
use OCA\Analytics\Db\DatasetMapper;
17
use OCA\Analytics\Db\StorageMapper;
18
use OCA\Analytics\Db\ThresholdMapper;
19
use OCP\AppFramework\Controller;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\ILogger;
22
use OCP\IRequest;
23
use OCP\ITagManager;
24
25
class DatasetController extends Controller
26
{
27
    private $logger;
28
    private $tagManager;
29
    private $ShareController;
30
    private $StorageMapper;
31
    private $DatasetMapper;
32
    private $ThresholdMapper;
33
    private $DataloadMapper;
34
    private $ActivityManager;
35
36
    public function __construct(
37
        $appName,
38
        IRequest $request,
39
        ILogger $logger,
40
        ITagManager $tagManager,
41
        ShareController $ShareController,
42
        StorageMapper $StorageMapper,
43
        DatasetMapper $DatasetMapper,
44
        ThresholdMapper $ThresholdMapper,
45
        DataloadMapper $DataloadMapper,
46
        ActivityManager $ActivityManager
47
    )
48
    {
49
        parent::__construct($appName, $request);
50
        $this->logger = $logger;
51
        $this->tagManager = $tagManager;
52
        $this->ShareController = $ShareController;
53
        $this->StorageMapper = $StorageMapper;
54
        $this->DatasetMapper = $DatasetMapper;
55
        $this->ThresholdMapper = $ThresholdMapper;
56
        $this->DataloadMapper = $DataloadMapper;
57
        $this->ActivityManager = $ActivityManager;
58
    }
59
60
    /**
61
     * get all datasets
62
     *
63
     * @NoAdminRequired
64
     */
65
    public function index()
66
    {
67
        $ownDatasets = $this->DatasetMapper->getDatasets();
68
        $sharedDatasets = $this->ShareController->getSharedDatasets();
69
        $ownDatasets = array_merge($ownDatasets, $sharedDatasets);
70
71
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
72
        foreach ($dataloads as $dataload) {
73
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
74
            //$this->logger->debug($key);
75
            if ($key !== '') {
76
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
77
                    $dataload['schedules'] = 1;
78
                } else {
79
                    $dataload['schedules'] = 0;
80
                }
81
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
82
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
83
            }
84
        }
85
        return new DataResponse($ownDatasets);
86
    }
87
88
    /**
89
     * get own dataset details
90
     *
91
     * @NoAdminRequired
92
     * @param int $datasetId
93
     * @return array
94
     */
95
    public function read(int $datasetId)
96
    {
97
        return $this->getOwnDataset($datasetId);
98
    }
99
100
    /**
101
     * get own dataset details
102
     *
103
     * @NoAdminRequired
104
     * @param int $datasetId
105
     * @param string|null $user_id
106
     * @return array
107
     */
108
    public function getOwnDataset(int $datasetId, string $user_id = null)
109
    {
110
        return $this->DatasetMapper->getOwnDataset($datasetId, $user_id);
111
    }
112
113
    /**
114
     * get own datasets which are marked as favorites
115
     *
116
     * @NoAdminRequired
117
     * @return array
118
     */
119
    public function getOwnFavoriteDatasets()
120
    {
121
        return $this->tagManager->load('analytics')->getFavorites();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->tagManager...ytics')->getFavorites() could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
122
    }
123
124
    /**
125
     * set/remove the favorite flag for a report
126
     *
127
     * @NoAdminRequired
128
     * @param int $datasetId
129
     * @param string $favorite
130
     * @return bool
131
     */
132
    public function setFavorite(int $datasetId, string $favorite)
133
    {
134
        if ($favorite === 'true') {
135
            $return = $this->tagManager->load('analytics')->addToFavorites($datasetId);
136
        } else {
137
            $return = $this->tagManager->load('analytics')->removeFromFavorites($datasetId);
138
        }
139
        return $return;
140
    }
141
142
    /**
143
     * create new dataset
144
     *
145
     * @NoAdminRequired
146
     * @param string $file
147
     * @param string $link
148
     * @return int
149
     */
150
    public function create($file = '', $link = '')
0 ignored issues
show
Unused Code introduced by
The parameter $link is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

150
    public function create($file = '', /** @scrutinizer ignore-unused */ $link = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    {
152
        //$this->logger->error('datasetcontroller 82: '.$file);
153
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
154
        $datasetId = $this->DatasetMapper->createDataset();
155
156
        if ($file === 'DEMO') {
157
            $name = 'Demo Report';
158
            $subheader = 'CSV Demo Data from GitHub';
159
            $parent = 0;
160
            $type = DataSourceController::DATASET_TYPE_EXTERNAL_FILE;
161
            $link = 'https://raw.githubusercontent.com/Rello/analytics/master/sample_data/sample1.csv';
162
            $visualization = 'ct';
163
            $chart = 'line';
164
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
165
        } elseif ($file !== '') {
166
            $name = explode('.', end(explode('/', $file)))[0];
167
            $subheader = $file;
168
            $parent = 0;
169
            $type = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
170
            $link = $file;
171
            $visualization = 'table';
172
            $chart = 'line';
173
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
174
        }
175
        return $datasetId;
176
    }
177
178
    /**
179
     * Delete Dataset and all depending objects
180
     *
181
     * @NoAdminRequired
182
     * @param int $datasetId
183
     * @return bool
184
     */
185
    public function delete(int $datasetId)
186
    {
187
        $this->ShareController->deleteShareByDataset($datasetId);
188
        $this->StorageMapper->deleteDataByDataset($datasetId);
189
        $this->DatasetMapper->deleteDataset($datasetId);
190
        $this->ThresholdMapper->deleteThresholdByDataset($datasetId);
191
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
192
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
193
        return true;
194
    }
195
196
    /**
197
     * get dataset details
198
     *
199
     * @NoAdminRequired
200
     * @param int $datasetId
201
     * @param $name
202
     * @param $subheader
203
     * @param int $parent
204
     * @param int $type
205
     * @param $link
206
     * @param $visualization
207
     * @param $chart
208
     * @param $chartoptions
209
     * @param $dataoptions
210
     * @param $dimension1
211
     * @param $dimension2
212
     * @param $value
213
     * @return bool
214
     */
215
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1 = null, $dimension2 = null, $value = null)
216
    {
217
        if ($type === DataSourceController::DATASET_TYPE_GROUP) {
218
            $parent = 0;
219
        }
220
        return $this->DatasetMapper->updateDataset($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value);
221
    }
222
223
    /**
224
     * get dataset details
225
     *
226
     * @NoAdminRequired
227
     * @param int $datasetId
228
     * @param $chartoptions
229
     * @param $dataoptions
230
     * @param $filteroptions
231
     * @return bool
232
     */
233
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions)
234
    {
235
        return $this->DatasetMapper->updateDatasetOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);
236
    }
237
}