Location: PHPKode > projects > Homeless Mangement Information System > hmis/query.php
<?php
//*Client Data System, Copyright (C) 2000, 2001 Tedd Kelleher.  This is free software, subject to the 
//*GNU GENERAL PUBLIC LICENSE, Version 2, June 1991 (in file named gpl.txt), which should accompany 
//*any distribution of this file.  Tedd Kelleher can be contacted at hide@address.com


$page_title = "Query Clients";
$page_access_levels = ":10:20:30:40:";

$page_id = "query_report";
$page_profile = "Query Client Data";
$instructions .= "Query client data by selecting parameters below. ";

////Header for every page that finds the include directory, connects to db, authenticates user access 
include("initialize_pointer.php");
if(!include($include_root."authenticate.inc")){echo "No Authentication"; exit; };

include($include_root."Questions_into_array_class.inc");
include($include_root."questions_display.inc");
include($include_root."Questions_answers_validation_class.inc");
//include($include_root."questions_into_tags_function.inc");
include($include_root."head.inc");
include($include_root."template_parser.inc");
include($include_root."page_elements_display.inc");
//include($include_root."client_id_class.inc");
//include($include_root."client_report_entry_class.inc");
include($include_root."client_profile_class.inc");
include( $include_root.'client_lookup_class.inc' );
include($include_root."reports_class.inc");
include($include_root."report_date_questions_class.inc");
include($include_root."relationships_class.inc");
include($include_root."query_class.inc");
include ( $include_root.'encryption_class.inc' );
include ( $include_root.'date_encrypted_functions.inc' );
include ( $include_root.'services_functions.inc' );
$html_template = "generic_form.html";

convert_post_and_get_variable ("query_page");

if (!$form_answer)
    $query_page = "";


switch ($query_page)    {

    case "blank":
    
        display_query_table ($form_answer, $query_page);
    
        break;
        
    case "submitted":
    
        display_query_table ($form_answer, $query_page);
    
        break;
        
    default:
    
        display_report_selection_table ($form_answer); 
 
}



function display_report_selection_table ($form_answer)
{
    GLOBAL $tag_values, $access_level;

    $html_template = "generic_form.html";
    
	$tag_values["{REMAINING_TAGSS}"] .= display_report_options ("client_identifier", "Available Client Profile Forms");
    $tag_values["{REMAINING_TAGSS}"] .= display_report_options ("client", "Available Client Forms");
    $tag_values["{REMAINING_TAGSS}"] .= display_report_options ("household", "Available Household Forms");
    $tag_values["{REMAINING_TAGSS}"] .= display_report_options ("referral", "Available Referral Forms");
    
    //Allow query of surveys by system administrator
    if ( $access_level == "10" ) {    
         $tag_values["{REMAINING_TAGSS}"] .= display_report_options ("survey", "Available Surveys");      
    }
    
    $tag_values["{FORM_END}"] .= display_hidden_form_values("query_page", "blank" );
    
    $tag_values["{FORM_END}"] .= form_end ("Submit Selection", 1);
    
	//display_value ($query_these);
}


