From 1b8df8d25aacb93580bb281696fea21a0efda79a Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 20 Oct 2021 15:13:30 -0500 Subject: [PATCH] Course backup: Fix images inside quizzes not working from import course - refs BT#19212 --- main/coursecopy/create_backup.php | 4 + .../Component/CourseCopy/CourseBuilder.php | 154 ++++++++++++++++++ .../Component/CourseCopy/CourseSelectForm.php | 4 +- 3 files changed, 160 insertions(+), 2 deletions(-) diff --git a/main/coursecopy/create_backup.php b/main/coursecopy/create_backup.php index c0dbcdae3c..e03777b5db 100755 --- a/main/coursecopy/create_backup.php +++ b/main/coursecopy/create_backup.php @@ -55,6 +55,10 @@ if (Security::check_token('post') && $cb = new CourseBuilder('complete'); $course = $cb->build(); } + // It builds the documents and items related to the LP + $cb->exportToCourseBuildFormat(); + // It builds documents added in text (quizzes, assignments) + $cb->restoreDocumentsFromList(); $zipFile = CourseArchiver::createBackup($course); echo Display::return_message(get_lang('BackupCreated'), 'confirm'); echo '
'; diff --git a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseBuilder.php b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseBuilder.php index c9603bf14b..7da0cb9a2c 100644 --- a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseBuilder.php +++ b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseBuilder.php @@ -103,6 +103,7 @@ class CourseBuilder to be added in the course obj (only works with LPs) */ public $specific_id_list = []; public $documentsAddedInText = []; + public $itemListToAdd = []; /** * Create a new CourseBuilder. @@ -1479,6 +1480,7 @@ class CourseBuilder $item['launch_data'] = $obj_item->launch_data; $item['audio'] = $obj_item->audio; $items[] = $item; + $this->itemListToAdd[$obj_item->item_type][] = $obj_item->path; } $sql = "SELECT id FROM $table_tool @@ -1565,6 +1567,157 @@ class CourseBuilder } } + /** + * It builds the resources used in a LP , also it adds the documents related. + */ + public function exportToCourseBuildFormat() + { + + if (empty($this->itemListToAdd)) { + return false; + } + $itemList = $this->itemListToAdd; + $courseId = api_get_course_int_id(); + $sessionId = api_get_session_id(); + $courseInfo = api_get_course_info_by_id($courseId); + if (isset($itemList['document'])) { + // Get parents + foreach ($itemList['document'] as $documentId) { + $documentInfo = \DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], true); + if (!empty($documentInfo['parents'])) { + foreach ($documentInfo['parents'] as $parentInfo) { + if (in_array($parentInfo['iid'], $itemList['document'])) { + continue; + } + $itemList['document'][] = $parentInfo['iid']; + } + } + } + + foreach ($itemList['document'] as $documentId) { + $documentInfo = \DocumentManager::get_document_data_by_id($documentId, $courseInfo['code']); + $items = \DocumentManager::get_resources_from_source_html( + $documentInfo['absolute_path'], + true, + TOOL_DOCUMENT + ); + + if (!empty($items)) { + foreach ($items as $item) { + // Get information about source url + $url = $item[0]; // url + $scope = $item[1]; // scope (local, remote) + $type = $item[2]; // type (rel, abs, url) + + $origParseUrl = parse_url($url); + $realOrigPath = isset($origParseUrl['path']) ? $origParseUrl['path'] : null; + + if ($scope == 'local') { + if ($type == 'abs' || $type == 'rel') { + $documentFile = strstr($realOrigPath, 'document'); + $documentFile = (string) $documentFile; + $realOrigPath = (string) $realOrigPath; + if (!empty($documentFile) && false !== strpos($realOrigPath, $documentFile)) { + $documentFile = str_replace('document', '', $documentFile); + $itemDocumentId = \DocumentManager::get_document_id($courseInfo, $documentFile); + // Document found! Add it to the list + if ($itemDocumentId) { + $itemList['document'][] = $itemDocumentId; + } + } + } + } + } + } + } + + $this->build_documents( + $sessionId, + $courseId, + true, + $itemList['document'] + ); + } + + if (isset($itemList['quiz'])) { + $this->build_quizzes( + $sessionId, + $courseId, + true, + $itemList['quiz'] + ); + } + + require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; + + if (!empty($itemList['thread'])) { + $threadList = []; + $em = Database::getManager(); + $repo = $em->getRepository('ChamiloCourseBundle:CForumThread'); + foreach ($itemList['thread'] as $threadId) { + /** @var \Chamilo\CourseBundle\Entity\CForumThread $thread */ + $thread = $repo->find($threadId); + if ($thread) { + $itemList['forum'][] = $thread->getForumId(); + $threadList[] = $thread->getIid(); + } + } + + if (!empty($threadList)) { + $this->build_forum_topics( + $sessionId, + $courseId, + null, + $threadList + ); + } + } + + $forumCategoryList = []; + if (isset($itemList['forum'])) { + foreach ($itemList['forum'] as $forumId) { + $forumInfo = get_forums($forumId); + $forumCategoryList[] = $forumInfo['forum_category']; + } + } + + if (!empty($forumCategoryList)) { + $this->build_forum_category( + $sessionId, + $courseId, + true, + $forumCategoryList + ); + } + + if (!empty($itemList['forum'])) { + $this->build_forums( + $sessionId, + $courseId, + true, + $itemList['forum'] + ); + } + + if (isset($itemList['link'])) { + $this->build_links( + $sessionId, + $courseId, + true, + $itemList['link'] + ); + } + + if (!empty($itemList['student_publication'])) { + $this->build_works( + $sessionId, + $courseId, + true, + $itemList['student_publication'] + ); + } + } + /** * Build the glossaries. * @@ -1869,6 +2022,7 @@ class CourseBuilder $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { $obj = new Work($row); + $this->findAndSetDocumentsInText($row['description']); $this->course->add_resource($obj); } } diff --git a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php index beb682dc00..52de20b7b7 100644 --- a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php +++ b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php @@ -115,7 +115,7 @@ class CourseSelectForm var name = d.elements[i].attributes.getNamedItem('name').nodeValue; if( name.indexOf('learnpath') > 0 || name.indexOf('quiz') > 0){ if(d.elements[i].checked){ - setCheckbox('document',true); + //setCheckbox('document',true); alert(message); break; } @@ -749,7 +749,7 @@ class CourseSelectForm var name = d.elements[i].attributes.getNamedItem('name').nodeValue; if( name.indexOf('learnpath') > 0 || name.indexOf('quiz') > 0){ if(d.elements[i].checked){ - setCheckbox('document',true); + //setCheckbox('document',true); alert(message); break; }