| Total Complexity | 69 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 | private $activitiesBySection = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor to initialize the course object. |
||
| 22 | * |
||
| 23 | * @param object $course The course object to be exported. |
||
| 24 | */ |
||
| 25 | public function __construct($course, array $activitiesBySection = []) |
||
| 26 | { |
||
| 27 | $this->course = $course; |
||
| 28 | $this->activitiesBySection = $activitiesBySection; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Export a section and its activities to the specified directory. |
||
| 33 | */ |
||
| 34 | public function exportSection(int $sectionId, string $exportDir): void |
||
| 35 | { |
||
| 36 | $sectionDir = $exportDir."/sections/section_{$sectionId}"; |
||
| 37 | |||
| 38 | if (!is_dir($sectionDir)) { |
||
| 39 | mkdir($sectionDir, api_get_permissions_for_new_directories(), true); |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($sectionId > 0) { |
||
| 43 | $learnpath = $this->getLearnpathById($sectionId); |
||
| 44 | if ($learnpath === null) { |
||
| 45 | throw new Exception("Learnpath with ID $sectionId not found."); |
||
| 46 | } |
||
| 47 | $sectionData = $this->getSectionData($learnpath); |
||
| 48 | } else { |
||
| 49 | $sectionData = [ |
||
| 50 | 'id' => 0, |
||
| 51 | 'number' => 0, |
||
| 52 | 'name' => get_lang('General'), |
||
| 53 | 'summary' => get_lang('GeneralResourcesCourse'), |
||
| 54 | 'sequence' => 0, |
||
| 55 | 'visible' => 1, |
||
| 56 | 'timemodified' => time(), |
||
| 57 | 'activities' => $this->getActivitiesForGeneral(), |
||
| 58 | ]; |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->createSectionXml($sectionData, $sectionDir); |
||
| 62 | $this->createInforefXml($sectionData, $sectionDir); |
||
| 63 | $this->exportActivities($sectionData['activities'], $exportDir, $sectionId); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get all general items not linked to any lesson (learnpath). |
||
| 68 | */ |
||
| 69 | public function getGeneralItems(): array |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the activities for the general section. |
||
| 108 | */ |
||
| 109 | public function getActivitiesForGeneral(): array |
||
| 110 | { |
||
| 111 | // Preferred path: reuse precomputed activities from MoodleExport |
||
| 112 | if (isset($this->activitiesBySection[0]) && is_array($this->activitiesBySection[0])) { |
||
| 113 | return $this->activitiesBySection[0]; |
||
| 114 | } |
||
| 115 | |||
| 116 | // Fallback to legacy behavior (kept for safety) |
||
| 117 | $generalLearnpath = (object) [ |
||
| 118 | 'items' => $this->getGeneralItems(), |
||
| 119 | 'source_id' => 0, |
||
| 120 | ]; |
||
| 121 | |||
| 122 | $activities = $this->getActivitiesForSection($generalLearnpath, true); |
||
| 123 | |||
| 124 | if (!in_array('folder', array_column($activities, 'modulename'), true)) { |
||
| 125 | $activities[] = [ |
||
| 126 | 'id' => 0, |
||
| 127 | 'moduleid' => 0, |
||
| 128 | 'modulename' => 'folder', |
||
| 129 | 'name' => 'Documents', |
||
| 130 | 'sectionid' => 0, |
||
| 131 | ]; |
||
| 132 | } |
||
| 133 | |||
| 134 | return $activities; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the learnpath object by its ID. |
||
| 139 | */ |
||
| 140 | public function getLearnpathById(int $sectionId): ?object |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get section data for a learnpath. |
||
| 153 | */ |
||
| 154 | public function getSectionData(object $learnpath): array |
||
| 155 | { |
||
| 156 | $sectionId = (int) $learnpath->source_id; |
||
| 157 | |||
| 158 | return [ |
||
| 159 | 'id' => $sectionId, |
||
| 160 | 'number' => $learnpath->display_order, |
||
| 161 | 'name' => $learnpath->name, |
||
| 162 | 'summary' => $learnpath->description, |
||
| 163 | 'sequence' => $sectionId, |
||
| 164 | 'visible' => $learnpath->visibility, |
||
| 165 | 'timemodified' => strtotime($learnpath->modified_on), |
||
| 166 | 'activities' => $this->getActivitiesForSection($learnpath), |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the activities for a specific section. |
||
| 172 | */ |
||
| 173 | public function getActivitiesForSection(object $learnpath, bool $isGeneral = false): array |
||
| 174 | { |
||
| 175 | $sectionId = $isGeneral ? 0 : (int) $learnpath->source_id; |
||
| 176 | |||
| 177 | // Preferred path: reuse precomputed activities from MoodleExport |
||
| 178 | if (isset($this->activitiesBySection[$sectionId]) && is_array($this->activitiesBySection[$sectionId])) { |
||
| 179 | return $this->activitiesBySection[$sectionId]; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Fallback to legacy behavior |
||
| 183 | $activities = []; |
||
| 184 | foreach ($learnpath->items as $item) { |
||
| 185 | $this->addActivityToList($item, $sectionId, $activities); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $activities; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Export the activities of a section. |
||
| 193 | */ |
||
| 194 | private function exportActivities(array $activities, string $exportDir, int $sectionId): void |
||
| 195 | { |
||
| 196 | $exportClasses = [ |
||
| 197 | 'quiz' => QuizExport::class, |
||
| 198 | 'glossary' => GlossaryExport::class, |
||
| 199 | 'url' => UrlExport::class, |
||
| 200 | 'assign' => AssignExport::class, |
||
| 201 | 'forum' => ForumExport::class, |
||
| 202 | 'page' => PageExport::class, |
||
| 203 | 'resource' => ResourceExport::class, |
||
| 204 | 'folder' => FolderExport::class, |
||
| 205 | 'feedback' => FeedbackExport::class, |
||
| 206 | ]; |
||
| 207 | |||
| 208 | foreach ($activities as $activity) { |
||
| 209 | $moduleName = $activity['modulename']; |
||
| 210 | if (isset($exportClasses[$moduleName])) { |
||
| 211 | $exportClass = new $exportClasses[$moduleName]($this->course); |
||
| 212 | $exportClass->export($activity['id'], $exportDir, $activity['moduleid'], $sectionId); |
||
| 213 | } else { |
||
| 214 | throw new \Exception("Export for module '$moduleName' is not supported."); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Check if an item is associated with any learnpath. |
||
| 221 | */ |
||
| 222 | private function isItemInLearnpath(object $item, string $type): bool |
||
| 223 | { |
||
| 224 | if (!empty($this->course->resources[RESOURCE_LEARNPATH])) { |
||
| 225 | foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) { |
||
| 226 | if (!empty($learnpath->items)) { |
||
| 227 | foreach ($learnpath->items as $learnpathItem) { |
||
| 228 | if ($learnpathItem['item_type'] === $type && $learnpathItem['path'] == $item->source_id) { |
||
| 229 | return true; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | return false; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add an activity to the activities list. |
||
| 241 | * Generic approach: for any activity in a real section (LP sectionId > 0), |
||
| 242 | * force a unique moduleid per LP item occurrence: |
||
| 243 | * moduleid = 900000000 + lp_item_id |
||
| 244 | */ |
||
| 245 | private function addActivityToList(array $item, int $sectionId, array &$activities): void |
||
| 332 | ]; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Determine the document type based on filetype and path. |
||
| 338 | */ |
||
| 339 | private function getDocumentType(string $filetype, string $path): ?string |
||
| 340 | { |
||
| 341 | if ('html' === pathinfo($path, PATHINFO_EXTENSION)) { |
||
| 342 | return 'page'; |
||
| 343 | } elseif ('file' === $filetype) { |
||
| 344 | return 'resource'; |
||
| 345 | } /*elseif ('folder' === $filetype) { |
||
| 346 | return 'folder'; |
||
| 347 | }*/ |
||
| 348 | |||
| 349 | return null; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Create the section.xml file. |
||
| 354 | */ |
||
| 355 | private function createSectionXml(array $sectionData, string $destinationDir): void |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Create the inforef.xml file for the section. |
||
| 374 | */ |
||
| 375 | private function createInforefXml(array $sectionData, string $destinationDir): void |
||
| 376 | { |
||
| 377 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 391 |
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.