Browse Source

Feature #272 - Course restorer: Now the imported course is converted to the system encoding. For example, in UTF-8 system you may import old 1.8.x archives that have ISO-8859-15 encoding.

Ivan Tcholakov 15 years ago
parent
commit
ea7db781e3

+ 168 - 8
main/coursecopy/classes/Course.class.php

@@ -39,6 +39,8 @@ class Course
 	var $path;
 	var $destination_path;
 	var $destination_db;
+	var $encoding;
+
 	/**
 	 * Create a new Course-object
 	 */
@@ -128,8 +130,6 @@ class Course
 					switch ($type) {
 
 						case RESOURCE_ANNOUNCEMENT:
-						case RESOURCE_EVENT:
-						case RESOURCE_WIKI:
 							$title = $resource->title;
 							$description = $resource->content;
 							break;
@@ -139,11 +139,17 @@ class Course
 							$description = $resource->comment;
 							break;
 
+						case RESOURCE_EVENT:
+							$title = $resource->title;
+							$description = $resource->content;
+							break;
+
 						case RESOURCE_FORUM:
+							$title = $resource->title;
+							$description = $resource->description;
+							break;
+
 						case RESOURCE_FORUMCATEGORY:
-						case RESOURCE_LINK:
-						case RESOURCE_LINKCATEGORY:
-						case RESOURCE_QUIZ:
 							$title = $resource->title;
 							$description = $resource->description;
 							break;
@@ -153,15 +159,33 @@ class Course
 							$description = $resource->text;
 							break;
 
+						case RESOURCE_FORUMTOPIC:
+							$title = $resource->title;
+							break;
+
 						case RESOURCE_GLOSSARY:
+							$title = $resource->name;
+							$description = $resource->description;
+							break;
+
 						case RESOURCE_LEARNPATH:
 							$title = $resource->name;
 							$description = $resource->description;
 							break;
 
-						case RESOURCE_FORUMTOPIC:
-						case RESOURCE_SCORM:
+						case RESOURCE_LINK:
+							$title = $resource->title;
+							$description = $resource->description;
+							break;
+
+						case RESOURCE_LINKCATEGORY:
+							$title = $resource->title;
+							$description = $resource->description;
+							break;
+
+						case RESOURCE_QUIZ:
 							$title = $resource->title;
+							$description = $resource->description;
 							break;
 
 						case RESOURCE_QUIZQUESTION:
@@ -169,6 +193,10 @@ class Course
 							$description = $resource->description;
 							break;
 
+						case RESOURCE_SCORM:
+							$title = $resource->title;
+							break;
+
 						case RESOURCE_SURVEY:
 							$title = $resource->title;
 							$description = $resource->subtitle;
@@ -183,6 +211,11 @@ class Course
 							$description = $resource->intro_text;
 							break;
 
+						case RESOURCE_WIKI:
+							$title = $resource->title;
+							$description = $resource->content;
+							break;
+
 						default:
 							break;
 					}
@@ -206,5 +239,132 @@ class Course
 		return $sample_text;
 	}
 
+	/**
+	 * Converts to the system encoding all the language-sensitive fields in the imported course.
+	 */
+	public function to_system_encoding() {
+
+		if (api_equal_encodings($this->encoding, api_get_system_encoding())) {
+			return;
+		}
+
+		foreach ($this->resources as $type => & $resources) {
+			if (count($resources) > 0) {
+				foreach ($resources as $id => & $resource) {
+
+					switch ($type) {
+
+						case RESOURCE_ANNOUNCEMENT:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->content = api_to_system_encoding($resource->content, $this->encoding);
+							break;
+
+						case RESOURCE_DOCUMENT:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->comment = api_to_system_encoding($resource->comment, $this->encoding);
+							break;
+
+						case RESOURCE_EVENT:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->content = api_to_system_encoding($resource->content, $this->encoding);
+							break;
+
+						case RESOURCE_FORUM:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_FORUMCATEGORY:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_FORUMPOST:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->text = api_to_system_encoding($resource->text, $this->encoding);
+							$resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding);
+							break;
+
+						case RESOURCE_FORUMTOPIC:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding);
+							$resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding);
+							break;
+
+						case RESOURCE_GLOSSARY:
+							$resource->name = api_to_system_encoding($resource->name, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_LEARNPATH:
+							$resource->name = api_to_system_encoding($resource->name, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							$resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding);
+							$resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding);
+							//$resource->author = api_to_system_encoding($resource->author, $this->encoding);  // Needs implamantation.
+							break;
+
+						case RESOURCE_LINK:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_LINKCATEGORY:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_QUIZ:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							break;
+
+						case RESOURCE_QUIZQUESTION:
+							$resource->question = api_to_system_encoding($resource->question, $this->encoding);
+							$resource->description = api_to_system_encoding($resource->description, $this->encoding);
+							if (is_array($resource->answers) && count($resource->answers) > 0) {
+								foreach ($resource->answers as $index => & $answer) {
+									$answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding);
+									$answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding);
+								}
+							}
+							break;
+
+						case RESOURCE_SCORM:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							break;
+
+						case RESOURCE_SURVEY:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding);
+							$resource->author = api_to_system_encoding($resource->author, $this->encoding);
+							$resource->intro = api_to_system_encoding($resource->intro, $this->encoding);
+							$resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding);
+							break;
+
+						case RESOURCE_SURVEYQUESTION:
+							$resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding);
+							$resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding);
+							break;
+
+						case RESOURCE_TOOL_INTRO:
+							$resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding);
+							break;
+
+						case RESOURCE_WIKI:
+							$resource->title = api_to_system_encoding($resource->title, $this->encoding);
+							$resource->content = api_to_system_encoding($resource->content, $this->encoding);
+							$resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding);
+							break;
+
+						default:
+							break;
+					}
+				}
+			}
+		}
+
+		$this->encoding = api_get_system_encoding();
+	}
+
 }
-?>

+ 5 - 3
main/coursecopy/classes/CourseRestorer.class.php

@@ -112,6 +112,9 @@ class CourseRestorer
 			$this->course->encoding = api_detect_encoding($sample_text);
 		}
 
+		// Encoding conversion of the course, if it is needed.
+		$this->course->to_system_encoding();
+
 		if (!empty($session_id)) {
 			$this->restore_documents($session_id,$destination_course_code);
 			$this->restore_quizzes($session_id);
@@ -896,8 +899,8 @@ class CourseRestorer
 
 			if ($question->quiz_type == 4) { // for answer type matching
 				foreach ($question->answers as $index => $answer) {
-				$sql = "INSERT INTO ".$table_ans." SET id= '".$answer['id']."',question_id = '".$new_id."', answer = '".Database::escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".Database::escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
-				Database::query($sql, __FILE__, __LINE__);
+					$sql = "INSERT INTO ".$table_ans." SET id= '".$answer['id']."',question_id = '".$new_id."', answer = '".Database::escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".Database::escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
+					Database::query($sql, __FILE__, __LINE__);
 				}
 			} else {
 				foreach ($question->answers as $index => $answer) {
@@ -1505,4 +1508,3 @@ class CourseRestorer
 		}
 	}
 }
-?>