Location: PHPKode > projects > Homeless Mangement Information System > hmis/generate_simulated_reports.php
<?php
//*Client Data System, Copyright (C) 2000, 2001, 2002, 2003 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

set_time_limit(3000);

$page_access_levels = ":10:";
//$page_profile = "View Client Information";

////Header for every page that finds the include directory, connects to db, authenticates user access 
include("initialize_pointer.php");
if(!include($include_root."authenticate_public.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."question_set_validation_function.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_class.inc");
include($include_root."insert_household_report_class.inc");
include($include_root."insert_client_report_class.inc");
include($include_root."client_profile_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."user_info_functions.inc");
include($include_root."referrals_class.inc");
include($include_root."encryption_class.inc");
include($include_root."date_encrypted_functions.inc");

//echo "report type: ".$report_type."<br>";
//echo "form_submitted: ".$form_submitted."<br>";
convert_post_and_get_variable ("report_name");
convert_post_and_get_variable ("report_unit");
//convert_post_and_get_variable ("new_report_client_id");
convert_post_and_get_variable ("edit_report_id");
$edit_report_id += 0;
convert_post_and_get_variable ("form_answer_added_client");
convert_post_and_get_variable ("empty_form");
convert_post_and_get_variable ("ref_to_org_id");
$ref_to_org_id += 0;




//Pull out array of end users
$sql = "SELECT user_id, org_id FROM user_info WHERE user_access_level = '40'";
$result = run_query ( $sql, 'Pulling end users' );
$end_users_count = num_rows ( $result ) - 1;
for ($r = 0; $r < $end_users_count + 1; $r++ ) {
	$end_user_raw = fetch_array ( $result, 'End user info', $r );
	$end_users[$r]["org_id"] = $end_user_raw["org_id"];
	$end_users[$r]["user_id"] = $end_user_raw["user_id"];
	$sqlb = "SELECT group_id FROM organizations WHERE org_id = '".$end_user_raw["org_id"]."'";
	$res = run_query ( $sqlb, 'get group id' );
	$end_users[$r]["group_id"] = fetch_result ( $res, 'End user group id' );

}


//Pull out the client questions once
$client_profile_questions = new Questions_into_array ('client_profile', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');
$client_assessment_questions = new Questions_into_array ('client_assessment', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');
$client_chronic_questions = new Questions_into_array ('client_chronic', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');

$client_income_questions = new Questions_into_array ('client_income', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');
$client_income_exit_followup_questions = new Questions_into_array ('client_income_exit_followup', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');

//Pull out the hh questions once
$hh_rpt_questions_household_intake = new Questions_into_array ('household_intake', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');

$hh_rpt_questions_household_exit_followup = new Questions_into_array ('household_exit_followup', 40, $end_users[1]["org_id"], $end_users[1]["group_id"], 'display');

//Loop through the number of simulated households we want to create
for ( $i = 0; $i < 50; $i++ )	{
    //Move backwards through time so not all records have same date, one hour back per set of clients
    //echo $i."  ";
    $start_time = time();
    
    $unix_date = $unix_date - 36000;
    if ( $unix_date <= 1 )   {
        $unix_date = time();
    
    }	
	
    //Generate a random user/org	
	$user_index = rand ( 0, $end_users_count );
	$org_id = $end_users[$user_index]["org_id"];
	$user_id = $end_users[$user_index]["user_id"];
	$group_id = $end_users[$user_index]["group_id"];
	//echo $org_id." - ".$user_id." - ".$group_id.'<p/>';
	
	
	//Generate random number of clients
	$client_count = rand ( 1, 4 );
    //$client_count = 1;
	for ( $c = 0; $c < $client_count; $c++ )	{
		
		$client_info[$c] = create_answers_client_identifier_report ($c);
			
	    $client_info[$c]['org_client_status'] = '1';
	}
	//$client_info["0"]['client_relationship_to_lead'] = 'head';

    //Insert the answers
	$insert_new_clients = new Client_id;
    //$client_info['client_id'] = '0';
    $insert_new_clients->client_id_assign($org_id);
    

    $insert_profile = new Client_profile;
    $insert_profile->insert_client_profile_answers ('client_profile', $client_info, $client_profile_questions->questions);

    $insert_cli_rpt = new Insert_client_report;
    
    $report_dates['begin'] = $unix_date;
    $report_dates['end'] = $unix_date;
    
    $increment_days = rand ( 3, 1800 );
    $increment_seconds = $increment_days * 86400;
    $exit_dates['begin'] = $report_dates['begin'] + $increment_seconds;
    $exit_dates['end'] = $report_dates['end'] + $increment_seconds;
    
    for ( $c = 0; $c < $client_count; $c++ )	{
        $vetted_value_client_assessment = create_answers_hh_report ( $client_assessment_questions );        
        $insert_cli_rpt->insert_client_report_answers ( 'client_assessment', $client_info[$c]['client_id'],
         $client_assessment_questions->questions, $vetted_value_client_assessment,
         'yes', '', $report_dates );
     
        $vetted_value_client_chronic = create_answers_hh_report ( $client_chronic_questions );        
        $insert_cli_rpt->insert_client_report_answers ( 'client_chronic', $client_info[$c]['client_id'],
         $client_chronic_questions->questions,
         $vetted_value_client_chronic, 'yes', '', $report_dates );
         
        $vetted_value_client_income = create_answers_hh_report ( $client_income_questions );        
        $insert_cli_rpt->insert_client_report_answers ( 'client_income', $client_info[$c]['client_id'],
         $client_income_questions->questions,
         $vetted_value_client_income, 'yes', '', $report_dates );        
        
        $vetted_value_client_income_exit_followup = create_answers_hh_report ( $client_income_exit_followup_questions );        
        $insert_cli_rpt->insert_client_report_answers ( 'client_income_exit_followup', $client_info[$c]['client_id'],
         $client_income_exit_followup_questions->questions,
         $vetted_value_client_income_exit_followup, 'yes', '', $exit_dates ); 
    }
        
    
    //$insert_profile->insert_client_profile_answers ('client_assessment', $client_info, $client_assessment_questions->questions);
    //$insert_profile->insert_client_profile_answers ('client_chronic', $client_info, $client_chronic_questions->questions);
    
    //$hh_rpt_questions = new Questions_into_array ('household_intake', 40, $org_id, $group_id, 'display');
    
    
    
    $vetted_value_household_intake = create_answers_hh_report ( $hh_rpt_questions_household_intake );
    $vetted_value_household_exit_followup = create_answers_hh_report ( $hh_rpt_questions_household_exit_followup );
    //var_dump ( $client_info );
    
    $hh_rpt = new Insert_household_report;
    
    $hh_rpt->insert_report_answers ('household_intake', $client_info, $hh_rpt_questions_household_intake->questions,
    $vetted_value_household_intake, $org_id,
     'yes', '', $report_dates);
    $hh_rpt->insert_report_answers ('household_exit_followup', $client_info, $hh_rpt_questions_household_exit_followup->questions,
    $vetted_value_household_exit_followup, $org_id,
     'yes', '', $exit_dates ); 
    
    $end_time = time();
    $elapsed =  $end_time - $start_time;
    echo 'Client count: '.$client_info[0]['client_id'].' -- '; 
    
    if ( time () > $sixty_sec_from_now )  {
        $rate_count = $client_info[0]['client_id'] - $start_client_id;
        echo '<p/>In last minute, created: '.$rate_count.' clients<p/>'; 
        
        $start_client_id = $client_info[0]['client_id'];
        $sixty_sec_from_now = time () + 60;    
    }
    $client_info = '';
	 
	
}	 




function create_answers_client_identifier_report ( $clnt_num)	{
	GLOBAL $org_id, $group_id, $client_profile_questions;

	//$client_profile_questions = new Questions_into_array ('client_profile', 40, $org_id, $group_id, 'display');
	
	foreach ( $client_profile_questions->questions AS $cur_question )	{
		
		switch ( $cur_question['question_type'] )	{
			
			case 'radio':
				$ind_client_info[$cur_question['question_id']] = pull_random_radio_answer ( $cur_question, $client_profile_questions->question_elements[$cur_question['question_id']] );
				break;
                
            case 'checkboxes':
				$ind_client_info[$cur_question['question_id']] = pull_random_checkboxes_answer ( $cur_question, $client_profile_questions->question_elements[$cur_question['question_id']] );
				break;
                
            case 'number':
				$ind_client_info[$cur_question['question_id']] = pull_random_number_answer ( $cur_question, $client_profile_questions->question_elements[$cur_question['question_id']] );
				break;
                
            case 'dropdown':
				$ind_client_info[$cur_question['question_id']] = pull_random_radio_answer ( $cur_question, $client_profile_questions->question_elements[$cur_question['question_id']] );
				break;
				
			default:
		
		}
	
	}
	
    if ( $clnt_num == 0 )    {
        $ind_client_info['client_relationship_to_lead'] = 'head of household';
    }
    else {
	    $ind_client_info['client_relationship_to_lead'] = 'spouse';
	}
    $ind_client_info['client_identifier'] = create_client_identifier ();
    $ind_client_info['org_generated_client_id_insert'] = create_org_generated_id();
    $ind_client_info['client_rpt_sharing_permission'] = 'yes';
    $ind_client_info['100'] = rand (1920, 2003);
    //$ind_client_info['101'] = rand (1920, 2003);
	$ind_client_info['client_id'] = '0'; 
	return $ind_client_info;
}



function create_answers_hh_report ( $questions_in_array )	{
	GLOBAL $org_id, $group_id;

	//$client_profile_questions = new Questions_into_array ('household_intake', 40, $org_id, $group_id, 'display');
	
	foreach ( $questions_in_array->questions AS $cur_question )	{
		
		switch ( $cur_question['question_type'] )	{
			
			case 'radio':
				$ind_client_info[$cur_question['question_id']] = pull_random_radio_answer ( $cur_question, $questions_in_array->question_elements[$cur_question['question_id']] );
				break;
                
            case 'checkboxes':
				$ind_client_info[$cur_question['question_id']] = pull_random_checkboxes_answer ( $cur_question, $questions_in_array->question_elements[$cur_question['question_id']] );
				break;
			
            case 'number':
				$ind_client_info[$cur_question['question_id']] = pull_random_number_answer ( $cur_question, $questions_in_array->question_elements[$cur_question['question_id']] );
				break;
                
            case 'dropdown':
				$ind_client_info[$cur_question['question_id']] = pull_random_radio_answer ( $cur_question, $questions_in_array->question_elements[$cur_question['question_id']] );
				break;
                
            case 'table':
				$ind_client_info[$cur_question['question_id']] = pull_random_table_answer ( $cur_question, $questions_in_array->question_elements[$cur_question['question_id']] );
				break;
            	
			default:
		
		}
	
	}
	
    	
    $ind_client_info['client_rpt_sharing_permission'] = 'yes';
	  
	return $ind_client_info;
}







function create_client_identifier ()	{

	$f = chr( rand (97, 122) );
	$m = chr( rand (97, 122) );
	$l = chr( rand (97, 122) );
	
	$month = rand (1, 12);
	$day = rand (1, 28);
	$year = rand (1925, 2003);
	$gender = rand (1, 2);
	if ( $gender == 1 ) {
		$gender = 'male';	
	}
	else {
		$gender = 'female';
	}
	$fin = $f.$m.$l.$month.$day.$year.$gender;
	
	return $fin;
	
}



function pull_random_radio_answer ( $cur_question, $elements ) {
    
    //var_dump (  $cur_question );  
    //echo '<p/>yoohoo<p/>';
    //var_dump (  $elements );
    //echo '<p/>______________________________________________<p/>';
    
    $picked = rand ( 0, count ( $elements )  );
    //echo "Picked".$picked."<p/>";
    $w = 0;
    foreach ( $elements AS $cur_element )   {
        
        //echo "Cre ele value: ".$cur_element['question_element_value']."<br>";
        $ele_val = $cur_element['question_element_value'];
        if ( $w == $picked ) {
            break 1;
        }
        $w++;          
    }
    //echo "The chosens value is: ".$ele_val."<p/>"; 
    return $ele_val;
    
}



function pull_random_checkboxes_answer ( $cur_question, $elements ) {
    
    //var_dump (  $cur_question );  
    //echo '<p/>yoohoo<p/>';
    //var_dump (  $elements );
    //echo '<p/>______________________________________________<p/>';
    
    $number_of_picked_answers = rand ( 1, ((count ( $elements ) ) ) );
    //echo "Number of answers picked: ".$number_of_picked_answers.'<p/>';
    
    for ( $we = 0; $we < $number_of_picked_answers; $we++ ) {
        $picked = rand ( 0, count ( $elements ) );
        //echo "Picked".$picked."<p/>";
        $w = 0;
        foreach ( $elements AS $cur_element )   {

            //echo "Cre ele value: ".$cur_element['question_element_value']."<br>";
            $ele_val = $cur_element['question_element_value'];
            if ( $w == $picked ) {
                break 1;
            }
            $w++;          
        }
        //echo "The chosens value is: ".$ele_val."<p/>";
        $ret_val[$picked] = $ele_val;
    } 
    return $ret_val;
    
}



function pull_random_table_answer ( $cur_q, $elements ) {
    
    //var_dump ( $cur_q );  
    //echo '<p/>yoohoo<p/>';
    //var_dump ( $elements );
    //echo '<p/>______________________________________________<p/>';
    
    
    foreach ( $elements['x'] AS $x_axis_id )    {
        foreach ( $elements['y'] AS $y_axis_id )    {

            $d = rand ( 1, 3 );
            
            if ( $d == 3 || $z < 1 ) {
            
                $ret_val[$x_axis_id['axis_id']][$y_axis_id['axis_id']] =
                    rand (3, 150 ); 
                    //rand ( $x_axis_id['axis_range_bottom'], $x_axis_id['axis_range_top'] ); 
                    
            }
        
            $z++;
            
        }
       
    }
    
        
    return $ret_val;
    
}



function pull_random_number_answer ( $cur_question, $elements ) {
    
    //var_dump (  $cur_question );  
    //echo '<p/>yoohoo<p/>';
    //var_dump (  $elements );
    $num = rand ( $cur_question['question_range_bottom'], $cur_question['question_range_top'] ); 
    
    
    return $ret_val;
    
}



function create_org_generated_id () {
    $first_name_length = rand ( 3, 8 );
    $last_name_length = rand ( 3, 8 );
    
    for ( $i = 0; $i < $first_name_length; $i++ ) {
        $f .= chr( rand (97, 122) );    
    }
    
    for ( $i = 0; $i < $first_name_length; $i++ ) {
        $l .= chr( rand (97, 122) );    
    }
    //echo $f." ".$l;
    return $f." ".$l;


}
                                    
?>
Return current item: Homeless Mangement Information System