Total Complexity | 45 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like CourseDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class CourseDriver extends Driver implements DriverInterface |
||
14 | { |
||
15 | public $name = 'CourseDriver'; |
||
16 | public $visibleFiles = []; |
||
17 | private $coursePath; |
||
18 | |||
19 | /** |
||
20 | * Setups the folder. |
||
21 | */ |
||
22 | public function setup() |
||
23 | { |
||
24 | $userId = api_get_user_id(); |
||
25 | $userInfo = api_get_user_info(); |
||
26 | $sessionId = api_get_session_id(); |
||
27 | $courseInfo = $this->connector->course; |
||
28 | |||
29 | if (!empty($courseInfo)) { |
||
30 | $coursePath = api_get_path(SYS_COURSE_PATH); |
||
31 | $courseDir = $courseInfo['directory'].'/document'; |
||
32 | $baseDir = $coursePath.$courseDir; |
||
33 | $this->coursePath = $baseDir; |
||
34 | |||
35 | // Creates shared folder |
||
36 | if (!file_exists($baseDir.'/shared_folder')) { |
||
37 | $title = get_lang('UserFolders'); |
||
38 | $folderName = '/shared_folder'; |
||
39 | //$groupId = 0; |
||
40 | $visibility = 0; |
||
41 | create_unexisting_directory( |
||
42 | $courseInfo, |
||
43 | $userId, |
||
44 | $sessionId, |
||
45 | 0, |
||
46 | null, |
||
47 | $baseDir, |
||
48 | $folderName, |
||
49 | $title, |
||
50 | $visibility |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | // Creates user-course folder |
||
55 | if (!file_exists($baseDir.'/shared_folder/sf_user_'.$userId)) { |
||
56 | $title = $userInfo['complete_name']; |
||
57 | $folderName = '/shared_folder/sf_user_'.$userId; |
||
58 | $visibility = 1; |
||
59 | create_unexisting_directory( |
||
60 | $courseInfo, |
||
61 | $userId, |
||
62 | $sessionId, |
||
63 | 0, |
||
64 | null, |
||
65 | $baseDir, |
||
66 | $folderName, |
||
67 | $title, |
||
68 | $visibility |
||
69 | ); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function getConfiguration() |
||
78 | { |
||
79 | if ($this->allow()) { |
||
80 | //$translator = $this->connector->translator; |
||
81 | //$code = $this->connector->course->getCode(); |
||
82 | $courseCode = $this->connector->course['code']; |
||
83 | $alias = $courseCode.' '.get_lang('Documents'); |
||
84 | $userId = api_get_user_id(); |
||
85 | $config = [ |
||
86 | 'driver' => 'CourseDriver', |
||
87 | 'path' => $this->getCourseDocumentSysPath(), |
||
88 | 'URL' => $this->getCourseDocumentRelativeWebPath(), |
||
89 | 'accessControl' => [$this, 'access'], |
||
90 | 'alias' => $alias, |
||
91 | 'attributes' => [ |
||
92 | // Hide shared_folder |
||
93 | [ |
||
94 | 'pattern' => '/shared_folder/', |
||
95 | 'read' => false, |
||
96 | 'write' => false, |
||
97 | 'hidden' => true, |
||
98 | 'locked' => false, |
||
99 | ], |
||
100 | [ |
||
101 | 'pattern' => '/^\/index.html$/', |
||
102 | 'read' => false, |
||
103 | 'write' => false, |
||
104 | 'hidden' => true, |
||
105 | 'locked' => false, |
||
106 | ], |
||
107 | ], |
||
108 | ]; |
||
109 | |||
110 | // admin/teachers can create dirs from ckeditor |
||
111 | if ($this->allowToEdit()) { |
||
112 | $config['attributes'][] = [ |
||
113 | 'pattern' => '/^\/learning_path$/', // block delete learning_path |
||
114 | 'read' => true, |
||
115 | 'write' => false, |
||
116 | 'hidden' => false, |
||
117 | 'locked' => true, |
||
118 | ]; |
||
119 | $config['attributes'][] = [ |
||
120 | 'pattern' => '/learning_path\/(.*)/', // allow edit/delete inside learning_path |
||
121 | 'read' => true, |
||
122 | 'write' => true, |
||
123 | 'hidden' => false, |
||
124 | 'locked' => false, |
||
125 | ]; |
||
126 | |||
127 | $defaultDisabled = $this->connector->getDefaultDriverSettings()['disabled']; |
||
128 | $defaultDisabled = array_flip($defaultDisabled); |
||
129 | unset($defaultDisabled['mkdir']); |
||
130 | $defaultDisabled = array_flip($defaultDisabled); |
||
131 | $config['disabled'] = $defaultDisabled; |
||
132 | } else { |
||
133 | $protectedFolders = \DocumentManager::getProtectedFolderFromStudent(); |
||
134 | foreach ($protectedFolders as $folder) { |
||
135 | $config['attributes'][] = [ |
||
136 | 'pattern' => $folder.'/', |
||
137 | 'read' => false, |
||
138 | 'write' => false, |
||
139 | 'hidden' => true, |
||
140 | 'locked' => false, |
||
141 | ]; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | $foldersToHide = \DocumentManager::get_all_document_folders( |
||
146 | $this->connector->course, |
||
147 | null, |
||
148 | false, |
||
149 | true |
||
150 | ); |
||
151 | |||
152 | // Teachers can see all files and folders see #1425 |
||
153 | if ($this->allowToEdit()) { |
||
154 | $foldersToHide = []; |
||
155 | } |
||
156 | |||
157 | if (!empty($foldersToHide)) { |
||
158 | foreach ($foldersToHide as $folder) { |
||
159 | $config['attributes'][] = [ |
||
160 | 'pattern' => '!'.$folder.'!', |
||
161 | 'read' => false, |
||
162 | 'write' => false, |
||
163 | 'hidden' => true, |
||
164 | 'locked' => false, |
||
165 | ]; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | // Hide all groups folders |
||
170 | $config['attributes'][] = [ |
||
171 | 'pattern' => '!_groupdocs_!', |
||
172 | 'read' => false, |
||
173 | 'write' => false, |
||
174 | 'hidden' => true, |
||
175 | 'locked' => false, |
||
176 | ]; |
||
177 | |||
178 | // Allow only the groups I have access |
||
179 | $allGroups = \GroupManager::getAllGroupPerUserSubscription($userId); |
||
180 | if (!empty($allGroups)) { |
||
181 | foreach ($allGroups as $groupInfo) { |
||
182 | $groupId = $groupInfo['iid']; |
||
183 | if (\GroupManager::user_has_access( |
||
184 | $userId, |
||
185 | $groupId, |
||
186 | \GroupManager::GROUP_TOOL_DOCUMENTS |
||
187 | )) { |
||
188 | $config['attributes'][] = [ |
||
189 | 'pattern' => '!'.$groupInfo['secret_directory'].'!', |
||
190 | 'read' => true, |
||
191 | 'write' => false, |
||
192 | 'hidden' => false, |
||
193 | 'locked' => false, |
||
194 | ]; |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $config; |
||
200 | } |
||
201 | |||
202 | return []; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * This is the absolute document course path like |
||
207 | * /var/www/portal/data/courses/XXX/document/. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getCourseDocumentSysPath() |
||
212 | { |
||
213 | $url = ''; |
||
214 | if ($this->allow()) { |
||
215 | $directory = $this->getCourseDirectory(); |
||
216 | $coursePath = $this->connector->paths['sys_course_path']; |
||
217 | $url = $coursePath.$directory.'/document/'; |
||
218 | } |
||
219 | |||
220 | return $url; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getCourseDocumentRelativeWebPath() |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getCourseDocumentWebPath() |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getCourseDirectory() |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function upload($fp, $dst, $name, $tmpname, $hashes = []) |
||
263 | { |
||
264 | // Needed to load course information in elfinder |
||
265 | $this->setConnectorFromPlugin(); |
||
266 | |||
267 | if ($this->allowToEdit()) { |
||
268 | // upload file by elfinder. |
||
269 | $size = filesize($tmpname); |
||
270 | |||
271 | // check the max filesize. |
||
272 | $maxFileSize = getIniMaxFileSizeInBytes(); |
||
273 | if ($maxFileSize > 0 && $size > $maxFileSize) { |
||
274 | $errorUploadMessage = get_lang('FileSizeIsTooBig').' '.get_lang('MaxFileSize').' : '.getIniMaxFileSizeInBytes(true); |
||
275 | $this->addError($errorUploadMessage); |
||
276 | |||
277 | return false; |
||
278 | } |
||
279 | |||
280 | $maxSpace = \DocumentManager::get_course_quota($this->connector->course['code']); |
||
281 | // Check if there is enough space to save the file. |
||
282 | if (!\DocumentManager::enough_space($size, $maxSpace)) { |
||
283 | return false; |
||
284 | } |
||
285 | |||
286 | $result = parent::upload($fp, $dst, $name, $tmpname); |
||
287 | $name = $result['name']; |
||
288 | $filtered = \URLify::filter($result['name'], 80, '', true); |
||
289 | |||
290 | if (strcmp($name, $filtered) != 0) { |
||
291 | $result = $this->customRename($result['hash'], $filtered); |
||
292 | } |
||
293 | |||
294 | $realPath = $this->realpath($result['hash']); |
||
295 | if (!empty($realPath)) { |
||
296 | // Getting file info |
||
297 | //$info = $elFinder->exec('file', array('target' => $file['hash'])); |
||
298 | /** @var elFinderVolumeLocalFileSystem $volume */ |
||
299 | //$volume = $info['volume']; |
||
300 | //$root = $volume->root(); |
||
301 | //var/www/chamilogits/data/courses/NEWONE/document |
||
302 | $realPathRoot = $this->getCourseDocumentSysPath(); |
||
303 | |||
304 | // Removing course path |
||
305 | $realPath = str_replace($realPathRoot, '/', $realPath); |
||
306 | add_document( |
||
307 | $this->connector->course, |
||
308 | $realPath, |
||
309 | 'file', |
||
310 | (int) $result['size'], |
||
311 | $result['name'] |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | return $result; |
||
316 | } |
||
317 | |||
318 | return false; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function rm($hash) |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function allow() |
||
356 | { |
||
357 | //if ($this->connector->security->isGranted('ROLE_ADMIN')) { |
||
358 | if (api_is_anonymous()) { |
||
359 | return false; |
||
360 | } |
||
361 | |||
362 | $isAllowedToEdit = api_is_allowed_to_edit(); |
||
363 | |||
364 | $block = api_get_configuration_value('block_editor_file_manager_for_students'); |
||
365 | if ($block && !$isAllowedToEdit) { |
||
366 | return false; |
||
367 | } |
||
368 | |||
369 | if (isset($this->connector->course) && !empty($this->connector->course)) { |
||
370 | $isDocumentsToolVisible = CourseHome::getToolVisibility( |
||
371 | TOOL_DOCUMENT, |
||
372 | api_get_course_int_id(), |
||
373 | api_get_session_id() |
||
374 | ); |
||
375 | |||
376 | if (!$isDocumentsToolVisible && !$isAllowedToEdit) { |
||
377 | return false; |
||
378 | } |
||
379 | |||
380 | return true; |
||
381 | } |
||
382 | |||
383 | return false; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Allow to upload/delete folder or files. |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | public function allowToEdit() |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function mkdir($path, $name) |
||
444 | } |
||
445 | |||
446 | /** |
||
483 |