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

$page_title = "Client Page";
//$page_id = "clients";
$page_access_levels = ":10:";
$page_profile = "View Client Information";
$instructions .= "View client information by inputting their ID below. ";

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


function build_question_element_creation_sql ($question_id, $question_type) {
    
    if ( $question_type == "table" ) {
        $question_elements_table_column_names = return_column_names_array("table_elements", 1);

        $sql = "SELECT ".$question_elements_table_column_names["list"]." FROM table_elements 
        WHERE question_id = '".$question_id."'";
        $insert_into_sql = "table_elements";
        
        $question_elements_table_column_names_p = return_column_names_array("table_profiles", 1);

        $sql_p = "SELECT ".$question_elements_table_column_names_p["list"]." FROM table_profiles 
        WHERE question_id = '".$question_id."'";
        $profile_row = run_query_return_single_row ($sql_p, "Pulling table profile for dump");
        
        
        for ($z=0; $z < $question_elements_table_column_names_p["column_count"]; $z++) {

                if ( strlen ( $profile_row[$z] ) > 0 ) {
                    $values_list_p .= "'".$profile_row[$z]."'";
                }
                else {
                    $values_list_p .= 'NULL';
                }
                if ($z != $question_elements_table_column_names_p["column_count"]-1)   {
                    $values_list_p .= ", "; 
                }
        }
       
        $dump_result .= "     run_query(\"INSERT INTO table_profiles (".$question_elements_table_column_names_p["list"].") VALUES (".$values_list_p.")\", \"Error inserting table profile #".$profile_row["question_id"]."\");\n";
        
        unset ($values_list_p);
   
    }
    else    {
        $question_elements_table_column_names = return_column_names_array("question_elements", 0);

        $sql = "SELECT ".$question_elements_table_column_names["list"]." FROM question_elements 
        WHERE question_id = '".$question_id."'";
        $insert_into_sql = "question_elements";
    }
    //echo $sql;
    $question_elements_result = run_query($sql, "Pulling all question elements result");
    if($question_elements_result){

        $question_elements_count = num_rows($question_elements_result);
        //echo"the count is" 

        for($i=0; $i < $question_elements_count; $i++)   {
        $question_elements_attributes_array = fetch_array($question_elements_result, "Fetching question array data for dump", $i);

            for ($z=0; $z < $question_elements_table_column_names["column_count"]; $z++) {

                if ( strlen ( $question_elements_attributes_array[$z] ) > 0 )   {
                
                    $values_list .= "'".$question_elements_attributes_array[$z]."'";
                }
                else {
                    $values_list .= 'NULL';
                }
                if ($z != $question_elements_table_column_names["column_count"]-1)   {
                    $values_list .= ", "; 
                }
            }

            $dump_result .= "          run_query(\"INSERT INTO ".$insert_into_sql." (".$question_elements_table_column_names["list"].") VALUES (".$values_list.")\", \"Error inserting question element #".$question_elements_attributes_array["question_id"]."\");\n";
            unset($values_list);
        }

    }
    
    return $dump_result;


}



//If a report name is submitted, vet it and build sql
if ($HTTP_GET_VARS["report_name"])  {
    if( ereg("[^a-zA-z0-9\_]", $HTTP_GET_VARS["report_name"]) )    {
        echo "Illegal report name"; exit;
    
    }
    $where_sql = " WHERE question_report_type_ids.report_type_id LIKE '".$HTTP_GET_VARS["report_name"]."'";
    $where_report_profile_sql = " WHERE report_type_id LIKE '".$HTTP_GET_VARS["report_name"]."'";
}
else    {
    $where_sql = "";
    $where_report_profile_sql = "";    
}    

//Pull out report_type_profile data
$rpt_profile_table_column_names_array = return_column_names_array("report_type_profile", 1);


