Browse Source

Bug #1370 - Partial fixing copy/backup/restore functionality for forums. Attachments and other linked resources are not supported yet.

Ivan Tcholakov 15 years ago
parent
commit
6c639ecf30

+ 3 - 2
main/coursecopy/classes/CourseBuilder.class.php

@@ -157,7 +157,7 @@ class CourseBuilder
 	 */
 	function build_forum_topics()
 	{
-		$table = Database :: get_course_table(TABLE_FORUM_POST);
+		$table = Database :: get_course_table(TABLE_FORUM_THREAD);
 		$sql = 'SELECT * FROM '.$table;
 		$db_result = api_sql_query($sql, __FILE__, __LINE__);
 		while ($obj = Database::fetch_object($db_result))
@@ -168,6 +168,7 @@ class CourseBuilder
 	}
 	/**
 	 * Build the forum-posts
+	 * TODO: All tree structure of posts should be built, attachments for example.
 	 */
 	function build_forum_posts()
 	{
@@ -176,7 +177,7 @@ class CourseBuilder
 		$db_result = api_sql_query($sql, __FILE__, __LINE__);
 		while ($obj = Database::fetch_object($db_result))
 		{
-			$forum_post = new ForumPost($obj->post_id, $obj->post_title, $obj->post_text, $obj->post_date, $obj->poster_ip, $obj->poster_name, $obj->post_notification, $obj->post_parent_id, $obj->thread_id, $obj->visible);
+			$forum_post = new ForumPost($obj->post_id, $obj->post_title, $obj->post_text, $obj->post_date, $obj->poster_id, $obj->poster_name, $obj->post_notification, $obj->post_parent_id, $obj->thread_id, $obj->forum_id, $obj->visible);
 			$this->course->add_resource($forum_post);
 		}
 	}

+ 73 - 15
main/coursecopy/classes/CourseRestorer.class.php

@@ -102,7 +102,10 @@ class CourseRestorer
 		$this->restore_documents();
 		$this->restore_scorm_documents();
 		$this->restore_course_descriptions();
-		//$this->restore_forums();
+		// Enabled by Ivan Tcholakov, 30-AUG-2009.
+		////$this->restore_forums();
+		$this->restore_forums();
+		//
 		$this->restore_quizzes(); // after restore_documents! (for correct import of sound/video)
 		$this->restore_learnpaths();
 		$this->restore_surveys();
