prepareActivityDirectory($exportDir, 'feedback', $moduleId);
// Get survey data from Chamilo
$surveyData = $this->getData($activityId, $sectionId);
// Create XML files for the survey
$this->createFeedbackXml($surveyData, $feedbackDir);
$this->createModuleXml($surveyData, $feedbackDir);
$this->createInforefXml($surveyData, $feedbackDir);
$this->createCalendarXml($surveyData, $feedbackDir);
$this->createCommentsXml($surveyData, $feedbackDir);
$this->createCompetenciesXml($surveyData, $feedbackDir);
$this->createCompletionXml($surveyData, $feedbackDir);
$this->createFiltersXml($surveyData, $feedbackDir);
$this->createGradeHistoryXml($surveyData, $feedbackDir);
$this->createGradesXml($surveyData, $feedbackDir);
$this->createRolesXml($surveyData, $feedbackDir);
}
/**
* Get survey data including questions and answers from Chamilo.
*/
public function getData(int $surveyId, int $sectionId): array
{
$adminData = MoodleExport::getAdminUserData();
$adminId = $adminData['id'];
$survey = $this->course->resources['survey'][$surveyId];
$questions = [];
foreach ($this->course->resources['survey_question'] as $question) {
if ((int) $question->survey_id === $surveyId) {
// Debugging
$questions[] = [
'id' => $question->id,
'text' => $question->survey_question,
'type' => $question->survey_question_type,
'options' => array_map(function ($answer) {
return $answer['option_text'];
}, $question->answers),
'position' => $question->sort,
'label' => '', // Default empty label
];
}
}
return [
'id' => $surveyId,
'moduleid' => $surveyId,
'modulename' => 'feedback',
'contextid' => $this->course->info['real_id'],
'sectionid' => $sectionId,
'sectionnumber' => 0,
'name' => $survey->title,
'intro' => $survey->intro,
'timemodified' => time(),
'questions' => $questions,
'users' => [$adminId],
'files' => [],
];
}
/**
* Create the feedback.xml file for the Moodle feedback activity.
*/
private function createFeedbackXml(array $surveyData, string $feedbackDir): void
{
$xmlContent = ''.PHP_EOL;
$xmlContent .= ''.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' '.htmlspecialchars($surveyData['name']).''.PHP_EOL;
$xmlContent .= ' '.htmlspecialchars($surveyData['intro']).''.PHP_EOL;
$xmlContent .= ' 1'.PHP_EOL;
$xmlContent .= ' 1'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' 1'.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' 1'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' '.$surveyData['timemodified'].''.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
// Map Chamilo questions to Moodle Feedback format
foreach ($surveyData['questions'] as $question) {
$xmlContent .= $this->createQuestionXml($question);
}
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= '';
$this->createXmlFile('feedback', $xmlContent, $feedbackDir);
}
/**
* Create the XML for a single question in Moodle Feedback format.
*/
private function createQuestionXml(array $question): string
{
$name = htmlspecialchars(strip_tags($question['text']), ENT_XML1 | ENT_QUOTES, 'UTF-8');
$label = htmlspecialchars(strip_tags($question['label']), ENT_XML1 | ENT_QUOTES, 'UTF-8');
$presentation = $this->getPresentation($question);
$hasValue = ($question['type'] === 'pagebreak') ? '0' : '1';
$xmlContent = '- '.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' '.$name.''.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' '.$presentation.''.PHP_EOL;
$xmlContent .= ' '.$this->mapQuestionType($question['type']).''.PHP_EOL;
$xmlContent .= ' '.$hasValue.''.PHP_EOL;
$xmlContent .= ' '.$question['position'].''.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' 0'.PHP_EOL;
$xmlContent .= ' '.PHP_EOL;
$xmlContent .= ' h'.PHP_EOL;
$xmlContent .= '
'.PHP_EOL;
return $xmlContent;
}
/**
* Get presentation for different question types.
*/
private function getPresentation(array $question): string
{
$options = array_map('strip_tags', $question['options']);
$sanitizedOptions = array_map(function ($option) {
return htmlspecialchars($option, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}, $options);
switch ($question['type']) {
case 'yesno':
case 'multiplechoice':
case 'multiplechoiceother':
return 'r>>>>>'.implode(PHP_EOL.'|', $sanitizedOptions);
case 'multipleresponse':
return 'c>>>>>'.implode(PHP_EOL.'|', $sanitizedOptions);
case 'dropdown':
return 'd>>>>>'.implode(PHP_EOL.'|', $sanitizedOptions);
case 'open':
return '30|5'; // Textarea with rows and cols
default:
return '';
}
}
/**
* Map Chamilo question type to Moodle Feedback type.
*/
private function mapQuestionType(string $chamiloType): string
{
$typeMap = [
'yesno' => 'multichoice',
'multiplechoice' => 'multichoice',
'multipleresponse' => 'multichoice',
'dropdown' => 'multichoice',
'multiplechoiceother' => 'multichoice',
'open' => 'textarea',
'pagebreak' => 'pagebreak',
];
return $typeMap[$chamiloType] ?? 'unknown';
}
}