Index: openacs-4/packages/bookmarks/sql/postgresql/bookmarks-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/bookmarks/sql/postgresql/bookmarks-create.sql,v
diff -u -r1.27 -r1.28
--- openacs-4/packages/bookmarks/sql/postgresql/bookmarks-create.sql	26 Jan 2016 08:43:56 -0000	1.27
+++ openacs-4/packages/bookmarks/sql/postgresql/bookmarks-create.sql	27 Jan 2016 11:03:44 -0000	1.28
@@ -647,8 +647,8 @@
 END;
 $$ LANGUAGE plpgsql;
 
-CREATE FUNCTION bookmark__toggle_open_close_all (integer, boolean, integer)
-RETURNS integer AS '
+CREATE OR REPLACE FUNCTION bookmark__toggle_open_close_all (bigint, boolean, integer)
+RETURNS integer AS $$
 DECLARE
 	p_browsing_user_id ALIAS FOR $1;	-- in bm_bookmarks.owner_id%TYPE,
 	p_closed_p ALIAS FOR $2;		-- in bm_in_closed_p.closed_p%TYPE default f,
@@ -678,7 +678,7 @@
 
 	RETURN 0;
 END;
-' LANGUAGE 'plpgsql';
+$$ LANGUAGE plpgsql;
 
 
 CREATE FUNCTION bookmark__new_root_folder (integer, integer)
Index: openacs-4/packages/bookmarks/sql/postgresql/upgrade/upgrade-5.1-5.2.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/bookmarks/sql/postgresql/upgrade/upgrade-5.1-5.2.sql,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/bookmarks/sql/postgresql/upgrade/upgrade-5.1-5.2.sql	26 Jan 2016 08:43:56 -0000	1.1
+++ openacs-4/packages/bookmarks/sql/postgresql/upgrade/upgrade-5.1-5.2.sql	27 Jan 2016 11:03:44 -0000	1.2
@@ -3,6 +3,7 @@
 DROP FUNCTION IF EXISTS bookmark__toggle_open_close (integer, integer);
 DROP FUNCTION IF EXISTS bookmark__update_in_closed_p_one_user (integer, integer);
 DROP FUNCTION IF EXISTS bookmark__initialize_in_closed_p (integer, integer, integer);
+DROP FUNCTION IF EXISTS bookmark__toggle_open_close_all (integer, boolean, integer);
 
 CREATE OR REPLACE FUNCTION bookmark__toggle_open_close (integer, bigint)
 RETURNS integer AS $$
@@ -143,3 +144,35 @@
 $$ LANGUAGE plpgsql;
 
 
+CREATE OR REPLACE FUNCTION bookmark__toggle_open_close_all (bigint, boolean, integer)
+RETURNS integer AS $$
+DECLARE
+	p_browsing_user_id ALIAS FOR $1;	-- in bm_bookmarks.owner_id%TYPE,
+	p_closed_p ALIAS FOR $2;		-- in bm_in_closed_p.closed_p%TYPE default f,
+	p_root_id ALIAS FOR $3;			-- in bm_bookmarks.parent_id%TYPE
+
+BEGIN
+	-- Change the value of closed_p for all folders belonging to the
+	-- user (except the root folder)
+	UPDATE bm_in_closed_p SET closed_p = p_closed_p
+	WHERE bookmark_id IN 
+	(
+		SELECT bm.bookmark_id FROM bm_bookmarks bm, bm_bookmarks bm2
+		WHERE tree_level(bm.tree_sortkey) > 1
+		  and bm2.bookmark_id = p_root_id
+                  and bm.tree_sortkey between bm2.tree_sortkey and tree_right(bm2.tree_sortkey)
+	); 
+
+	-- Update the value of in_closed_p for all bookmarks belonging to 
+	-- this user. We close/open all bookmarks except the top level ones.
+	UPDATE bm_in_closed_p SET in_closed_p = p_closed_p
+	WHERE bookmark_id IN 
+	(
+		SELECT bookmark_id FROM bm_bookmarks 
+		WHERE  tree_level(tree_sortkey) > 2
+	)
+	AND in_closed_p_id = p_browsing_user_id;	 	
+
+	RETURN 0;
+END;
+$$ LANGUAGE plpgsql;