course = $course; } /** * Abstract method for exporting the activity. * Must be implemented by child classes. */ abstract public function export($activityId, $exportDir, $moduleId, $sectionId); /** * Get the section ID for a given activity ID. */ public function getSectionIdForActivity(int $activityId, string $itemType): int { foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) { foreach ($learnpath->items as $item) { if ($item['item_type'] == $itemType && $item['path'] == $activityId) { return $learnpath->source_id; } } } return 0; } /** * Prepares the directory for the activity. */ protected function prepareActivityDirectory(string $exportDir, string $activityType, int $moduleId): string { $activityDir = "{$exportDir}/activities/{$activityType}_{$moduleId}"; if (!is_dir($activityDir)) { mkdir($activityDir, 0777, true); } return $activityDir; } /** * Creates a generic XML file. */ protected function createXmlFile(string $fileName, string $xmlContent, string $directory): void { $filePath = $directory.'/'.$fileName.'.xml'; if (file_put_contents($filePath, $xmlContent) === false) { throw new Exception("Error creating {$fileName}.xml"); } } /** * Creates the module.xml file. */ protected function createModuleXml(array $data, string $directory): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.$data['modulename'].''.PHP_EOL; $xmlContent .= ' '.$data['sectionid'].''.PHP_EOL; $xmlContent .= ' '.$data['sectionnumber'].''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.time().''.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' $@NULL@$'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' $@NULL@$'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('module', $xmlContent, $directory); } /** * Creates the grades.xml file. */ protected function createGradesXml(array $data, string $directory): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('grades', $xmlContent, $directory); } /** * Creates the inforef.xml file, referencing users and files associated with the activity. */ protected function createInforefXml(array $references, string $directory): void { // Start the XML content $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; // Add user references if provided if (isset($references['users']) && is_array($references['users'])) { $xmlContent .= ' '.PHP_EOL; foreach ($references['users'] as $userId) { $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($userId).''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; } $xmlContent .= ' '.PHP_EOL; } // Add file references if provided if (isset($references['files']) && is_array($references['files'])) { $xmlContent .= ' '.PHP_EOL; foreach ($references['files'] as $file) { $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($file['id']).''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; } $xmlContent .= ' '.PHP_EOL; } $xmlContent .= ''.PHP_EOL; $this->createXmlFile('inforef', $xmlContent, $directory); } /** * Creates the roles.xml file. */ protected function createRolesXml(array $activityData, string $directory): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('roles', $xmlContent, $directory); } /** * Creates the filters.xml file for the activity. */ protected function createFiltersXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('filters', $xmlContent, $destinationDir); } /** * Creates the grade_history.xml file for the activity. */ protected function createGradeHistoryXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('grade_history', $xmlContent, $destinationDir); } /** * Creates the completion.xml file. */ protected function createCompletionXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('completion', $xmlContent, $destinationDir); } /** * Creates the comments.xml file. */ protected function createCommentsXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' This is a sample comment'.PHP_EOL; $xmlContent .= ' Professor'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('comments', $xmlContent, $destinationDir); } /** * Creates the competencies.xml file. */ protected function createCompetenciesXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' Sample Competency'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('competencies', $xmlContent, $destinationDir); } /** * Creates the calendar.xml file. */ protected function createCalendarXml(array $activityData, string $destinationDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' Due Date'.PHP_EOL; $xmlContent .= ' '.time().''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''.PHP_EOL; $this->createXmlFile('calendar', $xmlContent, $destinationDir); } }