| Total Complexity | 127 |
| Total Lines | 956 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MoodleExport 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 MoodleExport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class MoodleExport |
||
| 20 | { |
||
| 21 | private $course; |
||
| 22 | private static $adminUserData = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor to initialize the course object. |
||
| 26 | */ |
||
| 27 | public function __construct(object $course) |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Export the Moodle course in .mbz format. |
||
| 45 | */ |
||
| 46 | public function export(string $courseId, string $exportDir, int $version) |
||
| 47 | { |
||
| 48 | $tempDir = api_get_path(SYS_ARCHIVE_PATH).$exportDir; |
||
| 49 | |||
| 50 | if (!is_dir($tempDir)) { |
||
| 51 | if (!mkdir($tempDir, api_get_permissions_for_new_directories(), true)) { |
||
| 52 | throw new Exception(get_lang('ErrorCreatingDirectory')); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | $courseInfo = api_get_course_info($courseId); |
||
| 57 | if (!$courseInfo) { |
||
| 58 | throw new Exception(get_lang('CourseNotFound')); |
||
| 59 | } |
||
| 60 | |||
| 61 | // Generate the moodle_backup.xml |
||
| 62 | $this->createMoodleBackupXml($tempDir, $version); |
||
| 63 | |||
| 64 | // Get the activities from the course |
||
| 65 | $activities = $this->getActivities(); |
||
| 66 | |||
| 67 | // Export course-related files |
||
| 68 | $courseExport = new CourseExport($this->course, $activities); |
||
| 69 | $courseExport->exportCourse($tempDir); |
||
| 70 | |||
| 71 | // Export files-related data and actual files |
||
| 72 | $pageExport = new PageExport($this->course); |
||
| 73 | $pageFiles = []; |
||
| 74 | $pageData = $pageExport->getData(0, 1); |
||
| 75 | if (!empty($pageData['files'])) { |
||
| 76 | $pageFiles = $pageData['files']; |
||
| 77 | } |
||
| 78 | $fileExport = new FileExport($this->course); |
||
| 79 | $filesData = $fileExport->getFilesData(); |
||
| 80 | $filesData['files'] = array_merge($filesData['files'], $pageFiles); |
||
| 81 | $fileExport->exportFiles($filesData, $tempDir); |
||
| 82 | |||
| 83 | // Export sections of the course |
||
| 84 | $this->exportSections($tempDir); |
||
| 85 | |||
| 86 | // Export all root XML files |
||
| 87 | $this->exportRootXmlFiles($tempDir); |
||
| 88 | |||
| 89 | // Compress everything into a .mbz (ZIP) file |
||
| 90 | $exportedFile = $this->createMbzFile($tempDir); |
||
| 91 | |||
| 92 | // Clean up temporary directory |
||
| 93 | $this->cleanupTempDir($tempDir); |
||
| 94 | |||
| 95 | return $exportedFile; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Export questions data to XML file. |
||
| 100 | */ |
||
| 101 | public function exportQuestionsXml(array $questionsData, string $exportDir): void |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Sets the admin user data. |
||
| 141 | */ |
||
| 142 | public function setAdminUserData(int $id, string $username, string $email): void |
||
| 143 | { |
||
| 144 | self::$adminUserData = [ |
||
| 145 | 'id' => $id, |
||
| 146 | 'contextid' => $id, |
||
| 147 | 'username' => $username, |
||
| 148 | 'idnumber' => '', |
||
| 149 | 'email' => $email, |
||
| 150 | 'phone1' => '', |
||
| 151 | 'phone2' => '', |
||
| 152 | 'institution' => '', |
||
| 153 | 'department' => '', |
||
| 154 | 'address' => '', |
||
| 155 | 'city' => 'London', |
||
| 156 | 'country' => 'GB', |
||
| 157 | 'lastip' => '127.0.0.1', |
||
| 158 | 'picture' => '0', |
||
| 159 | 'description' => '', |
||
| 160 | 'descriptionformat' => 1, |
||
| 161 | 'imagealt' => '$@NULL@$', |
||
| 162 | 'auth' => 'manual', |
||
| 163 | 'firstname' => 'Admin', |
||
| 164 | 'lastname' => 'User', |
||
| 165 | 'confirmed' => 1, |
||
| 166 | 'policyagreed' => 0, |
||
| 167 | 'deleted' => 0, |
||
| 168 | 'lang' => 'en', |
||
| 169 | 'theme' => '', |
||
| 170 | 'timezone' => 99, |
||
| 171 | 'firstaccess' => time(), |
||
| 172 | 'lastaccess' => time() - (60 * 60 * 24 * 7), |
||
| 173 | 'lastlogin' => time() - (60 * 60 * 24 * 2), |
||
| 174 | 'currentlogin' => time(), |
||
| 175 | 'mailformat' => 1, |
||
| 176 | 'maildigest' => 0, |
||
| 177 | 'maildisplay' => 1, |
||
| 178 | 'autosubscribe' => 1, |
||
| 179 | 'trackforums' => 0, |
||
| 180 | 'timecreated' => time(), |
||
| 181 | 'timemodified' => time(), |
||
| 182 | 'trustbitmask' => 0, |
||
| 183 | 'preferences' => [ |
||
| 184 | ['name' => 'core_message_migrate_data', 'value' => 1], |
||
| 185 | ['name' => 'auth_manual_passwordupdatetime', 'value' => time()], |
||
| 186 | ['name' => 'email_bounce_count', 'value' => 1], |
||
| 187 | ['name' => 'email_send_count', 'value' => 1], |
||
| 188 | ['name' => 'login_failed_count_since_success', 'value' => 0], |
||
| 189 | ['name' => 'filepicker_recentrepository', 'value' => 5], |
||
| 190 | ['name' => 'filepicker_recentlicense', 'value' => 'unknown'], |
||
| 191 | ], |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns hardcoded data for the admin user. |
||
| 197 | */ |
||
| 198 | public static function getAdminUserData(): array |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Fills missing resources from the learnpath into the course structure. |
||
| 205 | * |
||
| 206 | * This method checks if the course has a learnpath and ensures that all |
||
| 207 | * referenced resources (documents, quizzes, etc.) exist in the course's |
||
| 208 | * resources array by pulling them from the complete course object. |
||
| 209 | */ |
||
| 210 | private function fillResourcesFromLearnpath(object $complete): void |
||
| 211 | { |
||
| 212 | // Check if the course has learnpath |
||
| 213 | if (!isset($this->course->resources['learnpath'])) { |
||
| 214 | return; |
||
| 215 | } |
||
| 216 | |||
| 217 | foreach ($this->course->resources['learnpath'] as $learnpathId => $learnpath) { |
||
| 218 | if (!isset($learnpath->items)) { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | foreach ($learnpath->items as $item) { |
||
| 223 | $type = $item['item_type']; // Resource type (document, quiz, etc.) |
||
| 224 | $resourceId = $item['path']; // Resource ID in resources |
||
| 225 | |||
| 226 | // Check if the resource exists in the complete object and is not yet in the course resources |
||
| 227 | if (isset($complete->resources[$type][$resourceId]) && !isset($this->course->resources[$type][$resourceId])) { |
||
| 228 | // Add the resource directly to the original course resources structure |
||
| 229 | $this->course->resources[$type][$resourceId] = $complete->resources[$type][$resourceId]; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Fills missing exercise questions related to quizzes in the course. |
||
| 237 | * |
||
| 238 | * This method checks if the course has quizzes and ensures that all referenced |
||
| 239 | * questions exist in the course's resources array by pulling them from the complete |
||
| 240 | * course object. |
||
| 241 | */ |
||
| 242 | private function fillQuestionsFromQuiz(object $complete): void |
||
| 243 | { |
||
| 244 | // Check if the course has quizzes |
||
| 245 | if (!isset($this->course->resources['quiz'])) { |
||
| 246 | return; |
||
| 247 | } |
||
| 248 | |||
| 249 | foreach ($this->course->resources['quiz'] as $quizId => $quiz) { |
||
| 250 | if (!isset($quiz->obj->question_ids)) { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | |||
| 254 | foreach ($quiz->obj->question_ids as $questionId) { |
||
| 255 | // Check if the question exists in the complete object and is not yet in the course resources |
||
| 256 | if (isset($complete->resources['Exercise_Question'][$questionId]) && !isset($this->course->resources['Exercise_Question'][$questionId])) { |
||
| 257 | // Add the question directly to the original course resources structure |
||
| 258 | $this->course->resources['Exercise_Question'][$questionId] = $complete->resources['Exercise_Question'][$questionId]; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Export root XML files such as badges, completion, gradebook, etc. |
||
| 266 | */ |
||
| 267 | private function exportRootXmlFiles(string $exportDir): void |
||
| 268 | { |
||
| 269 | $this->exportBadgesXml($exportDir); |
||
| 270 | $this->exportCompletionXml($exportDir); |
||
| 271 | $this->exportGradebookXml($exportDir); |
||
| 272 | $this->exportGradeHistoryXml($exportDir); |
||
| 273 | $this->exportGroupsXml($exportDir); |
||
| 274 | $this->exportOutcomesXml($exportDir); |
||
| 275 | |||
| 276 | // Export quizzes and their questions |
||
| 277 | $activities = $this->getActivities(); |
||
| 278 | $questionsData = []; |
||
| 279 | foreach ($activities as $activity) { |
||
| 280 | if ($activity['modulename'] === 'quiz') { |
||
| 281 | $quizExport = new QuizExport($this->course); |
||
| 282 | $quizData = $quizExport->getData($activity['id'], $activity['sectionid']); |
||
| 283 | $questionsData[] = $quizData; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | $this->exportQuestionsXml($questionsData, $exportDir); |
||
| 287 | |||
| 288 | $this->exportRolesXml($exportDir); |
||
| 289 | $this->exportScalesXml($exportDir); |
||
| 290 | $this->exportUsersXml($exportDir); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Create the moodle_backup.xml file with the required course details. |
||
| 295 | */ |
||
| 296 | private function createMoodleBackupXml(string $destinationDir, int $version): void |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get all sections from the course. |
||
| 411 | */ |
||
| 412 | private function getSections(): array |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Get all activities from the course. |
||
| 440 | */ |
||
| 441 | private function getActivities(): array |
||
| 442 | { |
||
| 443 | $activities = []; |
||
| 444 | $glossaryAdded = false; |
||
| 445 | |||
| 446 | $lpIndex = []; |
||
| 447 | if (!empty($this->course->resources[RESOURCE_LEARNPATH])) { |
||
| 448 | foreach ($this->course->resources[RESOURCE_LEARNPATH] as $lp) { |
||
| 449 | $sid = (int) $lp->source_id; |
||
| 450 | foreach ($lp->items ?? [] as $it) { |
||
| 451 | $type = $it['item_type']; |
||
| 452 | if ($type === 'student_publication') { $type = 'assign'; } |
||
| 453 | elseif ($type === 'link') { $type = 'url'; } |
||
| 454 | elseif ($type === 'survey') { $type = 'feedback'; } |
||
| 455 | elseif ($type === 'document') { $type = 'document'; } |
||
| 456 | |||
| 457 | $rid = $it['path']; |
||
| 458 | $title = $it['title'] ?? ''; |
||
| 459 | |||
| 460 | if (ctype_digit((string) $rid)) { |
||
| 461 | $lpIndex[$sid][$type]['id'][(int) $rid] = $title; |
||
| 462 | } else { |
||
| 463 | $lpIndex[$sid][$type]['path'][(string) $rid] = $title; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | $titleFromLp = function (int $sectionId, string $moduleName, int $resourceId, string $fallback) use ($lpIndex) { |
||
| 470 | $type = in_array($moduleName, ['page','resource'], true) ? 'document' : $moduleName; |
||
| 471 | |||
| 472 | if (isset($lpIndex[$sectionId][$type]['id'][$resourceId])) { |
||
| 473 | return $lpIndex[$sectionId][$type]['id'][$resourceId]; |
||
| 474 | } |
||
| 475 | |||
| 476 | if ($type === 'assign' && isset($lpIndex[$sectionId]['student_publication']['id'][$resourceId])) { |
||
| 477 | return $lpIndex[$sectionId]['student_publication']['id'][$resourceId]; |
||
| 478 | } |
||
| 479 | |||
| 480 | if ($type === 'document') { |
||
| 481 | $doc = \DocumentManager::get_document_data_by_id($resourceId, $this->course->code); |
||
| 482 | if (!empty($doc['path'])) { |
||
| 483 | $p = (string) $doc['path']; |
||
| 484 | foreach ([$p, 'document/'.$p, '/'.$p] as $cand) { |
||
| 485 | if (isset($lpIndex[$sectionId]['document']['path'][$cand])) { |
||
| 486 | return $lpIndex[$sectionId]['document']['path'][$cand]; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | return $fallback; |
||
| 493 | }; |
||
| 494 | |||
| 495 | $activities[] = [ |
||
| 496 | 'id' => ActivityExport::DOCS_MODULE_ID, |
||
| 497 | 'sectionid' => 0, |
||
| 498 | 'modulename'=> 'folder', |
||
| 499 | 'moduleid' => ActivityExport::DOCS_MODULE_ID, |
||
| 500 | 'title' => 'Documents', |
||
| 501 | ]; |
||
| 502 | |||
| 503 | $htmlPageIds = []; |
||
| 504 | foreach ($this->course->resources as $resourceType => $resources) { |
||
| 505 | foreach ($resources as $resource) { |
||
| 506 | $exportClass = null; |
||
| 507 | $moduleName = ''; |
||
| 508 | $title = ''; |
||
| 509 | $id = 0; |
||
| 510 | $sectionId = 0; |
||
| 511 | |||
| 512 | // QUIZ |
||
| 513 | if ($resourceType === RESOURCE_QUIZ && $resource->obj->iid > 0) { |
||
| 514 | $exportClass = QuizExport::class; |
||
| 515 | $moduleName = 'quiz'; |
||
| 516 | $id = (int) $resource->obj->iid; |
||
| 517 | $title = $resource->obj->title ?? ''; |
||
| 518 | $sectionId = (new QuizExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 519 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 520 | } |
||
| 521 | // URL |
||
| 522 | elseif ($resourceType === RESOURCE_LINK && $resource->source_id > 0) { |
||
| 523 | $exportClass = UrlExport::class; |
||
| 524 | $moduleName = 'url'; |
||
| 525 | $id = (int) $resource->source_id; |
||
| 526 | $title = $resource->title ?? ''; |
||
| 527 | $sectionId = (new UrlExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 528 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 529 | } |
||
| 530 | // GLOSSARY (uno solo) |
||
| 531 | elseif ($resourceType === RESOURCE_GLOSSARY && $resource->glossary_id > 0 && !$glossaryAdded) { |
||
| 532 | $exportClass = GlossaryExport::class; |
||
| 533 | $moduleName = 'glossary'; |
||
| 534 | $id = 1; |
||
| 535 | $title = get_lang('Glossary'); |
||
| 536 | $sectionId = 0; |
||
| 537 | $glossaryAdded = true; |
||
| 538 | } |
||
| 539 | // FORUM |
||
| 540 | elseif ($resourceType === RESOURCE_FORUM && $resource->source_id > 0) { |
||
| 541 | $exportClass = ForumExport::class; |
||
| 542 | $moduleName = 'forum'; |
||
| 543 | $id = (int) $resource->obj->iid; |
||
| 544 | $title = $resource->obj->forum_title ?? ''; |
||
| 545 | $sectionId = (new ForumExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 546 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 547 | } |
||
| 548 | // DOCUMENT |
||
| 549 | elseif ($resourceType === RESOURCE_DOCUMENT && $resource->source_id > 0) { |
||
| 550 | $document = \DocumentManager::get_document_data_by_id($resource->source_id, $this->course->code); |
||
| 551 | $ext = strtolower(pathinfo($document['path'], PATHINFO_EXTENSION)); |
||
| 552 | |||
| 553 | // HTML → page |
||
| 554 | if ($ext === 'html' || $ext === 'htm') { |
||
| 555 | $exportClass = PageExport::class; |
||
| 556 | $moduleName = 'page'; |
||
| 557 | $id = (int) $resource->source_id; |
||
| 558 | $title = $document['title'] ?? ''; |
||
| 559 | $sectionId = (new PageExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 560 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 561 | $htmlPageIds[] = $id; |
||
| 562 | } |
||
| 563 | |||
| 564 | if ($resource->file_type === 'file' && !in_array($resource->source_id, $htmlPageIds, true)) { |
||
| 565 | $resourceExport = new ResourceExport($this->course); |
||
| 566 | $sectionTmp = $resourceExport->getSectionIdForActivity((int) $resource->source_id, $resourceType); |
||
| 567 | if ($sectionTmp > 0) { |
||
| 568 | $exportClass = ResourceExport::class; |
||
| 569 | $moduleName = 'resource'; |
||
| 570 | $id = (int) $resource->source_id; |
||
| 571 | $title = $resource->title ?? ''; |
||
| 572 | $sectionId = $sectionTmp; |
||
| 573 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | } |
||
| 577 | // INTRO → page |
||
| 578 | elseif ($resourceType === RESOURCE_TOOL_INTRO && $resource->source_id == 'course_homepage') { |
||
| 579 | $exportClass = PageExport::class; |
||
| 580 | $moduleName = 'page'; |
||
| 581 | $id = 0; |
||
| 582 | $title = get_lang('Introduction'); |
||
| 583 | $sectionId = 0; |
||
| 584 | } |
||
| 585 | // ASSIGN |
||
| 586 | elseif ($resourceType === RESOURCE_WORK && $resource->source_id > 0) { |
||
| 587 | $exportClass = AssignExport::class; |
||
| 588 | $moduleName = 'assign'; |
||
| 589 | $id = (int) $resource->source_id; |
||
| 590 | $title = $resource->params['title'] ?? ''; |
||
| 591 | $sectionId = (new AssignExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 592 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 593 | } |
||
| 594 | // FEEDBACK / SURVEY |
||
| 595 | elseif ($resourceType === RESOURCE_SURVEY && $resource->source_id > 0) { |
||
| 596 | $exportClass = FeedbackExport::class; |
||
| 597 | $moduleName = 'feedback'; |
||
| 598 | $id = (int) $resource->source_id; |
||
| 599 | $title = $resource->params['title'] ?? ''; |
||
| 600 | $sectionId = (new FeedbackExport($this->course))->getSectionIdForActivity($id, $resourceType); |
||
| 601 | $title = $titleFromLp($sectionId, $moduleName, $id, $title); |
||
| 602 | } |
||
| 603 | |||
| 604 | if ($exportClass && $moduleName) { |
||
| 605 | $activities[] = [ |
||
| 606 | 'id' => $id, |
||
| 607 | 'sectionid' => $sectionId, |
||
| 608 | 'modulename'=> $moduleName, |
||
| 609 | 'moduleid' => $id, |
||
| 610 | 'title' => $title, |
||
| 611 | ]; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | return $activities; |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Export the sections of the course. |
||
| 621 | */ |
||
| 622 | private function exportSections(string $exportDir): void |
||
| 623 | { |
||
| 624 | $sections = $this->getSections(); |
||
| 625 | |||
| 626 | foreach ($sections as $section) { |
||
| 627 | $sectionExport = new SectionExport($this->course); |
||
| 628 | $sectionExport->exportSection($section['id'], $exportDir); |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Create a .mbz (ZIP) file from the exported data. |
||
| 634 | */ |
||
| 635 | private function createMbzFile(string $sourceDir): string |
||
| 636 | { |
||
| 637 | $zip = new ZipArchive(); |
||
| 638 | $zipFile = $sourceDir.'.mbz'; |
||
| 639 | |||
| 640 | if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { |
||
| 641 | throw new Exception(get_lang('ErrorCreatingZip')); |
||
| 642 | } |
||
| 643 | |||
| 644 | $files = new RecursiveIteratorIterator( |
||
| 645 | new RecursiveDirectoryIterator($sourceDir), |
||
| 646 | RecursiveIteratorIterator::LEAVES_ONLY |
||
| 647 | ); |
||
| 648 | |||
| 649 | foreach ($files as $file) { |
||
| 650 | if (!$file->isDir()) { |
||
| 651 | $filePath = $file->getRealPath(); |
||
| 652 | $relativePath = substr($filePath, strlen($sourceDir) + 1); |
||
| 653 | |||
| 654 | if (!$zip->addFile($filePath, $relativePath)) { |
||
| 655 | throw new Exception(get_lang('ErrorAddingFileToZip').": $relativePath"); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | if (!$zip->close()) { |
||
| 661 | throw new Exception(get_lang('ErrorClosingZip')); |
||
| 662 | } |
||
| 663 | |||
| 664 | return $zipFile; |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Clean up the temporary directory used for export. |
||
| 669 | */ |
||
| 670 | private function cleanupTempDir(string $dir): void |
||
| 671 | { |
||
| 672 | $this->recursiveDelete($dir); |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Recursively delete a directory and its contents. |
||
| 677 | */ |
||
| 678 | private function recursiveDelete(string $dir): void |
||
| 679 | { |
||
| 680 | $files = array_diff(scandir($dir), ['.', '..']); |
||
| 681 | foreach ($files as $file) { |
||
| 682 | $path = "$dir/$file"; |
||
| 683 | is_dir($path) ? $this->recursiveDelete($path) : unlink($path); |
||
| 684 | } |
||
| 685 | rmdir($dir); |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Export badges data to XML file. |
||
| 690 | */ |
||
| 691 | private function exportBadgesXml(string $exportDir): void |
||
| 692 | { |
||
| 693 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 694 | $xmlContent .= '<badges>'.PHP_EOL; |
||
| 695 | $xmlContent .= '</badges>'; |
||
| 696 | file_put_contents($exportDir.'/badges.xml', $xmlContent); |
||
| 697 | } |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Export course completion data to XML file. |
||
| 701 | */ |
||
| 702 | private function exportCompletionXml(string $exportDir): void |
||
| 703 | { |
||
| 704 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 705 | $xmlContent .= '<completions>'.PHP_EOL; |
||
| 706 | $xmlContent .= '</completions>'; |
||
| 707 | file_put_contents($exportDir.'/completion.xml', $xmlContent); |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Export gradebook data to XML file. |
||
| 712 | */ |
||
| 713 | private function exportGradebookXml(string $exportDir): void |
||
| 714 | { |
||
| 715 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 716 | $xmlContent .= '<gradebook>'.PHP_EOL; |
||
| 717 | $xmlContent .= '</gradebook>'; |
||
| 718 | file_put_contents($exportDir.'/gradebook.xml', $xmlContent); |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Export grade history data to XML file. |
||
| 723 | */ |
||
| 724 | private function exportGradeHistoryXml(string $exportDir): void |
||
| 725 | { |
||
| 726 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 727 | $xmlContent .= '<grade_history>'.PHP_EOL; |
||
| 728 | $xmlContent .= '</grade_history>'; |
||
| 729 | file_put_contents($exportDir.'/grade_history.xml', $xmlContent); |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Export groups data to XML file. |
||
| 734 | */ |
||
| 735 | private function exportGroupsXml(string $exportDir): void |
||
| 736 | { |
||
| 737 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 738 | $xmlContent .= '<groups>'.PHP_EOL; |
||
| 739 | $xmlContent .= '</groups>'; |
||
| 740 | file_put_contents($exportDir.'/groups.xml', $xmlContent); |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Export outcomes data to XML file. |
||
| 745 | */ |
||
| 746 | private function exportOutcomesXml(string $exportDir): void |
||
| 747 | { |
||
| 748 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 749 | $xmlContent .= '<outcomes>'.PHP_EOL; |
||
| 750 | $xmlContent .= '</outcomes>'; |
||
| 751 | file_put_contents($exportDir.'/outcomes.xml', $xmlContent); |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Export roles data to XML file. |
||
| 756 | */ |
||
| 757 | private function exportRolesXml(string $exportDir): void |
||
| 758 | { |
||
| 759 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 760 | $xmlContent .= '<roles_definition>'.PHP_EOL; |
||
| 761 | $xmlContent .= ' <role id="5">'.PHP_EOL; |
||
| 762 | $xmlContent .= ' <name></name>'.PHP_EOL; |
||
| 763 | $xmlContent .= ' <shortname>student</shortname>'.PHP_EOL; |
||
| 764 | $xmlContent .= ' <nameincourse>$@NULL@$</nameincourse>'.PHP_EOL; |
||
| 765 | $xmlContent .= ' <description></description>'.PHP_EOL; |
||
| 766 | $xmlContent .= ' <sortorder>5</sortorder>'.PHP_EOL; |
||
| 767 | $xmlContent .= ' <archetype>student</archetype>'.PHP_EOL; |
||
| 768 | $xmlContent .= ' </role>'.PHP_EOL; |
||
| 769 | $xmlContent .= '</roles_definition>'.PHP_EOL; |
||
| 770 | |||
| 771 | file_put_contents($exportDir.'/roles.xml', $xmlContent); |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Export scales data to XML file. |
||
| 776 | */ |
||
| 777 | private function exportScalesXml(string $exportDir): void |
||
| 778 | { |
||
| 779 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 780 | $xmlContent .= '<scales>'.PHP_EOL; |
||
| 781 | $xmlContent .= '</scales>'; |
||
| 782 | file_put_contents($exportDir.'/scales.xml', $xmlContent); |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Export the user XML with admin user data. |
||
| 787 | */ |
||
| 788 | private function exportUsersXml(string $exportDir): void |
||
| 789 | { |
||
| 790 | $adminData = self::getAdminUserData(); |
||
| 791 | |||
| 792 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
| 793 | $xmlContent .= '<users>'.PHP_EOL; |
||
| 794 | $xmlContent .= ' <user id="'.$adminData['id'].'" contextid="'.$adminData['contextid'].'">'.PHP_EOL; |
||
| 795 | $xmlContent .= ' <username>'.$adminData['username'].'</username>'.PHP_EOL; |
||
| 796 | $xmlContent .= ' <idnumber>'.$adminData['idnumber'].'</idnumber>'.PHP_EOL; |
||
| 797 | $xmlContent .= ' <email>'.$adminData['email'].'</email>'.PHP_EOL; |
||
| 798 | $xmlContent .= ' <phone1>'.$adminData['phone1'].'</phone1>'.PHP_EOL; |
||
| 799 | $xmlContent .= ' <phone2>'.$adminData['phone2'].'</phone2>'.PHP_EOL; |
||
| 800 | $xmlContent .= ' <institution>'.$adminData['institution'].'</institution>'.PHP_EOL; |
||
| 801 | $xmlContent .= ' <department>'.$adminData['department'].'</department>'.PHP_EOL; |
||
| 802 | $xmlContent .= ' <address>'.$adminData['address'].'</address>'.PHP_EOL; |
||
| 803 | $xmlContent .= ' <city>'.$adminData['city'].'</city>'.PHP_EOL; |
||
| 804 | $xmlContent .= ' <country>'.$adminData['country'].'</country>'.PHP_EOL; |
||
| 805 | $xmlContent .= ' <lastip>'.$adminData['lastip'].'</lastip>'.PHP_EOL; |
||
| 806 | $xmlContent .= ' <picture>'.$adminData['picture'].'</picture>'.PHP_EOL; |
||
| 807 | $xmlContent .= ' <description>'.$adminData['description'].'</description>'.PHP_EOL; |
||
| 808 | $xmlContent .= ' <descriptionformat>'.$adminData['descriptionformat'].'</descriptionformat>'.PHP_EOL; |
||
| 809 | $xmlContent .= ' <imagealt>'.$adminData['imagealt'].'</imagealt>'.PHP_EOL; |
||
| 810 | $xmlContent .= ' <auth>'.$adminData['auth'].'</auth>'.PHP_EOL; |
||
| 811 | $xmlContent .= ' <firstname>'.$adminData['firstname'].'</firstname>'.PHP_EOL; |
||
| 812 | $xmlContent .= ' <lastname>'.$adminData['lastname'].'</lastname>'.PHP_EOL; |
||
| 813 | $xmlContent .= ' <confirmed>'.$adminData['confirmed'].'</confirmed>'.PHP_EOL; |
||
| 814 | $xmlContent .= ' <policyagreed>'.$adminData['policyagreed'].'</policyagreed>'.PHP_EOL; |
||
| 815 | $xmlContent .= ' <deleted>'.$adminData['deleted'].'</deleted>'.PHP_EOL; |
||
| 816 | $xmlContent .= ' <lang>'.$adminData['lang'].'</lang>'.PHP_EOL; |
||
| 817 | $xmlContent .= ' <theme>'.$adminData['theme'].'</theme>'.PHP_EOL; |
||
| 818 | $xmlContent .= ' <timezone>'.$adminData['timezone'].'</timezone>'.PHP_EOL; |
||
| 819 | $xmlContent .= ' <firstaccess>'.$adminData['firstaccess'].'</firstaccess>'.PHP_EOL; |
||
| 820 | $xmlContent .= ' <lastaccess>'.$adminData['lastaccess'].'</lastaccess>'.PHP_EOL; |
||
| 821 | $xmlContent .= ' <lastlogin>'.$adminData['lastlogin'].'</lastlogin>'.PHP_EOL; |
||
| 822 | $xmlContent .= ' <currentlogin>'.$adminData['currentlogin'].'</currentlogin>'.PHP_EOL; |
||
| 823 | $xmlContent .= ' <mailformat>'.$adminData['mailformat'].'</mailformat>'.PHP_EOL; |
||
| 824 | $xmlContent .= ' <maildigest>'.$adminData['maildigest'].'</maildigest>'.PHP_EOL; |
||
| 825 | $xmlContent .= ' <maildisplay>'.$adminData['maildisplay'].'</maildisplay>'.PHP_EOL; |
||
| 826 | $xmlContent .= ' <autosubscribe>'.$adminData['autosubscribe'].'</autosubscribe>'.PHP_EOL; |
||
| 827 | $xmlContent .= ' <trackforums>'.$adminData['trackforums'].'</trackforums>'.PHP_EOL; |
||
| 828 | $xmlContent .= ' <timecreated>'.$adminData['timecreated'].'</timecreated>'.PHP_EOL; |
||
| 829 | $xmlContent .= ' <timemodified>'.$adminData['timemodified'].'</timemodified>'.PHP_EOL; |
||
| 830 | $xmlContent .= ' <trustbitmask>'.$adminData['trustbitmask'].'</trustbitmask>'.PHP_EOL; |
||
| 831 | |||
| 832 | // Preferences |
||
| 833 | if (isset($adminData['preferences']) && is_array($adminData['preferences'])) { |
||
| 834 | $xmlContent .= ' <preferences>'.PHP_EOL; |
||
| 835 | foreach ($adminData['preferences'] as $preference) { |
||
| 836 | $xmlContent .= ' <preference>'.PHP_EOL; |
||
| 837 | $xmlContent .= ' <name>'.htmlspecialchars($preference['name']).'</name>'.PHP_EOL; |
||
| 838 | $xmlContent .= ' <value>'.htmlspecialchars($preference['value']).'</value>'.PHP_EOL; |
||
| 839 | $xmlContent .= ' </preference>'.PHP_EOL; |
||
| 840 | } |
||
| 841 | $xmlContent .= ' </preferences>'.PHP_EOL; |
||
| 842 | } else { |
||
| 843 | $xmlContent .= ' <preferences></preferences>'.PHP_EOL; |
||
| 844 | } |
||
| 845 | |||
| 846 | // Roles (empty for now) |
||
| 847 | $xmlContent .= ' <roles>'.PHP_EOL; |
||
| 848 | $xmlContent .= ' <role_overrides></role_overrides>'.PHP_EOL; |
||
| 849 | $xmlContent .= ' <role_assignments></role_assignments>'.PHP_EOL; |
||
| 850 | $xmlContent .= ' </roles>'.PHP_EOL; |
||
| 851 | |||
| 852 | $xmlContent .= ' </user>'.PHP_EOL; |
||
| 853 | $xmlContent .= '</users>'; |
||
| 854 | |||
| 855 | // Save the content to the users.xml file |
||
| 856 | file_put_contents($exportDir.'/users.xml', $xmlContent); |
||
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Export the backup settings, including dynamic settings for sections and activities. |
||
| 861 | */ |
||
| 862 | private function exportBackupSettings(array $sections, array $activities): array |
||
| 863 | { |
||
| 864 | // root-level settings |
||
| 865 | $settings = [ |
||
| 866 | ['level' => 'root', 'name' => 'filename', 'value' => 'backup-moodle-course-'.time().'.mbz'], |
||
| 867 | ['level' => 'root', 'name' => 'imscc11', 'value' => '0'], |
||
| 868 | ['level' => 'root', 'name' => 'users', 'value' => '1'], |
||
| 869 | ['level' => 'root', 'name' => 'anonymize', 'value' => '0'], |
||
| 870 | ['level' => 'root', 'name' => 'role_assignments', 'value' => '1'], |
||
| 871 | ['level' => 'root', 'name' => 'activities', 'value' => '1'], |
||
| 872 | ['level' => 'root', 'name' => 'blocks', 'value' => '1'], |
||
| 873 | ['level' => 'root', 'name' => 'files', 'value' => '1'], |
||
| 874 | ['level' => 'root', 'name' => 'filters', 'value' => '1'], |
||
| 875 | ['level' => 'root', 'name' => 'comments', 'value' => '1'], |
||
| 876 | ['level' => 'root', 'name' => 'badges', 'value' => '1'], |
||
| 877 | ['level' => 'root', 'name' => 'calendarevents', 'value' => '1'], |
||
| 878 | ['level' => 'root', 'name' => 'userscompletion', 'value' => '1'], |
||
| 879 | ['level' => 'root', 'name' => 'logs', 'value' => '0'], |
||
| 880 | ['level' => 'root', 'name' => 'grade_histories', 'value' => '0'], |
||
| 881 | ['level' => 'root', 'name' => 'questionbank', 'value' => '1'], |
||
| 882 | ['level' => 'root', 'name' => 'groups', 'value' => '1'], |
||
| 883 | ['level' => 'root', 'name' => 'competencies', 'value' => '0'], |
||
| 884 | ['level' => 'root', 'name' => 'customfield', 'value' => '1'], |
||
| 885 | ['level' => 'root', 'name' => 'contentbankcontent', 'value' => '1'], |
||
| 886 | ['level' => 'root', 'name' => 'legacyfiles', 'value' => '1'], |
||
| 887 | ]; |
||
| 888 | |||
| 889 | // section-level settings |
||
| 890 | foreach ($sections as $section) { |
||
| 891 | $settings[] = [ |
||
| 892 | 'level' => 'section', |
||
| 893 | 'section' => 'section_'.$section['id'], |
||
| 894 | 'name' => 'section_'.$section['id'].'_included', |
||
| 895 | 'value' => '1', |
||
| 896 | ]; |
||
| 897 | $settings[] = [ |
||
| 898 | 'level' => 'section', |
||
| 899 | 'section' => 'section_'.$section['id'], |
||
| 900 | 'name' => 'section_'.$section['id'].'_userinfo', |
||
| 901 | 'value' => '1', |
||
| 902 | ]; |
||
| 903 | } |
||
| 904 | |||
| 905 | // activity-level settings |
||
| 906 | foreach ($activities as $activity) { |
||
| 907 | $settings[] = [ |
||
| 908 | 'level' => 'activity', |
||
| 909 | 'activity' => $activity['modulename'].'_'.$activity['moduleid'], |
||
| 910 | 'name' => $activity['modulename'].'_'.$activity['moduleid'].'_included', |
||
| 911 | 'value' => '1', |
||
| 912 | ]; |
||
| 913 | $settings[] = [ |
||
| 914 | 'level' => 'activity', |
||
| 915 | 'activity' => $activity['modulename'].'_'.$activity['moduleid'], |
||
| 916 | 'name' => $activity['modulename'].'_'.$activity['moduleid'].'_userinfo', |
||
| 917 | 'value' => '1', |
||
| 918 | ]; |
||
| 919 | } |
||
| 920 | |||
| 921 | return $settings; |
||
| 922 | } |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Maps module name to item_type of c_lp_item. |
||
| 926 | * (c_lp_item.item_type: document, quiz, link, forum, student_publication, survey) |
||
| 927 | */ |
||
| 928 | private function mapToLpItemType(string $moduleOrItemType): string |
||
| 929 | { |
||
| 930 | switch ($moduleOrItemType) { |
||
| 931 | case 'page': |
||
| 932 | case 'resource': |
||
| 933 | return 'document'; |
||
| 934 | case 'assign': |
||
| 935 | return 'student_publication'; |
||
| 936 | case 'url': |
||
| 937 | return 'link'; |
||
| 938 | case 'feedback': |
||
| 939 | return 'survey'; |
||
| 940 | default: |
||
| 941 | return $moduleOrItemType; // quiz, forum... |
||
| 942 | } |
||
| 943 | } |
||
| 944 | |||
| 945 | /** Index titles by section + type + id from the LP items (c_lp_item.title). */ |
||
| 946 | private function buildLpTitleIndex(): array |
||
| 964 | } |
||
| 965 | |||
| 966 | /** Returns the LP title if it exists; otherwise, use the fallback. */ |
||
| 967 | private function titleFromLp(array $idx, int $sectionId, string $moduleName, int $resourceId, string $fallback): string |
||
| 968 | { |
||
| 969 | if ($sectionId <= 0) { |
||
| 970 | return $fallback; |
||
| 971 | } |
||
| 975 | } |
||
| 976 | } |
||
| 977 |