im_users_in_group

one of the documented procedures in this installation of the ACS
Usage:
im_users_in_group   db   group_id   current_user_id   { description "" }   { add_admin_links "0" }   { return_url "" }   { limit_to_users_in_group_id "" }   { dont_allow_users_in_group_id "" }   { link_type "" }   { also_add_to_group_id "" }
What it does:
Returns an html formatted list of all the users in the specified group. Includes optional links to add people, add/remove yourself, and spam Required Arguments: ------------------- - db: database handle - group_id: Group we're interested in. We'll display the users in this group - current_user_id: The user_id of the person viewing the page that called this function. This is used to add links like "Add yourself"... Optional Arguments: ------------------- - description: A description of the group. We use pass this to the spam function for UI - add_admin_links: Boolean. If 1, we add links to add/email people. - return_url: Where to go after we do something like add a user - limit_to_users_in_group_id: Only shows users who belong to group_id and who are also members of the group specified in limit_to_users_in_group_id. For example, if group_id is an intranet project, and limit_to_users_group_id is the group_id of the employees group, we only display users who are members of both the employees and this project groups - dont_allow_users_in_group_id: Similar to limit_to_users_in_group_id, but says that if a user belongs to the group_id specified by dont_allow_users_in_group_id, then don't display that user. - link_type: if set to "email_only" then the links returned have no html tags - also_add_to_group_id: If we're adding users to a group, we might also want to add them to another group at the same time. If you set also _add_to_group_id to a group_id, the user will be added first to group_id, then to also_add_to_group_id. Note that adding the person to both groups is NOT atomic. Notes: ------------------- This function has quickly grown out-of-hand with all the additional flags needed. Originally, it seemed like a good idea to encapsulate the admin functions for intranet projects/customers/etc. in one piece of reusable code. However, it would be more useful in the future to create separate functions for each category of group in the intranet, and to use ad_proc to allow for cleaner extension.
Defined in: /web/philip/tcl/intranet-defs.tcl

Source code:


    set html ""
    if { [empty_string_p $limit_to_users_in_group_id] } {
	set limit_to_group_id_sql ""
    } else {
	set limit_to_group_id_sql "and exists (select 1 
                                                 from user_group_map map2, user_groups ug
                                                where map2.group_id = ug.group_id
                                                  and map2.user_id = users.user_id 
                                                  and (map2.group_id = $limit_to_users_in_group_id 
                                                       or ug.parent_group_id = $limit_to_users_in_group_id))"
    } 
    if { [empty_string_p $dont_allow_users_in_group_id] } {
	set dont_allow_sql ""
    } else {
	set dont_allow_sql "and not exists (select 1 
                                              from user_group_map map2, user_groups ug
                                             where map2.group_id = ug.group_id
                                               and map2.user_id = users.user_id 
                                               and (map2.group_id = $dont_allow_users_in_group_id 
                                                    or ug.parent_group_id = $dont_allow_users_in_group_id))"
    } 

    # We need a "distinct" because there can be more than one
    # mapping between a user and a group, one for each role.
    #
    set sql_post_select "from users_active users, user_group_map map
where map.user_id = users.user_id
and map.group_id = $group_id
$limit_to_group_id_sql $dont_allow_sql
order by lower(users.last_name)"

    set sql_query "select distinct
 users.user_id, users.email, users.first_names || ' ' || users.last_name as name, users.last_name
$sql_post_select"
                     
    set selection [ns_db select $db $sql_query]

    set found 0
    set count 0
    while { [ns_db getrow $db $selection] } {
	incr count
	set_variables_after_query
	if { $current_user_id == $user_id } {
	    set found 1
	}
	if { $link_type == "email_only" } {
	    append html "  <li><a href=\"mailto:$email\">$name</a>\n"
	} else {
	    append html "  <li><a href=../users/view?[export_url_vars user_id]>$name</a>"
	}
	if { $add_admin_links } {
	    append html " (<a href=[im_url_stub]/member-remove-2?[export_url_vars user_id group_id return_url]>remove</a>)"
	}
	append html "\n"
    }
    
    if { [empty_string_p $html] } {
	set html "  <li><i>none</i>\n"
    }

    if { $add_admin_links } {

	if { $current_user_id > 0 } {
	    append html "  <p><a href=[im_url_stub]/member-add?role=member&[export_url_vars group_id return_url limit_to_users_in_group_id also_add_to_group_id]>Add a person</a>"
	    if { $found } {
		append html "  <br><a href=[im_url_stub]/member-remove-2?[export_url_vars group_id return_url limit_to_users_in_group_id]&user_id=$current_user_id>Remove yourself</a>"
	    } else {
		# We might not want to offer this user the chance to add him/herself (based on the 
		# group_id set by limit_to_users_in_group_id)
		if { [empty_string_p $dont_allow_users_in_group_id] } {
		    set offer_link 1
		} else {
		    set offer_link [database_to_tcl_string $db  "select decode(decode(count(1),0,0,1),1,0,1)
                               from user_group_map ugm, user_groups ug
                              where ugm.group_id = ug.group_id
                                and ugm.user_id=$current_user_id
                                and (ugm.group_id=$dont_allow_users_in_group_id 
                                     or ug.parent_group_id=$dont_allow_users_in_group_id)"]
		}
		if { $offer_link } {
		    append html "  <br><a href=[im_url_stub]/member-add-3?[export_url_vars group_id limit_to_users_in_group_id return_url also_add_to_group_id]&user_id_from_search=$current_user_id&role=administrator>Add yourself</a>"
		}
	    }
	    if { $count > 0 } {
		# set sql_post_select [im_reduce_spaces $sql_post_select]
		set group_id_list "${group_id},$limit_to_users_in_group_id"
		append html "  <br><a href=[im_url_stub]/spam/index?[export_url_vars group_id_list description return_url]>Spam people</a>"

		# If we have subprojects, then provide option to spam them
		set subgroup_ids [database_to_tcl_list $db  "select group_id from im_projects where parent_id = $group_id"]
		if { [llength $subgroup_ids] > 0 } {
		    set group_id_list "${group_id},[join $subgroup_ids ","]"
		    append html "  <br><a href=[im_url_stub]/spam/index?[export_url_vars group_id_list description return_url]&all_or_any=any>Spam people in subprojects</a>"
		}
	    }
	}
    }
    return $html


philg@mit.edu