Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/insert_household_report_class.inc
<?php
//*Client Data System, Copyright (C) 2000, 2001, 2002 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 Insert_household_report {
    var $report_id;

	function insert_report_answers ( $report_type,
	                                 $client_info, 
									 $questions_attributes, 
									 $vetted_value, 
									 $org_id, 
									 $share_answer,
                                     $edit_report_id='',
									 $report_dates='' )
	{
		GLOBAL $user_id, $unix_date, $unique_seq, $message, $message_type;
		
		$report_type = addslashes($report_type);
        
        //If they are editing a report, check to see if they have permission to edit
        if ( $edit_report_id )  {
            //Function from reports_class.inc
            $perms = report_permissions_household ( $edit_report_id, $org_id, $user_id );
            if ( $perms['edit_rpt'] != 1 )
			{
                echo 'Illegal attempt to submit edited form'; exit;
            }
        }
        else {
            //$perms = report_permissions_household ( $edit_report_id, $org_id, $user_id );
            $perms = per_group_report_type_id ( $user_id, $org_id, $report_type );
            if ( $perms['create_rpt'] != 1 )
			{
                echo 'Illegal attempt to create form'; exit;
            }
        }
        
        //Setup the report date insertions, default to current day if no dates are supplied
        if ( $report_dates )
		{
            $begin_date = $report_dates['begin'];
            $end_date = $report_dates['end'];
            //echo pretty_var_dump($report_dates);
        }
        else
		{
            $begin_date = $unix_date;
            $end_date = $unix_date;
        }
        
        //Convert dates into their encrypted date equivalent
        $begin_date = date_encrypted_find_index_unix_time ( $begin_date );
        if ($end_date)
		{
            $end_date = date_encrypted_find_index_unix_time ( $end_date );
            $end_date = "'".$end_date."'";
        }
	    else {
	        $end_date = 'NULL';    
	    }

        $en = new Encryption ();
        $unix_date_encrypted = addslashes ( $en->encrypt_data ( $unix_date ) );
		
        transaction_begin ( 'Trans begin for insert household report answers' );
        
		$sqla = "INSERT INTO report_profile (hh_report_id, report_type, report_org_id, report_user_id, report_date_begin, report_date_end,
         report_timestamp, report_sharing_permission)
          VALUES ('0', '".$report_type."', '".$org_id."', '".$user_id."', '".$begin_date."', ".$end_date.",
           '".$unix_date_encrypted."', '".$share_answer."')";
		run_query ( $sqla, 'Insert report Profile' );
        //echo $sqla."<p>";
        
		$sqlb = "SELECT rpt_prof_rowid FROM report_profile WHERE report_user_id = '".$user_id."'
         AND hh_report_id = '0' AND report_timestamp = '".$unix_date_encrypted."' ";
		$report_id_result = run_query ( $sqlb, 'Insert report ProfileB' );
        //echo $sqlb."<p>";
        
		$this->report_id = fetch_result ( $report_id_result, 'Extract report ID' );
        
		$sqlc = "UPDATE report_profile SET hh_report_id='".$this->report_id."'
         WHERE rpt_prof_rowid = '".$this->report_id."'";
		run_query ( $sqlc, 'Insert report ProfileC' );
        //echo $sqlc."<p>";
        
        
        
        //If we are editing a report, depreciate the old report
        if ($edit_report_id)    {
            //Make sure the report id is a number
            if ( ereg('[^0-9]', $edit_report_id) )  {
                echo 'Non numeric client report id.'; 
				transaction_rollback ( 'Non numeric client report id.' );
				exit;
            }
            
            $sqld = "UPDATE report_profile SET succeeded_by='".$this->report_id."'
             WHERE hh_report_id = '".$edit_report_id."'";
		    //echo $sqld."<p/>";
            run_query ( $sqld, 'Insert report ProfileC' );
            $message .= 'Successfully submitted edited household report. ';
            log_report_activity ( $user_id, 'household', $this->report_id, 'edit_hh_rpt' );
        }
        else
		{
            $message .= 'Successfully submitted new household report. ';
            log_report_activity ( $user_id, 'household', $this->report_id, 'create_hh_rpt' ); 
        }
        $message_type = 'ok';  
        
		//For each client_info entry, insert the report id and relationship to lead for this report
        //display_value ($client_info);
        
        foreach ( $client_info AS $current_client_id )
		{
			$sqld = "INSERT INTO report_relationship (hh_report_id, client_id,
             client_relationship_to_lead) VALUES ('".$this->report_id."', '".$current_client_id['client_id']."',
              '".$current_client_id['client_relationship_to_lead']."')"; 
			
            if ( !$current_client_id['client_relationship_to_lead'] ) {
                echo 'Client relationship not defined, insert failed<p>'; 
				transaction_rollback ( 'Client relationship not defined, insert failed<p>' );
				exit;
                
            }
            
            run_query ( $sqld, 'Insert report ProfileD' );
        
	    }
        
		//echo '<table border=1 bordercolor=red><tr><td>'.$sqla.'</td></tr><tr><td>'.$sqlb.'</td></tr><tr><td>'.$sqlc.'</td></tr><tr><td>'.$sqld.'</td></td></table>';
		
        //From encryption_class.inc
        $en = new Encryption();
		//Loop through the questions set
		//echo 'q attribues: <font size=1>'.var_dump($questions_attributes).'</font>';
		foreach ( $questions_attributes AS $question_id => $current_question )
		{
            //Find the real question id if the question id has the report type id at begining
            $real_qid = str_replace ( $report_type.'__', '', $question_id );
		    
            //If the answer is an array (i.e., checkboxes or table) loop through array
			if(is_array($vetted_value[$question_id]))
			{
				//echo "Array Answers: ".$question_id."<br>";
                //var_dump ( $current_question ); echo "<p><p>";
                
			    switch ($current_question['question_type']) {
                    
                    case 'checkboxes':
                    
                        foreach($vetted_value[$question_id] AS $array_answer)
						{
						//echo $array_answer."<br>";
                            if ( $array_answer )    {
						        if ( $current_question['question_encrypted'] == 1 )  {
                                    $array_answer = addslashes( $en->encrypt_data( $array_answer ) );
                                }   
                                
                                $sql = "INSERT INTO report_answers (hh_report_id, question_id, report_answer) VALUES
                                 ('".$this->report_id."', '".$real_qid."', '".$array_answer."')";
						        run_query ($sql, 'Insert array report answer');
                                //echo '<font color=red size=1>'.$sql."</font><p>";
                            }
					    }
                        break;
                    
                    case 'table':
                        foreach ($vetted_value[$question_id] AS $x_key => $y_array) {
                            foreach ($y_array AS $y_key => $table_answer)   {
                                $sql = "INSERT INTO report_answers (hh_report_id, question_id, 
                                axis_x, axis_y, report_answer_int) 
                                VALUES ('".$this->report_id."', '".$real_qid."', '".$x_key."', '".$y_key."', '".$table_answer."')";
						        run_query ($sql, "Insert array report answer");
                                //echo '<font color=green size=1>'.$sql."</font><p>";
                            }
                        }
                        
                        break;
                        
                    default:
                        echo 'No question type defined in client_report_entry_class.inc'; 
                
                }
		    }
			else
			{
                if ( $vetted_value[$question_id] )
                {
				    //Actually insert the report answers
                    
                    if ( $current_question['question_encrypted'] == 1 )
                    {
                        $vetted_value[$question_id] = $en->encrypt_data( $vetted_value[$question_id] );
                    }
                    
                    $vetted_value[$question_id] = addslashes($vetted_value[$question_id]);
                    
                    //echo "Current question type is: ".$current_question["question_type"]."<br/>";
                    if ( $current_question['question_type'] == 'number' || $current_question['question_type'] == 'date' )
                    {
                        //If a non answer option as indicated by a "#", insert into text field
                        //echo "Ans in insert is ".$vetted_value[$question_id]."<p>";
                        if ( strstr ( $vetted_value[$question_id], '#' ) )
                        {
                            $insert_field = 'report_answer';
                        }
                        else
                        {
                            $insert_field = 'report_answer_int';
                        }
                    }
                    elseif ( $current_question['question_type'] == 'textarea' )
                    {
                        $text_ans = $vetted_value[$question_id];
                        $vetted_value[$question_id] = md5 ( $vetted_value[$question_id].$user_id.$unix_date );
                        $sqlta = "
                        INSERT INTO textarea_answers ( textarea_answer, textarea_unique ) 
                            VALUES ( '".$text_ans."', '".$vetted_value[$question_id]."' )
                        ";
                        run_query ( $sqlta, 'Insert tx ans c' );
                        $insert_field = 'report_answer';
                    }
                    else
					{
                        $insert_field = 'report_answer';
                    }
                    
				    //echo "Question #: ".$question_id." Answer: ".$vetted_value[$question_id]."<br>";
				    $sql = "INSERT INTO report_answers (hh_report_id, question_id, ".$insert_field.") VALUES
                     ('".$this->report_id."', '".$real_qid."', '".$vetted_value[$question_id]."')";
				    run_query ($sql, 'Insert report answer');
                }
			}
        
		}
        
        transaction_commit ( 'Trans commit for insert_report_answers (household)' );
        
	    return $this->report_id;
    }



    function find_newest_report_in_series ($edit_rpt)
	{
        $newer_report_id = $edit_rpt;
        
        while ( $newer_report_id > 0 )
        {
            $sql = "SELECT succeeded_by FROM report_profile WHERE hh_report_id = '".$newer_report_id."'";
            $w = run_query ($sql, "Finding newer reports");
            $row_count = num_rows( $w );
            if ( $row_count > 0 ) {
                $x = fetch_array ($w, "Fetching newer hh report id array", 0);
                $temp_newer_report_id = $x[0];
            }
            else
			{
                echo "tried to retieve non-existant row in find_newest_report_in_series<p/>";
                $newer_report_id = 0;
            }
            //We found the newest report, so jump out of the loop
            if ( $temp_newer_report_id == "" || $temp_newer_report_id == "-2" )
            {
                $newest_report_id = $newer_report_id;
                $newer_report_id = 0; 
            }
            //We did not find the newest (i.e., it has a sucessor), or loop again
            else
            {
                $newer_report_id = $temp_newer_report_id;
           }
        
        }
		//echo '<br>Newest report id in series is: '.$newest_report_id;
        return $newest_report_id; 
    }


    function delete_household_report ( $edit_report_id )
    {
        //Make sure the report id is a number
        GLOBAL $org_id, $message, $message_type;
        if( ereg("[^0-9]", $edit_report_id ) )
		{
            echo "Non numeric hh report id."; exit;
        }
        
        //If they are editing a report, check to see they are from the same organization that owns the report
        if ( $edit_report_id )  {
            //Function from reports_class.inc
            $perms = report_permissions_household ( $edit_report_id, $org_id, $_SESSION['user_idB'] );
            if ( $perms['edit_rpt'] != 1 )    {
                echo "Illegal attempt to submit edited form"; exit;
            }
        }
        
        //Find the newest client report in the string, and depreciate that
        $newest_rpt_id = $this->find_newest_report_in_series ($edit_report_id);
        
        $sqld = "UPDATE report_profile SET succeeded_by = '-2' WHERE hh_report_id = '".$newest_rpt_id."'";
		//echo $sqld."<p/>";
        run_query ($sqld, "Insert report ProfileC");
        $message_type = "ok";
        $message = "Successfully deleted household form. ";
        log_report_activity ( $_SESSION['user_idB'], 'household', $edit_report_id, 'delete_hh_rpt' );      
    }



