|
|
|
@ -1,4 +1,5 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
/* For licensing terms, see /license.txt */ |
|
|
|
/* For licensing terms, see /license.txt */ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -6,8 +7,6 @@ |
|
|
|
* |
|
|
|
* |
|
|
|
* @author José Loguercio <jose.loguercio@beeznest.com>, |
|
|
|
* @author José Loguercio <jose.loguercio@beeznest.com>, |
|
|
|
* @author Julio Montoya <gugli100@gmail.com> |
|
|
|
* @author Julio Montoya <gugli100@gmail.com> |
|
|
|
* |
|
|
|
|
|
|
|
* @package chamilo.library |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class MoodleImport |
|
|
|
class MoodleImport |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -22,6 +21,7 @@ class MoodleImport |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function import($uploadedFile) |
|
|
|
public function import($uploadedFile) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
$debug = false; |
|
|
|
if (UPLOAD_ERR_OK !== $uploadedFile['error']) { |
|
|
|
if (UPLOAD_ERR_OK !== $uploadedFile['error']) { |
|
|
|
throw new Exception(get_lang('UploadError')); |
|
|
|
throw new Exception(get_lang('UploadError')); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -37,19 +37,18 @@ class MoodleImport |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$filePath = $cachePath.$name; |
|
|
|
$filePath = $cachePath.$name; |
|
|
|
|
|
|
|
|
|
|
|
if (!is_readable($filePath)) { |
|
|
|
if (!is_readable($filePath)) { |
|
|
|
throw new Exception(get_lang('UploadError')); |
|
|
|
throw new Exception(get_lang('UploadError')); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$mimeType = mime_content_type($filePath); |
|
|
|
$mimeType = mime_content_type($filePath); |
|
|
|
|
|
|
|
|
|
|
|
$folder = api_get_unique_id(); |
|
|
|
$folder = api_get_unique_id(); |
|
|
|
$destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder; |
|
|
|
$destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder; |
|
|
|
|
|
|
|
|
|
|
|
mkdir($destinationDir, api_get_permissions_for_new_directories(), true); |
|
|
|
mkdir($destinationDir, api_get_permissions_for_new_directories(), true); |
|
|
|
|
|
|
|
|
|
|
|
switch ($mimeType) { |
|
|
|
switch ($mimeType) { |
|
|
|
|
|
|
|
case 'application/gzip': |
|
|
|
case 'application/x-gzip': |
|
|
|
case 'application/x-gzip': |
|
|
|
$backUpFile = new PharData($filePath); |
|
|
|
$backUpFile = new PharData($filePath); |
|
|
|
|
|
|
|
|
|
|
|
@ -69,7 +68,7 @@ class MoodleImport |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($packageContent)) { |
|
|
|
if (!empty($packageContent)) { |
|
|
|
foreach ($packageContent as $index => $value) { |
|
|
|
foreach ($packageContent as $index => $value) { |
|
|
|
if ($value['filename'] == 'moodle_backup.xml') { |
|
|
|
if ($value['filename'] === 'moodle_backup.xml') { |
|
|
|
$mainFileKey = $index; |
|
|
|
$mainFileKey = $index; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -107,6 +106,13 @@ class MoodleImport |
|
|
|
$mainFileModuleValues = $this->getAllQuestionFiles($filesXml); |
|
|
|
$mainFileModuleValues = $this->getAllQuestionFiles($filesXml); |
|
|
|
$currentResourceFilePath = $destinationDir.'/files/'; |
|
|
|
$currentResourceFilePath = $destinationDir.'/files/'; |
|
|
|
$importedFiles = []; |
|
|
|
$importedFiles = []; |
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('loading files'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$_POST['moodle_import'] = true; |
|
|
|
|
|
|
|
$_POST['language'] = $courseInfo['language']; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($mainFileModuleValues as $fileInfo) { |
|
|
|
foreach ($mainFileModuleValues as $fileInfo) { |
|
|
|
$dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
|
|
|
$dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
|
|
|
foreach (new RecursiveIteratorIterator($dirs) as $file) { |
|
|
|
foreach (new RecursiveIteratorIterator($dirs) as $file) { |
|
|
|
@ -114,6 +120,13 @@ class MoodleImport |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($importedFiles[$fileInfo['filename']])) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log($fileInfo['filename']); |
|
|
|
|
|
|
|
} |
|
|
|
$files = []; |
|
|
|
$files = []; |
|
|
|
$files['file']['name'] = $fileInfo['filename']; |
|
|
|
$files['file']['name'] = $fileInfo['filename']; |
|
|
|
$files['file']['tmp_name'] = $file->getPathname(); |
|
|
|
$files['file']['tmp_name'] = $file->getPathname(); |
|
|
|
@ -122,20 +135,19 @@ class MoodleImport |
|
|
|
$files['file']['size'] = $fileInfo['filesize']; |
|
|
|
$files['file']['size'] = $fileInfo['filesize']; |
|
|
|
$files['file']['from_file'] = true; |
|
|
|
$files['file']['from_file'] = true; |
|
|
|
$files['file']['move_file'] = true; |
|
|
|
$files['file']['move_file'] = true; |
|
|
|
$_POST['language'] = $courseInfo['language']; |
|
|
|
|
|
|
|
$_POST['moodle_import'] = true; |
|
|
|
$title = isset($fileInfo['title']) ? $fileInfo['title'] : pathinfo($fileInfo['filename'], PATHINFO_FILENAME); |
|
|
|
|
|
|
|
|
|
|
|
$data = DocumentManager::upload_document( |
|
|
|
$data = DocumentManager::upload_document( |
|
|
|
$files, |
|
|
|
$files, |
|
|
|
'/moodle', |
|
|
|
'/moodle', |
|
|
|
isset($fileInfo['title']) ? $fileInfo['title'] : pathinfo($fileInfo['filename'], PATHINFO_FILENAME), |
|
|
|
$title, |
|
|
|
'', |
|
|
|
'', |
|
|
|
null, |
|
|
|
null, |
|
|
|
null, |
|
|
|
'overwrite', |
|
|
|
true, |
|
|
|
true, |
|
|
|
true, |
|
|
|
true, |
|
|
|
'file', |
|
|
|
'file', |
|
|
|
// This is to validate spaces as hyphens |
|
|
|
|
|
|
|
false |
|
|
|
false |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
@ -146,7 +158,6 @@ class MoodleImport |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$xml = @file_get_contents($destinationDir.'/moodle_backup.xml'); |
|
|
|
$xml = @file_get_contents($destinationDir.'/moodle_backup.xml'); |
|
|
|
|
|
|
|
|
|
|
|
$doc = new DOMDocument(); |
|
|
|
$doc = new DOMDocument(); |
|
|
|
$res = @$doc->loadXML($xml); |
|
|
|
$res = @$doc->loadXML($xml); |
|
|
|
|
|
|
|
|
|
|
|
@ -157,23 +168,31 @@ class MoodleImport |
|
|
|
throw new Exception(get_lang('FailedToImportThisIsNotAMoodleFile')); |
|
|
|
throw new Exception(get_lang('FailedToImportThisIsNotAMoodleFile')); |
|
|
|
} |
|
|
|
} |
|
|
|
$activities = $doc->getElementsByTagName('activity'); |
|
|
|
$activities = $doc->getElementsByTagName('activity'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('Loading activities: '.count($activities)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
foreach ($activities as $activity) { |
|
|
|
foreach ($activities as $activity) { |
|
|
|
if (empty($activity->childNodes->length)) { |
|
|
|
if (empty($activity->childNodes->length)) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$currentItem = []; |
|
|
|
$currentItem = []; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($activity->childNodes as $item) { |
|
|
|
foreach ($activity->childNodes as $item) { |
|
|
|
$currentItem[$item->nodeName] = $item->nodeValue; |
|
|
|
$currentItem[$item->nodeName] = $item->nodeValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$moduleName = isset($currentItem['modulename']) ? $currentItem['modulename'] : false; |
|
|
|
$moduleName = isset($currentItem['modulename']) ? $currentItem['modulename'] : false; |
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('moduleName: '.$moduleName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch ($moduleName) { |
|
|
|
switch ($moduleName) { |
|
|
|
case 'forum': |
|
|
|
case 'forum': |
|
|
|
require_once '../forum/forumfunction.inc.php'; |
|
|
|
|
|
|
|
$catForumValues = []; |
|
|
|
$catForumValues = []; |
|
|
|
|
|
|
|
|
|
|
|
// Read the current forum module xml. |
|
|
|
// Read the current forum module xml. |
|
|
|
$moduleDir = $currentItem['directory']; |
|
|
|
$moduleDir = $currentItem['directory']; |
|
|
|
$moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
|
|
|
$moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
|
|
|
@ -212,6 +231,10 @@ class MoodleImport |
|
|
|
// var_dump($moduleValues); // <-- uncomment this to see the final array |
|
|
|
// var_dump($moduleValues); // <-- uncomment this to see the final array |
|
|
|
|
|
|
|
|
|
|
|
$exercise = new Exercise($courseInfo['real_id']); |
|
|
|
$exercise = new Exercise($courseInfo['real_id']); |
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('quiz:'.$moduleValues['name']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$title = Exercise::format_title_variable($moduleValues['name']); |
|
|
|
$title = Exercise::format_title_variable($moduleValues['name']); |
|
|
|
$exercise->updateTitle($title); |
|
|
|
$exercise->updateTitle($title); |
|
|
|
$exercise->updateDescription($moduleValues['intro']); |
|
|
|
$exercise->updateDescription($moduleValues['intro']); |
|
|
|
@ -241,10 +264,7 @@ class MoodleImport |
|
|
|
|
|
|
|
|
|
|
|
// Ok, we got the Quiz and create it, now its time to add the Questions |
|
|
|
// Ok, we got the Quiz and create it, now its time to add the Questions |
|
|
|
foreach ($moduleValues['question_instances'] as $index => $question) { |
|
|
|
foreach ($moduleValues['question_instances'] as $index => $question) { |
|
|
|
$questionsValues = $this->readMainQuestionsXml( |
|
|
|
$questionsValues = $this->readMainQuestionsXml($questionsXml, $question['questionid']); |
|
|
|
$questionsXml, |
|
|
|
|
|
|
|
$question['questionid'] |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
$moduleValues['question_instances'][$index] = $questionsValues; |
|
|
|
$moduleValues['question_instances'][$index] = $questionsValues; |
|
|
|
// Set Question Type from Moodle XML element <qtype> |
|
|
|
// Set Question Type from Moodle XML element <qtype> |
|
|
|
$qType = $moduleValues['question_instances'][$index]['qtype']; |
|
|
|
$qType = $moduleValues['question_instances'][$index]['qtype']; |
|
|
|
@ -258,6 +278,9 @@ class MoodleImport |
|
|
|
if (empty($questionInstance)) { |
|
|
|
if (empty($questionInstance)) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('question: '.$question['questionid']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$questionInstance->updateTitle( |
|
|
|
$questionInstance->updateTitle( |
|
|
|
$moduleValues['question_instances'][$index]['name'] |
|
|
|
$moduleValues['question_instances'][$index]['name'] |
|
|
|
@ -282,7 +305,6 @@ class MoodleImport |
|
|
|
//Save normal question if NOT media |
|
|
|
//Save normal question if NOT media |
|
|
|
if ($questionInstance->type != MEDIA_QUESTION) { |
|
|
|
if ($questionInstance->type != MEDIA_QUESTION) { |
|
|
|
$questionInstance->save($exercise); |
|
|
|
$questionInstance->save($exercise); |
|
|
|
|
|
|
|
|
|
|
|
// modify the exercise |
|
|
|
// modify the exercise |
|
|
|
$exercise->addToList($questionInstance->id); |
|
|
|
$exercise->addToList($questionInstance->id); |
|
|
|
$exercise->update_question_positions(); |
|
|
|
$exercise->update_question_positions(); |
|
|
|
@ -354,11 +376,15 @@ class MoodleImport |
|
|
|
$_POST['category_id'] = 0; |
|
|
|
$_POST['category_id'] = 0; |
|
|
|
$_POST['target'] = '_blank'; |
|
|
|
$_POST['target'] = '_blank'; |
|
|
|
|
|
|
|
|
|
|
|
Link::addlinkcategory("link"); |
|
|
|
Link::addlinkcategory('link'); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
|
|
|
error_log('Finish'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
removeDir($destinationDir); |
|
|
|
removeDir($destinationDir); |
|
|
|
unlink($filePath); |
|
|
|
unlink($filePath); |
|
|
|
|
|
|
|
|
|
|
|
@ -916,7 +942,7 @@ class MoodleImport |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Process Chamilo True False. |
|
|
|
* Process Chamilo True False. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param object $objAnswer |
|
|
|
* @param Answer $objAnswer |
|
|
|
* @param array $answerValues |
|
|
|
* @param array $answerValues |
|
|
|
* @param int $position |
|
|
|
* @param int $position |
|
|
|
* @param int $questionWeighting |
|
|
|
* @param int $questionWeighting |
|
|
|
|