Browse Source

[svn r10879] Added files for specific update from 1.6.x to 1.8.0. Introducing version numbering will hopefully help writing code that can upgrade several versions in a row

Yannick Warnier 18 years ago
parent
commit
f05b48973f

+ 687 - 0
main/install/update-db-1.6.x-1.8.0.inc.php

@@ -0,0 +1,687 @@
+<?php
+/*
+==============================================================================
+	Dokeos - elearning and course management software
+
+	Copyright (c) 2004-2007 Dokeos S.A.
+	Copyright (c) 2003 Ghent University (UGent)
+	Copyright (c) 2001 Universite catholique de Louvain (UCL)
+	Copyright (c) various contributors
+
+	For a full list of contributors, see "credits.txt".
+	The full license can be read in "license.txt".
+
+	This program is free software; you can redistribute it and/or
+	modify it under the terms of the GNU General Public License
+	as published by the Free Software Foundation; either version 2
+	of the License, or (at your option) any later version.
+
+	See the GNU General Public License for more details.
+
+	Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
+	Mail: info@dokeos.com
+==============================================================================
+*/
+/**
+==============================================================================
+* Update the Dokeos database from an older version
+* Notice : This script has to be included by index.php or update_courses.php
+*
+* @package dokeos.install
+* @todo
+* - conditional changing of tables. Currently we execute for example
+* ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
+* - reorganise code into functions
+* @todo use database library
+==============================================================================
+*/
+
+//load helper functions
+require_once("install_upgrade.lib.php");
+
+/*
+==============================================================================
+		MAIN CODE
+==============================================================================
+*/
+
+//check if we come from index.php or update_courses.php - otherwise display error msg
+if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
+{
+	//check if the current Dokeos install is elligible for update
+	if (empty ($updateFromConfigFile) || !file_exists($_POST['updatePath'].$updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version))
+	{
+		echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
+								'.get_lang('PleasGoBackToStep1').'.
+							    <p><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></p>
+							    </td></tr></table></form></body></html>';
+
+		exit ();
+	}
+	
+	//get_config_param() comes from install_functions.inc.php and 
+	//actually gets the param from 
+	$_configuration['db_glue'] = get_config_param('dbGlu');
+
+	if ($singleDbForm)
+	{
+		$_configuration['table_prefix'] = get_config_param('courseTablePrefix');
+	}
+
+	$dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
+
+	if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
+	{
+		$dbScormForm = $dbPrefixForm.$dbScormForm;
+	}
+
+	if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
+	{
+		$dbScormForm = $dbPrefixForm.'scorm';
+	}
+	@mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
+
+	//if error on connection to the database, show error and exit
+	if (mysql_errno() > 0)
+	{
+		$no = mysql_errno();
+		$msg = mysql_error();
+
+		echo '<hr>['.$no.'] - '.$msg.'<hr>
+								'.get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br><br>
+								'.get_lang('PleaseCheckTheseValues').' :<br><br>
+							    <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br>
+								<b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br>
+								<b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br><br>
+								'.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
+							    <p><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
+							    </td></tr></table></form></body></html>';
+
+		exit ();
+	}
+
+	/*
+	-----------------------------------------------------------
+		Normal upgrade procedure:
+		start by updating main, statistic, user databases
+	-----------------------------------------------------------
+	*/
+	//if this script has been included by index.php, not update_courses.php, so
+	// that we want to change the main databases as well...
+	$only_test = true;
+	if (defined('DOKEOS_INSTALL')) 
+	{
+		/**
+		 * Update the databases "pre" migration
+		 */
+		include ("../lang/english/create_course.inc.php");
+
+		if ($languageForm != 'english')
+		{
+			//languageForm has been escaped in index.php
+			include ("../lang/$languageForm/create_course.inc.php");
+		}
+
+		//TODO deal with migrate-db-1.6.x-1.8.0-pre.sql here
+		//get the main queries list (m_q_list)
+		$m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
+		if(count($m_q_list)>0)
+		{
+			//now use the $m_q_list
+			/**
+			 * We connect to the right DB first to make sure we can use the queries
+			 * without a database name
+			 */
+			mysql_select_db($dbNameForm);
+			foreach($m_q_list as $query){
+				if($only_test){
+					echo "mysql_query($dbNameForm,$query)<br/>";
+				}else{
+					$res = mysql_query($query);
+				}
+			}
+		}
+		//manual updates in here
+		//update all registration_date, expiration_date and active fields
+		//$sql_upd = "UPDATE user SET registration_date=NOW()";
+		//$res_upd = mysql_query($sql_upd);
+		//end of manual updates
+		
+		//get the stats queries list (s_q_list)
+		$s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
+		if(count($s_q_list)>0)
+		{
+			//now use the $s_q_list
+			/**
+			 * We connect to the right DB first to make sure we can use the queries
+			 * without a database name
+			 */
+			mysql_select_db($dbStatsForm);
+			foreach($s_q_list as $query){
+				if($only_test){
+					echo "mysql_query($dbStatsForm,$query)<br/>";
+				}else{
+					$res = mysql_query($query);
+				}
+			}
+		}
+		//get the user queries list (u_q_list)
+		$u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
+		if(count($u_q_list)>0)
+		{
+			//now use the $u_q_list
+			/**
+			 * We connect to the right DB first to make sure we can use the queries
+			 * without a database name
+			 */
+			mysql_select_db($dbUserForm);
+			foreach($u_q_list as $query){
+				if($only_test){
+					echo "mysql_query($dbUserForm,$query)<br/>";
+				}else{
+					$res = mysql_query($query);
+				}
+			}
+		}
+		//the SCORM database doesn't need a change in the pre-migrate part - ignore
+
+		die();
+		//TODO only update this table
+		/*
+		$language_table = "`$dbNameForm`.`language`";
+		fill_language_table($language_table);
+		
+		//set the settings from the form or the old config into config settings. 
+		//These settings are considered "safe" because they are entered by the admin
+		$installation_settings['institution_form'] = $institutionForm;
+		$installation_settings['institution_url_form'] = $institutionUrlForm;
+		$installation_settings['campus_form'] = $campusForm;
+		$installation_settings['email_form'] = $emailForm;
+		$installation_settings['admin_last_name'] = $adminLastName;
+		$installation_settings['admin_first_name'] = $adminFirstName;
+		$installation_settings['language_form'] = $languageForm;
+		$installation_settings['allow_self_registration'] = $allowSelfReg;
+		$installation_settings['allow_teacher_self_registration'] = $allowSelfRegProf;
+		$installation_settings['admin_phone_form'] = $adminPhoneForm;
+		
+		//put the settings into the settings table (taken from CSV file)
+		$current_settings_table = "`$dbNameForm`.`settings_current`";
+		fill_current_settings_table($current_settings_table, $installation_settings);
+		
+		//put the options into the options table (taken from CSV file)
+		$settings_options_table = "`$dbNameForm`.`settings_options`";
+		fill_settings_options_table($settings_options_table);
+
+		//mysql_query("INSERT INTO `$dbNameForm`.`course_module` (`name`,`link`,`image`,`row`,`column`,`position`) VALUES
+		//								('AddedLearnpath', NULL, 'scormbuilder.gif', 0, 0, 'external'),
+		//								('".TOOL_BACKUP."', 'coursecopy/backup.php' , 'backup.gif', 2, 1, 'courseadmin'),
+		//								('".TOOL_COPY_COURSE_CONTENT."', 'coursecopy/copy_course.php' , 'copy.gif', 2, 2, 'courseadmin'),
+		//								('".TOOL_RECYCLE_COURSE."', 'coursecopy/recycle_course.php' , 'recycle.gif', 2, 3, 'courseadmin')");
+		//...
+		//mysql_query("UPDATE `$dbNameForm`.`course_module` SET name='".TOOL_LEARNPATH."' WHERE link LIKE 'scorm/%'");
+		*/
+	}
+
+	/*
+	-----------------------------------------------------------
+		Update the Dokeos course databases
+		this part can be accessed in two ways:
+		- from the normal upgrade process
+		- from the script update_courses.php,
+		which is used to upgrade more than MAX_COURSE_TRANSFER courses
+
+		Every time this script is accessed, only
+		MAX_COURSE_TRANSFER courses are upgraded.
+	-----------------------------------------------------------
+	*/
+
+	//get the courses databases queries list (c_q_list)
+	$c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
+	if(count($c_q_list)>0)
+	{
+		//get the courses list
+		mysql_select_db($dbNameForm);
+		$res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
+		if($res===false){die('Error while querying the courses list in update_db.inc.php');}
+		if(mysql_num_rows($res)>0)
+		{
+			while($row = mysql_fetch_array($res))
+			{
+				$list[] = $row;
+			}
+			foreach($list as $row)
+			{
+				//now use the $c_q_list
+				/**
+				 * We connect to the right DB first to make sure we can use the queries
+				 * without a database name
+				 */
+				mysql_select_db($row['db_name']);
+				foreach($c_q_list as $query)
+				{
+					if($only_test)
+					{
+						echo "mysql_query(".$row['db_name'].",$query)<br/>";
+					}else{
+						$res = mysql_query($query);
+					}
+				}
+				//update course manually
+				//update group_category.forum_state
+				//update group_info.tutor_id (put it in group_tutor table?)
+				//update group_info.forum_state, forum_id
+				
+				//update forum tables (migrate from bb_ tables to forum_ tables)
+				//migrate categories
+				$sql_orig = "SELECT * FROM bb_categories";
+				$res_orig = mysql_query($sql_orig);
+				while($row = mysql_fetch_array($res_orig)){
+					$sql = "INSERT INTO forum_category " .
+							"(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
+							"('".$row['cat_id']."','".$row['cat_title']."','','".$row['cat_order']."',0)";
+					$res = mysql_query($sql);
+				}
+				$sql_orig = "SELECT * FROM bb_forums ORDER BY forum_last_post_id desc";
+				$res_orig = mysql_query($sql_orig);
+				$order = 1;
+				while($row = mysql_fetch_array($res_orig)){
+					$sql = "INSERT INTO forum_forum " .
+							"(forum_id,forum_category,allow_edit,forum_comment," .
+							"forum_title," .
+							"forum_last_post, forum_threads," .
+							"locked, forum_posts, " .
+							"allow_new_threads, forum_order) VALUES " .
+							"('".$row['forum_id']."','".$row['cat_id']."',1,'".$row['forum_desc']."'," .
+							"'".$row['forum_name']."'," .
+							"'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
+							"0,'".$row['forum_posts']."'," .
+							"1,$order)";
+					$res = mysql_query($sql);
+					$order++;
+				}
+				$sql_orig = "SELECT * FROM bb_topics";
+				$res_orig = mysql_query($sql_orig);
+				while($row = mysql_fetch_array($res_orig)){
+					//convert time from varchar to datetime
+					$time = $row['topic_time'];
+					$name = $row['prenom']." ".$row['nom'];
+					$sql = "INSERT INTO forum_thread " .
+							"(thread_id,forum_id,thread_poster_id," .
+							"locked,thread_replies,thread_sticky,thread_title," .
+							"thread_poster_name, thread_date, thread_last_post," .
+							"thread_views) VALUES " .
+							"('".$row['topic_id']."','".$row['forum_id']."','".$row['topic_poster']."'," .
+							"0,'".$row['topic_replies']."',0,'".$row['topic_title']."'," .
+							"'$name','$time','".$row['topic_last_post_id']."'," .
+							"'".$row['topic_views']."')";
+					$res = mysql_query($sql);
+				}
+				$sql_orig = "SELECT * FROM bb_posts, bb_posts_text WHERE bb_posts.post_id = bb_posts_text.post_id";
+				$res_orig = mysql_query($sql_orig);
+				while($row = mysql_fetch_array($res_orig)){
+					//convert time from varchar to datetime
+					$time = $row['post_time'];
+					$name = $row['prenom']." ".$row['nom'];
+					$sql = "INSERT INTO forum_post " .
+							"(post_id,forum_id,thread_id," .
+							"poster_id,post_parent_id,visible, " .
+							"post_title,poster_name, post_text, " .
+							"post_date, post_notification) VALUES " .
+							"('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
+							"'".$row['poster_id']."','".$row['parent_id']."',1," .
+							"'".$row['post_title']."','$name', '".$row['post_text']."'," .
+							"'$time',0)";
+					$res = mysql_query($sql);
+				}
+			}
+		}
+	}
+
+	$newPath = str_replace('\\', '/', realpath('../..')).'/';
+
+	$coursePath = array ();
+	$courseDB = array ();
+	$nbr_courses = 0;
+
+	if ($result = mysql_query("SELECT code,db_name,directory,course_language FROM `$dbNameForm`.`course` WHERE target_course_code IS NULL"))
+	{
+		$i = 0;
+
+		$nbr_courses = mysql_num_rows($result);
+
+		while ($i < MAX_COURSE_TRANSFER && (list ($course_code, $mysql_base_course, $directory, $languageCourse) = mysql_fetch_row($result)))
+		{
+			if (!file_exists($newPath.'courses/'.$directory))
+			{
+				if ($singleDbForm)
+				{
+					$prefix = $_configuration['table_prefix'].$mysql_base_course.$_configuration['db_glue'];
+
+					$mysql_base_course = $dbNameForm.'`.`'.$_configuration['table_prefix'].$mysql_base_course;
+				}
+				else
+				{
+					$prefix = '';
+				}
+
+				$coursePath[$course_code] = $directory;
+				$courseDB[$course_code] = $mysql_base_course;
+
+				include ("../lang/english/create_course.inc.php");
+
+				if ($languageCourse != 'english')
+				{
+					include ("../lang/$languageCourse/create_course.inc.php");
+				}
+
+				//TODO process the whole course database migration here. Call an external
+				//script/function to keep this script tidy
+
+				// Set item-properties of dropbox files
+				/*
+				$sql = "SELECT * FROM `$mysql_base_course".$_configuration['db_glue']."dropbox_file` f, `".$_configuration['db_glue']."_base_course".$_configuration['db_glue']."dropbox_post` p WHERE f.id = p.file_id";
+				$res = mysql_query($sql);
+				while ($obj = mysql_fetch_object($res))
+				{
+					$sql = "INSERT INTO `$mysql_base_course".$_configuration['db_glue']."item_property` SET ";
+					$sql .= " tool = '".TOOL_DROPBOX."', ";
+					$sql .= " insert_date = '".$obj->upload_date."', ";
+					$sql .= " lastedit_date = '".$obj->last_upload_date."', ";
+					$sql .= " ref = '".$obj->id."', ";
+					$sql .= " lastedit_type = 'DropboxFileAdded', ";
+					$sql .= " to_group_id = '0', ";
+					$sql .= " to_user_id = '".$obj->dest_user_id."', ";
+					$sql .= " insert_user_id = '".$obj->uploader_id."'";
+					mysql_query($sql);
+				}
+				*/
+
+				$i ++;
+			}
+			else
+			{
+				$nbr_courses --;
+			}
+		}
+	}
+}
+else
+{
+	echo 'You are not allowed here !';
+}
+
+
+
+
+
+
+
+
+/**
+* This function stores the forum category in the database. The new category is added to the end. 
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function store_forumcategory($values)
+{
+	global $table_categories; 
+	global $_course;
+	global $_user; 
+	
+	// find the max cat_order. The new forum category is added at the end => max cat_order + &
+	$sql="SELECT MAX(cat_order) as sort_max FROM ".mysql_real_escape_string($table_categories);
+	$result=api_sql_query($sql);
+	$row=mysql_fetch_array($result);
+	$new_max=$row['sort_max']+1;
+
+	$sql="INSERT INTO ".$table_categories." (cat_title, cat_comment, cat_order) VALUES ('".mysql_real_escape_string($values['forum_category_title'])."','".mysql_real_escape_string($values['forum_category_comment'])."','".mysql_real_escape_string($new_max)."')";
+	api_sql_query($sql); 
+	$last_id=mysql_insert_id();
+	api_item_property_update($_course, TOOL_FORUM_CATEGORY, $last_id,"ForumCategoryAdded", $_user['user_id']);
+	return array('id'=>$last_id,'title'=>$values['forum_category_title']) ;
+}
+
+/**
+* This function stores the forum in the database. The new forum is added to the end. 
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function store_forum($values)
+{
+	global $table_forums; 
+	global $_course;
+	global $_user; 
+
+	// find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
+	$sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category=".mysql_real_escape_string($values['forum_category']);
+	$result=api_sql_query($sql);
+	$row=mysql_fetch_array($result);
+	$new_max=$row['sort_max']+1;
+
+	
+	$sql="INSERT INTO ".$table_forums." 
+				(forum_title, forum_comment, forum_category, allow_anonymous, allow_edit, approval_direct_post, allow_attachments, allow_new_threads, default_view, forum_of_group, forum_group_public_private, forum_order) 
+				VALUES ('".mysql_real_escape_string($values['forum_title'])."',
+					'".mysql_real_escape_string($values['forum_comment'])."',
+					'".mysql_real_escape_string($values['forum_category'])."',
+					'".mysql_real_escape_string($values['allow_anonymous_group']['allow_anonymous'])."',
+					'".mysql_real_escape_string($values['students_can_edit_group']['students_can_edit'])."',
+					'".mysql_real_escape_string($values['approval_direct_group']['approval_direct'])."',
+					'".mysql_real_escape_string($values['allow_attachments_group']['allow_attachments'])."',
+					'".mysql_real_escape_string($values['allow_new_threads_group']['allow_new_threads'])."', 
+					'".mysql_real_escape_string($values['default_view_type_group']['default_view_type'])."', 
+					'".mysql_real_escape_string($values['group_forum'])."', 
+					'".mysql_real_escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."', 
+					'".mysql_real_escape_string($new_max)."')";
+	api_sql_query($sql, __LINE__,__FILE__); 
+	$last_id=mysql_insert_id();
+	api_item_property_update($_course, TOOL_FORUM, $last_id,"ForumCategoryAdded", $_user['user_id']);
+	return array('id'=>$last_id, 'title'=>$values['forum_title']);
+}
+
+/**
+* This function stores a new thread. This is done through an entry in the forum_thread table AND 
+* in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet))
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function store_thread($values)
+{
+	global $table_threads; 
+	global $table_posts; 
+	global $_user; 
+	global $_course; 
+	global $current_forum; 
+	
+	// We first store an entry in the forum_thread table because the thread_id is used in the forum_post table
+	$sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_views, thread_date, thread_sticky) 
+			VALUES ('".mysql_real_escape_string($values['post_title'])."',
+					'".mysql_real_escape_string($values['forum_id'])."',
+					'".mysql_real_escape_string($values['user_id'])."', 
+					'".mysql_real_escape_string($values['poster_name'])."', 
+					'".mysql_real_escape_string($values['topic_views'])."', 
+					'".mysql_real_escape_string($values['post_date'])."',
+					'".mysql_real_escape_string($values['thread_sticky'])."')";
+	$result=api_sql_query($sql, __LINE__, __FILE__);
+	$last_thread_id=mysql_insert_id();
+	api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", $_user['user_id']);
+	// if the forum properties tell that the posts have to be approved we have to put the whole thread invisible
+	// because otherwise the students will see the thread and not the post in the thread. 
+	// we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have 
+	// to make the thread visible AND the post
+	if ($values['visible']==0)
+	{
+		api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", $_user['user_id']);
+		$visible=1;
+	}
+
+	return $last_thread_id; 
+}
+
+/**
+* This function migrates the threads of a given phpbb forum to a new forum of the new forum tool
+* @param $phpbb_forum_id the forum_id of the old (phpbb) forum
+* @param $new_forum_id the forum_id in the new forum
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function migrate_threads_of_forum($phpbb_forum_id, $new_forum_id)
+{
+	global $phpbb_threads; 
+	global $table_forums; 
+	
+	$table_users = Database :: get_main_table(TABLE_MAIN_USER);
+	
+	$sql_phpbb_threads="SELECT forum.*, users.user_id 
+							FROM $phpbb_threads forum, $table_users users 
+							WHERE forum_id='".mysql_real_escape_string($phpbb_forum_id)."' 
+							AND forum.nom=users.lastname AND forum.prenom=users.firstname
+							";
+	$result_phpbb_threads=api_sql_query($sql_phpbb_threads);
+	$threads_counter=0;
+	while ($row_phpbb_threads=mysql_fetch_array($result_phpbb_threads))
+	{
+		$values['post_title']=$row_phpbb_threads['topic_title']; 
+		$values['forum_id']=$new_forum_id;
+		$values['user_id']=$row_phpbb_threads['user_id'];
+		$values['poster_name']=0;
+		$values['topic_views']=$row_phpbb_threads['topic_views'];
+		$values['post_date']=$row_phpbb_threads['topic_time'];
+		$values['thread_sticky']=0;
+		$values['visible']=$row_phpbb_threads['visible'];
+		//my_print_r($values);
+		$new_forum_thread_id=store_thread($values);
+		
+		// now we migrate the posts of the given thread
+		$posts_counter=$posts_counter+migrate_posts_of_thread($row_phpbb_threads['topic_id'], $new_forum_thread_id, $new_forum_id);
+		
+		$threads_counter++;
+	}
+	
+	// Now we update the forum_forum table with the total number of posts for the given forum.
+	$sql="UPDATE $table_forums 
+			SET forum_posts='".mysql_real_escape_string($posts_counter)."',
+			forum_threads='".mysql_real_escape_string($threads_counter)."' 
+			WHERE forum_id='".mysql_real_escape_string($new_forum_id)."'";
+	//echo $sql; 
+	$result=api_sql_query($sql);
+	return array("threads"=>$threads_counter, "posts"=>$posts_counter);
+}
+
+/**
+* This function migrates the posts of a given phpbb thread (topic) to a thread in the new forum tool
+* @param $phpbb_forum_id the forum_id of the old (phpbb) forum
+* @param $new_forum_id the forum_id in the new forum
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function migrate_posts_of_thread($phpbb_thread_id, $new_forum_thread_id, $new_forum_id)
+{
+	global $phpbb_posts;
+	global $phpbb_poststext; 
+	global $table_posts; 
+	global $table_threads; 
+	global $added_resources; 
+	
+	$table_users = Database :: get_main_table(TABLE_MAIN_USER);
+	$table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
+	
+	
+	$post_counter=0;
+	
+	$sql_phpbb_posts="SELECT posts.*, posts_text.*, users.user_id, users.lastname, users.firstname FROM $phpbb_posts posts, $phpbb_poststext posts_text, $table_users users 
+						WHERE posts.post_id=posts_text.post_id
+						AND posts.nom=users.lastname 
+						AND posts.prenom=users.firstname
+						AND posts.topic_id='".mysql_real_escape_string($phpbb_thread_id)."'
+						";
+	$result_phpbb_posts=api_sql_query($sql_phpbb_posts);
+	while($row_phpbb_posts=mysql_fetch_array($result_phpbb_posts))
+	{
+		$values=array();
+		$values['post_title']=$row_phpbb_posts['post_title'];
+		$values['post_text']=$row_phpbb_posts['post_text'];
+		$values['thread_id']=$new_forum_thread_id;
+		$values['forum_id']=$new_forum_id; 
+		$values['user_id']=$row_phpbb_posts['user_id']; 
+		$values['post_date']=$row_phpbb_posts['post_time'];
+		$values['post_notification']=$row_phpbb_posts['topic_notify'];
+		$values['post_parent_id']=0; 
+		$values['visible']=1;		
+	
+		// We first store an entry in the forum_post table
+		$sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id,  post_date, post_notification, post_parent_id, visible) 
+				VALUES ('".mysql_real_escape_string($values['post_title'])."',
+						'".mysql_real_escape_string($values['post_text'])."',
+						'".mysql_real_escape_string($values['thread_id'])."',
+						'".mysql_real_escape_string($values['forum_id'])."',
+						'".mysql_real_escape_string($values['user_id'])."',
+						'".mysql_real_escape_string($values['post_date'])."',
+						'".mysql_real_escape_string($values['post_notification'])."',
+						'".mysql_real_escape_string($values['post_parent_id'])."',
+						'".mysql_real_escape_string($values['visible'])."')";
+		$result=api_sql_query($sql, __LINE__, __FILE__);
+		$post_counter++;
+		$last_post_id=mysql_insert_id();
+		
+		
+		// We check if there added resources and if so we update them
+		if (in_array($row_phpbb_posts['post_id'],$added_resources))
+		{
+			$sql_update_added_resource="UPDATE $table_added_resources 
+					SET source_type='forum_post', source_id='".mysql_real_escape_string($last_post_id)."'
+					WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."' AND source_id='".mysql_real_escape_string($row_phpbb_posts['post_id'])."'";
+			echo $sql_update_added_resource;
+			$result=api_sql_query($sql_update_added_resource, __LINE__, __FILE__);
+		}
+
+
+	}
+	
+	// update the thread_last_post of the post table AND the 
+	$sql="UPDATE $table_threads SET thread_last_post='".mysql_real_escape_string($last_post_id)."', 
+			thread_replies='".mysql_real_escape_string($post_counter-1)."'
+			WHERE thread_id='".mysql_real_escape_string($new_forum_thread_id)."'";
+	//echo $sql; 
+	$result=api_sql_query($sql, __LINE__, __FILE__);
+	//echo $sql; 
+	return $post_counter; 
+}
+
+/**
+* This function gets all the added resources for phpbb forum posts
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function get_added_resources()
+{
+	$table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
+	$return_array=array();
+
+	// TODO: now we also migrate the added resources. 
+	$sql_added_resources="SELECT * FROM $table_added_resources WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."'";
+	$result=api_sql_query($sql_added_resources);
+	while ($row=mysql_fetch_array($result))
+	{
+		$return_array[]=$row['source_id'];
+	}
+	return $return_array;
+}
+
+/**
+* This function gets the forum category information based on the name 
+* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+* @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
+*/
+function get_forumcategory_id_by_name($forum_category_name)
+{
+	global $table_categories;
+	
+	$sql="SELECT cat_id FROM $table_categories WHERE cat_title='".mysql_real_escape_string($forum_category_name)."'";
+	//echo $sql; 
+	$result=api_sql_query($sql,__LINE__,__FILE__);
+	$row=mysql_fetch_array($result);
+	//echo $row['cat_id'];
+	return $row['cat_id'];
+}
+?>

