Index: openacs-4/packages/imsld/catalog/imsld.en_US.ISO-8859-1.xml
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/catalog/imsld.en_US.ISO-8859-1.xml,v
diff -u -r1.34.4.5 -r1.34.4.6
--- openacs-4/packages/imsld/catalog/imsld.en_US.ISO-8859-1.xml	2 Jul 2007 15:22:08 -0000	1.34.4.5
+++ openacs-4/packages/imsld/catalog/imsld.en_US.ISO-8859-1.xml	9 Jul 2007 16:09:50 -0000	1.34.4.6
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
-<message_catalog package_key="imsld" package_version="1.1d" locale="en_US" charset="ISO-8859-1">
+<message_catalog package_key="imsld" package_version="1.2d" locale="en_US" charset="ISO-8859-1">
 
   <msg key="_Assigned_users">| Assigned users:</msg>
   <msg key="_Component">Component Identifier</msg>
@@ -146,13 +146,10 @@
   <msg key="Learning_Object">Learning Object</msg>
   <msg key="Learning_Objects">Learning Objects</msg>
   <msg key="Level">Level</msg>
-  <msg key="Local_Properties">Local Properties</msg>
   <msg key="li_activity_title_li">&lt;li&gt; %activity_title% &lt;/li&gt;</msg>
   <msg key="li_file_name_li">&lt;li&gt; %file_name% &lt;/li&gt;</msg>
   <msg key="li_impozzible_li">&lt;li&gt; impozzible &lt;/li&gt;</msg>
-  <msg key="lt_Local-personal_Prop">Local-personal Properties</msg>
-  <msg key="lt_Local-role_Properti">Local-role Properties</msg>
-  <msg key="lt_Global-personal_Pro">Global-personal Properties</msg>
+  <msg key="Local_Properties">Local Properties</msg>
   <msg key="lt___imsldcreate_new_run">[_ imsld.create_new_run]</msg>
   <msg key="lt_Activity_Description_">Activity Description Identifier</msg>
   <msg key="lt_Activity_Structure_Id">Activity Structure Identifier</msg>
@@ -186,6 +183,7 @@
   <msg key="lt_Expression_One_Identi">Expression One Identifier</msg>
   <msg key="lt_Expression_Two_Identi">Expression Two Identifer</msg>
   <msg key="lt_Finish_role_managemen">Finish role management</msg>
+  <msg key="lt_Global-personal_Pro">Global-personal Properties</msg>
   <msg key="lt_h2Uploading_new_IMS_L">&lt;h2&gt;Uploading new IMS Learning Design&lt;/h2&gt;</msg>
   <msg key="lt_If_you_are_not_sure_t">If you are not sure that everything is OK, please</msg>
   <msg key="lt_Import_IMS-LD_ZIP_Fil">Import IMS-LD ZIP File</msg>
@@ -231,6 +229,8 @@
   <msg key="lt_Learning_Objectives_I">Learning Objectives ID</msg>
   <msg key="lt_Learning_Unit_finishe">Learning Unit finished</msg>
   <msg key="lt_li_desc_no_file_assoc">&lt;li&gt; desc (no file associated) &lt;/li&gt;</msg>
+  <msg key="lt_Local-personal_Prop">Local-personal Properties</msg>
+  <msg key="lt_Local-role_Properti">Local-role Properties</msg>
   <msg key="lt_manifest_namespace_is">manifest namespace is not imscp</msg>
   <msg key="lt_Max_number_of_student">Max. number of students per group: %max_persons%</msg>
   <msg key="lt_Members_not_allowed_n">Members $not_allowed_name are already members of other subroles of role $parent_role_name.</msg>
@@ -347,6 +347,7 @@
   <msg key="lt_Unestable_state_reach">Unestable state reached</msg>
   <msg key="lt_Unstable_state_reache">Unstable state reached when evaluating conditions (An infinite loop was detected). Please, contact the administrator and review the conditions of the UoL.</msg>
   <msg key="lt_Uploading_and_process">Uploading and processing your course, please wait...</msg>
+  <msg key="lt_User_does_not_belong_">User does not belong to any role instance</msg>
   <msg key="lt_username_br__________">%username% &lt;br&gt; 
                        %sender_name% has triggered this notification. Please do not replay to this email. &lt;br&gt;&lt;br&gt; 
                        Go to the UoL: &lt;a href=\&quot;%imsld_url%\&quot;&gt;%imsld_title%&lt;/a&gt; (in the community: &lt;a href=\&quot;%community_url%\&quot;&gt;%community_name%&lt;/a&gt; community) for further details.</msg>
@@ -386,8 +387,8 @@
   <msg key="No_IMS_LD">No IMS LD</msg>
   <msg key="No_info_was_found">No info was found</msg>
   <msg key="No_properties_found">No properties found.</msg>
-  <msg key="No_users">No users</msg>
   <msg key="No_user_selected">No user selected.</msg>
+  <msg key="No_users">No users</msg>
   <msg key="None">None</msg>
   <msg key="None_please_select">None, please select</msg>
   <msg key="Not_defined">Not defined</msg>
@@ -469,16 +470,17 @@
   <msg key="units-of-learning_Administration">Units of Learning Administration</msg>
   <msg key="Uploading_IMS_LD">Uploading IMS LD</msg>
   <msg key="URI">URI</msg>
-  <msg key="User_Choice">User Choice?</msg>
-  <msg key="User_activity_reports">User activity reports</msg>
   <msg key="user">user</msg>
+  <msg key="User_activity_reports">User activity reports</msg>
+  <msg key="User_Choice">User Choice?</msg>
   <msg key="Users">Users</msg>
   <msg key="users">users</msg>
   <msg key="Value">Value</msg>
   <msg key="Version">Version</msg>
   <msg key="view_file">view file</msg>
   <msg key="View_members">View members</msg>
   <msg key="View_Roles">View Roles</msg>
+  <msg key="Viewed">Viewed</msg>
   <msg key="waiting">waiting</msg>
   <msg key="When_Condition_True">When Condition True</msg>
   <msg key="With_Control">With Control</msg>
Index: openacs-4/packages/imsld/tcl/imsld-condition-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-condition-procs.tcl,v
diff -u -r1.24.4.1 -r1.24.4.2
--- openacs-4/packages/imsld/tcl/imsld-condition-procs.tcl	27 Jun 2007 10:21:58 -0000	1.24.4.1
+++ openacs-4/packages/imsld/tcl/imsld-condition-procs.tcl	9 Jul 2007 16:09:51 -0000	1.24.4.2
@@ -303,7 +303,7 @@
             and content_revision__is_live(activity_id) = 't'
         }] } {
             # mark the support activity completed for all the users in the run
-            set role_part_id_list [imsld::get_role_part_from_activity -activity_type learning -leaf_id $activity_item_id]
+            set role_part_id_list [imsld::get_role_part_from_activity -activity_type support -leaf_id $activity_item_id]
 
             set users_in_run [db_list get_users_in_run {
                 select gmm.member_id 
@@ -331,7 +331,7 @@
                         -act_id $act_id \
                         -role_part_id $role_part_id \
                         -element_id $activity_id \
-                        -type learning \
+                        -type support \
                         -code_call
                 }
             }
@@ -356,6 +356,7 @@
                 and ar1.object_id_one=gmm.group_id 
                 group by member_id
             }]