@@ -391,13 +394,31 @@ class CourseRestorer
 		if ($this->course->has_resources(RESOURCE_FORUM))
 		{
 			$table_forum = Database :: get_course_table(TABLE_FORUM, $this->course->destination_db);
-			$table_topic = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
+			$table_topic = Database :: get_course_table(TABLE_FORUM_THREAD, $this->course->destination_db);
 			$table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
 			$resources = $this->course->resources;
 			foreach ($resources[RESOURCE_FORUM] as $id => $forum)
 			{
 				$cat_id = $this->restore_forum_category($forum->category_id);
-				$sql = "INSERT INTO ".$table_forum." SET forum_name = '".Database::escape_string($forum->title)."', forum_desc = '".Database::escape_string($forum->description)."', cat_id = '".$cat_id."', forum_access='2'";
+				$sql = "INSERT INTO ".$table_forum.
+					" SET forum_title = '".Database::escape_string($forum->title).
+					"', forum_comment = '".Database::escape_string($forum->description).
+					"', forum_category = ".(int)Database::escape_string($cat_id).
+					", forum_last_post = ".(int)Database::escape_string($forum->last_post).
+					", forum_threads = ".(int)Database::escape_string($forum->topics).
+					", forum_posts = ".(int)Database::escape_string($forum->posts).
+					", allow_anonymous = ".(int)Database::escape_string($forum->allow_anonymous).
+					", allow_edit = ".(int)Database::escape_string($forum->allow_edit).
+					", approval_direct_post = '".Database::escape_string($forum->approval_direct_post).
+					"', allow_attachments = ".(int)Database::escape_string($forum->allow_attachments).
+					", allow_new_threads = ".(int)Database::escape_string($forum->allow_new_topics).
+					", default_view = '".Database::escape_string($forum->default_view).
+					"', forum_of_group = '".Database::escape_string($forum->of_group).
+					"', forum_group_public_private = '".Database::escape_string($forum->group_public_private).
+					"', forum_order = ".(int)Database::escape_string($forum->order).
+					", locked = ".(int)Database::escape_string($forum->locked).
+					", session_id = ".(int)Database::escape_string($forum->session_id).
+					", forum_image = '".Database::escape_string($forum->image)."'";
 				api_sql_query($sql, __FILE__, __LINE__);
 				$new_id = Database::get_last_insert_id();
 				$this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id;
@@ -416,7 +437,7 @@ class CourseRestorer
 				if ($forum_topics > 0)
 				{
 					$last_post = $this->course->resources[RESOURCE_FORUMPOST][$forum->last_post];
-					$sql = "UPDATE ".$table_forum." SET forum_topics = ".$forum_topics.", forum_last_post_id = ".$last_post->destination_id." WHERE forum_id = '".$new_id."' ";
+					$sql = "UPDATE ".$table_forum." SET forum_threads = ".$forum_topics.", forum_last_post = ".(int)$last_post->destination_id." WHERE forum_id = ".(int)$new_id;
 					api_sql_query($sql, __FILE__, __LINE__);
 				}
 			}
@@ -432,7 +453,20 @@ class CourseRestorer
 		$forum_cat = $resources[RESOURCE_FORUMCATEGORY][$id];
 		if (!$forum_cat->is_restored())
 		{
-			$sql = "INSERT INTO ".$forum_cat_table." SET cat_title = '".Database::escape_string($forum_cat->title.' ('.$this->course->code.')')."'";
+			$title = $forum_cat->title;
+			if (!preg_match('/.*\((.+)\)$/', $title, $matches)) // This is for avoiding repetitive adding of training code after several backup/restore cycles.
+			{
+				if ($matches[1] != $this->course->code)
+				{
+					$title = $title.' ('.$this->course->code.')';
+				}
+			}
+			$sql = "INSERT INTO ".$forum_cat_table.
+				" SET cat_title = '".Database::escape_string($title).
+				"', cat_comment = '".Database::escape_string($forum_cat->description).
+				"', cat_order = ".(int)Database::escape_string($forum_cat->order).
+				", locked = ".(int)Database::escape_string($forum_cat->locked).
+				", session_id = ".(int)Database::escape_string($forum_cat->session_id);
 			api_sql_query($sql, __FILE__, __LINE__);
 			$new_id = Database::get_last_insert_id();
 			$this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
@@ -445,10 +479,22 @@ class CourseRestorer
 	 */
 	function restore_topic($id, $forum_id)
 	{
-		$table = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
+		$table = Database :: get_course_table(TABLE_FORUM_THREAD, $this->course->destination_db);
 		$resources = $this->course->resources;
 		$topic = $resources[RESOURCE_FORUMTOPIC][$id];
-		$sql = "INSERT INTO ".$table." SET topic_title = '".Database::escape_string($topic->title)."', topic_time = '".$topic->time."', nom = '".Database::escape_string($topic->lastname)."', prenom = '".Database::escape_string($topic->firstname)."', topic_notify = '".$topic->topic_notify."', forum_id = '".$forum_id."'";
+		$sql = "INSERT INTO ".$table.
+			" SET thread_title = '".Database::escape_string($topic->title).
+			"', forum_id = ".(int)Database::escape_string($forum_id).
+			", thread_date = '".Database::escape_string($topic->time).
+			"', thread_poster_id = ".(int)Database::escape_string($topic->topic_poster_id).
+			", thread_poster_name = '".Database::escape_string($topic->topic_poster_name).
+			"', thread_views = ".(int)Database::escape_string($topic->views).
+			", thread_sticky = ".(int)Database::escape_string($topic->sticky).
+			", locked = ".(int)Database::escape_string($topic->locked).
+			", thread_close_date = '".Database::escape_string($topic->time_closed).
+			"', thread_weight = ".(float)Database::escape_string($topic->weight).
+			", thread_title_qualify = '".Database::escape_string($topic->title_qualify).
+			"', thread_qualify_max = ".(float)Database::escape_string($topic->qualify_max);
 		api_sql_query($sql, __FILE__, __LINE__);
 		$new_id = Database::get_last_insert_id();
 		$this->course->resources[RESOURCE_FORUMTOPIC][$id]->destination_id = $new_id;
@@ -461,30 +507,42 @@ class CourseRestorer
 				$this->restore_post($post_id, $new_id, $forum_id);
 			}
 		}
+		$last_post = $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
+		if (is_object($last_post))
+		{
+			$sql = "UPDATE ".$table." SET thread_last_post = ".(int)$last_post->destination_id;
+			api_sql_query($sql, __FILE__, __LINE__);
+		}
 		if ($topic_replies >= 0)
 		{
-			$last_post = $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
-			$sql = "UPDATE ".$table." SET topic_replies = '".$topic_replies."', topic_last_post_id = ".$last_post->destination_id;
+			$sql = "UPDATE ".$table." SET thread_replies = ".$topic_replies;
 			api_sql_query($sql, __FILE__, __LINE__);
 		}
 		return $new_id;
 	}
 	/**
-	 * restore a forum-post
-	 * @todo restore tree-structure of posts.
+	 * Restore a forum-post
+	 * @TODO Restore tree-structure of posts. For example: attachments to posts.
 	 */
 	function restore_post($id, $topic_id, $forum_id)
 	{
 		$table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
-		$table_posttext = Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE, $this->course->destination_db);
 		$resources = $this->course->resources;
 		$post = $resources[RESOURCE_FORUMPOST][$id];
