} else {
                    		iDatabase::select_db($dbNameForm);
                    	}
                    	
                    	//Count of rows                    	
                        $sql 	= "SHOW TABLES LIKE '$just_table_name'";
                        $result = iDatabase::query($sql);
                        
                        if (Database::num_rows($result)) {
                        
                            $sql 	= "SELECT count(*) FROM $old_table";
                            $result = iDatabase::query($sql);

                            $old_count = 0;
                            if ($result) {
                                $row 		= iDatabase::fetch_row($result);
                                $old_count = $row[0];
                            } else {
                                Log::error("Count(*) in table $old_table failed");
                            }                       
                        
                            Log::notice("# rows in $old_table: $old_count");

                            $sql = "SELECT * FROM $old_table";
                            $result = iDatabase::query($sql);

                            $count = 0;
                            
                            /* Loads the main database */
                            iDatabase::select_db($dbNameForm);
                            
                    $reflink_flip = array_flip($reflink_unique);

                    if (is_array($wiki_id)) {
	                    foreach ($wiki_id as $key => $wiki_page) {
	                    	$pag_id = $reflink_flip[$reflink[$key]];
	                    	$sql= "UPDATE $t_wiki SET page_id='".($pag_id + 1)."' WHERE id = '$wiki_page'";
	                    	$res_update = iDatabase::query($sql);
	                    }
                    }

                    // Insert page_id into wiki_conf table, actually this table is empty
				   	$query = "SELECT DISTINCT page_id FROM $t_wiki ORDER BY page_id";
				   	$myres_wiki = iDatabase::query($query);

				   	if (iDatabase::num_rows($myres_wiki) > 0) {
					   	while ($row_wiki = iDatabase::fetch_row($myres_wiki)) {
					   		$page_id = $row_wiki[0];
					   		$query = "INSERT INTO ".$t_wiki_conf." (page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3) VALUES ('".$page_id."','','','','','','','')";
	                   		$myres_wiki_conf = iDatabase::query($query);
					   	}
				   	}

   				}
			}
		}
	}

} else {

	echo 'You are not allowed here !' . __FILE__;
            $upd = "UPDATE $dbNameForm.user SET hr_dept_id = 0";
            iDatabase::query($upd);
        }

        // Updating score display for each gradebook category
        // first we check if there already is migrated data to categoy_id field
        $query = "SELECT id FROM $dbNameForm.gradebook_score_display WHERE category_id = 0";
        $rs_check = iDatabase::query($query);

        if (iDatabase::num_rows($rs_check) > 0) {
            // get all gradebook categories id
            $a_categories = array();
            $query = "SELECT id FROM $dbNameForm.gradebook_category";
            $rs_gradebook = iDatabase::query($query);
            if (iDatabase::num_rows($rs_gradebook) > 0) {
                while ($row_gradebook = iDatabase::fetch_row($rs_gradebook)) {
                    $a_categories[] = $row_gradebook[0];
                }
            }

            // get all gradebook score display
            $query = "SELECT * FROM $dbNameForm.gradebook_score_display";
            $rs_score_display = iDatabase::query($query);
            if (iDatabase::num_rows($rs_score_display) > 0) {
                $score_color_percent = api_get_setting('gradebook_score_display_colorsplit');
                while ($row_score_display = iDatabase::fetch_array($rs_score_display)) {
                    $score = $row_score_display['score'];
                    $display = $row_score_display['display'];
                    foreach ($a_categories as $category_id) {
                        $ins = "INSERT INTO $dbNameForm.gradebook_score_display(score, display, category_id, score_color_percent) VALUES('$score', '$display', $category_id, '$score_color_percent')";
                        iDatabase::query($ins);