+ 247 - 0
main/install/update-files-1.6.x-1.8.0.inc.php

@@ -0,0 +1,247 @@
+<?php
+/*
+==============================================================================
+	Dokeos - elearning and course management software
+
+	Copyright (c) 2004 Dokeos S.A.
+	Copyright (c) 2003 Ghent University (UGent)
+	Copyright (c) 2001 Universite catholique de Louvain (UCL)
+
+	For a full list of contributors, see "credits.txt".
+	The full license can be read in "license.txt".
+
+	This program is free software; you can redistribute it and/or
+	modify it under the terms of the GNU General Public License
+	as published by the Free Software Foundation; either version 2
+	of the License, or (at your option) any later version.
+
+	See the GNU General Public License for more details.
+
+	Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
+	Mail: info@dokeos.com
+==============================================================================
+*/
+/**
+==============================================================================
+* Updates the Dokeos files from an older version
+* IMPORTANT: This script has to be included by install/index.php and update_courses.php
+*
+* DOKEOS_INSTALL is defined in the install/index.php
+* DOKEOS_COURSE_UPDATE is defined in update_courses.php
+*
+* When DOKEOS_INSTALL or DOKEOS_COURSE_UPDATE is defined, do for every course:
+* - remove the .htaccess in the document folder
+* - remove the index.php in the group folder
+* - write a new group/index.php file, make it an empty html file
+* - remove the index.php of the course folder
+* - write a new index.php file in the course folder, with some settings
+* - create a 'temp' directory in the course folder
+* - move the course folder inside the courses folder of the new Dokeos installation
+* - move the group documents from the group folder to the document folder,
+*   keeping subfolders intact
+* - stores all documents inside the database (document and item_property tables)
+* - remove the visibility field from the document table
+* - update the item properties of the group documents
+*
+* Additionally, when DOKEOS_INSTALL is defined
+* - write a config file, configuration.php, with important settings
+* - write a .htaccess file (with instructions for Apache) in the courses directory
+* - remove the new main/upload/users directory and rename the main/img/users
+*   directory of the old version to main/upload/users
+* - rename the old configuration.php to configuration.php.old,
+*   or if this fails delete the old configuration.php
+*
+* @package dokeos.install
+==============================================================================
+*/
+/*
+==============================================================================
+		FUNCTIONS
+==============================================================================
+*/
+
+/**
+* This function puts the documents of the upgraded courses
+* into the necessary tables of the new version:
+* the document and item_property tables.
+*
+* It is used to upgrade from Dokeos 1.5.x versions to
+* Dokeos 1.6
+*
+* @return boolean true if everything worked, false otherwise
+*/
+function fill_document_table($dir)
+{
+	global $newPath, $course, $mysql_base_course, $_configuration;
+
+	$documentPath = $newPath.'courses/'.$course.'/document';
+
+	if (!@ $opendir = opendir($dir))
+	{
+		return false;
+	}
+
+	while ($readdir = readdir($opendir))
+	{
+		if ($readdir != '..' && $readdir != '.' && $readdir != '.htaccess')
+		{
+			$path = str_replace($documentPath, '', $dir.'/'.$readdir);
+			$file_date = date("Y-m-d H:i:s", filemtime($dir.'/'.$readdir));
+
+			if (is_file($dir.'/'.$readdir))
+			{
+				$file_size = filesize($dir.'/'.$readdir);
+
+				$result = mysql_query("SELECT id,visibility FROM `$mysql_base_course".$_configuration['db_glue']."document` WHERE path='".addslashes($path)."' LIMIT 0,1");
+
+				if (list ($id, $visibility) = mysql_fetch_row($result))
+				{
+					mysql_query("UPDATE `$mysql_base_course".$_configuration['db_glue']."document` SET filetype='file',title='".addslashes($readdir)."',size='$file_size' WHERE id='$id' AND path='".addslashes($path)."'");
+				}
+				else
+				{
+					mysql_query("INSERT INTO `$mysql_base_course".$_configuration['db_glue']."document`(path,filetype,title,size) VALUES('".addslashes($path)."','file','".addslashes($readdir)."','$file_size')");
+
+					$id = mysql_insert_id();
+				}
+
+				$visibility = ($visibility == 'v') ? 1 : 0;
+
+				mysql_query("INSERT INTO `$mysql_base_course".$_configuration['db_glue']."item_property`(tool,ref,visibility,lastedit_type,to_group_id,insert_date,lastedit_date) VALUES('document','$id','$visibility','DocumentAdded','0','".$file_date."','".$file_date."')");
+			}
+			elseif (is_dir($dir.'/'.$readdir))
+			{
+				$result = mysql_query("SELECT id,visibility FROM `$mysql_base_course".$_configuration['db_glue']."document` WHERE path='".addslashes($path)."' LIMIT 0,1");
+
+				if (list ($id, $visibility) = mysql_fetch_row($result))
+				{
+					mysql_query("UPDATE `$mysql_base_course".$_configuration['db_glue']."document` SET filetype='folder',title='".addslashes($readdir)."' WHERE id='$id' AND path='".addslashes($path)."'");
+				}
+				else
+				{
+					mysql_query("INSERT INTO `$mysql_base_course".$_configuration['db_glue']."document`(path,filetype,title) VALUES('".addslashes($path)."','folder','".addslashes($readdir)."')");
+
+					$id = mysql_insert_id();
+				}
+
+				$visibility = ($visibility == 'v') ? 1 : 0;
+
+				mysql_query("INSERT INTO `$mysql_base_course".$_configuration['db_glue']."item_property`(tool,ref,visibility, lastedit_type, to_group_id,insert_date,lastedit_date) VALUES('document','$id','$visibility','FolderCreated','0','".$file_date."','".$file_date."')");
+
+				if (!fill_document_table($dir.'/'.$readdir))
+				{
+					return false;
+				}
+			}
+		}
+	}
+
+	closedir($opendir);
+
+	return true;
+}
+
+/*
+==============================================================================
+		INIT SECTION
+==============================================================================
+*/
+
+if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
+{
+	$newPath = str_replace('\\', '/', realpath('../..')).'/';
+	$oldPath = $_POST['updatePath'];
+
+	foreach ($coursePath as $key => $course)
+	{
+		$mysql_base_course = $courseDB[$key];
+
+		@ unlink($oldPath.$course.'/document/.htaccess');
+
+		@ unlink($oldPath.$course.'/group/index.php');
+
+		if ($fp = @ fopen($oldPath.$course.'/group/index.php', 'w'))
+		{
+			fputs($fp, '<html></html>');
+
+			fclose($fp);
+		}
+
+		@ unlink($oldPath.$course.'/index.php');
+
+		if ($fp = @ fopen($oldPath.$course.'/index.php', 'w'))
+		{
+			fputs($fp, '<?php
+															$cidReq = "'.$key.'";
+															$dbname = "'.str_replace($dbPrefixForm, '', $mysql_base_course).'";
+									
+															include("../../main/course_home/course_home.php");
+															?>');
+
+			fclose($fp);
+		}
+
+		@ mkdir($oldPath.$course.'/temp', 0777);
+
+		@ rename($oldPath.$course, $newPath.'courses/'.$course);
+
+		// Move group documents to document folder of the course
+		$group_dir = $newPath.'courses/'.$course.'/group';
+
+		if ($dir = @ opendir($group_dir))
+		{
+			while (($entry = readdir($dir)) !== false)
+			{
+				if ($entry != '.' && $entry != '..' && is_dir($group_dir.'/'.$entry))
+				{
+					$from_dir = $group_dir.'/'.$entry;
+					$to_dir = $newPath.'courses/'.$course.'/document/'.$entry;
+
+					@ rename($from_dir, $to_dir);
+				}
+			}
+
+			closedir($dir);
+		}
+
+		fill_document_table($newPath.'courses/'.$course.'/document');
+
+		mysql_query("ALTER TABLE `$mysql_base_course".$_configuration['db_glue']."document` DROP `visibility`");
+
+		// Update item_properties of group documents
+		$sql = "SELECT d.id AS doc_id, g.id AS group_id FROM `$mysql_base_course".$_configuration['db_glue']."group_info` g,`$mysql_base_course".$_configuration['db_glue']."document` d WHERE path LIKE CONCAT(g.secret_directory,'%')";
+		$res = mysql_query($sql);
+
+		while ($group_doc = mysql_fetch_object($res))
+		{
+			$sql = "UPDATE `$mysql_base_course".$_configuration['db_glue']."item_property` SET to_group_id = '".$group_doc->group_id."', visibility = '1' WHERE ref = '".$group_doc->doc_id."' AND tool = '".TOOL_DOCUMENT."'";
+			mysql_query($sql);
+		}
+	}
+
+	if (defined('DOKEOS_INSTALL'))
+	{
+		// Write the Dokeos config file
+		write_dokeos_config_file($newPath.'main/inc/conf/configuration.php');
+		// Write a distribution file with the config as a backup for the admin
+		write_dokeos_config_file($newPath.'main/inc/conf/configuration.dist.php');
+		// Write a .htaccess file in the course repository
+		write_courses_htaccess_file($urlAppendPath);
+
+		require_once ('../inc/lib/fileManage.lib.php');
+		// First remove the upload/users directory in the new installation
+		removeDir($newPath.'main/upload/users');
+		// Move the old user images to the new installation
+		@ rename($oldPath.'main/img/users', $newPath.'main/upload/users');
+
+		if (!@ rename($oldPath.'main/inc/conf/configuration.php', $oldPath.'main/inc/conf/configuration.php.old'))
+		{
+			unlink($oldPath.'main/inc/conf/configuration.php');
+		}
+	}
+}
+else
+{
+	echo 'You are not allowed here !';
+}
+?>