+
             foreach user $users_in_run {
                 if { [imsld::user_participate_p -run_id $run_id -act_id $act_id -user_id $user] } {
                     imsld::mark_act_finished -act_id $act_id \
Index: openacs-4/packages/imsld/tcl/imsld-monitor-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-monitor-procs.tcl,v
diff -u -r1.2.4.3 -r1.2.4.4
--- openacs-4/packages/imsld/tcl/imsld-monitor-procs.tcl	28 Jun 2007 15:12:32 -0000	1.2.4.3
+++ openacs-4/packages/imsld/tcl/imsld-monitor-procs.tcl	9 Jul 2007 16:09:51 -0000	1.2.4.4
@@ -480,7 +480,6 @@
 } { 
     @param environment_item_id
     @param run_id
-    @option resource_mode
     @param dom_node
     @param dom_doc
 
Index: openacs-4/packages/imsld/tcl/imsld-parse-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-parse-procs.tcl,v
diff -u -r1.63.4.3 -r1.63.4.4
--- openacs-4/packages/imsld/tcl/imsld-parse-procs.tcl	2 Jul 2007 15:22:09 -0000	1.63.4.3
+++ openacs-4/packages/imsld/tcl/imsld-parse-procs.tcl	9 Jul 2007 16:09:51 -0000	1.63.4.4
@@ -957,7 +957,7 @@
         set title [imsld::parse::get_title -node $global_def_node -prefix imsld]
         set datatype [$global_def_node selectNodes "*\[local-name()='datatype'\]" ] 
         imsld::parse::validate_multiplicity -tree $global_def_node -multiplicity 1 -element_name "global-definition datatype" -equal
-        set datatype [string tolower [imsld::parse::get_attribute -node $global_def_node -attr_name datatype]]
+        set datatype [string tolower [imsld::parse::get_attribute -node $datatype -attr_name datatype]]
         set initial_value [$global_def_node selectNodes "*\[local-name()='initial-value'\]"] 
         imsld::parse::validate_multiplicity -tree $initial_value -multiplicity 1 -element_name "global-definition initial-value" -lower_than
         if { [llength $initial_value] } {
@@ -3358,11 +3358,13 @@
             }
         }
         
-        set complete_act_id [imsld::item_revision_new -attributes [list [list time_in_seconds $time_in_seconds] \
-                                                                       [list when_condition_true_id $when_condition_true_id] \
-                                                                       [list when_prop_val_is_set_xml $when_prop_value_is_set_xml]] \
-                                 -content_type imsld_complete_act \
-                                 -parent_id $parent_id]
+	if { ![string eq "" $time_in_seconds] || ![string eq "" $when_prop_value_is_set_xml] ||![string eq "" $when_condition_true_id]  } {
+	    set complete_act_id [imsld::item_revision_new -attributes [list [list time_in_seconds $time_in_seconds] \
+									   [list when_condition_true_id $when_condition_true_id] \
+									   [list when_prop_val_is_set_xml $when_prop_value_is_set_xml]] \
+				     -content_type imsld_complete_act \
+				     -parent_id $parent_id]
+	}
 
         if { [llength $when_prop_value_is_set] } {
             #search properties in expression 
Index: openacs-4/packages/imsld/tcl/imsld-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-procs.tcl,v
diff -u -r1.95.2.5 -r1.95.2.6
--- openacs-4/packages/imsld/tcl/imsld-procs.tcl	27 Jun 2007 10:21:58 -0000	1.95.2.5
+++ openacs-4/packages/imsld/tcl/imsld-procs.tcl	9 Jul 2007 16:09:51 -0000	1.95.2.6
@@ -488,9 +488,9 @@
 
         dom createDocument foo foo_doc
         set foo_node [$foo_doc documentElement]
-        if { [string eq $$type "learning"] } {
+        if { [string eq $type "learning"] } {
             set resources_activities_list [imsld::process_learning_activity_as_ul -run_id $run_id -activity_item_id $activity_item_id -resource_mode "t" -dom_node $foo_node -dom_doc $foo_doc]
-        } elseif { [string eq $$type "support"] } {
+        } elseif { [string eq $type "support"] } {
             set resources_activities_list [imsld::process_support_activity_as_ul -run_id $run_id -activity_item_id $activity_item_id -resource_mode "t" -dom_node $foo_node -dom_doc $foo_doc]
         } else {
             set resources_activities_list [imsld::process_activity_structure_as_ul -run_id $run_id -structure_item_id $activity_item_id -resource_mode "t" -dom_node $foo_node -dom_doc $foo_doc]
@@ -1030,7 +1030,7 @@
             }
         }
     }
-    
+
     if { [string eq $type "learning"] || [string eq $type "support"] || [string eq $type "structure"] } {
         foreach referencer_structure_list [db_list_of_lists referencer_structure { *SQL* }] {
             set structure_id [lindex $referencer_structure_list 0]
@@ -1049,6 +1049,7 @@
                 if { ![db_string completed_p { *SQL* }] } {
                     # there is at leas one no-completed activity, so we can't mark this activity structure yet
                     set scturcture_finished_p 0
+		    break
                 } else {
                     incr total_completed
                 }
@@ -1080,7 +1081,6 @@
     # 4. let's see if the finished play triggers the ending of the method which
     # references it.
     set role_part_id_list [imsld::get_role_part_from_activity -activity_type $type -leaf_id [db_string get_item_id { select item_id from cr_revisions where revision_id = :element_id}]]
-
     foreach role_part_id $role_part_id_list {
         db_1row context_info {
             select acts.act_id,
@@ -1182,6 +1182,7 @@
                         }
                     }
                 }
+
                 imsld::mark_act_finished -act_id $act_id \
                     -play_id $play_id \
                     -imsld_id $imsld_id \
@@ -1405,28 +1406,58 @@
         set object_id_two [lindex $referenced_activity 0]
         set rel_type [lindex $referenced_activity 1]
         switch $rel_type {
-            imsld_as_la_rel -
+            imsld_as_la_rel {
+		set complete_act_id [db_string completion_restriction {
+		    select complete_act_id 
+		    from imsld_learning_activities
+		    where activity_id = content_item__get_live_revision(:object_id_two)
+		} -default ""]
+		
+		if { (![string eq [db_string finished_p {                    
+		    select status from imsld_status_user 
+                    where related_id = content_item__get_live_revision(:object_id_two) 
+                    and user_id = :user_id 
+                    and status = 'finished'
+                    and run_id = :run_id
+		} -default ""] "finished"] && ![string eq $complete_act_id ""]) \
+			 || (![string eq [db_string started_p {
+			     select status from imsld_status_user 
+			     where related_id = content_item__get_live_revision(:object_id_two) 
+			     and user_id = :user_id 
+			     and status = 'started'
+			     and run_id = :run_id
+			 } -default ""] "started"] && [string eq $complete_act_id ""]) } {
+		    set all_completed 0
+		    break
+		}
+	    }
             imsld_as_sa_rel {
-                # is the activity finished ?
-                if { ![db_0or1row completed_p {
-                    select 1 from imsld_status_user 
+		set complete_act_id [db_string completion_restriction {
+		    select complete_act_id 
+		    from imsld_support_activities
+		    where activity_id = content_item__get_live_revision(:object_id_two)
+		} -default ""]
+		
+		if { (![string eq [db_string finished_p {                    
+		    select status from imsld_status_user 
                     where related_id = content_item__get_live_revision(:object_id_two) 
                     and user_id = :user_id 
                     and status = 'finished'
                     and run_id = :run_id
-                }] } {
-                    set all_completed 0
-                }
+		} -default ""] "finished"] && ![string eq $complete_act_id ""]) \
+			 || (![string eq [db_string started_p {
+			     select status from imsld_status_user 
+			     where related_id = content_item__get_live_revision(:object_id_two) 
+			     and user_id = :user_id 
+			     and status = 'started'
+			     and run_id = :run_id
+			 } -default ""] "started"] && [string eq $complete_act_id ""]) } {
+		    set all_completed 0
+		    break
+		}
             }
             imsld_as_as_rel {
-                # search recursively trough the referenced 
-                db_1row get_activity_structure_info {
-                    select structure_id
-                    from imsld_activity_structuresi
-                    where item_id = :object_id_two
-                    and content_revision__is_live(structure_id) = 't'
-                }
-                # is the activity finished ?
+		# the activity structure must be marked as finished
                 if { ![db_0or1row completed_p {
                     select 1 from imsld_status_user 
                     where related_id = :structure_id 
@@ -1435,15 +1466,13 @@
                     and run_id = :run_id
                 }] } {
                     set all_completed 0
+		    break
                 }
-                if { ![imsld::structure_finished_p -run_id $run_id -structure_id $structure_id -user_id $user_id] } {
-                    set all_completed 0
-                }
             }
         }
     }
     return $all_completed
-} 
+}
 
 ad_proc -public imsld::role_part_finished_p { 
     -role_part_id:required
@@ -2714,6 +2743,12 @@
     set completed_list [list]
     # get the structure info
     db_1row structure_info { *SQL* }
+
+    # if any of the referenced activities from the activity structure doesn't have a completion restriction
+    # and the activity structure is of type "sequence", there wouldn't be a way to advance in the structure,
+    # so, if this is the case, the structure is treated as of type "selection"
+    set completion_restriction [imsld::structure_completion_resctriction_p -run_id $run_id -structure_item_id $structure_item_id]
+
     # get the referenced activities which are referenced from the structure
     foreach referenced_activity [db_list_of_lists struct_referenced_activities { *SQL* }] {
         # get all the directly referenced activities (from the activity structure)
@@ -2728,41 +2763,93 @@
                     select sort_order from imsld_as_la_rels where rel_id = :rel_id
                 }
                 set completed_p [db_0or1row completed_p { *SQL* }]
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
                 # show the activity only if:
+		# 0. the activity is visible
                 # 1. it has been already completed
                 # 2. if the structure-type is "selection"
-                # 3. if it is the next activity to be done (and structure-type is "sequence") 
-                if { $completed_p || [string eq $complete_act_id ""] || [string eq $structure_type "selection"] || ([string eq $is_visible_p "t"] && [lsearch -exact $next_activity_id_list $activity_id] != -1) } {
-                    set activity_node [$dom_doc createElement li]
-                    $activity_node setAttribute class "liOpen"
+		# 3. if the activity has no completion restriction
+                # 4. if it is the next activity to be done (and structure-type is "sequence") 
 
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $activity_node appendChild $a_node
+                if { $completed_p || [string eq $complete_act_id ""] || [string eq $structure_type "selection"] || (([lsearch -exact $next_activity_id_list $activity_id] != -1) || !$completion_restriction) && [string eq $is_visible_p "t"] } {
 
-                    set text [$dom_doc createTextNode " "]
-                    $activity_node appendChild $text
+		    if { !$started_activity_p && [string eq $is_visible_p "t"] } {
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$activity_node appendChild $b_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    } else {
+			# bold letters
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$activity_node appendChild $a_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    }
 
-                    if { !$completed_p } {
-                        set input_node [$dom_doc createElement a]
-                        $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
-			$input_node setAttribute class "finish"
-			$input_node setAttribute title "[_ imsld.finish_activity]"
-			set text [$dom_doc createTextNode "[_ imsld.finish]"]
-			$input_node appendChild $text
-                        $activity_node appendChild $input_node
-                    } else {
-			set img_node [$dom_doc createElement img]
-			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-			$img_node setAttribute border "0"
-			$img_node setAttribute alt "[_ imsld.finished]"
-			$img_node setAttribute title "[_ imsld.finished]"
-                        $activity_node appendChild $img_node
-                    }
+		    if { $completed_p } {
 
+			if { [string eq $user_choice_p "t"] } {
+			    # the activity is finished
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.finished]"
+			    $img_node setAttribute title "[_ imsld.finished]"
+			    $activity_node appendChild $img_node
+			} else {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    } else {
+
+			if { [string eq $user_choice_p "t"] } {
+			    
+			    # show the finish button
+			    set input_node [$dom_doc createElement a]
+			    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
+			    $input_node setAttribute class "finish"
+			    $input_node setAttribute title "[_ imsld.finish_activity]"
+			    set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			    $input_node appendChild $text
+			    $activity_node appendChild $input_node
+			} elseif { $started_activity_p } {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    }
                     set completed_list [linsert $completed_list $sort_order [$activity_node asList]]
                 }
             }
@@ -2772,41 +2859,93 @@
                 db_1row get_sort_order {
                     select sort_order from imsld_as_sa_rels where rel_id = :rel_id
                 }
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
                 set completed_p [db_0or1row completed_p { *SQL* }]
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
+
                 # show the activity only if:
+		# 0. the activity is visible
                 # 1. it has been already completed
                 # 2. if the structure-type is "selection"
                 # 3. if it is the next activity to be done (and structure-type is "sequence") 
-                if { $completed_p || [string eq $complete_act_id ""] || [string eq $structure_type "selection"] || ([string eq $is_visible_p "t"] && [lsearch -exact $next_activity_id_list $activity_id] != -1) } {
-                    set activity_node [$dom_doc createElement li]
-                    $activity_node setAttribute class "liOpen"
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $activity_node appendChild $a_node
+		# 4. if the activity has no completion restriction
+                if { [string eq $is_visible_p "t"] && ($completed_p || [string eq $complete_act_id ""] || [string eq $structure_type "selection"] || (([lsearch -exact $next_activity_id_list $activity_id] != -1) || !$completion_restriction) && [string eq $is_visible_p "t"] } {
 
-                    set text [$dom_doc createTextNode " "]
-                    $activity_node appendChild $text
-                    
-                    if { !$completed_p } {
-                        set input_node [$dom_doc createElement a]
-                        $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
-			$input_node setAttribute class "finish"
-			$input_node setAttribute title "[_ imsld.finish_activity]"
-			set text [$dom_doc createTextNode "[_ imsld.finish]"]
-			$input_node appendChild $text
-                        $activity_node appendChild $input_node
-                    } else {
-			set img_node [$dom_doc createElement img]
-			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-			$img_node setAttribute border "0"
-			$img_node setAttribute alt "[_ imsld.finished]"
-			$img_node setAttribute title "[_ imsld.finished]"
-                        $activity_node appendChild $img_node
-                    }
+		    if { !$started_activity_p && [string eq $is_visible_p "t"] } {
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$activity_node appendChild $b_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    } else {
+			# bold letters
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$activity_node appendChild $a_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+			
+		    }
 
+		    if { $completed_p } {
+			if { [string eq $user_choice_p "t"] } {
+			    # the activity is finished
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.finished]"
+			    $img_node setAttribute title "[_ imsld.finished]"
+			    $activity_node appendChild $img_node
+			} else {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    } else {
+			if { [string eq $user_choice_p "t"] } {
+			    
+			    # show the finish button
+			    set input_node [$dom_doc createElement a]
+			    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
+			    $input_node setAttribute class "finish"
+			    $input_node setAttribute title "[_ imsld.finish_activity]"
+			    set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			    $input_node appendChild $text
+			    $activity_node appendChild $input_node
+			} elseif { $started_activity_p } {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    }
                     set completed_list [linsert $completed_list $sort_order [$activity_node asList]]
                 }
             }
@@ -2820,16 +2959,32 @@
                 db_1row get_sort_order {
                     select sort_order from imsld_as_as_rels where rel_id = :rel_id
                 }
-                set started_p [db_0or1row as_completed_p { *SQL* }]
+                set started_p [db_0or1row as_started_p { *SQL* }]
+		set completed_p [db_0or1row as_completed_p { *SQL* }]
+
+
                 if { $started_p || [string eq $structure_type "selection"] } {
-                    set structure_node [$dom_doc createElement li]
-                    $structure_node setAttribute class "liOpen"
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $structure_node appendChild $a_node
+		    if { $completed_p } {
+			set structure_node [$dom_doc createElement li]
+			$structure_node setAttribute class "liOpen"
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$structure_node appendChild $a_node
+		    } else {
+			set structure_node [$dom_doc createElement li]
+			$structure_node setAttribute class "liOpen"
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$structure_node appendChild $b_node
+		    }
 
                     set nested_activities_list [imsld::generate_structure_activities_list -imsld_id $imsld_id \
                                                     -run_id $run_id \
@@ -2884,105 +3039,206 @@
         and ar.object_id_two = :user_id
         and iruge.run_id = :run_id
     }]
+    set active_acts_list [imsld::active_acts -run_id $run_id -user_id $user_id]
 
     # get the referenced role parts
     foreach role_part_list [db_list_of_lists referenced_role_parts { *SQL* }] {
         set type [lindex $role_part_list 0]
         set activity_id [lindex $role_part_list 1]
         set role_part_id [lindex $role_part_list 2]
-        set act_id [lindex $role_part_list 3]        
-        set play_id [lindex $role_part_list 4]
+        set act_id [lindex $role_part_list 3]      
+	set act_item_id [lindex $role_part_list 4]
+        set play_id [lindex $role_part_list 5]
 
         switch $type {
             learning {
                 # add the learning activity to the tree
                 db_1row get_learning_activity_info { *SQL* }
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
                 set completed_activity_p [db_0or1row already_completed {
                     select 1 from imsld_status_user 
                     where related_id = :activity_id 
                     and user_id = :user_id 
                     and run_id = :run_id
                     and status = 'finished'
                 }]
-                if { $completed_activity_p || [lsearch -exact $next_activity_id_list $activity_id] != -1 && ([string eq $complete_act_id ""] || [string eq $is_visible_p "t"]) } {
-                    set activity_node [$dom_doc createElement li]
-                    $activity_node setAttribute class "liOpen"
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $activity_node appendChild $a_node
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
+                if { $completed_activity_p || ([lsearch -exact $next_activity_id_list $activity_id] != -1) || ([string eq $complete_act_id ""] && [string eq $is_visible_p "t"] && [lsearch -exact $active_acts_list $act_item_id] != -1) } {
 
-                    set text [$dom_doc createTextNode " "]
-                    $activity_node appendChild $text
-                        
-                    if { !$completed_activity_p } {
-                        set input_node [$dom_doc createElement a]
-                        $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
-			$input_node setAttribute class "finish"
-			$input_node setAttribute title "[_ imsld.finish_activity]"
-			set text [$dom_doc createTextNode "[_ imsld.finish]"]
-			$input_node appendChild $text
-                        $activity_node appendChild $input_node
-                    } else {
-			set img_node [$dom_doc createElement img]
-			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-			$img_node setAttribute border "0"
-			$img_node setAttribute alt "[_ imsld.finished]"
-			$img_node setAttribute title "[_ imsld.finished]"
-                        $activity_node appendChild $img_node
-                    }
+		    if { !$started_activity_p && [string eq $is_visible_p "t"] } {
+			# bold letters
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$activity_node appendChild $b_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    } else {
+			set activity_node [$dom_doc createElement li]
+			$activity_node setAttribute class "liOpen"
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$activity_node appendChild $a_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+			
+		    }
 
+		    if { $completed_activity_p } {
+
+			if { [string eq $user_choice_p "t"] } {
+			    # the activity is finished
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.finished]"
+			    $img_node setAttribute title "[_ imsld.finished]"
+			    $activity_node appendChild $img_node
+			} else {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    } elseif { [string eq $is_visible_p "t"] } {
+
+			if { [string eq $user_choice_p "t"] } {
+			    
+			    # show the finish button
+			    set input_node [$dom_doc createElement a]
+			    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
+			    $input_node setAttribute class "finish"
+			    $input_node setAttribute title "[_ imsld.finish_activity]"
+			    set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			    $input_node appendChild $text
+			    $activity_node appendChild $input_node
+			} elseif { $started_activity_p } {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    }
+
                     $dom_node appendChild $activity_node
                 }
             }
             support {
                 # add the support activity to the tree
                 db_1row get_support_activity_info { *SQL* }
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
                 set completed_activity_p [db_0or1row already_completed {
                     select 1 from imsld_status_user 
                     where related_id = :activity_id 
                     and user_id = :user_id 
                     and run_id = :run_id
                     and status = 'finished'
                 }]
-                if { $completed_activity_p || [lsearch -exact $next_activity_id_list $activity_id] != -1 && ([string eq $complete_act_id ""] || [string eq $is_visible_p "t"]) } {
-                    set activity_node [$dom_doc createElement li]
-                    $activity_node setAttribute class "liOpen"
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $activity_node appendChild $a_node
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
 
-                    set text [$dom_doc createTextNode " "]
-                    $activity_node appendChild $text
-                    
-                    if { !$completed_activity_p } {
-                        set input_node [$dom_doc createElement a]
-                        $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
-			$input_node setAttribute class "finish"
-			$input_node setAttribute title "[_ imsld.finish_activity]"
-			set text [$dom_doc createTextNode "[_ imsld.finish]"]
-			$input_node appendChild $text
-                        $activity_node appendChild $input_node
-                    } else {
-			set img_node [$dom_doc createElement img]
-			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-			$img_node setAttribute border "0"
-			$img_node setAttribute alt "[_ imsld.finished]"
-			$img_node setAttribute title "[_ imsld.finished]"
-                        $activity_node appendChild $img_node
-                    }
+                if { $completed_activity_p || ([lsearch -exact $next_activity_id_list $activity_id] != -1) || ([string eq $complete_act_id ""] && [string eq $is_visible_p "t"] && [lsearch -exact $active_acts_list $act_item_id] != -1) } {
+		    set activity_node [$dom_doc createElement li]
+		    $activity_node setAttribute class "liOpen"
+		    if { !$started_activity_p && [string eq $is_visible_p "t"] } {
+			# bold letters
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$activity_node appendChild $b_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    } else {
+			# bold letters
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$activity_node appendChild $a_node
+			
+			set text [$dom_doc createTextNode " "]
+			$activity_node appendChild $text
+		    }
 
+		    if { $completed_activity_p } {
+
+			if { [string eq $user_choice_p "t"] } {
+			    # the activity is finished
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.finished]"
+			    $img_node setAttribute title "[_ imsld.finished]"
+			    $activity_node appendChild $img_node
+			} else {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    } else {
+			if { [string eq $user_choice_p "t"] } {
+			    # show the finish button
+			    set input_node [$dom_doc createElement a]
+			    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
+			    $input_node setAttribute class "finish"
+			    $input_node setAttribute title "[_ imsld.finish_activity]"
+			    set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			    $input_node appendChild $text
+			    $activity_node appendChild $input_node
+			} elseif { $started_activity_p } {
+			    # the activity has been viewed
+			    set img_node [$dom_doc createElement img]
+			    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			    $img_node setAttribute border "0"
+			    $img_node setAttribute alt "[_ imsld.Viewed]"
+			    $img_node setAttribute title "[_ imsld.Viewed]"
+			    $activity_node appendChild $img_node
+			}
+		    }
                     $dom_node appendChild $activity_node
                 }
             }
             structure {
                 # this is a special case since there are some conditions to check
-                # in order to determine if the referenced activities have to be shown
+                # in order to determine if the referenced activities have to be shown.
                 # because of that the proc generate_structure_activities_list is called,
                 # which returns a tcl list in tDOM format.
                 
@@ -2992,16 +3248,31 @@
                 # (if it is the next activity to be done then it should had been marked as started 
                 #  in the "structure_next_activity" function. which is the case when structure-type is "sequence")
                 db_1row get_activity_structure_info { *SQL* }
-                set started_p [db_0or1row as_completed_p { *SQL* }]
+                set started_p [db_0or1row as_started_p { *SQL* }]
+                set completed_p [db_0or1row as_completed_p { *SQL* }]
                 if { $started_p } {
-                    set structure_node [$dom_doc createElement li]
-                    $structure_node setAttribute class "liOpen"
-                    set a_node [$dom_doc createElement a]
-                    $a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
-                    $a_node setAttribute target "content"
-                    set text [$dom_doc createTextNode "$activity_title"]
-                    $a_node appendChild $text
-                    $structure_node appendChild $a_node
+		    if { $completed_p } {
+			set structure_node [$dom_doc createElement li]
+			$structure_node setAttribute class "liOpen"
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$structure_node appendChild $a_node
+		    } else {
+			set structure_node [$dom_doc createElement li]
+			$structure_node setAttribute class "liOpen"
+			set b_node [$dom_doc createElement b]
+			set a_node [$dom_doc createElement a]
+			$a_node setAttribute href "[imsld::activity_url -activity_id $structure_id -run_id $run_id -user_id $user_id]"
+			$a_node setAttribute target "content"
+			set text [$dom_doc createTextNode "$activity_title"]
+			$a_node appendChild $text
+			$b_node appendChild $a_node
+			$structure_node appendChild $b_node
+		    }
+
                     set nested_list [imsld::generate_structure_activities_list -imsld_id $imsld_id \
                                          -run_id $run_id \
                                          -structure_item_id $structure_item_id \
@@ -3063,77 +3334,367 @@
             learning {
                 # add the learning activity to the tree
                 db_1row get_learning_activity_info { *SQL* }
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
                 set completed_activity_p [db_0or1row la_already_completed { *SQL* }]
-                set activity_node [$dom_doc createElement li]
-                $activity_node setAttribute class "liOpen"
-                set a_node [$dom_doc createElement a]
-                $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                $a_node setAttribute target "content"
-                set text [$dom_doc createTextNode "$activity_title"]
-                $a_node appendChild $text
-                $activity_node appendChild $a_node
-                
-                set text [$dom_doc createTextNode " "]
-                $activity_node appendChild $text
-                
-                if { !$completed_activity_p } {
-		    set input_node [$dom_doc createElement a]
-		    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
-		    $input_node setAttribute class "finish"
-		    $input_node setAttribute title "[_ imsld.finish_activity]"
-		    set text [$dom_doc createTextNode "[_ imsld.finish]"]
-		    $input_node appendChild $text
-		    $activity_node appendChild $input_node
-                } else {
-		    set img_node [$dom_doc createElement img]
-		    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-		    $img_node setAttribute border "0"
-		    $img_node setAttribute alt "[_ imsld.finished]"
-		    $img_node setAttribute title "[_ imsld.finished]"
-		    $activity_node appendChild $img_node
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
+
+		if { !$started_activity_p } {
+		    set activity_node [$dom_doc createElement li]
+		    $activity_node setAttribute class "liOpen"
+		    set b_node [$dom_doc createElement b]
+		    set a_node [$dom_doc createElement a]
+		    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+		    $a_node setAttribute target "content"
+		    set text [$dom_doc createTextNode "$activity_title"]
+		    $a_node appendChild $text
+		    $b_node appendChild $a_node
+		    $activity_node appendChild $b_node
+		    
+		    set text [$dom_doc createTextNode " "]
+		    $activity_node appendChild $text
+		} else {
+		    # bold letters
+		    set activity_node [$dom_doc createElement li]
+		    $activity_node setAttribute class "liOpen"
+		    set a_node [$dom_doc createElement a]
+		    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+		    $a_node setAttribute target "content"
+		    set text [$dom_doc createTextNode "$activity_title"]
+		    $a_node appendChild $text
+		    $activity_node appendChild $a_node
+		    
+		    set text [$dom_doc createTextNode " "]
+		    $activity_node appendChild $text
+		}
+		
+
+		if { $completed_activity_p } {
+
+		    if { [string eq $user_choice_p "t"] } {
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.finished]"
+			$img_node setAttribute title "[_ imsld.finished]"
+			$activity_node appendChild $img_node
+		  
+		    } else {
+			# the activity has been viewed
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.Viewed]"
+			$img_node setAttribute title "[_ imsld.Viewed]"
+			$activity_node appendChild $img_node
+		    }
+		} else {
+
+		    if { [string eq $user_choice_p "t"] } {
+		    
+			# show the button to finish the activity
+			set input_node [$dom_doc createElement a]
+			$input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-learning.imsld"
+			$input_node setAttribute class "finish"
+			$input_node setAttribute title "[_ imsld.finish_activity]"
+			set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			$input_node appendChild $text
+			$b_node appendChild $input_node 
+		    
+		    } elseif { $started_activity_p } {
+			
+			# the activity has been viewed
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.Viewed]"
+			$img_node setAttribute title "[_ imsld.Viewed]"
+			$activity_node appendChild $img_node
+		    }
                 }
-                
                 $dom_node appendChild $activity_node
             }
             support {
                 # add the support activity to the tree
                 db_1row get_support_activity_info { *SQL* }
+                set started_activity_p [db_0or1row already_started {
+                    select 1 from imsld_status_user 
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and run_id = :run_id
+                    and status = 'started'
+                }]
+		set user_choice_p [db_string user_choice_p {select user_choice_p from imsld_complete_actsi where item_id = :complete_act_id and content_revision__is_live(complete_act_id) = 't'} -default "f"]
                 set completed_activity_p [db_0or1row sa_already_completed { *SQL* }]
-                set activity_node [$dom_doc createElement li]
-                $activity_node setAttribute class "liOpen"
-                set a_node [$dom_doc createElement a]
-                $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
-                $a_node setAttribute target "content"
-                set text [$dom_doc createTextNode "$activity_title"]
-                $a_node appendChild $text
-                $activity_node appendChild $a_node
-                
-                set text [$dom_doc createTextNode " "]
-                $activity_node appendChild $text
-                
-                if { !$completed_activity_p } {
-		    set input_node [$dom_doc createElement a]
-		    $input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
-		    $input_node setAttribute class "finish"
-		    $input_node setAttribute title "[_ imsld.finish_activity]"
-		    set text [$dom_doc createTextNode "[_ imsld.finish]"]
-		    $input_node appendChild $text
-		    $activity_node appendChild $input_node
-                } else {
-		    set img_node [$dom_doc createElement img]
-		    $img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
-		    $img_node setAttribute border "0"
-		    $img_node setAttribute alt "[_ imsld.finished]"
-		    $img_node setAttribute title "[_ imsld.finished]"
-		    $activity_node appendChild $img_node
+
+		if { !$started_activity_p } {
+		    set activity_node [$dom_doc createElement li]
+		    $activity_node setAttribute class "liOpen"
+		    set b_node [$dom_doc createElement b]
+		    set a_node [$dom_doc createElement a]
+		    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+		    $a_node setAttribute target "content"
+		    set text [$dom_doc createTextNode "$activity_title"]
+		    $a_node appendChild $text
+		    $b_node appendChild $a_node
+		    $activity_node appendChild $b_node
+		    
+		    set text [$dom_doc createTextNode " "]
+		    $activity_node appendChild $text
+		} else {
+		    # bold letters
+		    set activity_node [$dom_doc createElement li]
+		    $activity_node setAttribute class "liOpen"
+		    set a_node [$dom_doc createElement a]
+		    $a_node setAttribute href "[imsld::activity_url -activity_id $activity_id -run_id $run_id -user_id $user_id]"
+		    $a_node setAttribute target "content"
+		    set text [$dom_doc createTextNode "$activity_title"]
+		    $a_node appendChild $text
+		    $activity_node appendChild $a_node
+		    
+		    set text [$dom_doc createTextNode " "]
+		    $activity_node appendChild $text
+		}
+		
+		if { $completed_activity_p } {
+
+		    if { [string eq $user_choice_p "t"] } {
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/completed.png"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.finished]"
+			$img_node setAttribute title "[_ imsld.finished]"
+			$activity_node appendChild $img_node
+		  
+		    } else {
+			# the activity has been viewed
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.Viewed]"
+			$img_node setAttribute title "[_ imsld.Viewed]"
+			$activity_node appendChild $img_node
+		    }
+		} else {
+		    if { [string eq $user_choice_p "t"] } {
+		    
+			# show the button to finish the activity
+			set b_node [$dom_doc createElement b]
+			set input_node [$dom_doc createElement a]
+			$input_node setAttribute href "finish-component-element-${imsld_id}-${run_id}-${play_id}-${act_id}-${role_part_id}-${activity_id}-support.imsld"
+			$input_node setAttribute class "finish"
+			$input_node setAttribute title "[_ imsld.finish_activity]"
+			set text [$dom_doc createTextNode "[_ imsld.finish]"]
+			$input_node appendChild $text
+			$b_node appendChild $input_node 
+			$activity_node appendChild $b_node
+		    
+		    } elseif { $started_activity_p } {
+			
+			# the activity has been viewed
+			set img_node [$dom_doc createElement img]
+			$img_node setAttribute src "[lindex [site_node::get_url_from_object_id -object_id $imsld_package_id] 0]/resources/viewed.ico"
+			$img_node setAttribute border "0"
+			$img_node setAttribute alt "[_ imsld.Viewed]"
+			$img_node setAttribute title "[_ imsld.Viewed]"
+			$activity_node appendChild $img_node
+		    }
                 }
-                
                 $dom_node appendChild $activity_node
             }
         }
     }
 }
 
+ad_proc -public imsld::structure_completion_resctriction_p { 
+    -run_id:required
+    -structure_item_id:required
+} {
+    @param run_id
+    @param structure_item_id
+    
+    @return Returns 0 if any of the referenced activities from the structure_id doesn't have a completion restriction
+} {
+
+    foreach referenced_activity [db_list_of_lists struct_referenced_activities {
+        select ar.object_id_two,
+        ar.rel_type
+        from acs_rels ar, imsld_activity_structuresi ias
+        where ar.object_id_one = ias.item_id
+        and ias.item_id = :structure_item_id
+	and content_revision__is_live(ias.structure_id) = 't'
+        order by ar.object_id_two
+    }] {
+        # get all the directly referenced activities (from the activity structure)
+        set object_id_two [lindex $referenced_activity 0]
+        set rel_type [lindex $referenced_activity 1]
+        switch $rel_type {
+            imsld_as_la_rel {
+		if { [string eq "" [db_string la_completion_restriction {
+		    select complete_act_id 
+		    from imsld_learning_activitiesi
+		    where item_id = :object_id_two
+		    and content_revision__is_live(activity_id) = 't'
+		}]] } {
+		    # no restriction found, break
+		    return 0
+		}
+	    }
+            imsld_as_sa_rel {
+		if { [string eq "" [db_string sa_completion_restriction {
+		    select complete_act_id 
+		    from imsld_support_activitiesi
+		    where item_id = :object_id_two
+		    and content_revision__is_live(activity_id) = 't'
+		}]] } {
+		    # no restriction found, break
+		    return 0
+		}
+            }
+            imsld_as_as_rel {
+                # search recursively trough the referenced activities
+                return [imsld::structure_completion_resctriction_p -run_id $run_id -structure_item_id $object_id_two]
+            }
+        }
+    }
+    # every referenced activity has a completion restriction
+    return 1
+}
+
+ad_proc -public imsld::active_acts { 
+    -run_id:required
+    -user_id:required
+    {-previous_list {}}
+} {
+    @param run_id
+    @param user
+    @param previous_list
+    
+    @return Returns the list of possible active acts for the user
+} {
+    set active_acts_list [list]
+
+    set all_acts_list [db_list get_acts_in_run {
+	select iai.act_id
+	from imsld_runs ir, 
+	imsld_imsldsi iii,
+	imsld_methodsi imi,
+	imsld_playsi ipi,
+	imsld_actsi iai 
+	where ir.run_id=:run_id
+	and iii.imsld_id=ir.imsld_id 
+	and imi.imsld_id=iii.item_id 
+	and imi.item_id=ipi.method_id 
+	and iai.play_id=ipi.item_id
+	order by ipi.sort_order, iai.sort_order
+    }]
+    set i 0
+    set continue 1
+    while { $i < [llength $all_acts_list] && $continue == 1 } {
+	set act_in_run [lindex $all_acts_list $i]
+	incr i
+	# let's see if the user participates in the act
+	if { [imsld::user_participate_p -run_id $run_id -act_id $act_in_run -user_id $user_id] \
+		 && ![imsld::act_finished_p -run_id $run_id -act_id $act_in_run -user_id $user_id] } {
+	    # let's see if the act doesn't have any completion restriction:
+	    # 1. time-limit
+	    # 2. when-property-is-set
+	    # 3. when-condition-true
+	    # 4. when role-part-is-completed
+	    # 5. any referenced activity structure (which by default have a completion restriction)
+	    set act_in_run_item_id [content::revision::item_id -revision_id $act_in_run] 
+
+	    # 1. time-limit, 2. when-property-is-set, 3. when-condition-true: all the info is stored via complete_act_id in the acts table
+		if { ![string eq "" [db_string complete_act_id {select complete_act_id from imsld_acts where act_id = :act_in_run}]] && [lsearch -exact $previous_list $act_in_run_item_id] == -1 } {
+		# there is a completion restriction, stop here
+		lappend active_acts_list $act_in_run_item_id
+		break
+	    }
+
+	    # 4. when role-part-is-completed, 5. referenced activity structures
+	    #    This is a special case, since if any of the activities referenced by the role part doesn't have a completion restriction
+	    #    then the act has to be appended to the list
+	    #    Note: The role parts that finish the act are mapped to the act via imsld_act_rp_completed_rel (acs_rels)
+
+	    set role_parts_list [db_list related_role_parts {
+		select item_id
+		from imsld_role_partsi
+		where act_id = :act_in_run_item_id
+		and content_revision__is_live(role_part_id) = 't'
+		order by sort_order
+	    }]
+	    foreach role_part_item_id $role_parts_list {
+		# get all the activities in the role part and see if none has any compleion resctriction 
+
+		db_1row get_role_part_activity {
+		    select case
+		    when learning_activity_id is not null
+		    then 'learning'
+		    when support_activity_id is not null
+		    then 'support'
+		    when activity_structure_id is not null
+		    then 'structure'
+		    else 'none'
+		    end as type,
+		    learning_activity_id,
+		    support_activity_id,
+		    activity_structure_id
+		    from imsld_role_partsi
+		    where item_id = :role_part_item_id
+		    and content_revision__is_live(role_part_id) = 't'
+		}
+		set continue 0
+		# check if the referenced activities have been finished
+		switch $type {
+		    learning {
+			if { [string eq "" [db_string la_completion_restriction {
+			    select complete_act_id 
+			    from imsld_learning_activitiesi
+			    where item_id = :learning_activity_id
+			    and content_revision__is_live(activity_id) = 't'
+			}]] } {
+			    # activity without restriction found, 
+			    # append the act to the list of active acts
+			    set continue 1
+			    break
+			}
+		    }
+		    support {
+			if { [string eq "" [db_string sa_completion_restriction {
+			    select complete_act_id 
+			    from imsld_support_activitiesi
+			    where item_id = :support_activity_id
+			    and content_revision__is_live(activity_id) = 't'
+			}]] } {
+			    # activity without restriction found, 
+			    # append the act to the list of active acts
+			    set continue 1
+			    break
+			}
+		    }
+		    structure {
+			# every activity structure has a completion restriction (at leat, every activity must be visited)
+			# so we can stop here
+			set continue 0
+			break
+		    }
+		}
+	    }
+	    # if we reached this point, the act must be shown
+	    if { [lsearch -exact $previous_list $act_in_run_item_id] == -1 } {
+		# add the act to the list only if it wasn't in the list already
+		lappend active_acts_list $act_in_run_item_id
+	    }
+	}   
+    }
+    return [concat $previous_list $active_acts_list]
+}
+
 ad_proc -public imsld::get_next_activity_list { 
     -run_id:required
     {-user_id ""}
@@ -3234,6 +3795,7 @@
             }
             continue
         }
+
         if { ![imsld::act_finished_p -run_id $run_id -act_id $act_id -user_id $user_id] } {
             if {[imsld::user_participate_p -run_id $run_id -act_id $act_id -user_id $user_id]} {
                 lappend next_act_item_id_list [content::revision::item_id -revision_id $act_id]
@@ -3285,6 +3847,9 @@
         }
     }
 
+    # append to the list of "active acts" those which don't have any "completion" restrictions
+    set next_act_item_id_list [imsld::active_acts -run_id $run_id -user_id $user_id -previous_list $next_act_item_id_list]
+
     # 1. for each act in the next_act_id_list
     # 1.2. for each role_part in the act
     # 1.2.1 find the next activity referenced by the role_part
@@ -3293,7 +3858,6 @@
     # 2.2.1.2 if it is an activity structure we have verify which activities are already completed and return the next
     #         activity in the activity structure, handling the case when the next activity is also an activity structure
 
-
     set user_roles_list [imsld::roles::get_user_roles -user_id $user_id -run_id $run_id]
     set next_activity_id_list [list]
     foreach act_item_id $next_act_item_id_list {
@@ -3561,7 +4125,7 @@
         set activity_id [lindex $activity_list 0]
         set activity_item_id [lindex $activity_list 1]
         set activity_type [lindex $activity_list 2]
-        
+
         #get info
         set role_part_id_list [imsld::get_role_part_from_activity -activity_type $activity_type -leaf_id $activity_item_id]
         set imsld_id [imsld::get_imsld_from_activity -activity_id $activity_id -activity_type $activity_type]
Index: openacs-4/packages/imsld/tcl/imsld-procs.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-procs.xql,v
diff -u -r1.37.4.2 -r1.37.4.3
--- openacs-4/packages/imsld/tcl/imsld-procs.xql	26 Jun 2007 16:34:05 -0000	1.37.4.2
+++ openacs-4/packages/imsld/tcl/imsld-procs.xql	9 Jul 2007 16:09:51 -0000	1.37.4.3
@@ -898,7 +898,8 @@
 	<fullquery name="imsld::structure_next_activity.get_la_info">
 		<querytext>
     
-                    select la.activity_id as learning_activity_id
+                    select la.activity_id as learning_activity_id,
+		    complete_act_id
                     from imsld_learning_activitiesi la
                     where la.item_id = :object_id_two
                     and content_revision__is_live(la.activity_id) = 't'
@@ -1905,7 +1906,7 @@
 		</querytext>
 	</fullquery>
 
-	<fullquery name="imsld::generate_structure_activities_list.as_completed_p">
+	<fullquery name="imsld::generate_structure_activities_list.as_started_p">
 		<querytext>
 
                     select 1 from imsld_status_user
@@ -1917,6 +1918,18 @@
 		</querytext>
 	</fullquery>
 
+	<fullquery name="imsld::generate_structure_activities_list.as_completed_p">
+		<querytext>
+
+                    select 1 from imsld_status_user
+                    where related_id = :structure_id 
+                    and user_id = :user_id 
+                    and status = 'finish'
+                    and run_id = :run_id
+                
+		</querytext>
+	</fullquery>
+
 	<fullquery name="imsld::generate_activities_tree.current_role">
 		<querytext>
 
@@ -1947,6 +1960,7 @@
         content_item__get_live_revision(coalesce(rp.learning_activity_id,rp.support_activity_id,rp.activity_structure_id)) as activity_id,
         rp.role_part_id,
         ia.act_id,
+        ia.item_id as act_item_id,
         ip.play_id
         from imsld_role_partsi rp, imsld_actsi ia, imsld_playsi ip, imsld_imsldsi ii, imsld_attribute_instances attr,
         imsld_methodsi im,imsld_rolesi iri
@@ -2017,7 +2031,7 @@
 		</querytext>
 	</fullquery>
 
-	<fullquery name="imsld::generate_activities_tree.as_completed_p">
+	<fullquery name="imsld::generate_activities_tree.as_started_p">
 		<querytext>
 
                     select 1 from imsld_status_user
@@ -2029,6 +2043,18 @@
 		</querytext>
 	</fullquery>
 
+	<fullquery name="imsld::generate_activities_tree.as_completed_p">
+		<querytext>
+
+                    select 1 from imsld_status_user
+                    where related_id = :activity_id 
+                    and user_id = :user_id 
+                    and status = 'finished'
+                    and run_id = :run_id
+                    
+		</querytext>
+	</fullquery>
+
 	<fullquery name="imsld::generate_runtime_assigned_activities_tree.imsld_info">
 		<querytext>
 
Index: openacs-4/packages/imsld/tcl/imsld-runtime-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/tcl/imsld-runtime-procs.tcl,v
diff -u -r1.16.4.4 -r1.16.4.5
--- openacs-4/packages/imsld/tcl/imsld-runtime-procs.tcl	27 Jun 2007 10:21:58 -0000	1.16.4.4
+++ openacs-4/packages/imsld/tcl/imsld-runtime-procs.tcl	9 Jul 2007 16:09:51 -0000	1.16.4.5
@@ -160,7 +160,7 @@
 	    set role_instance_id [imsld::roles::get_user_role_instance -run_id $run_id -role_id $role_id -user_id $user_id]
 	    if { !$role_instance_id } {
 		# runtime error... the user doesn't belong to any role instance
-		util_user_message -message "<#_ User does not belong to any role instance #>"
+		util_user_message -message "[_ imsld.lt_User_does_not_belong_]"
 		ad_script_abort
 	    }
 	}
@@ -411,7 +411,7 @@
         set role_instance_id [imsld::roles::get_user_role_instance -run_id $run_id -role_id $role_id -user_id $user_id]
         if { !$role_instance_id } {
             # runtime error... the user doesn't belong to any role instance
-	    util_user_message -message "<#_ User does not belong to any role instance #>" 
+	    util_user_message -message "[_ imsld.lt_User_does_not_belong_]" 
 	    ad_script_abort 
         }
     }
Index: openacs-4/packages/imsld/www/activity-frame.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/www/activity-frame.tcl,v
diff -u -r1.16.4.1 -r1.16.4.2
--- openacs-4/packages/imsld/www/activity-frame.tcl	27 Jun 2007 10:21:59 -0000	1.16.4.1
+++ openacs-4/packages/imsld/www/activity-frame.tcl	9 Jul 2007 16:09:51 -0000	1.16.4.2
@@ -52,6 +52,165 @@
      )
 }
 
+set activity_item_id [content::revision::item_id -revision_id $activity_id]
+
+set referencer_structure_item_id ""
+
+if { [string eq $activity_type "learning"] } {
+    if { [string eq "" [db_string completion_restriction {select complete_act_id from imsld_learning_activities where activity_id = :activity_id}]] } {
+	# the learning activity has been visited and doesn't have any completion restriction.
+	# if it is referenced from an activity structure, verify if every referenced activity have been visted
+	db_0or1row referenced_from_structure_p {
+	    select ar.object_id_one as referencer_structure_item_id
+	    from acs_rels ar
+	    where ar.object_id_two = :activity_item_id
+	}
+    }
+} elseif { [string eq $activity_type "support"] } {
+    if { [string eq "" [db_string completion_restriction {select complete_act_id from imsld_support_activities where activity_id = :activity_id}]] } {
+	# the learning activity has been visited and doesn't have any completion restriction.
+	# if it is referenced from an activity structure, verify if every referenced activity have been visted
+	db_0or1row referenced_from_structure_p {
+	    select ar.object_id_one as referencer_structure_item_id
+	    from acs_rels ar
+	    where ar.object_id_two = :activity_item_id
+	}
+    }
+}
+
+if { ![string eq "" $referencer_structure_item_id] } {
+    db_1row get_structure_info {
+	select structure_id,
+	number_to_select
+	from imsld_activity_structuresi
+	where item_id = :referencer_structure_item_id
+	and content_revision__is_live(structure_id) = 't'
+    }
+    
+    # if the structure hasn't been finished
+    if { ![db_0or1row already_finished {
+	select 1
+	from imsld_status_user
+	where related_id = :structure_id
+	and user_id = :user_id
+	and run_id = :run_id
+	and status = 'finished'
+    }] } {
+	set mark_structure_finished_p 1
+	foreach referenced_activity [db_list_of_lists struct_referenced_activities {
+	    select ar.object_id_two,
+	    ar.rel_type
+	    from acs_rels ar
+	    where ar.object_id_one = :referencer_structure_item_id
+	    order by ar.object_id_two
+	}] {
+	    set object_id_two [lindex $referenced_activity 0]
+	    set rel_type [lindex $referenced_activity 1]
+	    switch $rel_type {
+		imsld_as_la_rel {
+		    # if the activity doesn't have any completrion restriction 
+		    # and it hasn't been started, cancel the completion of the structure
+		    set referenced_activity_id [content::item::get_live_revision -item_id $object_id_two]
+		    set la_completion_restriction [db_string la_completion_restriction {
+			select complete_act_id
+			from imsld_learning_activities
+			where activity_id = :referenced_activity_id
+		    }]
+		    if { ([db_0or1row la_already_started_p {
+			select 1
+			from imsld_status_user
+			where related_id = :referenced_activity_id
+			and user_id = :user_id
+			and run_id = :run_id
+			and status = 'started'
+		    }] && [string eq "" $la_completion_restriction]) \
+			     || [db_0or1row la_already_finished {
+				 select 1
+				 from imsld_status_user
+				 where related_id = :referenced_activity_id
+				 and user_id = :user_id
+				 and run_id = :run_id
+				 and status = 'finished'
+			     }] } {
+			# the activity has been visited
+			incr total_completed
+		    } else {
+			set mark_structure_finished_p 0
+			break
+		    }
+		}
+		imsld_as_sa_rel {
+		    # if the activity doesn't have any completrion restriction 
+		    # and it hasn't been started, cancel the completion of the structure
+		    set referenced_activity_id [content::item::get_live_revision -item_id $object_id_two]
+		    if { ([db_0or1row la_already_started_p {
+			select 1
+			from imsld_status_user
+			where related_id = :referenced_activity_id
+			and user_id = :user_id
+			and run_id = :run_id
+			and status = 'started'
+		    }] && [string eq "" $sa_completion_restriction]) \
+			     || [db_0or1row la_already_finished {
+				 select 1
+				 from imsld_status_user
+				 where related_id = :referenced_activity_id
+				 and user_id = :user_id
+				 and run_id = :run_id
+				 and status = 'finished'
+			     }] } {
+			# the activity has been visited
+			incr total_completed
+		    } else {
+			set mark_structure_finished_p 0
+			break
+		    }
+		} imsld_as_as_rel {
+		    # if the referenced activity structure hasn't been finished, don't finish the activity structure
+		    set structure_id [content::item::get_live_revision -item_id $object_id_two]
+		    if { ![db_0or1row sa_already_finished_p {
+			select 1
+			from imsld_status_user
+			where related_id = :referenced_activity_id
+			and user_id = :user_id
+			and run_id = :run_id
+			and status = 'finished'
+		    }] } {
+			set mark_structure_finished_p 0
+			break
+		    } else {
+			incr total_completed
+		    }
+		}
+	    }
+	}
+	if { $mark_structure_finished_p || (![string eq $number_to_select ""] && ($total_completed >= $number_to_select)) } {
+	    # mark the structure as finished
+
+	    set role_part_id_list [imsld::get_role_part_from_activity -activity_type structure -leaf_id $referencer_structure_item_id]
+	    foreach role_part_id $role_part_id_list {
+	    db_1row context_info {
+		select acts.act_id,
+		plays.play_id
+		from imsld_actsi acts, imsld_playsi plays, imsld_role_parts rp
+		where rp.role_part_id = :role_part_id
+		and rp.act_id = acts.item_id
+		and acts.play_id = plays.item_id
+	    }
+		imsld::finish_component_element -imsld_id $imsld_id \
+		-run_id $run_id \
+		-play_id $play_id \
+		-act_id $act_id \
+		-role_part_id $role_part_id \
+		-element_id $structure_id \
+		-type structure \
+		-user_id $user_id \
+		-code_call
+	    }
+	}
+    }
+}
+
 set supported_roles [db_list supported_roles_list { select iri.role_id 
                                              from imsld_rolesi iri, 
                                              acs_rels ar,  
Index: openacs-4/packages/imsld/www/resources/viewed.ico
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/imsld/www/resources/viewed.ico,v
diff -u -r1.1 -r1.1.2.1
Binary files differ