Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/reports_class.inc
<?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

class Reports
{
	var $question_set;
    
    
    /********************************************************************************************
     * Name: display_household_reports_table: 
     * Description: Return all report forms for client.
     *
     * Details and Notes: 
     * 
     * @param: $client_id
     * @param: $org_id
     * @return: html of household report table
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
    function display_household_reports_table ($client_id, $org_id)
    {
	    //GLOBAL $head_dynamic_style;// $unique_seq;
        GLOBAL $user_id, $organization_term, $group_term;
        
        $sql = "
        SELECT 
            DISTINCT(report_profile.hh_report_id), report_relationship.client_id, report_org_id,
             report_timestamp, report_type, report_profile.succeeded_by, report_date_begin, report_date_end 
		    FROM 
                report_relationship INNER JOIN report_profile ON ( report_profile.hh_report_id = report_relationship.hh_report_id )
            WHERE 
                report_relationship.client_id = '".$client_id."'
				AND (report_profile.succeeded_by IS NULL OR report_profile.succeeded_by = '-2')
			ORDER BY report_date_begin";
        
		$result =  run_query($sql, "TestB");
		$number_of_reports = num_rows($result);
		
		$final_html .= "<table><tr><td class=\"generictabletop\" colspan=\"6\">Existing Household Forms in System</td></tr>";
		$final_html .= "<tr><td class=\"genericsubtop\">".$organization_term."&nbsp;</td><td class=\"genericsubtop\">Submit Date</td><td class=\"genericsubtop\">Form Type</td><td class=\"genericsubtop\">Details</td><td class=\"genericsubtop\">Action Date(s)</td></tr>";
		$en = new Encryption();
        
		for ( $i = 0; $i < $number_of_reports; $i++)
        {
            $report = fetch_array ($result, "Result", $i);
            
            //Pull the permissions on the report
            $perms = report_permissions_household ( $report['hh_report_id'], $org_id, $user_id );
            
            if ( $perms['read_rpt'] == 1 && $report['report_type'] != 'INTAKE' && $report['succeeded_by'] != '-2' )
            {
			    $printed_rpt_counter++;
                
			    $sqly = "SELECT org_name FROM organizations WHERE org_id = '".$report['report_org_id']."'";
			    $org_name_result = run_query ( $sqly, 'Org Name Pull' );
			    $report_org_name = fetch_result ( $org_name_result, 'Org name pull' );
                $report['report_timestamp'] = $en->decrypt_data ( stripslashes ( $report['report_timestamp'] ) );
                $report_date = safe_date( 'm-d-Y', $report['report_timestamp'] );
			    $report_profile_array = pull_report_type_profile ( $report['report_type'] );
                $report_title = $report_profile_array['report_title'];
                
                if ( $report['report_type'] == 'household_service' )
                {
                    $sqly = "select service_display_term from services_taxonomy right join report_answers on services_taxonomy.service_id = report_answers.report_answer where question_id = 99 and report_answers.hh_report_id = '".$report["hh_report_id"]."' order by service_display_term";
                    $service_name_result = run_query ( $sqly, 'Service Name Pull' );
                    $form_details = fetch_result( $service_name_result, 'Service name pull' );
                }
                else
                {
                    $form_details = "";
                }
                
                if ( $report_profile_array['report_on_timeperiod'] == 'yes' )
                {
                    $action_date =  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_begin'] ) );
                    $action_date .= " - ";
                    if ( $report['report_date_end'] != 0 )
                    {
                        $action_date .=  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_end'] ) );
                    }
                    else
                    {
                        $action_date .= "&lt;pending&gt;";
                    }
                }
                elseif ( $report_profile_array['report_on_timeperiod'] == 'yes_begin_only' )
                {
                    $action_date =  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_begin'] ) );
                }
                else
                {
                    $action_date = "";
                }
                
                //if ( $report['succeeded_by'] == '-2' )  { $report_title .= ' (Deleted)'; }
                
                $final_html .= "<tr><td>".$report_org_name."</td><td>".$report_date."</td><td><a href=\"report.php?report_unit=household&edit_report_id=".$report['hh_report_id']."&report_type_id=".$report['report_type']."&empty_form=yes\">".$report_title."</a></td><td>".$form_details."</td><td>".$action_date."</td></tr>";
            }
		}
		if ( $printed_rpt_counter == 0 )	{$final_html .= "<tr><td colspan=\"5\">No accessible forms</td></tr>";}
        
		$final_html .= "</table>";
        
		return $final_html;
			//else {echo "No reports<br>";}
	}


    /********************************************************************************************
     * Name: display_client_reports_table: 
     * Description: Return all report forms for client.
     *
     * Details and Notes: 
     * 
     * @param: $client_id
     * @param: $org_id
     * @return: html of client report table
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
    function display_client_reports_table ($client_id, $org_id)
    {
	    //GLOBAL $head_dynamic_style, $unique_seq;
        GLOBAL $user_id, $organization_term, $agency_term;
        
		$sql = "
        SELECT 
            client_rpt_profile.report_org_id, client_rpt_profile.report_timestamp, 
            client_rpt_profile.report_type, client_rpt_profile.client_rpt_id, client_rpt_profile.succeeded_by, 
            client_rpt_profile.report_date_beginc, client_rpt_profile.report_date_endc
        
		FROM 
            client_rpt_profile 
        WHERE
		    client_rpt_profile.client_id = '".$client_id."' AND
			(client_rpt_profile.succeeded_by IS NULL OR client_rpt_profile.succeeded_by = '-2')  
        ORDER BY report_date_beginc
        ";
        
		$result =  run_query($sql, "TestB");
		$number_of_reports = num_rows($result);
        
		$final_html .= "<table><tr><td class=\"generictabletop\" colspan=\"6\">Existing Client Forms in System</td></tr>";
		$final_html .= "<tr><td class=\"genericsubtop\">".$organization_term."&nbsp;</td><td class=\"genericsubtop\">Submit Date</td><td class=\"genericsubtop\">Form Type</td><td class=\"genericsubtop\">Details</td><td class=\"genericsubtop\">Action Date(s)</td></tr>";
		$en = new Encryption();
        
        for ( $i = 0; $i < $number_of_reports; $i++ )
        {
            $report = fetch_array ( $result, 'Result', $i );
            $perms = report_permissions_client ( $report['client_rpt_id'], $org_id, $user_id );
            if ( $perms['read_rpt'] == 1 && $report['succeeded_by'] != '-2')
            {
                $printed_rpt_counter++;
                
                $sqly = "SELECT org_name FROM organizations WHERE org_id = '".$report["report_org_id"]."'";
                $org_name_result = run_query ( $sqly, 'Org Name Pull' );    
                $report_org_name = fetch_result( $org_name_result, 'Org name pull' );
                $report['report_timestamp'] = $en->decrypt_data ( stripslashes ( $report['report_timestamp'] ) );
                $report_date = safe_date( 'm-d-Y', $report['report_timestamp'] );
                $report_profile_array = pull_report_type_profile ( $report['report_type'] );
                $report_title = $report_profile_array['report_title'];
                
                if ( $report['report_type'] == 'client_service' )
                {
                    $sqly = "select service_display_term from services_taxonomy right join client_rpt_answers on services_taxonomy.service_id = client_rpt_answers.client_rpt_answer where client_rpt_question_id = 99 and client_rpt_answers.client_rpt_id = '".$report["client_rpt_id"]."' order by service_display_term";
                    $service_name_result = run_query ( $sqly, 'Service Name Pull' );
                    $form_details = fetch_result( $service_name_result, 'Service name pull' );
                }
                else
                {
                    $form_details = "";
                }
                
                if ( $report_profile_array['report_on_timeperiod'] == 'yes' )
                {
                    $action_date =  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_beginc'] ) );
                    $action_date .= " - ";
                    if ( $report['report_date_endc'] != 0 )
                    {
                        $action_date .=  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_endc'] ) );
                    }
                    else
                    {
                        $action_date .= "&lt;pending&gt;";
                    }
                }
                elseif ( $report_profile_array['report_on_timeperiod'] == 'yes_begin_only' )
                {
                    $action_date =  safe_date( 'm-d-Y', date_encrypted_translate_index_to_unix_time( $report['report_date_beginc'] ) );
                }
                else
                {
                    $action_date = "";
                }
                
                //if ( $report['succeeded_by'] == '-2' )  { $report_title .= ' (Deleted)'; }      
                
                $final_html .= "<tr><td>".$report_org_name."</td><td>".$report_date."</td><td><a href=\"report.php?edit_report_id=".$report["client_rpt_id"]."&report_type_id=".$report['report_type']."&empty_form=yes&report_unit=client\">".$report_title."</a></td><td>".$form_details."</td><td>".$action_date."</td></tr>";
            }
        }
        if($printed_rpt_counter == 0)	{$final_html .= "<tr><td colspan=\"4\">No accessible forms</td></tr>";}
        
        $final_html .= "</table>";
        
        return $final_html;
	}



    /********************************************************************************************
     * Name: pull_existing_report_profile: 
     * Description: Return report profiles.
     *
     * Details and Notes: 
     * 
     * @param: $edit_report_id
     * @return: array of report vars
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
	function pull_existing_report_profile ($edit_report_id)
    {
		$edit_report_id += 0;
		$sql = "SELECT * FROM report_profile WHERE hh_report_id = '".$edit_report_id."'";
		$result = run_query ( $sql, 'Existing report profile pull' );
		$report_profile_array = fetch_array ( $result, "Pulling existing report profile", '0' );
        
        $en = new Encryption();
        //echo 'Here: '.$report_profile_array['report_timestamp'].'</p>';
        $report_profile_array['report_timestamp'] = $en->decrypt_data ( stripslashes ( $report_profile_array['report_timestamp'] ) );
		//echo 'Here dec: '.$report_profile_array['report_timestamp'].' - '.time().'</p>';
        
		$user_info_array = pull_user_variables ( $report_profile_array['report_user_id'] );
        		
		$all_report_variables_array = array_merge ( $report_profile_array, $user_info_array );
		
		return $all_report_variables_array;
     
	}



    /********************************************************************************************
     * Name: pull_existing_client_rpt_profile: 
     * Description: Return all report profiles.
     *
     * Details and Notes: 
     * 
     * @param: $edit_report_id
     * @return: array of report vars
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
    function pull_existing_client_rpt_profile ($edit_report_id) 
	{
		$edit_report_id += 0;
        
        //immediately return null if id=0 to avoid fetch_array() error since result set contains no rows!
        if ($edit_report_id==0) return null;
        
		$sql = "SELECT * FROM client_rpt_profile WHERE client_rpt_id = '".$edit_report_id."'";
        
		$result = run_query ( $sql, 'Exisitng report profile pull' );
        
        if ($result)
        {
		    $report_profile_array = fetch_array ( $result, 'Pulling existing report profile', '0' );
            //echo pretty_var_dump($report_profile_array);
        }
        
        //$report_profile_array['report_date_beginc'] = date_encrypted_translate_index_to_unix_time ( $report_profile_array['report_date_beginc'] );
		$en = new Encryption();
        
        $report_profile_array['report_timestamp'] = $en->decrypt_data ( stripslashes ( $report_profile_array['report_timestamp'] ) );
        
        if (is_array($report_profile_array) )
        {
		    $user_info_array = pull_user_variables ( $report_profile_array['report_user_id'] );
        }
        
		$all_report_variables_array = array_merge ( $report_profile_array, $user_info_array );
		
		return $all_report_variables_array;
	}


    /********************************************************************************************
     * Name: display_report_profile: 
     * Description: Return all report profiles.
     *
     * Details and Notes: 
     * 
     * @param: $report_title
     * @param: $user_name
     * @param: $user_org_name
     * @param: $report_date
     * @return: html of report profile header table
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
	function display_report_profile ($report_title, $user_name, $user_org_name, $report_date)
	{
		GLOBAL $organization_term;

		$final_html .= "\n<table class=\"generictable\"><tr>";
		$final_html .= "<td class=\"generictabletop\"><span class=\"yellow\">".$report_title." Form</span></td>";
		$final_html .= "<td class=\"generictabletop\"><span class=\"smaller75\">Submitted by:<br/>".$user_name."</span></td>";
		$final_html .= "<td class=\"generictabletop\"><span class=\"smaller75\">".$organization_term.":<br/> ".$user_org_name."</span></td>";
		$final_html .= "<td class=\"generictabletop\"><span class=\"smaller75\">".safe_date("F d, Y g:i a", $report_date)."</span></td>";
		$final_html .= "</table>";

		return $final_html;
	}


    /********************************************************************************************
     * Name: pull_existing_report_answers: 
     * Description: Return answer array.
     *
     * Details and Notes: 
     * 
     * @param: $edit_report_id
     * @param: $questions
     * @param: $questions_elements
     * @param: $id_addon = ''
     * @return: html of report profile header table
     * 
     * History: Created 1/17/03 by Tedd.
     ********************************************************************************************/
	function pull_existing_report_answers ( $edit_report_id, $questions, $questions_elements, $id_addon = '' )
    {
        //If there is one result, we are allowed to edit this report
		//if (num_rows($result) == 1)	{
			$sqlb = "SELECT * FROM report_answers WHERE hh_report_id = '".$edit_report_id."'";
            //echo $sqlb."<p>";
			$resultb = run_query ( $sqlb, 'Pulling existing report answers' );
			
			$answer_count = num_rows ($resultb);
			
            $en = new Encryption();
                    
            for ( $i = 0; $i < $answer_count; $i++ )
            {
				$report_answer_row = fetch_array ( $resultb, 'Report Answer Array', $i );
				//echo "count is ".$i.'<br/>';
				
                $question_number = $report_answer_row['question_id'];
				$modded_qid = $id_addon.$report_answer_row['question_id'];
                
                //echo 'modded qid is '.$modded_qid.'<br/>';
                $question_type = $questions[$modded_qid]['question_type'];
                                
                //If encrypted, decrypt
                if ( $questions[$question_number]['question_encrypted'] == 1 )  {
                        
                    if ( $question_type == 'number' || $question_type == 'date' )   {
                    
                        $report_answer_row['report_answer_int'] = stripslashes( $en->decrypt_data( $report_answer_row['report_answer_int'] ) );
                        
                    }
                    else {
                        $report_answer_row['report_answer'] = stripslashes( $en->decrypt_data( $report_answer_row['report_answer'] ) );
                    
                    }
                }
                
                
				
                //echo $question_type.":::TYPE<br/>";
                
				if ( $question_type == 'radio' || $question_type == 'dropdown' )
                {
				    //echo "radio<br/>";
                   
                    foreach ( $questions_elements[$modded_qid] AS $element_key => $element)
                    {
						//echo "Element key radio: ".$element_key." element: ".$element["question_element_value"]." Report Answer: <br>";
						
                        
                        if ( $report_answer_row['report_answer'] == $element['question_element_value'] )
                        {
							//echo "This is it: ".$element_key;
							$report_answer[$modded_qid] = $element_key;
							break 1;
                        }
					}
                }
                
				elseif ( $question_type == 'checkboxes' )
                {
                    //echo "Checkboxes<br/>";
				   foreach ( $questions_elements[$modded_qid] AS $element_key => $element )
                   {
						//echo "Element key checkboxes: ".$element_key." element: ".$element["question_element_value"]." Report Answer: <br>";
                        
                        if ( $report_answer_row['report_answer'] == $element['question_element_value'] )
                        {
							//echo "This is it CHECKED: ".$element_key;
							$report_answer[$modded_qid][$element_key] = 'yes';
							break 1;
						}
					}
				}
                
				elseif	( $question_type == 'text' )
                {
                    //echo "  Text<br/>";
				    $report_answer[$modded_qid] = stripslashes( $report_answer_row['report_answer'] );
                    
                    //If a 'no answer' explanation, put the answer in the 'noan' value
                    if ( $report_answer_row['report_answer'] == '#88#' 
                    || $report_answer_row['report_answer'] == '#98#' 
                    || $report_answer_row['report_answer'] == '#99#' 
                    )
                    {
                        $report_answer['noan'][$modded_qid] = $report_answer_row['report_answer'];
                        //Unset the question type so we don't return a value
                        $report_answer[$modded_qid] = '';  
                    }
                }
                
                elseif	($question_type == 'number' || $question_type == 'date' )
                {
                    //echo "  Number<br/>";
				    $report_answer[$modded_qid] = $report_answer_row['report_answer_int'];
                    //If a 'no answer' explanation, put the answer in the 'noan' value
                    if ( $report_answer_row['report_answer'] == '#88#' 
                    || $report_answer_row['report_answer'] == '#98#' 
                    || $report_answer_row['report_answer'] == '#99#' 
                    )
                    {
                        $report_answer['noan'][$modded_qid] = $report_answer_row['report_answer'];
                        //Unset the question type so we don't return a value
                        $report_answer[$modded_qid] = '';  
                    }
                    //echo $question_type." - ".$report_answer[$question_number]."<br/>";
				    //echo $report_answer_row["report_question_id"]."questid "."&nbsp;".$report_answer[$report_answer_row["report_question_id"]]." questid<br/>";
				
				}
                
                elseif	( $question_type == 'table' )
                {
                    //echo "  Text<br/>";
                    if ( $report_answer_row['report_answer_int'] )
                    {
				        $report_answer[$modded_qid][$report_answer_row['axis_x']][$report_answer_row['axis_y']]
                         = $report_answer_row['report_answer_int'];
                    }
                    else
                    {
                        $report_answer[$modded_qid][$report_answer_row['axis_x']][$report_answer_row['axis_y']]
                         = stripslashes($report_answer_row['report_answer'] );
                    }
                }
                
                elseif	( $question_type == 'textarea' )
                {
                    //echo "  Textarea<br/>";
				    //$report_answer[$modded_qid] = stripslashes( $report_answer_row['report_answer'] );
                    $sqlta = "
                    SELECT textarea_answer FROM textarea_answers 
                        WHERE textarea_unique LIKE '".$report_answer_row['report_answer']."'";
                    $res = run_query ( $sqlta, 'Pulling texans' );
                    $report_answer[$modded_qid] = fetch_result ( $res, 'Txtarea ans' );
                    
                    //If a 'no answer' explanation, put the answer in the 'noan' value
                    if ( $report_answer[$modded_qid] == '#88#' 
                    || $report_answer[$modded_qid] == '#98#' 
                    || $report_answer[$modded_qid] == '#99#' 
                    )
                    {
                    $report_answer['noan'][$modded_qid] = $report_answer[$modded_qid];
                    //Unset the question type so we don't return a value
                    $report_answer[$modded_qid] = '';
                    }
                    $report_answer[$modded_qid] = stripslashes( $report_answer[$modded_qid] );
                }
                
                else
                {
                
                    //echo "No question type defined: ".$question_type.$report_answer_row["report_question_id"]."<br/>";
                
                }
			}
		//}
		//else {echo "Illegal attempt to edit report, IP logged"; exit;}
        //var_dump ( $report_answer );
		return $report_answer;
	}