-		$sql = "INSERT INTO ".$table_post." SET topic_id = '".$topic_id."', post_time = '".$post->post_time."', forum_id = '".$forum_id."', nom = '".Database::escape_string($post->lastname)."', prenom = '".Database::escape_string($post->firstname)."', topic_notify = '".$post->topic_notify."', poster_ip = '".$post->poster_ip."'";
+		$sql = "INSERT INTO ".$table_post.
+			" SET post_title = '".Database::escape_string($post->title).
+			"', post_text = '".Database::escape_string($post->text).
+			"', thread_id = ".(int)Database::escape_string($topic_id).
+			", post_date = '".Database::escape_string($post->post_time).
+			"', forum_id = ".(int)Database::escape_string($forum_id).
+			", poster_id = ".(int)Database::escape_string($post->poster_id).
+			", poster_name = '".Database::escape_string($post->poster_name).
+			"', post_notification = ".(int)Database::escape_string($post->topic_notify).
+			", post_parent_id = ".(int)Database::escape_string($post->parent_post_id).
+			", visible = ".(int)Database::escape_string($post->visible);
 		api_sql_query($sql, __FILE__, __LINE__);
 		$new_id = Database::get_last_insert_id();
 		$this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
-		$sql = "INSERT INTO ".$table_posttext." SET post_text = '".Database::escape_string($post->text)."', post_title = '".Database::escape_string($post->title)."', post_id = '".$new_id."'";
-		api_sql_query($sql, __FILE__, __LINE__);
 		return $new_id;
 	}
 	/**

+ 9 - 4
main/coursecopy/classes/ForumPost.class.php

@@ -42,9 +42,9 @@ class ForumPost extends Resource
 	 */
 	var $post_time;
 	/**
-	 * Poster-ip
+	 * Poster id
 	 */
-	var $poster_ip;
+	var $poster_id;
 	/**
 	 * Poster name
 	 */
@@ -61,6 +61,10 @@ class ForumPost extends Resource
 	  * Topic id
 	  */
 	 var $topic_id;
+	 /**
+	  * Forum id
+	  */
+	 var $forum_id;
 	 /**
 	  * Visible flag
 	  */
@@ -68,17 +72,18 @@ class ForumPost extends Resource
 	 /**
 	 * Create a new ForumPost
 	 */
-	function ForumPost($id, $title, $text, $post_time, $poster_ip, $poster_name, $topic_notify, $parent_post_id, $topic_id, $visible)
+	function ForumPost($id, $title, $text, $post_time, $poster_id, $poster_name, $topic_notify, $parent_post_id, $topic_id, $forum_id, $visible)
 	{
 		parent::Resource($id, RESOURCE_FORUMPOST);
 		$this->title = $title;
 		$this->text = $text;
 		$this->post_time = $post_time;
-		$this->poster_ip = $poster_ip;
+		$this->poster_id = $poster_id;
 		$this->poster_name = $poster_name;
 		$this->topic_notify = $topic_notify;
 		$this->parent_post_id = $parent_post_id;
 		$this->topic_id = $topic_id;
+		$this->forum_id = $forum_id;
 		$this->visible = $visible;
 	}
 	/**

+ 9 - 3
main/coursecopy/classes/Resource.class.php

@@ -134,10 +134,12 @@ class Resource
 	/**
 	 * Get the constant which defines the tool of this resource. This is
 	 * used in the item_properties table.
+	 * @param bool $for_item_property_table (optional)	Added by Ivan, 29-AUG-2009: A parameter for resolving differencies between defined TOOL_* constants and hardcoded strings that are stored in the database.
+	 * Example: The constant TOOL_THREAD is defined in the main_api.lib.php with the value 'thread', but the "Forums" tool records in the field 'tool' in the item property table the hardcoded value 'forum_thread'.
 	 * @todo once the RESOURCE_* constants are replaced by the globally
 	 * defined TOOL_* constants, this function will be replaced by get_type()
 	 */
-	function get_tool()
+	function get_tool($for_item_property_table = true)
 	{
 		switch($this->get_type())
 		{
@@ -153,9 +155,15 @@ class Resource
 				return TOOL_LEARNPATH;
 			case RESOURCE_ANNOUNCEMENT:
 				return TOOL_ANNOUNCEMENT;
+			case RESOURCE_FORUMCATEGORY:
+				return 'forum_category'; // Ivan, 29-AUG-2009: A constant like TOOL_FORUM_CATEGORY is missing in main_api.lib.php. Such a constant has been defined in the forum tool for local needs.
 			case RESOURCE_FORUM:
 				return TOOL_FORUM;
 			case RESOURCE_FORUMTOPIC:
+				if ($for_item_property_table)
+				{
+					return 'forum_thread'; // Ivan, 29-AUG-2009: A hardcoded value that the "Forums" tool stores in the item property table.
+				}
 				return TOOL_THREAD;
 			case RESOURCE_FORUMPOST:
 				return TOOL_POST;
@@ -167,8 +175,6 @@ class Resource
 			//	return TOOL_INTRO;
 			//case RESOURCE_LINKCATEGORY:
 			//	return TOOL_LINK_CATEGORY;
-			//case RESOURCE_TOOL_FORUMCATEGORY:
-			//	return TOOL_FORUM_CATEGORY;
 			//case RESOURCE_SCORM:
 			//	return TOOL_SCORM_DOCUMENT;
 			case RESOURCE_SURVEY: