Fixing exercise description paths during zip copying see BT#5731

skala
Julio Montoya 12 years ago
parent c2ff2f095d
commit 775f996262
  1. 56
      main/coursecopy/classes/CourseRestorer.class.php
  2. 24
      main/inc/lib/document.lib.php

@ -390,7 +390,7 @@ class CourseRestorer
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$document->path);
if (UTF8_CONVERT) $content = utf8_encode($content);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content, $this->course->code,$this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$result = file_put_contents($path.$document->path,$content);
}
@ -433,7 +433,6 @@ class CourseRestorer
$course_path = $path; // "/var/www/wiener/courses/"
$orig_base_folder = $document_path[1];
$orig_base_path = $course_path.$document_path[0].'/'.$document_path[1];
//echo '$orig_base_path'; var_dump($orig_base_path);
if (is_dir($orig_base_path)) {
@ -485,7 +484,7 @@ class CourseRestorer
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($dest_document_path);
if (UTF8_CONVERT) $content = utf8_encode($content);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content, $this->course->code, $this->course->destination_path);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$result = file_put_contents($dest_document_path,$content);
}
}
@ -516,7 +515,7 @@ class CourseRestorer
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$new_file_name);
if (UTF8_CONVERT) $content = utf8_encode($content);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$result = file_put_contents($path.$new_file_name, $content);
}
}
@ -547,7 +546,7 @@ class CourseRestorer
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$new_file_name);
if (UTF8_CONVERT) $content = utf8_encode($content);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$result = file_put_contents($path.$new_file_name, $content);
}
}
@ -582,7 +581,7 @@ class CourseRestorer
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$document->path);
if (UTF8_CONVERT) $content = utf8_encode($content);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$result = file_put_contents($path.$document->path, $content);
}
}
@ -685,9 +684,7 @@ class CourseRestorer
}
rename($this->course->backup_path.'/'.$document->path,$this->course->backup_path.'/'.$new_file_name);
copyDirTo($this->course->backup_path.'/'.$new_file_name, $path.dirname($new_file_name), false);
rename($this->course->backup_path.'/'.$new_file_name,$this->course->backup_path.'/'.$document->path);
break;
@ -720,7 +717,7 @@ class CourseRestorer
$params['forum_category'] = $cat_id;
unset($params['forum_id']);
$params['forum_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['forum_comment'], $this->course->code, $this->course->destination_path);
$params['forum_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['forum_comment'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
if (!empty($params['forum_image'])) {
$original_forum_image = $this->course->path.'upload/forum/images/'.$params['forum_image'];
@ -777,7 +774,7 @@ class CourseRestorer
}
$params = (array) $forum_cat->obj;
$params['c_id'] = $this->destination_course_id;
$params['cat_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['cat_comment'], $this->course->code, $this->course->destination_path);
$params['cat_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['cat_comment'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
unset($params['cat_id']);
$params = self::DBUTF8_array($params);
$new_id = Database::insert($forum_cat_table, $params);
@ -839,7 +836,7 @@ class CourseRestorer
$params['poster_id'] = $this->first_teacher_id;
$params['post_date'] = api_get_utc_datetime();
unset($params['post_id']);
$params['post_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['post_text'], $this->course->code, $this->course->destination_path);
$params['post_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($params['post_text'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$new_id = Database::insert($table_post, $params);
api_item_property_update($this->destination_course_info, TOOL_FORUM_POST, $new_id, 'PostAdded', api_get_user_id(), 0, 0, null, null);
$this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
@ -889,7 +886,7 @@ class CourseRestorer
foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro) {
$sql = "DELETE FROM ".$tool_intro_table." WHERE c_id = ".$this->destination_course_id." AND id='".self::DBUTF8escapestring($tool_intro->id)."'";
Database::query($sql);
$tool_intro->intro_text = DocumentManager::replace_urls_inside_content_html_from_copy_course($tool_intro->intro_text,$this->course->code,$this->course->destination_path);
$tool_intro->intro_text = DocumentManager::replace_urls_inside_content_html_from_copy_course($tool_intro->intro_text,$this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$tool_intro_table." SET c_id = ".$this->destination_course_id." , id='".self::DBUTF8escapestring($tool_intro->id)."', intro_text = '".self::DBUTF8escapestring($tool_intro->intro_text)."'";
Database::query($sql);
@ -935,7 +932,7 @@ class CourseRestorer
$resources = $this->course->resources;
foreach ($resources[RESOURCE_EVENT] as $id => $event) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$event->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($event->content, $this->course->code, $this->course->destination_path);
$event->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($event->content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table." SET
c_id = ".$this->destination_course_id." ,
@ -1004,7 +1001,7 @@ class CourseRestorer
}
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$description_content = DocumentManager::replace_urls_inside_content_html_from_copy_course($cd->content, $this->course->code, $this->course->destination_path);
$description_content = DocumentManager::replace_urls_inside_content_html_from_copy_course($cd->content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$condition_session = "";
if (!empty($session_id)) {
@ -1028,7 +1025,7 @@ class CourseRestorer
foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($announcement->content, $this->course->code, $this->course->destination_path);
$announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($announcement->content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table." " ."SET
c_id = ".$this->destination_course_id." ,
@ -1112,7 +1109,7 @@ class CourseRestorer
}
if ($id != -1) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$quiz->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($quiz->description, $this->course->code, $this->course->destination_path);
$quiz->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($quiz->description, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
global $_custom;
if (isset($_custom['exercises_clean_dates_when_restoring']) && $_custom['exercises_clean_dates_when_restoring']) {
@ -1195,7 +1192,7 @@ class CourseRestorer
$table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$question->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->description, $this->course->code, $this->course->destination_path);
$question->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->description, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_que." SET
c_id = ".$this->destination_course_id." ,
@ -1257,8 +1254,8 @@ class CourseRestorer
foreach ($question->answers as $index => $answer) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path);
$answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path);
$answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_ans." SET
c_id = ".$this->destination_course_id." ,
@ -1316,7 +1313,6 @@ class CourseRestorer
$question_option_id = Database::insert($table_options, $item);
$new_options[$obj->obj->id] = $question_option_id;
}
//var_dump($new_options, $correct_answers);
foreach($correct_answers as $answer_id => $correct_answer) {
$params = array();
$params['correct'] = $new_options[$correct_answer];
@ -1350,10 +1346,10 @@ class CourseRestorer
$result_check = Database::query($sql_check);
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$survey->title = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->title, $this->course->code, $this->course->destination_path);
$survey->subtitle = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->subtitle, $this->course->code, $this->course->destination_path);
$survey->intro = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->intro, $this->course->code, $this->course->destination_path);
$survey->surveythanks = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->surveythanks, $this->course->code, $this->course->destination_path);
$survey->title = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->title, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$survey->subtitle = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->subtitle, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$survey->intro = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->intro, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$survey->surveythanks = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->surveythanks, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_sur." " ."SET
c_id = ".$this->destination_course_id." ,
@ -1501,7 +1497,7 @@ class CourseRestorer
$table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$question->survey_question = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->survey_question, $this->course->code, $this->course->destination_path);
$question->survey_question = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->survey_question, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_que." SET
c_id = ".$this->destination_course_id." ,
@ -1520,7 +1516,7 @@ class CourseRestorer
foreach ($question->answers as $index => $answer) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$answer['option_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['option_text'], $this->course->code, $this->course->destination_path);
$answer['option_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['option_text'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_ans." SET
c_id = ".$this->destination_course_id." ,
@ -1895,7 +1891,7 @@ class CourseRestorer
}
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$glossary->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($glossary->description, $this->course->code, $this->course->destination_path);
$glossary->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($glossary->description, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO ".$table_glossary." SET c_id = ".$this->destination_course_id." , name = '".self::DBUTF8escapestring($glossary->name)."', description = '".self::DBUTF8escapestring($glossary->description)."', display_order='".self::DBUTF8escapestring($glossary->display_order)."' $condition_session ";
Database::query($sql);
@ -1921,7 +1917,7 @@ class CourseRestorer
// the sql statement to insert the groups from the old course to the new course
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$wiki->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($wiki->content, $this->course->code, $this->course->destination_path);
$wiki->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($wiki->content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$sql = "INSERT INTO $table_wiki (c_id, page_id, reflink, title, content, user_id, group_id, dtime, progress, version, session_id) VALUES (
".$this->destination_course_id." ,
@ -1962,7 +1958,7 @@ class CourseRestorer
foreach ($resources[RESOURCE_THEMATIC] as $id => $thematic) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$thematic->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($thematic->content, $this->course->code, $this->course->destination_path);
$thematic->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($thematic->content, $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$thematic->params['c_id'] = $this->destination_course_id;
$thematic->params['id'] = null;
$last_id = Database::insert($table_thematic, $thematic->params, false);
@ -2010,7 +2006,7 @@ class CourseRestorer
foreach ($resources[RESOURCE_ATTENDANCE] as $id => $obj) {
// check resources inside html from fckeditor tool and copy correct urls into recipient course
$obj->params['description'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($obj->params['description'], $this->course->code, $this->course->destination_path);
$obj->params['description'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($obj->params['description'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']);
$obj->params['id'] = null;
$obj->params['c_id'] = $this->destination_course_id;

@ -1569,7 +1569,6 @@ class DocumentManager {
$max = 5;
$attributes = array();
$wanted_attributes = array('src', 'url', '@import', 'href', 'value', 'flashvars');
$explode_attributes = array('flashvars' => 'file');
$abs_path = '';
@ -1876,7 +1875,9 @@ class DocumentManager {
'))' .
'|' .
// '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
'(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/', $attrString, $regs
'(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
$attrString,
$regs
);
} catch (Exception $e) {
error_log('Caught exception: ' . $e->getMessage(), 0);
@ -1934,7 +1935,7 @@ class DocumentManager {
* @param string destination course directory
* @return string new content html with replaced urls or return false if content is not a string
*/
static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory) {
static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory, $origin_course_path_from_zip = null, $origin_course_info_path = null) {
require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
if (empty($content_html)) {
@ -1943,7 +1944,16 @@ class DocumentManager {
$orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
$orig_course_info = api_get_course_info($origin_course_code);
$orig_course_path = api_get_path(SYS_PATH) . 'courses/' . $orig_course_info['path'] . '/';
//Course does not exist in the current DB probably this cames from a zip file?
if (empty($orig_course_info)) {
if (!empty($origin_course_path_from_zip)) {
$orig_course_path = $origin_course_path_from_zip.'/';
$orig_course_info_path = $origin_course_info_path;
}
} else {
$orig_course_path = api_get_path(SYS_PATH).'courses/'.$orig_course_info['path'] . '/';
$orig_course_info_path = $orig_course_info['path'];
}
$destination_course_code = CourseManager::get_course_id_from_path($destination_course_directory);
$destination_course_info = api_get_course_info($destination_course_code);
@ -1959,6 +1969,7 @@ class DocumentManager {
$real_orig_url = $source[0]; // url
$scope_url = $source[1]; // scope (local, remote)
$type_url = $source[2]; // tyle (rel, abs, url)
// Get path and query from origin url
$orig_parse_url = parse_url($real_orig_url);
$real_orig_path = $orig_parse_url['path'];
@ -1979,7 +1990,6 @@ class DocumentManager {
$document_file = strstr($real_orig_path, 'document');
if (strpos($real_orig_path, $document_file) !== false) {
$origin_filepath = $orig_course_path.$document_file;
$destination_filepath = $dest_course_path.$document_file;
@ -2002,7 +2012,6 @@ class DocumentManager {
if (!file_exists($destination_filepath)) {
$result = @copy($origin_filepath, $destination_filepath);
if ($result) {
$filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $destination_filepath);
$size = filesize($destination_filepath);
@ -2016,7 +2025,7 @@ class DocumentManager {
// Replace origin course path by destination course path
if (strpos($content_html, $real_orig_url) !== false) {
//$origin_course_code
$url_course_path = str_replace($orig_course_info['path'] . '/' . $document_file, '', $real_orig_path);
$url_course_path = str_replace($orig_course_info_path.'/'.$document_file, '', $real_orig_path);
$destination_url = $url_course_path . $destination_course_directory . '/' . $document_file . $dest_url_query;
//If the course code doesn't exist in the path? what we do? Nothing! see BT#1985
@ -2035,7 +2044,6 @@ class DocumentManager {
}
} else {
if ($type_url == 'url') {
}
}
}

Loading…
Cancel
Save