Test Setup Failed
Push — master ( 4e700f...c7183e )
by Julito
63:12
created

CourseDriver::getConfiguration()   D

Complexity

Conditions 9
Paths 17

Size

Total Lines 95
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 61
nc 17
nop 0
dl 0
loc 95
rs 4.9931
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor\Driver;
5
6
/**
7
 * Class CourseDriver
8
 *
9
 * @package Chamilo\CoreBundle\Component\Editor\Driver
10
 *
11
 */
12
class CourseDriver extends Driver implements DriverInterface
13
{
14
    public $name = 'CourseDriver';
15
    public $visibleFiles = [];
16
    private $coursePath;
17
18
    /**
19
     * Setups the folder
20
     */
21
    public function setup()
22
    {
23
        $userId = api_get_user_id();
24
        $sessionId = api_get_session_id();
25
        $userInfo = $this->connector->user;
26
        $courseInfo = $this->connector->course;
27
28
        if (!empty($courseInfo)) {
29
            $coursePath = api_get_path(SYS_COURSE_PATH);
30
            $courseDir = $courseInfo['directory'].'/document';
31
            $baseDir = $coursePath.$courseDir;
32
            $this->coursePath = $baseDir;
33
34
            // Creates shared folder
35
            if (!file_exists($baseDir.'/shared_folder')) {
36
                $title = get_lang('UserFolders');
37
                $folderName = '/shared_folder';
38
                //$groupId = 0;
39
                $visibility = 0;
40
                create_unexisting_directory(
41
                    $courseInfo,
42
                    $userId,
43
                    $sessionId,
44
                    0,
45
                    null,
46
                    $baseDir,
47
                    $folderName,
48
                    $title,
49
                    $visibility
50
                );
51
            }
52
53
            // Creates user-course folder
54 View Code Duplication
            if (!file_exists($baseDir.'/shared_folder/sf_user_'.$userId)) {
55
                $title = $userInfo['complete_name'];
56
                $folderName = '/shared_folder/sf_user_'.$userId;
57
                $visibility = 1;
58
                create_unexisting_directory(
59
                    $courseInfo,
60
                    $userId,
61
                    $sessionId,
62
                    0,
63
                    null,
64
                    $baseDir,
65
                    $folderName,
66
                    $title,
67
                    $visibility
68
                );
69
            }
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getConfiguration()
77
    {
78
        if ($this->allow()) {
79
            //$translator = $this->connector->translator;
80
            //$code = $this->connector->course->getCode();
81
            $courseCode = $this->connector->course['code'];
82
            $alias = $courseCode.' '.get_lang('Documents');
83
            $userId = api_get_user_id();
84
85
            $config = array(
86
                'driver' => 'CourseDriver',
87
                'path' => $this->getCourseDocumentSysPath(),
88
                'URL' => $this->getCourseDocumentRelativeWebPath(),
89
                'accessControl' => array($this, 'access'),
90
                'alias' => $alias,
91
                'attributes' => array(
92
                    // Hide shared_folder
93
                    array(
94
                        'pattern' => '/shared_folder/',
95
                        'read' => false,
96
                        'write' => false,
97
                        'hidden' => true,
98
                        'locked' => false
99
                    )
100
                )
101
            );
102
103
            // admin/teachers can create dirs from ckeditor
104
            if ($this->allowToEdit()) {
105
                $defaultDisabled = $this->connector->getDefaultDriverSettings()['disabled'];
106
                $defaultDisabled = array_flip($defaultDisabled);
107
                unset($defaultDisabled['mkdir']);
108
                $defaultDisabled = array_flip($defaultDisabled);
109
                $config['disabled'] = $defaultDisabled;
110
            }
111
112
            $foldersToHide = \DocumentManager::get_all_document_folders(
113
                $this->connector->course,
114
                null,
115
                false,
116
                true
117
            );
118
119
            // Teachers can see all files and folders see #1425
120
            if ($this->allowToEdit()) {
121
                $foldersToHide = [];
122
            }
123
124
            if (!empty($foldersToHide)) {
125
                foreach ($foldersToHide as $folder) {
126
                    $config['attributes'][] = [
127
                        'pattern' => '!'.$folder.'!',
128
                        'read' => false,
129
                        'write' => false,
130
                        'hidden' => true,
131
                        'locked' => false
132
                    ];
133
                }
134
            }
135
136
            // Hide all groups folders
137
            $config['attributes'][] = [
138
                'pattern' => '!_groupdocs_!',
139
                'read' => false,
140
                'write' => false,
141
                'hidden' => true,
142
                'locked' => false
143
            ];
144
145
            // Allow only the groups I have access
146
            $allGroups = \GroupManager::getAllGroupPerUserSubscription($userId);
147
            if (!empty($allGroups)) {
148
                foreach ($allGroups as $groupInfo) {
149
                    $groupId = $groupInfo['iid'];
150
                    if (\GroupManager::user_has_access(
151
                        $userId,
152
                        $groupId,
153
                        \GroupManager::GROUP_TOOL_DOCUMENTS
154
                    )) {
155
                        $config['attributes'][] = [
156
                            'pattern' => '!'.$groupInfo['secret_directory'].'!',
157
                            'read' => true,
158
                            'write' => false,
159
                            'hidden' => false,
160
                            'locked' => false
161
                        ];
162
                    }
163
                }
164
            }
165
166
            return $config;
167
        }
168
169
        return array();
170
    }
171
172
    /**
173
     * This is the absolute document course path like
174
     * /var/www/portal/data/courses/XXX/document/
175
     * @return string
176
     */
177
    public function getCourseDocumentSysPath()
178
    {
179
        $url = '';
180
        if ($this->allow()) {
181
            $directory = $this->getCourseDirectory();
182
            $coursePath = $this->connector->paths['sys_course_path'];
183
            $url = $coursePath.$directory.'/document/';
184
        }
185
186
        return $url;
187
    }
188
189
    /**
190
     * @return string
191
     */
192 View Code Duplication
    public function getCourseDocumentRelativeWebPath()
193
    {
194
        $url = null;
195
        if ($this->allow()) {
196
            $directory = $this->getCourseDirectory();
197
            $url = api_get_path(REL_COURSE_PATH).$directory.'/document/';
198
        }
199
200
        return $url;
201
    }
202
203
204
    /**
205
     * @return string
206
     */
207 View Code Duplication
    public function getCourseDocumentWebPath()
208
    {
209
        $url = null;
210
        if ($this->allow()) {
211
            $directory = $this->getCourseDirectory();
212
            $url = api_get_path(WEB_COURSE_PATH).$directory.'/document/';
213
        }
214
215
        return $url;
216
    }
217
218
    /**
219
     *
220
     * @return string
221
     */
222
    public function getCourseDirectory()
223
    {
224
        return $this->connector->course['directory'];
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function upload($fp, $dst, $name, $tmpname, $hashes = array())
231
    {
232
        $this->setConnectorFromPlugin();
233
234
        if ($this->allowToEdit()) {
235
            // upload file by elfinder.
236
            $result = parent::upload($fp, $dst, $name, $tmpname);
237
            $name = $result['name'];
238
            $filtered = \URLify::filter($result['name'], 80, '', true);
239
240
            if (strcmp($name, $filtered) != 0) {
241
                $result = $this->customRename($result['hash'], $filtered);
242
            }
243
244
            $realPath = $this->realpath($result['hash']);
245
            if (!empty($realPath)) {
246
                // Getting file info
247
                //$info = $elFinder->exec('file', array('target' => $file['hash']));
248
                /** @var elFinderVolumeLocalFileSystem $volume */
249
                //$volume = $info['volume'];
250
                //$root = $volume->root();
251
                //var/www/chamilogits/data/courses/NEWONE/document
252
                $realPathRoot = $this->getCourseDocumentSysPath();
253
254
                // Removing course path
255
                $realPath = str_replace($realPathRoot, '/', $realPath);
256
                add_document(
257
                    $this->connector->course,
258
                    $realPath,
259
                    'file',
260
                    intval($result['size']),
261
                    $result['name']
262
                );
263
            }
264
265
            return $result;
266
        }
267
268
        return false;
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function rm($hash)
275
    {
276
        // elfinder does not delete the file
277
        //parent::rm($hash);
278
        $this->setConnectorFromPlugin();
279
280
        if ($this->allowToEdit()) {
281
            $path = $this->decode($hash);
282
            $stat = $this->stat($path);
283
            $stat['realpath'] = $path;
284
            $this->removed[] = $stat;
285
286
            $realFilePath = $path;
287
            $coursePath = $this->getCourseDocumentSysPath();
288
            $filePath = str_replace($coursePath, '/', $realFilePath);
289
290
            \DocumentManager::delete_document(
291
                $this->connector->course,
292
                $filePath,
293
                $coursePath
294
            );
295
296
            return true;
297
        }
298
299
        return false;
300
    }
301
302
    /**
303
     * @return bool
304
     */
305
    public function allow()
306
    {
307
        //if ($this->connector->security->isGranted('ROLE_ADMIN')) {
308
        if (api_is_anonymous()) {
309
            return false;
310
        }
311
312
        if (isset($this->connector->course) && !empty($this->connector->course)) {
313
            return true;
314
        }
315
316
        return false;
317
    }
318
319
    /**
320
     * Allow to upload/delete folder or files
321
     *
322
     * @return bool
323
     */
324
    public function allowToEdit()
325
    {
326
        $allow = $this->allow();
327
328
        return $allow && api_is_allowed_to_edit(null, true);
329
    }
330
331
    /**
332
     * @inheritdoc
333
     */
334
    protected function _mkdir($path, $name)
335
    {
336
        if ($this->allowToEdit() == false) {
337
            return false;
338
        }
339
340
        $path = $this->_joinPath($path, $name);
341
342
        if (mkdir($path)) {
343
            $this->setConnectorFromPlugin();
344
            chmod($path, $this->options['dirMode']);
345
            clearstatcache();
346
            $_course = $this->connector->course;
347
            $realPathRoot = $this->getCourseDocumentSysPath();
348
349
            // Removing course path
350
            $newPath = str_replace($realPathRoot, '/', $path);
351
            $documentId = add_document(
352
                $_course,
353
                $newPath,
354
                'folder',
355
                0,
356
                $name,
357
                null,
358
                0,
359
                true,
360
                api_get_group_id(),
361
                api_get_session_id(),
362
                api_get_user_id()
363
            );
364
365
            if (empty($documentId)) {
366
                unlink($path);
367
368
                return false;
369
            }
370
371
            return $path;
372
        }
373
374
        return false;
375
    }
376
377
    /**
378
     * @param string $attr
379
     * @param string $path
380
     * @param $data
381
     * @param CourseDriver $volume
382
     */
383
    /*public function access($attr, $path, $data, $volume)
384
    {
385
        if ($path == $this->coursePath) {
386
387
            return true;
388
        }
389
390
        $allowToEdit = $this->allowToEdit();
391
        if ($allowToEdit) {
392
            return true;
393
        }
394
395
        $path = str_replace($this->coursePath, '', $path);
396
        $documentId = \DocumentManager::get_document_id($this->connector->course, $path);
397
398
        if ($documentId) {
399
400
            $result = \DocumentManager::is_visible_by_id(
401
                $documentId,
402
                $this->connector->course,
403
                api_get_session_id(),
404
                api_get_user_id()
405
            );
406
            return false;
407
        }
408
409
        return false;
410
411
    }*/
412
}
413