<?php
/**********************************************************************
***********************************************************************/
/*
	item_codes table is used to store both multiply foreign codes and 
	sale kits definition.
*/


function add_data_access_group($user_id, $data_access_group_id)
{
     $created_by = $_SESSION['wa_current_user']->user;
     $created_on = date('Y-m-d H:i:s');  
     
	$sql = "INSERT INTO ".TB_PREF."user_data_access_groups
			(user_id, dag_id, created_by, created_on) 
		VALUES( ".db_escape($user_id).", ".db_escape($data_access_group_id).",
                         ".db_escape($created_by).",  ".db_escape($created_on).")";
               	  		

	db_query($sql,"user_data_access_groups could not be added");
}

function delete_user_data_access_group($id)
{
    $sql="DELETE FROM ".TB_PREF."user_data_access_groups WHERE id=".db_escape($id);
	db_query($sql,"an user_data_access_groups could not be deleted");
}



function get_user_data_access_groups($user_id)
{
    $sql="SELECT dag.id, dag.dag_id,
                 lv.value_name
        
          FROM ".TB_PREF."user_data_access_groups dag,  
               ".TB_PREF."list_values lv

          WHERE dag.dag_id = lv.id AND
                user_id=".db_escape($user_id)." 
            ORDER BY lv.value_name ";

	return db_query($sql,"all user_data_access_groups could not be retrieved");	
}


function get_existing_data_access_group($dag_user_id, $data_access_group_id, $selected_id)
{
    $sql = "SELECT dag_id
            FROM ".TB_PREF."user_data_access_groups
            WHERE user_id = ".db_escape($dag_user_id)."AND 
                    dag_id = ".db_escape($data_access_group_id)." AND    
                    id <> ".db_escape($selected_id);
    
    $result = db_query($sql, "Could not get existing data access group.");
    return db_num_rows($result);    
}


?>