prepareActivityDirectory($exportDir, 'forum', $moduleId); // Retrieve forum data $forumData = $this->getData($activityId, $sectionId); // Generate XML files for the forum $this->createForumXml($forumData, $forumDir); $this->createModuleXml($forumData, $forumDir); $this->createGradesXml($forumData, $forumDir); $this->createGradeHistoryXml($forumData, $forumDir); $this->createInforefXml($forumData, $forumDir); $this->createRolesXml($forumData, $forumDir); $this->createCalendarXml($forumData, $forumDir); $this->createCommentsXml($forumData, $forumDir); $this->createCompetenciesXml($forumData, $forumDir); $this->createFiltersXml($forumData, $forumDir); } /** * Get all forum data from the course. */ public function getData(int $forumId, int $sectionId): ?array { $forum = $this->course->resources['forum'][$forumId]->obj; $adminData = MoodleExport::getAdminUserData(); $adminId = $adminData['id']; $threads = []; foreach ($this->course->resources['thread'] as $threadId => $thread) { if ($thread->obj->forum_id == $forumId) { // Get the posts for each thread $posts = []; foreach ($this->course->resources['post'] as $postId => $post) { if ($post->obj->thread_id == $threadId) { $posts[] = [ 'id' => $post->obj->post_id, 'userid' => $adminId, 'message' => $post->obj->post_text, 'created' => strtotime($post->obj->post_date), 'modified' => strtotime($post->obj->post_date), ]; } } $threads[] = [ 'id' => $thread->obj->thread_id, 'title' => $thread->obj->thread_title, 'userid' => $adminId, 'timemodified' => strtotime($thread->obj->thread_date), 'usermodified' => $adminId, 'posts' => $posts, ]; } } $fileIds = []; return [ 'id' => $forumId, 'moduleid' => $forumId, 'modulename' => 'forum', 'contextid' => $this->course->info['real_id'], 'name' => $forum->forum_title, 'description' => $forum->forum_comment, 'timecreated' => time(), 'timemodified' => time(), 'sectionid' => $sectionId, 'sectionnumber' => 1, 'userid' => $adminId, 'threads' => $threads, 'users' => [$adminId], 'files' => $fileIds, ]; } /** * Create the forum.xml file with all forum data. */ private function createForumXml(array $forumData, string $forumDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' general'.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($forumData['name']).''.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($forumData['description']).''.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 100'.PHP_EOL; $xmlContent .= ' 512000'.PHP_EOL; $xmlContent .= ' 9'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' '.$forumData['timemodified'].''.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; // Add forum threads $xmlContent .= ' '.PHP_EOL; foreach ($forumData['threads'] as $thread) { $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($thread['title']).''.PHP_EOL; $xmlContent .= ' '.$thread['firstpost'].''.PHP_EOL; $xmlContent .= ' '.$thread['userid'].''.PHP_EOL; $xmlContent .= ' -1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' '.$thread['timemodified'].''.PHP_EOL; $xmlContent .= ' '.$thread['usermodified'].''.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; // Add forum posts to the thread $xmlContent .= ' '.PHP_EOL; foreach ($thread['posts'] as $post) { $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.$post['parent'].''.PHP_EOL; $xmlContent .= ' '.$post['userid'].''.PHP_EOL; $xmlContent .= ' '.$post['created'].''.PHP_EOL; $xmlContent .= ' '.$post['modified'].''.PHP_EOL; $xmlContent .= ' '.$post['mailed'].''.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($post['subject']).''.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($post['message']).''.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; } $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.$thread['userid'].''.PHP_EOL; $xmlContent .= ' '.$thread['timemodified'].''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; } $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''; $this->createXmlFile('forum', $xmlContent, $forumDir); } }