@ -34,17 +34,18 @@ class CourseArchiver
* Write a course and all its resources to a zip-file.
* @return string A pointer to the zip-file
*/
static function write_course($course) {
static function write_course($course)
{
$perm_dirs = api_get_permissions_for_new_directories();
CourseArchiver::clean_backup_dir();
// Create a temp directory
$tmp_dir_name = 'CourseArchiver_' . api_get_unique_id();
$backup_dir = api_get_path(SYS_ARCHIVE_PATH) . $tmp_dir_name . '/';
$tmp_dir_name = 'CourseArchiver_'.api_get_unique_id();
$backup_dir = api_get_path(SYS_ARCHIVE_PATH).$tmp_dir_name . '/';
// All course-information will be stored in course_info.dat
$course_info_file = $backup_dir . 'course_info.dat';
$course_info_file = $backup_dir.'course_info.dat';
$zip_dir = api_get_path(SYS_ARCHIVE_PATH);
$user = api_get_user_info();
$date = new DateTime(api_get_local_time());
@ -71,11 +72,9 @@ class CourseArchiver
error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
}
//Documents
// Copy all documents to the temp-dir
if (is_array($course->resources[RESOURCE_DOCUMENT])) {
foreach ($course->resources[RESOURCE_DOCUMENT] as $id => $ document) {
if (isset($course->resources[RESOURCE_DOCUMENT]) & & is_array($course->resources[RESOURCE_DOCUMENT])) {
foreach ($course->resources[RESOURCE_DOCUMENT] as $document) {
if ($document->file_type == DOCUMENT) {
$doc_dir = $backup_dir . $document->path;
@mkdir(dirname($doc_dir), $perm_dirs, true);
@ -90,14 +89,14 @@ class CourseArchiver
// Copy all scorm documents to the temp-dir
if (isset($course->resources[RESOURCE_SCORM]) & & is_array($course->resources[RESOURCE_SCORM])) {
foreach ($course->resources[RESOURCE_SCORM] as $id => $ document) {
foreach ($course->resources[RESOURCE_SCORM] as $document) {
$doc_dir = dirname($backup_dir . $document->path);
@mkdir($doc_dir, $perm_dirs, true);
FileManager::copyDirTo($course->path . $document->path, $doc_dir, false);
}
}
// Copy calendar attachments
// Copy calendar attachments.
if (isset($course->resources[RESOURCE_EVENT]) & & is_array($course->resources[RESOURCE_EVENT])) {
$doc_dir = dirname($backup_dir . '/upload/calendar/');
@ -105,14 +104,14 @@ class CourseArchiver
FileManager::copyDirTo($course->path . 'upload/calendar/', $doc_dir, false);
}
//Copy learningpath author image
// Copy Learning path author image.
if (isset($course->resources[RESOURCE_LEARNPATH]) & & is_array($course->resources[RESOURCE_LEARNPATH])) {
$doc_dir = dirname($backup_dir . '/upload/learning_path/');
@mkdir($doc_dir, $perm_dirs, true);
FileManager::copyDirTo($course->path . 'upload/learning_path/', $doc_dir, false);
}
//Copy announcements attachments
// Copy announcements attachments.
if (isset($course->resources[RESOURCE_ANNOUNCEMENT]) & & is_array($course->resources[RESOURCE_ANNOUNCEMENT])) {
$doc_dir = dirname($backup_dir . '/upload/announcements/');
@ -126,7 +125,7 @@ class CourseArchiver
//$zip->deleteByIndex(0);
// Remove the temp-dir.
rmdirr($backup_dir);
return '' . $zip_file;
return $zip_file;
}
/**
@ -174,28 +173,39 @@ class CourseArchiver
* @param boolean $delete Delete the file after reading the course?
* @todo Check if the archive is a correct Chamilo-export
*/
static function read_course($filename, $delete = false) {
static function read_course($filename, $delete = false)
{
CourseArchiver::clean_backup_dir();
// Create a temp directory
$tmp_dir_name = 'CourseArchiver_' . uniqid('');
$unzip_dir = api_get_path(SYS_ARCHIVE_PATH) . '' . $tmp_dir_name;
@ mkdir($unzip_dir, api_get_permissions_for_new_directories(), true);
@ copy(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename, $unzip_dir . '/backup.zip');
$unzip_dir = api_get_path(SYS_ARCHIVE_PATH).$tmp_dir_name;
mkdir($unzip_dir, api_get_permissions_for_new_directories(), true);
copy(api_get_path(SYS_ARCHIVE_PATH).$filename, $unzip_dir.'/backup.zip');
// unzip the archive
$zip = new PclZip($unzip_dir . '/backup.zip');
@chdir($unzip_dir);
$zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
$zip = new PclZip($unzip_dir.'/backup.zip');
chdir($unzip_dir);
$list = $zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
if ($list == 0) {
/*global $app;
$errorMessage = $zip->errorInfo(true);
$app['session']->getFlashBag()->add('warning', $errorMessage);*/
}
// remove the archive-file
if ($delete) {
@unlink(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename);
if (file_exists(api_get_path(SYS_ARCHIVE_PATH).$filename)) {
unlink(api_get_path(SYS_ARCHIVE_PATH).$filename);
}
}
// read the course
// Read the course
if (!is_file('course_info.dat')) {
return new Course();
}
$fp = @fopen('course_info.dat', "r");
$contents = @fread($fp, filesize('course_info.dat'));
@fclose($fp);
$contents = file_get_contents('course_info.dat');
// CourseCopyLearnpath class appeared in Chamilo 1.8.7, it is the former Learnpath class in the "Copy course" tool.
// For backward comaptibility with archives created on Chamilo 1.8.6.2 or older systems, we have to do the following:
// Before unserialization, if class name "Learnpath" was found, it should be renamed as "CourseCopyLearnpath".