function display_query_table ($form_answer, $query_page="")
{
    GLOBAL $tag_values, $html_template, $unix_date, $group_id, $org_id, $access_level, $question_validation_error;
    
    $html_template = "query.html";
    
    //echo "<hr color=purple>In display query table ".$query_page.pretty_var_dump($form_answer)."<hr color=purple>";
    
    $qr = new Query;
        
    //Start with the assumption that we can read the reports
    $perms_read_rpt = 1;
    
	switch ( $query_page )	{
	
		case "blank":
			
			$last_year_unix = time_add ("Y", -1, $unix_date);
			$next_day_unix =  time_add ("d", 1, $unix_date);
			$dates = new Report_date_questions($last_year_unix, $next_day_unix);
			
			break;
		
		case "submitted":
			
			$dates = new Report_date_questions();
			$dates->validate_dates_exist (true);
			if (!$question_validation_error)
    			$valid_date = new Questions_answers_validation($dates->questions, $dates->question_elements, $form_answer);
            //else
                //$dates = new Report_date_questions($last_year_unix, $next_day_unix);
			
			break;
	    
		default:
		
			echo "In the default query display"; exit;
	}
	
	//Space added at end to give tag a value for access level 10 (no orgs), so we don't get "no tag" error
	$tag_values["{ORGS_TABLE}"] = $qr->build_related_organizations_checkboxes_table ($group_id, $org_id, $access_level,
	 $form_answer, $query_page)." ";
	
	$tag_values["{DATES_TABLE}"] = $dates->build_report_date_table ($valid_date->vetted_form_answer);
	$query_date = $dates->translate_report_dates_to_unix_time($valid_date->vetted_form_answer);
	
	$query_these_client_id_reports = validate_report_selection ('client_identifier', $form_answer);
	$query_these_client_reports = validate_report_selection ('client', $form_answer);	
    $query_these_household_reports = validate_report_selection ('household', $form_answer);
    $query_these_referral_reports = validate_report_selection ('referral', $form_answer);
    if ( $access_level == "10" )
	{
        $query_these_survey_reports = validate_report_selection ('survey', $form_answer);
	}
    
	//Cycle through the possible client id reports
    if ( is_array ( $query_these_client_id_reports ) )
    {
		foreach ($query_these_client_id_reports AS $rpt_nm => $current)
		{
        	//echo "1. Cycling through report, on: ".$rpt_nm."<p>";
        	//Place the values indicating which reports we are querying in the form, hidden, and build sql
			$tag_values["{FORM_END}"] .= display_hidden_form_values("form_answer[".$rpt_nm."]", "yes" );
            
        	$qr->pull_query_questions ( $rpt_nm );
        	$qr->validate_query_questions( $rpt_nm, $form_answer );
			$qr->build_client_sql ( $rpt_nm, $query_date["begin"], $query_date["end"] );
            
        	$tag_values["{QUERY_TABLE}"] .= $qr->build_query_table ($rpt_nm);
            
            //Check if user is allowed to read this record, so we can supress client lists if they cannot read
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );
    	}
	}
	
	
	//Cycle through the possible client reports
    if ( is_array ( $query_these_client_reports ) ) 
    {
		foreach ($query_these_client_reports AS $rpt_nm => $current)  
		{
        	//echo "2. Cycling through report, on: ".$rpt_nm."<p>";
        	//Place the values indicating which repots we are querying in the form, hidden, and build sql
			$tag_values["{FORM_END}"] .= display_hidden_form_values("form_answer[".$rpt_nm."]", "yes" );

        	$qr->pull_query_questions ( $rpt_nm );
        	$qr->validate_query_questions( $rpt_nm, $form_answer );
			$qr->build_client_sql ( $rpt_nm, $query_date["begin"], $query_date["end"] );

        	$tag_values["{QUERY_TABLE}"] .= $qr->build_query_table ($rpt_nm);
            
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );
            
            //Check if user is allowed to read this record, so we can supress client lists if they cannot read
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );

    	}
	}
	
    //Cycle through the possible survey reports
    if ( is_array ( $query_these_survey_reports ) ) 
    {
		foreach ($query_these_survey_reports AS $rpt_nm => $current)
		{
        	//echo "3. Cycling through report, on: ".$rpt_nm."<p>";
        	//Place the values indicating which repots we are querying in the form, hidden, and build sql
			$tag_values["{FORM_END}"] .= display_hidden_form_values("form_answer[".$rpt_nm."]", "yes" );
            
        	$qr->pull_query_questions ( $rpt_nm );
        	$qr->validate_query_questions( $rpt_nm, $form_answer );
			$qr->build_client_sql ( $rpt_nm, $query_date["begin"], $query_date["end"] );
            
        	$tag_values["{QUERY_TABLE}"] .= $qr->build_query_table ($rpt_nm);
            
            //Check if user is allowed to read this record, so we can supress client lists if they cannot read
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );
    	}
	}
    
    
    //Cycle through the possible household reports
    if ( is_array ( $query_these_household_reports ) ) 
    {
		foreach ($query_these_household_reports AS $rpt_nm => $current)
		{
        	//echo "4. Cycling through report, on: ".$rpt_nm."<p>";
        	//Place the values indicating which repots we are querying in the form, hidden
			$tag_values["{FORM_END}"] .= display_hidden_form_values("form_answer[".$rpt_nm."]", "yes" );
            
        	$qr->pull_query_questions ( $rpt_nm );
        	$qr->validate_query_questions( $rpt_nm, $form_answer );
			$qr->build_household_sql ( $rpt_nm, $query_date["begin"], $query_date["end"] );
            
        	$tag_values["{QUERY_TABLE}"] .= $qr->build_query_table ($rpt_nm);
            
            //Check if user is allowed to read this record, so we can supress client lists if they cannot read
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );
    	}
	}
    
    
    //Cycle through the possible referral reports
    if ( is_array ( $query_these_referral_reports ) ) 
    {
		foreach ( $query_these_referral_reports AS $rpt_nm => $current )
		{
        	//echo "5. Cycling through report, on: ".$rpt_nm."<p>";
        	//Place the values indicating which repots we are querying in the form, hidden
			$tag_values["{FORM_END}"] .= display_hidden_form_values("form_answer[".$rpt_nm."]", "yes" );
			
        	$qr->pull_query_questions ( $rpt_nm );
        	$qr->validate_query_questions( $rpt_nm, $form_answer );
			$qr->build_household_sql ( $rpt_nm, $query_date["begin"], $query_date["end"] );
			
        	$tag_values["{QUERY_TABLE}"] .= $qr->build_query_table ($rpt_nm);
            
            //Check if user is allowed to read this record, so we can supress client lists if they cannot read
            $perms_read_rpt = check_report_read_permission ( $perms_read_rpt, $rpt_nm );
    	}
	}
	
	
	if ( $query_page == "submitted" )
	{
		$ht = "<table>";
	    transaction_begin( 'Query page' );
		$ht .= "<tr><td class=\"generictabletop\" colspan=\"2\">Results from query variables selected below --</td></tr>";
		if ( $query_these_client_reports || $query_these_client_id_reports )
		{
			//$z = time();
            $ht .= "<tr><td class=\"generictabletop\">Count of Clients: ".$qr->count_clients()."</td></tr>";
			//$zz = time();
            //$dif = $zz - $z;
            //echo 'Client count time: '.$z.'--'.$zz.'--'.$dif.'<p/>';
			//if ( (count ( $query_these_client_reports ) + count ( $query_these_client_id_reports )) < 2 )	{
				//$ht .= "<tr><td class=\"generictabletop\">Number of Service Days Provided to Clients: ".$qr->count_client_service_days()."</td></tr>";
			//}
		}
		if ( $query_these_household_reports  )	
		{
			$ht .= "<tr><td class=\"generictabletop\">Count of Households: ".$qr->count_households()."</td></tr>";			
			
            //todo bugbug: commented out this line as it doesn't quite make sense???  Why return service days, for instance, when simply querying household_intake forms...
			//if ($qr->get_days_of_service)
            //    $ht .= "<tr><td class=\"generictabletop\">Number of Service Days Provided to Households: ".$qr->count_household_service_days()."</td></tr>";
			$ht .= "<tr><td class=\"generictabletop\">Count of Single Clients: ".$client_count = $qr->count_single_clients()."</td></tr>";
			$ht .= "<tr><td class=\"generictabletop\">Count of Individuals in Families: ".$qr->count_individuals_in_families()."</td></tr>";
		}
        
        
        if ( $query_these_referral_reports  )
        {
			$ht .= "<tr><td class=\"generictabletop\">Count of Households: ".$qr->count_households()."</td></tr>";
			
			$ht .= "<tr><td class=\"generictabletop\">Count of Single Clients: ".$client_count = $qr->count_single_clients()."</td></tr>";
			$ht .= "<tr><td class=\"generictabletop\">Count of Individuals in Families: ".$qr->count_individuals_in_families()."</td></tr>";		 
		}
        
		
        if ( $query_these_survey_reports )
        {
            $ht .= "<tr><td class=\"generictabletop\">Count of Matching Surveys: ".$client_count = $qr->count_surveys()."</td></tr>";    
            $ht .= "<tr><td>".$qr->table_averages()."</td></tr>";
            //$ht .= "<tr><td>".$qr->text_answers_listing ()."</td></tr>";
        }
        
        transaction_commit();
		$ht .= "</table>";	
	}
	
	$tag_values["{RESULT_TABLE}"] = $ht.' <p/>';
	
    //Find out if the user does not have permissions to read any of the reports
    
    
    if ( $perms_read_rpt == 1 )   {  
    
        $tag_values["{RESULT_TABLE}"] .= '<p/>'.$qr->clients_list().'<p/>';
    }

    $tag_values["{FORM_END}"] .= display_hidden_form_values("query_page", "submitted" );

    $tag_values["{FORM_END}"] .= form_end ("Submit Query", 1); 

}



function check_report_read_permission ( $previous_read_perm, $report_type ) 
{
    GLOBAL $user_id, $org_id;
    
    $perms = pull_report_permissions_by_report_type_id ( $user_id, $org_id, $report_type );
    
    //echo "<p>Perm is: ".$perms['read_rpt']."<p>";
    if ( $perms['read_rpt'] != 1 )
	{
        $ret_val = -1;
    }
    else
	{
        $ret_val = $previous_read_perm;
    }
    return $ret_val;

}



//Define the values for tags contained in the html template
//$tag_values["{FORM_END}"] .= display_hidden_form_values	("report_type", $report_type);
//$tag_values["{QUERY_RESULTS}"] = $query->display_query_results_table ($query_result["client_count"], $query_result["service_days_count"], $report_type);
$tag_values["{FORM_START}"] = form_start ($this_page);

$tag_values["{PAGE TITLE}"] = page_title_table();
$tag_values["{MENU}"] = menu();
$tag_values["{HEAD}"] = head ($page_title, $head_page_specific, $head_dynamic_style);
$tag_values["{FOOTER}"] = footer();

//echo "Template is: ".$html_template."<br>";
template_parser ($html_template, $tag_values);



?>
Return current item: Homeless Mangement Information System