   /********************************************************************************************
    * Name: find_report_question_set: 
    * Description: Return answer array.
    *
    * Details and Notes: 
    * 
    * @param: $report_id
    * @return: array of questions
    * 
    * History: Created 1/17/03 by Tedd.
    ********************************************************************************************/
	function find_report_question_set ($report_id)
    {
		$sql = "SELECT report_type FROM report_profile WHERE hh_report_id = '".$report_id."'";
		//echo $sql;
		$result = run_query($sql, "Finding the report type");
        
		$this->question_set = fetch_result($result, "Finding the report type return");
		
		return $this->question_set;
	}


   /********************************************************************************************
    * Name: build_household_report: 
    * Description: Return html.
    *
    * Details and Notes: 
    * 
    * @param: $questions
    * @param: $question_elements
    * @param: $form_answer
    * @param: $date_html=""
    * @param: $share_answer
    * @return: html
    * 
    * History: Created 1/17/03 by Tedd.
    ********************************************************************************************/
    function build_household_report ($questions, $question_elements, $form_answer, $date_html="", $share_answer)
    {
   	    GLOBAL $organization_term;
        
        if ( $date_html )
        {
            $html .= $date_html;
        }
        
        $td_count = 0;
        $html .= "\n<table>";
        
		foreach($questions AS $current_question)
        {
			//Make rows two questions long using <tr> and </tr>
			if ($td_count == 2)	{$html .= "</tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>"; $td_count = 0; }
			if ($td_count == 0) {$html .= "<tr>\n"; }
			
			$html .= question_display ($current_question, $question_elements[$current_question["question_id"]], $form_answer);
			$td_count++;
		}
        
		//If there are an odd numer of questions, finish the table row and close it
		if($td_count == "1"){$html .= "<td>&nbsp;</td></tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>";}
		if($td_count == "2"){$html .= "</tr>\n";}
        
        
        //echo "Share answer is ".$share_answer."<p>";
        if ( $share_answer == "no" )
		{
            $checked_no = " checked=\"checked\"";
        }
        else
		{
            $checked_yes = " checked=\"checked\"";
        }
        
        $html .= "<tr><td colspan=\"2\">Share report with other ".$organization_term."s?&nbsp;
                        Yes<input type=\"radio\" name=\"form_answer[share_report]\" value=\"yes\" ".build_tabindex_html(1100)." ".$checked_yes.">&nbsp;
                        No <input type=\"radio\" name=\"form_answer[share_report]\" value=\"no\"  ".build_tabindex_html(1100)." ".$checked_no."></td></tr>";
	    
		$html .= "</table>\n";
        return $html;
	}
    
    
   /********************************************************************************************
    * Name: validate_household_share_answer: 
    * Description: Return whether form answers are to be shared.
    *
    * Details and Notes: 
    * 
    * @param: $form_answer
    * @return: yes/no shared?
    * 
    * History: Created 1/17/03 by Tedd.
    ********************************************************************************************/
    function validate_household_share_answer ( $form_answer )
    {
        if ( $form_answer['share_report'] == 'no' )
        {
            return 'no';
        }
        else
        {
            return 'yes';
        }
        
    
    }
    
    
} //END Reports Class