/*
	function insert_edited_report_answers ($report_id, $client_info, $questions_attributes, $vetted_value)	{
		GLOBAL $org_id, $user_id, $unix_date, $client_info;
		
		$sqla = "UPDATE report_profile SET report_org_id='".$org_id."', report_user_id='".$user_id."',
         report_timestamp='".$unix_date."'  WHERE hh_report_id='".$report_id."'";
		run_query ($sqla, "Insert report ProfileBB");

		//$sqld = "INSERT INTO report_relationship (hh_report_id, report_relationship_client_id, client_relationship_to_lead) VALUES ('".$report_id."', '".$client_id."', 'lead')"; 
		//run_query ($sqld, "Insert report ProfileD");
		

		//Delete the old answers
		$sql = "DELETE FROM report_answers WHERE hh_report_id = '".$report_id."'";
		run_query ($sql, "Insert report answer");

		//Loop through the questions set
		foreach($questions_attributes AS $question_id => $current_question)	{
		
			//If the answer is an array (i.e., checkboxes) loop through array
			if(is_array($vetted_value[$question_id]))	{
				//echo "Array Answers: ".$question_id."<br>";
								
				foreach($vetted_value[$question_id] AS $array_answer)		{
					//echo $array_answer."<br>";
					$sql = "INSERT INTO report_answers (hh_report_id, question_id, report_answer) VALUES ('".$report_id."', '".$question_id."', '".$array_answer."')";
					run_query ($sql, "Insert array report answer");
																			}
														}
			else	{
				$vetted_value[$question_id] = addslashes($vetted_value[$question_id]);
				
                if ( $current_question["question_type"] == "number" )   { 
                        $insert_field = "report_answer_int"; 
                    }
                    
                    else    { $insert_field = "report_answer";
                    }
                
				//echo "Question #: ".$question_id." Answer: ".$vetted_value[$question_id]."<br>";
				$sqlb = "INSERT INTO report_answers (hh_report_id, question_id, ".$insert_field.") VALUES ('".$report_id."', '".$question_id."', '".$vetted_value[$question_id]."')";
				run_query ($sqlb, "Insert report answer");
					}

																			}
				////Erase and reinsert the report_relationships between clients
				//Delete the old relationships
				$sql = "DELETE FROM report_relationship WHERE hh_report_id = '".$report_id."'";
				run_query ($sql, "Delete old report relationships");

				foreach ($client_info AS $current_client_id)	{

					$sqld = "INSERT INTO report_relationship (hh_report_id, report_relationship_client_id, client_relationship_to_lead) VALUES ('".$report_id."', '".$current_client_id["client_id"]."', '".$current_client_id["client_relationship_to_lead"]."')"; 
					run_query ($sqld, "Insert report relationships for edited form");
			
																}

					}

													
*/

}

?>
Return current item: Homeless Mangement Information System