$sqlp = "SELECT * FROM report_type_profile".$where_report_profile_sql;
$report_profile_array = run_query_return_array ( $sqlp, "Pulling array of report profiles" );
//Loop through the question_profiles rows
foreach ( $report_profile_array AS $cur_rpt_profile )   {
    for ($u = 1; $u < $rpt_profile_table_column_names_array["column_count"]+1; $u++ )   {
        $values_listb .= "'".$cur_rpt_profile[$u]."'";
        if ($u != $rpt_profile_table_column_names_array["column_count"])   {
            $values_listb .= ", "; 
        }
    }  

    $dump_result .= "\n////".$cur_rpt_profile['report_title']." -- ".$cur_rpt_profile['report_description']." -- ".$cur_rpt_profile['report_type_id']."\n\n";
    $dump_result .= "run_query(\"INSERT INTO report_type_profile (".$rpt_profile_table_column_names_array["list"].") VALUES (".$values_listb.")\", \"Error inserting question report profile #\");\n";
    unset($values_listb);
    
    $where_sql = " WHERE question_report_type_ids.report_type_id LIKE '".$cur_rpt_profile['report_type_id']."'";





//$question_table_column_names = return_column_names_array("questions", 1);
$question_table_column_names = return_column_names_array("questions", 0);

//Remove the report_type_id from list
//$question_table_column_names['list'] = str_replace ( 'report_type_id,', '', $question_table_column_names['list'] );

$question_table_column_names["list"] = str_replace ( 'question_id', 'questions.question_id', $question_table_column_names["list"] );

$sql = "
SELECT 
    ".$question_table_column_names["list"].",  
    question_report_type_ids.report_type_id 
    FROM 
        questions
        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id)
    ".$where_sql;
    
$question_table_column_names["list"] = str_replace ( 'questions.question_id', 'question_id', $question_table_column_names["list"] );    

//echo $sql;
$questions_result = run_query($sql, "Pulling all question result");
$question_count = num_rows($questions_result);
$total_columns = $question_table_column_names["column_count"] ;

for($i=0; $i < $question_count; $i++)   {
    $question_attributes_array = fetch_array($questions_result, "Fetching question array data for dump", $i);
    
    for ($z=0; $z < $total_columns; $z++) {
        //If not the "report_type_id"
        //if ( $z != 11 ) {
            if ( strlen ( $question_attributes_array[$z] ) > 0 )    {            
                $values_list .= "'".$question_attributes_array[$z]."'";
            }
            else {
                $values_list .= 'NULL';    
            }
            if ($z != $question_table_column_names["column_count"] - 1 )   {
                $values_list .= ", "; 
            }
        //}
    }



//Check to see if this question has already been inserted
if ( $question_has_been_inserted[$question_attributes_array["question_id"]] != 'yes' )  {

    $dump_result .= "\n    //".$question_attributes_array['question_title'];
    $dump_result .= "\n    run_query(\"INSERT INTO questions (".$question_table_column_names["list"].") VALUES (".$values_list.")\", \"Error inserting question #".$question_attributes_array["question_id"]."\");\n";

    //Loop through the report_typs_ids associated with question
    $sqlk = "SELECT report_type_id FROM question_report_type_ids WHERE question_id = '".$question_attributes_array["question_id"]."'";
    $rpt_type_ids_arrray = run_query_return_array ( $sqlk, "Pulling report_type_ids associations with questions" );

    foreach ( $rpt_type_ids_arrray AS $cur_prt_type_id )    {

        $dump_result .= "\n    run_query(\"INSERT INTO question_report_type_ids ( question_id, report_type_id ) VALUES ( '".$question_attributes_array['question_id']."', '".$cur_prt_type_id['report_type_id']."' )\", \"Error inserting report_type_ids for question #".$question_attributes_array["question_id"]."\");\n";
    }
    $question_has_been_inserted[$question_attributes_array["question_id"]] = 'yes';

}
else {
    $dump_result .= "\n    //".$question_attributes_array['question_title']." -- .Already inserted above";
}
unset($values_list);



$dump_result .= "\n";
$dump_result .= build_question_element_creation_sql ($question_attributes_array["question_id"], $question_attributes_array["question_type"]);
}


}



echo $dump_result;

?>
Return current item: Homeless Mangement Information System