| Total Complexity | 43 |
| Total Lines | 420 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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() |
||
| 227 | { |
||
| 228 | $url = null; |
||
| 229 | if ($this->allow()) { |
||
| 230 | $directory = $this->getCourseDirectory(); |
||
| 231 | $url = api_get_path(REL_COURSE_PATH).$directory.'/document/'; |
||
| 232 | } |
||
| 233 | |||
| 234 | return $url; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getCourseDocumentWebPath() |
||
| 241 | { |
||
| 242 | $url = null; |
||
| 243 | if ($this->allow()) { |
||
| 244 | $directory = $this->getCourseDirectory(); |
||
| 245 | $url = api_get_path(WEB_COURSE_PATH).$directory.'/document/'; |
||
| 246 | } |
||
| 247 | |||
| 248 | return $url; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getCourseDirectory() |
||
| 255 | { |
||
| 256 | return $this->connector->course['directory']; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function upload($fp, $dst, $name, $tmpname, $hashes = []) |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | public function rm($hash) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | public function allow() |
||
| 346 | { |
||
| 347 | //if ($this->connector->security->isGranted('ROLE_ADMIN')) { |
||
| 348 | if (api_is_anonymous()) { |
||
| 349 | return false; |
||
| 350 | } |
||
| 351 | |||
| 352 | $isAllowedToEdit = api_is_allowed_to_edit(); |
||
| 353 | |||
| 354 | $block = api_get_configuration_value('block_editor_file_manager_for_students'); |
||
| 355 | if ($block && !$isAllowedToEdit) { |
||
| 356 | return false; |
||
| 357 | } |
||
| 358 | |||
| 359 | if (isset($this->connector->course) && !empty($this->connector->course)) { |
||
| 360 | $isDocumentsToolVisible = CourseHome::getToolVisibility( |
||
| 361 | TOOL_DOCUMENT, |
||
| 362 | api_get_course_int_id(), |
||
| 363 | api_get_session_id() |
||
| 364 | ); |
||
| 365 | |||
| 366 | if (!$isDocumentsToolVisible && !$isAllowedToEdit) { |
||
| 367 | return false; |
||
| 368 | } |
||
| 369 | |||
| 370 | return true; |
||
| 371 | } |
||
| 372 | |||
| 373 | return false; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Allow to upload/delete folder or files. |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function allowToEdit() |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | public function mkdir($path, $name) |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $attr |
||
| 437 | * @param string $path |
||
| 438 | * @param $data |
||
| 439 | * @param CourseDriver $volume |
||
| 472 |