| Total Complexity | 60 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SectionExport 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 SectionExport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class SectionExport |
||
| 16 | { |
||
| 17 | private $course; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor to initialize the course object. |
||
| 21 | * |
||
| 22 | * @param object $course The course object to be exported. |
||
| 23 | */ |
||
| 24 | public function __construct($course) |
||
| 25 | { |
||
| 26 | $this->course = $course; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Export a section and its activities to the specified directory. |
||
| 31 | */ |
||
| 32 | public function exportSection(int $sectionId, string $exportDir): void |
||
| 33 | { |
||
| 34 | $sectionDir = $exportDir."/sections/section_{$sectionId}"; |
||
| 35 | |||
| 36 | if (!is_dir($sectionDir)) { |
||
| 37 | mkdir($sectionDir, api_get_permissions_for_new_directories(), true); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($sectionId > 0) { |
||
| 41 | $learnpath = $this->getLearnpathById($sectionId); |
||
| 42 | if ($learnpath === null) { |
||
| 43 | throw new Exception("Learnpath with ID $sectionId not found."); |
||
| 44 | } |
||
| 45 | $sectionData = $this->getSectionData($learnpath); |
||
| 46 | } else { |
||
| 47 | $sectionData = [ |
||
| 48 | 'id' => 0, |
||
| 49 | 'number' => 0, |
||
| 50 | 'name' => get_lang('General'), |
||
| 51 | 'summary' => get_lang('GeneralResourcesCourse'), |
||
| 52 | 'sequence' => 0, |
||
| 53 | 'visible' => 1, |
||
| 54 | 'timemodified' => time(), |
||
| 55 | 'activities' => $this->getActivitiesForGeneral(), |
||
| 56 | ]; |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->createSectionXml($sectionData, $sectionDir); |
||
| 60 | $this->createInforefXml($sectionData, $sectionDir); |
||
| 61 | $this->exportActivities($sectionData['activities'], $exportDir, $sectionId); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get all general items not linked to any lesson (learnpath). |
||
| 66 | */ |
||
| 67 | public function getGeneralItems(): array |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the activities for the general section. |
||
| 106 | */ |
||
| 107 | public function getActivitiesForGeneral(): array |
||
| 108 | { |
||
| 109 | $generalLearnpath = (object) [ |
||
| 110 | 'items' => $this->getGeneralItems(), |
||
| 111 | 'source_id' => 0, |
||
| 112 | ]; |
||
| 113 | |||
| 114 | $activities = $this->getActivitiesForSection($generalLearnpath, true); |
||
| 115 | |||
| 116 | if (!in_array('folder', array_column($activities, 'modulename'))) { |
||
| 117 | $activities[] = [ |
||
| 118 | 'id' => 0, |
||
| 119 | 'moduleid' => 0, |
||
| 120 | 'modulename' => 'folder', |
||
| 121 | 'name' => 'Documents', |
||
| 122 | 'sectionid' => 0, |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | |||
| 126 | return $activities; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the learnpath object by its ID. |
||
| 131 | */ |
||
| 132 | public function getLearnpathById(int $sectionId): ?object |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get section data for a learnpath. |
||
| 145 | */ |
||
| 146 | public function getSectionData(object $learnpath): array |
||
| 147 | { |
||
| 148 | return [ |
||
| 149 | 'id' => $learnpath->source_id, |
||
| 150 | 'number' => $learnpath->display_order, |
||
| 151 | 'name' => $learnpath->name, |
||
| 152 | 'summary' => $learnpath->description, |
||
| 153 | 'sequence' => $learnpath->source_id, |
||
| 154 | 'visible' => $learnpath->visibility, |
||
| 155 | 'timemodified' => strtotime($learnpath->modified_on), |
||
| 156 | 'activities' => $this->getActivitiesForSection($learnpath), |
||
| 157 | ]; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the activities for a specific section. |
||
| 162 | */ |
||
| 163 | public function getActivitiesForSection(object $learnpath, bool $isGeneral = false): array |
||
| 164 | { |
||
| 165 | $activities = []; |
||
| 166 | $sectionId = $isGeneral ? 0 : $learnpath->source_id; |
||
| 167 | |||
| 168 | foreach ($learnpath->items as $item) { |
||
| 169 | $this->addActivityToList($item, $sectionId, $activities); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $activities; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Export the activities of a section. |
||
| 177 | */ |
||
| 178 | private function exportActivities(array $activities, string $exportDir, int $sectionId): void |
||
| 179 | { |
||
| 180 | $exportClasses = [ |
||
| 181 | 'quiz' => QuizExport::class, |
||
| 182 | 'glossary' => GlossaryExport::class, |
||
| 183 | 'url' => UrlExport::class, |
||
| 184 | 'assign' => AssignExport::class, |
||
| 185 | 'forum' => ForumExport::class, |
||
| 186 | 'page' => PageExport::class, |
||
| 187 | 'resource' => ResourceExport::class, |
||
| 188 | 'folder' => FolderExport::class, |
||
| 189 | 'feedback' => FeedbackExport::class, |
||
| 190 | ]; |
||
| 191 | |||
| 192 | foreach ($activities as $activity) { |
||
| 193 | $moduleName = $activity['modulename']; |
||
| 194 | if (isset($exportClasses[$moduleName])) { |
||
| 195 | $exportClass = new $exportClasses[$moduleName]($this->course); |
||
| 196 | $exportClass->export($activity['id'], $exportDir, $activity['moduleid'], $sectionId); |
||
| 197 | } else { |
||
| 198 | throw new \Exception("Export for module '$moduleName' is not supported."); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Check if an item is associated with any learnpath. |
||
| 205 | */ |
||
| 206 | private function isItemInLearnpath(object $item, string $type): bool |
||
| 207 | { |
||
| 208 | if (!empty($this->course->resources[RESOURCE_LEARNPATH])) { |
||
| 209 | foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) { |
||
| 210 | if (!empty($learnpath->items)) { |
||
| 211 | foreach ($learnpath->items as $learnpathItem) { |
||
| 212 | if ($learnpathItem['item_type'] === $type && $learnpathItem['path'] == $item->source_id) { |
||
| 213 | return true; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Add an activity to the activities list. |
||
| 225 | */ |
||
| 226 | private function addActivityToList(array $item, int $sectionId, array &$activities): void |
||
| 303 | ]; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Determine the document type based on filetype and path. |
||
| 309 | */ |
||
| 310 | private function getDocumentType(string $filetype, string $path): ?string |
||
| 311 | { |
||
| 312 | if ('html' === pathinfo($path, PATHINFO_EXTENSION)) { |
||
| 313 | return 'page'; |
||
| 314 | } elseif ('file' === $filetype) { |
||
| 315 | return 'resource'; |
||
| 316 | } /*elseif ('folder' === $filetype) { |
||
| 317 | return 'folder'; |
||
| 318 | }*/ |
||
| 319 | |||
| 320 | return null; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Create the section.xml file. |
||
| 325 | */ |
||
| 326 | private function createSectionXml(array $sectionData, string $destinationDir): void |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Create the inforef.xml file for the section. |
||
| 345 | */ |
||
| 346 | private function createInforefXml(array $sectionData, string $destinationDir): void |
||
| 347 | { |
||
| 348 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 361 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.