Index: openacs-4/packages/dotlrn-ecommerce/tcl/implementation-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/tcl/implementation-procs.tcl,v diff -u -r1.2 -r1.3 --- openacs-4/packages/dotlrn-ecommerce/tcl/implementation-procs.tcl 2 Jun 2005 06:39:42 -0000 1.2 +++ openacs-4/packages/dotlrn-ecommerce/tcl/implementation-procs.tcl 4 Jun 2005 10:05:18 -0000 1.3 @@ -11,20 +11,74 @@ } ad_proc -callback ecommerce::after-checkout -impl dotlrn-ecommerce { - -user_id - -product_id + -user_id:required + -product_id:required + -patron_id -price } { - Add user to community + Add users to community } { - # Get community mapped to product - db_foreach communities { - select community_id - from dotlrn_ecommerce_section - where product_id = :product_id - } { - ns_log notice "dotlrn-ecommerce callback: Adding user $user_id to community $community_id" + # Check first if user_id is actually a group + if { [acs_object_type $user_id] == "group" } { + set user_ids [db_list group_members { + select distinct object_id_two + from acs_rels + where object_id_one = :user_id + and rel_type = 'membership_rel' + }] + ns_log notice "dotlrn-ecommerce callback: Adding users ([join $user_ids ,]) in group $user_id" + } else { + set user_ids [list $user_id] + } - dotlrn_community::add_user $community_id $user_id + if { [exists_and_not_null patron_id] } { + if { ! [dotlrn::user_p -user_id $patron_id] } { + dotlrn::user_add -user_id $patron_id + } } + + foreach user_id $user_ids { + if { ! [dotlrn::user_p -user_id $user_id] } { + dotlrn::user_add -user_id $user_id + } + + # Get community mapped to product + db_foreach communities { + select community_id + from dotlrn_ecommerce_section + where product_id = :product_id + } { + ns_log notice "dotlrn-ecommerce callback: Adding user $user_id to community $community_id" + + if { [catch { + + dotlrn_community::add_user $community_id $user_id + + # Keep track of patron relationships + if { [exists_and_not_null patron_id] } { + if { [db_0or1row member_rel { + select rel_id + from dotlrn_member_rels_full + where community_id = :community_id + and user_id = :user_id + limit 1 + }] } { + set patron_rel_id [db_exec_plsql relate_patron { + select acs_rel__new (null, + 'membership_patron_rel', + :rel_id, + :patron_id, + null, + null, + null) + }] + } + } + + } errMsg] } { + # Fixes for possible double click + ns_log notice "dotlrn-ecommerce callback: Probably a double-click: $errMsg" + } + } + } } \ No newline at end of file