//////////////////////////////////////
/////// standalone functions /////////
/////// follow...            /////////
//////////////////////////////////////

/********************************************************************************************
 * Name: validate_household_share_answer: 
 * Description: Pulls out the report profile array (e.g., "report_title", "report_on_unit")
 *
 * Details and Notes: 
 * 
 * @param: $report_name
 * @return: report profile array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_report_type_profile ( $report_name ) 
{
    if ( ereg ( "[^0-9a-zA-Z\_]", $report_name ) )
    {
        echo 'Illegal Report Type Requested'; exit;
    }
    //echo pretty_var_dump ($report_name);
    $sql = "SELECT * FROM report_type_profile WHERE report_type_id LIKE '".$report_name."'";
    //echo $sql."<p>";
    $return_result = run_query_return_single_row ( $sql, 'Pulling report_type_profile' );
    
    if ( !$return_result)
    {
        echo 'No matching reports foundD'; exit;
    }
    
    return $return_result;
}


/********************************************************************************************
 * Name: pull_client_report_profile: 
 * Description: Pulls out the report profile based on the report id
 *
 * Details and Notes: 
 * 
 * @param: $report_id
 * @return: report profile array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_client_report_profile ($report_id)
{
    if (ereg("[^0-9]", $report_id))
    {
        echo "Illegal client Form ID Requested"; exit;
    }
    
    $sql = "SELECT * FROM client_rpt_profile WHERE client_rpt_id = '".$report_id."'";
    $report_array = run_query_return_single_row( $sql, "Pulling client report profile array" );
      
    if ( !$report_array)
    {
        echo "No matching reports found for idd"; exit;   
    }
    
    return $report_array;

}


/********************************************************************************************
 * Name: pull_household_report_profile: 
 * Description: Pulls out the report profile based on the report id
 *
 * Details and Notes: 
 * 
 * @param: $report_id
 * @return: report profile array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_household_report_profile ($report_id)
{
    if (ereg("[^0-9]", $report_id))
    {
        echo "Illegal Report ID Requested"; exit;
    }
    
    $sql = "SELECT * FROM report_profile WHERE hh_report_id = '".$report_id."'";
    $report_array = run_query_return_single_row( $sql, "Pulling client report profile array" );
      
    if ( !$report_array)
    {
        echo "No matching reports found for id"; exit;
    }
    
    return $report_array;
}



/********************************************************************************************
 * Name: pull_array_of_report_type_id: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $report_type_id
 * @return: result set
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_array_of_report_type_id ($report_type_id)
{
    if( ereg("[^A-Za-z0-9]\_", $report_type_id) )
    {
        $report_type_id = "";
        echo "Improper report type id, can't pull";
        exit;
    }
    
    $sql = "SELECT * FROM report_type_profile WHERE report_type_id LIKE '".$report_type_id."'";    
    $return_result = run_query_return_single_row ($sql, "Pulling report type info nbased on report type id");
    
    return $return_result;
}



/********************************************************************************************
 * Name: pull_names_of_reports: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $report_on_unit
 * @return: result set
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_names_of_reports ($report_on_unit)
{
    if ( $report_on_unit != 'client_identifier' )
        $sql = "SELECT * FROM report_type_profile WHERE report_on_unit LIKE '".$report_on_unit."%' AND report_on_unit != 'client_identifier'";
    else
        $sql = "SELECT * FROM report_type_profile WHERE report_on_unit LIKE '".$report_on_unit."%'";
    
    $return_result = run_query_return_array ( $sql, 'Pulling available reports list' );
    
    return $return_result;
}



//INNER JOIN per_group_associated_reports 
//           ON ( report_type_profile.report_on_unit = per_group_associated_reports.report_type_id )


/********************************************************************************************
 * Name: pull_names_of_reports_user_can_create: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $user_id, $org_id, $report_on_unit
 * @return: report set
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_names_of_reports_user_can_create ( $user_id, $org_id, $report_on_unit )
{
    $sql = "
    SELECT 
        DISTINCT(report_type_profile.report_type_id), report_type_profile.* 
    FROM
        per_group_profile
               
        INNER JOIN per_associated_users 
           ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
        INNER JOIN per_group_associated_orgs 
           ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
        INNER JOIN per_group_associated_reports 
           ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id ),
           
         report_type_profile
        
        WHERE
            
           report_type_profile.report_on_unit LIKE '".$report_on_unit."%'
           AND
           report_type_profile.report_on_unit != 'client_identifier'
           
           AND
               per_group_profile.org_id = '".$org_id."'   
           
           AND ( 
               per_associated_users.user_id = '".$user_id."' 
               OR
               per_associated_users.user_id = '1'
               )
           AND 
               (        
               per_group_associated_reports.report_type_id = report_type_profile.report_type_id
               OR
               per_group_associated_reports.report_type_id = 'ALL'
               )
           AND 
               per_group_associated_reports.create_rpt = '1' 
    ";
    
    //echo $sql;
    
    
    //$sql = "SELECT * FROM report_type_profile WHERE report_on_unit LIKE '".$report_on_unit."'";
    $return_result = run_query_return_array ($sql, "Pulling available reports list");
    
    //var_dump ( $return_result );
    
    return $return_result;
}



/********************************************************************************************
 * Name: pull_report_permissions_by_report_type_id: 
 * Description: 
 * 
 * Details and Notes: 
 * 
 * @param: $user_id, $org_id, $report_on_unit
 * @return: result set
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_report_permissions_by_report_type_id ( $user_id, $org_id, $report_on_unit )
{
    $sql = "
    SELECT 
        DISTINCT(per_group_associated_reports.report_type_id), per_group_associated_reports.* 
    FROM
        per_group_profile
        
        INNER JOIN per_associated_users 
           ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
        INNER JOIN per_group_associated_orgs 
           ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
        INNER JOIN per_group_associated_reports 
           ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id )
        
        WHERE
               per_group_profile.org_id = '".$org_id."'   
         
           AND ( 
               per_associated_users.user_id = '".$user_id."' 
               OR
               per_associated_users.user_id = '1'
               )
           AND 
               (        
               per_group_associated_reports.report_type_id = '".$report_on_unit."'
               OR
               per_group_associated_reports.report_type_id = 'ALL'
               )
           
        ";
    
    //echo $sql;
    $return_result = run_query_return_single_row ( $sql, 'Pulling report permission by report_type id' );
        
    return $return_result;
}



/********************************************************************************************
 * Name: per_group_report_type_id: 
 * Description: Returns the permissions associated with a specific report_type_id
 * (such as "household_intake")
 *
 * Details and Notes: 
 * 
 * @param: $user_id, $org_id, $report_type_id
 * @return: result set
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
//
function per_group_report_type_id ( $user_id, $org_id, $report_type_id )
{
    $sql = "
    SELECT 
        per_group_associated_reports.* 
    FROM
        per_group_profile
               
        INNER JOIN per_associated_users 
           ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
        INNER JOIN per_group_associated_orgs 
           ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
        INNER JOIN per_group_associated_reports 
           ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id )
        
        WHERE
              (
              per_group_associated_reports.report_type_id = '".$report_type_id."'
              OR
              per_group_associated_reports.report_type_id = 'ALL'  
              ) 
                  
           AND
               per_group_profile.org_id = '".$org_id."'   
         
           AND ( 
               per_associated_users.user_id = '".$user_id."' 
               OR
               per_associated_users.user_id = '1'
               )
         
    ";
        
    //echo $sql;
    
    $return_result = run_query_return_single_row ($sql, 'Pulling available reports list' );
    
    return $return_result;
}



/********************************************************************************************
 * Name: pull_other_organization_report_permissions: 
 * Description: Pulls permitted report type ids
 *
 * Details and Notes: Pull report type ids (i.e., shelter_referral) of reports an organization
 * can create for another organization (such as referrals).  The user has to have access to 
 * the report, and the other org has to have given access to the report
 * 
 * @param: $user_id, $user_org_id, $outside_org_id
 * @return: report id array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_other_organization_report_permissions ( $user_id, $user_org_id, $outside_org_id )
{
    $sql = "
    SELECT 
        DISTINCT(as_reports_outside.report_type_id), as_reports_outside.* 
    FROM
        per_group_profile
               
        INNER JOIN per_associated_users 
           ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
        INNER JOIN per_group_associated_orgs 
           ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
        INNER JOIN per_group_associated_reports 
           ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id ),
           
           
        per_group_profile AS as_group_profile_outside 
                   
        INNER JOIN per_associated_users AS as_users_outside 
           ON ( as_group_profile_outside.per_group_id = as_users_outside.per_group_id )
        INNER JOIN per_group_associated_orgs AS as_orgs_outside
           ON ( as_group_profile_outside.per_group_id = as_orgs_outside.per_group_id ) 
        INNER JOIN per_group_associated_reports AS as_reports_outside
           ON ( as_group_profile_outside.per_group_id = as_reports_outside.per_group_id ) 
                 
        
        WHERE
               per_group_profile.org_id = '".$user_org_id."'   
         
           AND ( 
               per_associated_users.user_id = '".$user_id."' 
               OR
               per_associated_users.user_id = '1'
               )
               
               
               
       AND 
               (        
               per_group_associated_reports.report_type_id LIKE as_reports_outside.report_type_id
               OR
               per_group_associated_reports.report_type_id LIKE 'ALL'
               
               )
               
               
               
        AND    
               
           as_group_profile_outside.org_id = '".$outside_org_id."'   
           
           AND ( 
               as_users_outside.user_id = '".$user_id."' 
               OR
               as_users_outside.user_id = '1'
               )
               
        AND
        
            as_orgs_outside.org_id =  '".$user_org_id."'
        
";
    
    
    //echo $sql;
    
    $return_array = run_query_return_array ( $sql, 'Pulling report permission by report_type id', 'report_type_id' );
    
    //var_dump ( $return_array );
        
    return $return_array;
}



/********************************************************************************************
 * Name: pull_names_of_all_reports_available_for_editing: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $access_level
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_names_of_all_reports_available_for_editing ( $access_level )
{
    $sql = "SELECT * FROM report_type_profile";
    $return_result = run_query_return_array ($sql, "Pulling available reports list");
    
    foreach ( $return_result AS $current_result )
    {
        if (ereg (":".$access_level.":", $current_result["report_edit_access_levels"]) )
        {
            $return_array[$current_result["report_type_id"]] = $current_result;
        }
    }
    
    return $return_array;
}



/********************************************************************************************
 * Name: check_that_report_type_exists: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $report_type_id
 * @return: 0/1 false/true report exists?
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function check_that_report_type_exists ($report_type_id)
{
    if( ereg("[^A-Za-z0-9]\_", $report_type_id) ) {
        $report_type_id = "";
        echo "Improper report type id";
        exit;
    }
    
    $sql = "SELECT * FROM report_type_profile WHERE report_type_id LIKE '".$report_type_id."'";    
    $result = run_query ($sql, "Checking that report type exists");
    $count = num_rows ($result);
    if ($count < 1) {
        echo "Report type does not exist";
        return 0;
    }
    else
    {
        return 1;
    }
   
}



/********************************************************************************************
 * Name: report_permissions_household: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $report_id, $org_id, $user_id
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function report_permissions_household ( $report_id, $org_id, $user_id )
{
    //GLOBAL $unique_seq;
    //echo "report iD is ".$report_id."<p>";
    $report_id += 0;
    $org_id += 0;
    $user_id += 0;
    
    
    $sql = "
    SELECT per_group_associated_reports.*, report_profile.report_sharing_permission, report_profile.report_org_id 
    FROM
     per_group_profile 
     INNER JOIN per_associated_users 
        ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
     INNER JOIN per_group_associated_orgs 
        ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
     INNER JOIN per_group_associated_reports 
        ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id )
     INNER JOIN report_profile 
        ON ( report_profile.hh_report_id = '".$report_id."' )
     
     WHERE
        per_group_profile.org_id = report_profile.report_org_id  
              
        AND (
            per_group_associated_orgs.org_id = '".$org_id."'
            OR
            per_group_associated_orgs.org_id = '1' 
            )            
        AND ( 
            per_associated_users.user_id = '".$user_id."' 
            OR
            per_associated_users.user_id = '1'
            )
        AND 
            (        
            per_group_associated_reports.report_type_id = report_profile.report_type
            OR
            per_group_associated_reports.report_type_id = 'ALL'
            )
     
     
     ";
    
    //echo $sql;
    $share_array = run_query_return_single_row ( $sql, 'Pulling hh form permissions' );
	
	//echo '<hr color=red>'.pretty_var_dump ($share_array);
	//$share_array['share_rpt'] = $share_array['report_sharing_permission'];
	//echo pretty_var_dump($share_array).'<hr color=red>';
	
     //If the report is not owned by the currently logged in organization, and it is not shared, return no sharing permissions   
    if ( $share_array['report_org_id'] != $org_id && $share_array['report_sharing_permission'] != 'yes' )   {
        //unset($share_array);
        
        $share_array['create_rpt'] = '0';
        $share_array['read_rpt'] = '0';
        $share_array['edit_rpt'] = '0';
		$share_array['report_sharing_permission'] = 'no'; //default
        //echo "no shared ".$report_id."<p>";
    }
	
	
    
    return $share_array;
}



/********************************************************************************************
 * Name: report_permissions_client: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $report_id, $org_id, $user_id
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function report_permissions_client ( $report_id, $org_id, $user_id )
{
    //GLOBAL $unique_seq;
    //echo "reporot_id is ". $report_id."<p>";   
    $report_id += 0;
    $org_id += 0;
    $user_id += 0;
    
    
    $sql = "
    SELECT per_group_associated_reports.*, client_rpt_profile.client_rpt_sharing_permission, client_rpt_profile.report_org_id 
    FROM
     per_group_profile 
     INNER JOIN per_associated_users 
        ON ( per_group_profile.per_group_id = per_associated_users.per_group_id )
     INNER JOIN per_group_associated_orgs 
        ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
     INNER JOIN per_group_associated_reports 
        ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id )
     INNER JOIN client_rpt_profile 
        ON ( client_rpt_profile.client_rpt_id = '".$report_id."' )
        
     WHERE
        per_group_profile.org_id = client_rpt_profile.report_org_id  
              
        AND (
            per_group_associated_orgs.org_id = '".$org_id."'
            OR
            per_group_associated_orgs.org_id = '1' 
            )            
        AND ( 
            per_associated_users.user_id = '".$user_id."' 
            OR
            per_associated_users.user_id = '1'
            )
        AND 
            (        
            per_group_associated_reports.report_type_id = client_rpt_profile.report_type
            OR
            per_group_associated_reports.report_type_id = 'ALL'
            )
     
     
     ";
 
    //echo $sql;
    $share_array = run_query_return_single_row ( $sql, "Pulling client form permissions" );
    
    //echo "<p>AAA";
    //var_dump($share_array);
    //echo "<p>BBB";
    
    //If the report is not owned by the currently logged in organization, and it is not shared, return no sharing permissions   
    if ( $share_array['report_org_id'] != $org_id && $share_array['client_rpt_sharing_permission'] != 'yes' )   {
        //unset($share_array);       
        //echo "Unsetting perms<p>";
        $share_array['create_rpt'] = '0';
        $share_array['read_rpt'] = '0';
        $share_array['edit_rpt'] = '0';
         
    }
    
    return $share_array;
}



/********************************************************************************************
 * Name: per_group_report_type_id_notification: 
 * Description: Returns an arry of users to notify associated with a specific report_type_id (such as "household_intake") 
 *
 * Details and Notes: 
 * 
 * @param: $org_id, $report_type_id, $action
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function per_group_report_type_id_notification ( $org_id, $report_type_id, $action )    {

    $sql = "
    SELECT 
        DISTINCT ( per_associated_users_notificati.user_id ) 
    FROM
        per_group_profile        
               
        INNER JOIN per_associated_users_notificati 
           ON ( per_group_profile.per_group_id = per_associated_users_notificati.per_group_id )
        INNER JOIN per_group_associated_orgs 
           ON ( per_group_profile.per_group_id = per_group_associated_orgs.per_group_id ) 
        INNER JOIN per_group_associated_reports 
           ON ( per_group_profile.per_group_id = per_group_associated_reports.per_group_id )
        
        WHERE
              (
              per_group_associated_reports.report_type_id = '".$report_type_id."'
              OR
              per_group_associated_reports.report_type_id = 'ALL'  
              ) 
                  
           AND
               per_group_profile.org_id = '".$org_id."'
               
           AND per_group_associated_reports.".$action."_rpt = '1'   
                                
        ";
        
        //echo $sql;

    $return_result = run_query_return_array ( $sql, 'Pulling user to notify' );
    
    return $return_result;

}




/********************************************************************************************
 * Name: list_previous_versions_of_client_report: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt, $rep_type=''
 * @return: html
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function list_previous_versions_of_client_report ($edit_rpt, $rep_type='')
{
    $rp = new Reports;
    $z = $rp->pull_existing_client_rpt_profile ($edit_rpt);
	
	if ($rep_type)
		$report_type_id = $rep_type;
	else
		$report_type_id = $page_id;
    
    $newer_reports_array = pull_newer_client_reports ($z["succeeded_by"]);    
    if ( is_array ( $newer_reports_array ) ) {
        $newer_reports_array = array_reverse ( $newer_reports_array );
    }
    $older_reports_array = pull_old_client_reports ($edit_rpt);
    
    $merged_array = array_merge_recursive( $newer_reports_array, $older_reports_array);
    
    if ( count ( $merged_array ) > 0 )   {
        //echo pretty_var_dump($merged_array);
        $f_html .= "<table><tr><td class=\"generictabletop\">Other drafts of this form</td></tr>";
        $f_html .= "\n<tr><td><form name=\"formid2_".$page_id."\" method=\"post\" "; 
	    $f_html .= "action=\"report.php?empty_form=yes&report_unit=client\">";
	    $f_html .= "<input type=\"hidden\" name=\"form_submitted\" value=\"".$page_id."\">\n";
		$f_html .= "<input type=\"hidden\" name=\"report_type_id\" value=\"".$report_type_id."\">\n";
        $f_html .= "\n<select name=\"edit_report_id\">";
        foreach ( $merged_array AS $cur_element )
		{
            $f_html .= "<option value=\"".$cur_element["rpt_id"]."\">".$cur_element["rpt_id"]."  --".$cur_element["description"]."</option>";
        }
        $f_html .= "</select>";
        $f_html .= form_end ($button_message="Click to load form");
        $f_html .= "</td></tr></table>";
        return $f_html;
    }
}



/********************************************************************************************
 * Name: list_previous_versions_of_hh_report: 
 * Description: Pulls out the report profile based on the report id
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt
 * @return: html
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function list_previous_versions_of_hh_report ($edit_rpt)
{
    $rp = new Reports;
    
    $z = $rp->pull_existing_report_profile ($edit_rpt);
    
    $newer_reports_array = pull_newer_hh_reports ($z["succeeded_by"]);    
    if ( is_array ( $newer_reports_array ) ) {
        $newer_reports_array = array_reverse ( $newer_reports_array );
    }
    $older_reports_array = pull_old_hh_reports ($edit_rpt);
    
    $merged_array = array_merge_recursive( $newer_reports_array, $older_reports_array);
    
    if ( count ( $merged_array ) > 0 )   {
    	//echo pretty_var_dump($merged_array);
        $f_html .= "<table><tr><td class=\"generictabletop\">Other drafts of this form</td></tr>";
        $f_html .= "\n<tr><td><form name=\"formid2_".$page_id."\" method=\"post\" "; 
	    $f_html .= "action=\"report.php?empty_form=yes&report_unit=household_service\">";
	    $f_html .= "<input type=\"hidden\" name=\"form_submitted\" value=\"".$page_id."\">\n";
        
        $f_html .= "\n<select name=\"edit_report_id\">";
        foreach ( $merged_array AS $cur_element )
        {
            $f_html .= "<option value=\"".$cur_element["rpt_id"]."\">".$cur_element["rpt_id"]."  --".$cur_element["description"]."</option>";
        }
        $f_html .= "</select>";
        $f_html .= form_end ($button_message="Click to load form");
        $f_html .= "</td></tr></table>";
        return $f_html;
    }
}



/********************************************************************************************
 * Name: pull_old_client_reports: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_old_client_reports ($edit_rpt)
{
    $rp = new Reports;
    $next_report_id = $edit_rpt;
    $z = $rp->pull_existing_client_rpt_profile ($next_report_id);
    $sql = "SELECT client_rpt_id FROM client_rpt_profile WHERE succeeded_by = '".$next_report_id."'";
    $w = run_query ($sql, "Finding old reports");
    $row_count = num_rows( $w );
    if ( $row_count > 0 ) {
        $x = fetch_array ($w, "Fetching old client report id array", 0);
        $next_report_id = $x[0];
    }
    else {
        $next_report_id = -3;
    }
    
    while ( $next_report_id > -2 )  {
    
        $z = $rp->pull_existing_client_rpt_profile ($next_report_id);
        //echo "Old report ID: ".$z["client_rpt_id"]." -- Succeeded by: ".$z["succeeded_by"]."<br/>";
        $r_array[$z["client_rpt_id"]]["description"]
         = $z["client_rpt_id"]."  - ".$z["user_name_last"].", ".$z["user_name_first"]."; ".safe_date("M j, Y, g:i:s a",
         $z["report_timestamp"])." (older)";
        $r_array[$z["client_rpt_id"]]["rpt_id"] = $z["client_rpt_id"];
        //display_value ($z);
        
        
        $sql = "SELECT client_rpt_id FROM client_rpt_profile WHERE succeeded_by = '".$next_report_id."'";
        $w = run_query ($sql, "Finding old reports");
        $row_count = num_rows( $w );
        if ( $row_count > 0 ) {
            $x = fetch_array ($w, "Fetching old client report id array", 0);
            $next_report_id = $x[0];
        }
        else {
            $next_report_id = -3;
        }
    
    
    }    
    
    return $r_array;

}



/********************************************************************************************
 * Name: pull_old_hh_reports: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_old_hh_reports ($edit_rpt)
{
    $rp = new Reports;
    $next_report_id = $edit_rpt;
    $z = $rp->pull_existing_report_profile ($next_report_id);
    $sql = "SELECT hh_report_id FROM report_profile WHERE succeeded_by = '".$next_report_id."'";
    $w = run_query ($sql, "Finding old reports");
    $row_count = num_rows( $w );
    if ( $row_count > 0 ) {
        $x = fetch_array ($w, "Fetching old client report id array", 0);
        $next_report_id = $x[0];
    }
    else
    {
        $next_report_id = -3;
    }
    
    while ( $next_report_id > -2 )
    {
        $z = $rp->pull_existing_report_profile ($next_report_id);
        //echo "Old report ID: ".$z["client_rpt_id"]." -- Succeeded by: ".$z["succeeded_by"]."<br/>";
        $r_array[$z["hh_report_id"]]["description"]
         = $z["hh_report_id"]."  - ".$z["user_name_last"].", ".$z["user_name_first"]."; ".safe_date("M j, Y, g:i:s a",
         $z["report_timestamp"])." (older)";
        $r_array[$z["hh_report_id"]]["rpt_id"] = $z["hh_report_id"];
        //display_value ($z);
        
        
        $sql = "SELECT hh_report_id FROM report_profile WHERE succeeded_by = '".$next_report_id."'";
        $w = run_query ($sql, "Finding old reports");
        $row_count = num_rows( $w );
        if ( $row_count > 0 ) {
            $x = fetch_array ($w, "Fetching old hh report id array", 0);
            $next_report_id = $x[0];
        }
        else {
            $next_report_id = -3;
        }
    
    
    }
    
    return $r_array;
}



/********************************************************************************************
 * Name: pull_newer_client_reports: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_newer_client_reports ($edit_rpt)
{
    $rp = new Reports;
    
    $newer_report_id = $edit_rpt;
    
    while ( $newer_report_id > -2 )
    {
        $z = $rp->pull_existing_client_rpt_profile ($newer_report_id);
        //echo "Newer report ID: ".$z["client_rpt_id"]." -- Succeeded by: ".$z["succeeded_by"]."<br/>";
        
        if ( $z["succeeded_by"] == "")
        {
            $newer_report_id = -3;
            $age_desc = " (CURRENT)";
        
        }
        elseif ( $z["succeeded_by"] == "-2")
        {
            $newer_report_id = -3;
            $age_desc = " (DELETED)";
        
        }
        
        else {
            $newer_report_id = $z["succeeded_by"];
            $age_desc = " (newer)";    
        }
        
        $r_array[$z["client_rpt_id"]]["description"] = $z["client_rpt_id"]."  - ".$z["user_name_last"].", ".$z["user_name_first"].";
        ".safe_date("M j, Y, g:i:s a", $z["report_timestamp"]).$age_desc;  
        $r_array[$z["client_rpt_id"]]["rpt_id"] = $z["client_rpt_id"];
    
    }
    return $r_array;
 
}



/********************************************************************************************
 * Name: pull_newer_hh_reports: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $edit_rpt
 * @return: array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function pull_newer_hh_reports ($edit_rpt)
{
    $rp = new Reports;
    
    $newer_report_id = $edit_rpt;
    
    while ( $newer_report_id > -2 )
    {
        $z = $rp->pull_existing_report_profile ($newer_report_id);
        //echo "Newer report ID: ".$z["client_rpt_id"]." -- Succeeded by: ".$z["succeeded_by"]."<br/>";
        
        if ( $z["succeeded_by"] == "")
        {
            $newer_report_id = -3;
            $age_desc = " (CURRENT)";
        
        }
        elseif ( $z["succeeded_by"] == "-2")
        {
            $newer_report_id = -3;
            $age_desc = " (DELETED)";
        
        }
        
        else
        {
            $newer_report_id = $z["succeeded_by"];
            $age_desc = " (newer)";
        }
        
        $r_array[$z["hh_report_id"]]["description"] = $z["hh_report_id"]."  - ".$z["user_name_last"].", ".$z["user_name_first"].";
        ".safe_date("M j, Y, g:i:s a", $z["report_timestamp"]).$age_desc;
        $r_array[$z["hh_report_id"]]["rpt_id"] = $z["hh_report_id"];
    
    }
    return $r_array;
}



/********************************************************************************************
 * Name: log_report_activity: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $user_id, $report_type, $report_id, $report_action
 * @return: report profile array
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function log_report_activity ( $user_id, $report_type, $report_id, $report_action )
{
    GLOBAL $unix_date;
    $sql = "
    INSERT INTO log_report_activity (
        user_id,
        report_type,
        report_id,
        report_action,
        log_session_id,
        log_date_stamp
        )
        VALUES (
        '".$user_id."',
        '".$report_type."',
        '".$report_id."',
        '".$report_action."',
        '".$_SESSION['sess_idB']."',
        '".$unix_date."'        
        )";
        run_query ( $sql, 'log_rpt_act' );
}



/********************************************************************************************
 * Name: duplicate_form_submission_check: 
 * Description: 
 *
 * Details and Notes: 
 * 
 * @param: $form_signature
 * @return: 1/0 true/false form submitted
 * 
 * History: Created 1/17/03 by Tedd.
 ********************************************************************************************/
function duplicate_form_submission_check ( $form_signature )
{
    GLOBAL $question_validation_error, $message, $message_type;
    
    //echo $form_signature;
    settype ( $_SESSION['submitted_form_signatures_array'], 'array' );
    
    $z = array_search ( $form_signature, $_SESSION['submitted_form_signatures_array'] );
    
    if ( strlen ( $z ) > 0 )
    {
        $ret_val = 1;
        
        //Comment out the following three lines if you want to disable the duplicate form check
        $question_validation_error['fsig'] = 'Duplicate form submission';
        $message .= 'This form was submitted previously';
        $message_type = 'error';
        
    }
    else
    {
        $ret_val = 0;
        array_push ( $_SESSION['submitted_form_signatures_array'], $form_signature );
        $_SESSION['submitted_form_signatures_array'] = array_slice ( $_SESSION['submitted_form_signatures_array'], -10 );
        
    }
    
    //var_dump ( $_SESSION['submitted_form_signatures_array'] );
    
    return $ret_val;
}



?>
Return current item: Homeless Mangement Information System