Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/edit_questions_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 Edit_questions
{
	var $form_answer_edit;
	
	function edit_questions_table ($questions, $question_elements, $form_answer = "")
    {
		GLOBAL $message, $message_type;
		
		$dynamic_table .= "<table width=\"650\">";
		if(is_array($questions))
        {
			$td_count = 0;
			
			foreach($questions as $key => $current_question)
            {
				//Make rows two questions long using <tr> and </tr>
				if ($td_count == 2)	{$dynamic_table .= "</tr>"; $td_count = 0; }
				if ($td_count == 0) {$dynamic_table .= "<tr>"; }
				$td_count++;
				
				//Send the remaining questions to function to generate HTML, with the include td turned on
				
                $dynamic_table .= $this->display_question_specific_edit ($current_question);
			}
	    }
		else
        {
			$dynamic_table .= "<tr><td>There are no custom questions defined for this report.  You can add custom questions to this report by filling in the \"Add New Custom Questions\" form below.</td></tr>";
		}
        
	    //If there are an odd numer of questions, finish the table row and close it
	    if($td_count == "1"){$dynamic_table .= "<td>&nbsp;</td></tr>";}
	    
	    $dynamic_table .= "</table>";
        
	    return $dynamic_table;
	}



    function display_question_specific_edit ($current_question)
    {
        GLOBAL $system_directory;
        
        $final_html .= "<td class=\"wrap\">Title: ".$current_question["question_title"];
        $final_html .= "<br/>Type: ".$current_question["question_type"]."<br/>";
        $final_html .= "Display Order: ".$current_question["question_display_order"]."<br/>";
        $final_html .= "<a href=\"".$system_directory."edit_specific_question.php?page_id=".$current_question["report_type_id"]."&question_edit_id=".$current_question["question_id"]."\" target=\"_blank\">Click Here to Edit</a></td>";    
    
        return $final_html;
    }



    function form_answer_validation ($element_title, $question_id, $form_answer_edit, $ereg, $field_size, $required,
                                     $range_low = "", $range_high = "")
    {
	    GLOBAL $question_validation_error;
        
	    if(ereg("[^".$ereg."]", $form_answer_edit))
        {
		    question_error_marking ($question_id, "Only Letters, Numbers, and Spaces Allowed in ".$element_title.". ");
		    $form_answer_edit = trim($form_answer_edit);
		    $form_answer_edit = htmlspecialchars($form_answer_edit);
        }
        
	    if(strlen ($form_answer_edit) > $field_size)
        {
		    question_error_marking ($question_id, "Overflow, Illegal Entry in ".$element_title.". ");
		    $form_answer_edit = "";
        }
        
	    if($required == "yes" && !$form_answer_edit)
        {
		    question_error_marking ($question_id, "Entry required for ".$element_title.". ");
        }
        
	    if ( $range_low )
        {
		    if ( $form_answer_edit < $range_low || $form_answer_edit > $range_high )
            {
			    question_error_marking ($question_id, $element_title." must be between ".$range_low." and ".$range_high.". ");
			    $form_answer_edit = $range_high;
		    }
	    }
            $this->form_answer_edit[$question_id] = $form_answer_edit;
		    return $form_answer_edit;
    }



    function display_new_question_entry ()
    {
	    $final_html .= "\n<table><tr><td id=\"QUEST_new\">";
	    $final_html .= "<span class=\"headlinesub\">New Question Title:</span>";
	    $final_html .= "<input type=\"text\" "; 
	    $final_html .= "name=\"new_question_answer[question_title] ";	
	    $final_html .= "size=\"55\" ";
	    $final_html .= "maxlength=\"1000\" ";
	    $final_html .= "value=\"".$this->form_answer_edit["new"]."\"><br/>";
        
	    $final_html .= "Text Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"text\" checked >&nbsp;&nbsp; ";
	    $final_html .= "Number Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"number\">&nbsp;&nbsp; ";
	    $final_html .= "Checkboxes Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"checkboxes\">&nbsp;&nbsp; ";
	    $final_html .= "Radio Buttons Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"radio\"><br/>";
	    $final_html .= "Dropdown Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"dropdown\">&nbsp;&nbsp;";
        $final_html .= "Table Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"table\">&nbsp;&nbsp;";
        $final_html .= "Textarea Question<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"textarea\">";
        $final_html .= "Date<input type=\"radio\" name=\"new_question_answer[question_type]\" value=\"date\">";
	    $final_html .= "</td></tr></table>\n";
        
	    return $final_html;
    }



    function process_new_question_entry ($new_question_answer)
    {
        //echo "<br>process_new_question_entry ($new_question_answer)";
	    GLOBAL $question_validation_error;
	    GLOBAL $edit_page_id;
	    GLOBAL $group_id;
	    GLOBAL $org_id;
	    GLOBAL $access_level, $unique_seq;
        
	    $question_title = $this->form_answer_validation ("\"Question Title\"", "new", $new_question_answer["question_title"],
         "A-Za-z0-9 \:\.\-\?\)\(", "500", "yes");
        
    	if (     $new_question_answer["question_type"] != "text"
               && $new_question_answer["question_type"] != "number"
                && $new_question_answer["question_type"] != "checkboxes"
                 && $new_question_answer["question_type"] != "radio"
                  && $new_question_answer["question_type"] != "dropdown"
                   && $new_question_answer["question_type"] != "table"
                    && $new_question_answer["question_type"] != "textarea"
                     && $new_question_answer["question_type"] != "date" )
        {
		    $question_type = "text"; question_error_marking ("new", "Illegal Question Type. ");
		}
		else
        {
            $question_type = $new_question_answer["question_type"]; 
        }
        
	    if (!$question_validation_error["new"])
        {
		    //Assign "insert_group_id" only if appropriate to the access level (group or org)
		    if ($access_level == "10" || $access_level == "20")	{$insert_group_id = $group_id;}
            
            transaction_begin ( 'Begin new question insertion' );
            
		    $sql = "INSERT INTO questions
                    (question_id, question_title, question_type, question_owner_group_id, question_owner_org_id, question_required,
                      question_visible)
                    VALUES ('0', '".$question_title."', '".$question_type."', '".$insert_group_id."',
                             '".$org_id."', 'yes', 'yes')";
		    //echo $sql;
            run_query ($sql, "New Question Insert A");
            
		    //$id_sql = "SELECT question_row_id FROM questions WHERE question_title
            // LIKE '".$question_title."' ORDER BY question_row_id DESC";
		    //$result = run_query ($id_sql, "New Question InsertB");
		    //$quest_row_id = fetch_array($result, "Find question_row_id", "0");
		    //$z_question_row_id = $unique_seq."question_row_id";
            //$question_row_id = $quest_row_id[$z_question_row_id];
            
		    $id_sqlb = "SELECT question_id FROM questions WHERE question_id IS NOT NULL ORDER BY question_id DESC";
		    $resultb = run_query ($id_sqlb, "New Question InsertC");
		    $quest_id = fetch_array ($resultb, "Find question_id", 0);
            $question_id = $quest_id[0] + 1;  
		    $update_sql = "UPDATE questions SET question_id='".$question_id."'
                           WHERE question_id = '0'";
		    run_query ($update_sql, "New Question Insert C");
            
            if ( $new_question_answer["question_type"] == "table" )
            {
                $sqlh = "INSERT INTO table_profiles (question_id, table_type) VALUES ( '".$question_id."', 'numbers')";
                run_query ($sqlh, "Setting table profile for new table question");
            }
            
            $sql = "INSERT INTO question_report_type_ids
                   (question_id, report_type_id)
                   VALUES ('".$question_id."', '".$edit_page_id."')";
		    run_query ($sql, "New Question Insert B");
            transaction_commit ();
	    }
    }
}



class Edit_report_type_profile
{
    var $rpt_attributes;
    var $question_def;
    var $question_def_elements;
    
    function Edit_report_type_profile ($report_title)
    {
        $this->load_questions_defining_report ();
        
        if ($report_title)
        {
            $this->rpt_attributes =  pull_report_type_profile ($report_title);
        }
    
    }



    function process_submitted_report_profile ($form_answers)
    {
        GLOBAL $question_validation_error, $message, $message_type;
        
        $val = new Questions_answers_validation($this->question_def, $this->question_def_elements, $form_answers);
        
        secondary_form_answer_validation ("Report ID", "report_type_id", $val->vetted_form_answer["report_type_id"], "", $ereg="a-z\_");
        secondary_form_answer_validation ("Access levels", "report_edit_access_levels",
         $val->vetted_form_answer["report_edit_access_levels"], "", $ereg="0-9\:");
        
        if ( !$question_validation_error )  {
            transaction_begin ( "Create new report type" );
            $sql = "UPDATE report_type_profile SET
            report_title='".$val->vetted_value["report_title"]."',
             report_description='".$val->vetted_value["report_description"]."',
              report_instructions='".$val->vetted_value["report_instructions"]."',
               report_on_unit='".$val->vetted_value["report_on_unit"]."',
                report_on_timeperiod='".$val->vetted_value["report_on_timeperiod"]."',
                 report_that_follows='".$val->vetted_value["report_that_follows"]."',
                  report_edit_access_levels='".$val->vetted_value["report_edit_access_levels"]."'
                   WHERE report_type_id LIKE '".$this->rpt_attributes["report_type_id"]."'";
            
            run_query ( $sql, "Updating report type profile values" );
            
            $message = "Report attributes successfully edited. ";
            $message_type = "ok"; 
            transaction_commit ();
            //Reload the now edited questions
            $this->rpt_attributes =  pull_report_type_profile ($this->rpt_attributes["report_type_id"]);
        }
        //Since there were errors, return the form answers
        else {
            $this->rpt_attributes = $val->form_answer;
        }
    }
    
    
    
    function display_report_type_profile ()
    {
        GLOBAL $this_page;
        
        $ht .= form_start ($this_page."?edit_page_id=".$this->rpt_attributes["report_type_id"]);
        $ht .= "<table>"; 
        $ht .= "<tr><td class=\"generictabletop\">";
        $ht .= "Attributes of Form";
        $ht .= "</td></tr>";
        
        foreach ( $this->question_def AS $cur_quest )
        {
            $ht .= "<tr>";
            $ht .= question_display ( $cur_quest, $this->question_def_elements[$cur_quest["question_id"]], $this->rpt_attributes );
            $ht .= "</tr>";
        }
        
        $ht .= "</table>";
        $ht .= display_hidden_form_values ( 'edit_report_profile', 'yes' );
        $ht .= submit_button ("profile_form", "Submit report profile changes");
        $ht .=  "</form>\n";
        
        return $ht;
    }



    function load_questions_defining_report ()
    {
        $this->question_def["report_type_id"] = array ("question_id" => "report_type_id",
         "question_title" => "Report ID: ", "question_type" => "text", "question_field_size" => "30",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "30");

        $this->question_def["report_title"] = array ("question_id" => "report_title",
         "question_title" => "Form title: ", "question_type" => "text", "question_field_size" => "255",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "75");
        
        $this->question_def["report_description"] = array ("question_id" => "report_description",
         "question_title" => "Form description: ", "question_type" => "text", "question_field_size" => "255",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "75");
        
        $this->question_def["report_instructions"] = array ("question_id" => "report_instructions",
         "question_title" => "Report instructions: ", "question_type" => "textarea", "question_field_size" => "13",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "no", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "75");
          
        $this->question_def["report_on_unit"] = array ("question_id" => "report_on_unit", "question_title" => "Report on unit:",
         "question_type" => "radio", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "0",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "",
            "question_visible" => "yes", "question_ecma" => "");
            
            $this->question_def_elements["report_on_unit"]["client_identifier"] = array ("question_element_id" => "client_identifier",
             "question_id" => "report_on_unit", "question_element_title" => "client_identifier", "question_element_value" => "client_identifier");
            
            $this->question_def_elements["report_on_unit"]["client"] = array ("question_element_id" => "client",
             "question_id" => "report_on_unit", "question_element_title" => "client", "question_element_value" => "client");
            
            $this->question_def_elements["report_on_unit"]["household"] = array ("question_element_id" => "household",
             "question_id" => "report_on_unit", "question_element_title" => "household", "question_element_value" => "household");
            
            $this->question_def_elements["report_on_unit"]["referral"] = array ("question_element_id" => "referral",
             "question_id" => "report_on_unit", "question_element_title" => "referral", "question_element_value" => "referral");
            
            $this->question_def_elements["report_on_unit"]["vacancy"] = array ("question_element_id" => "vacancy",
             "question_id" => "report_on_unit", "question_element_title" => "vacancy", "question_element_value" => "vacancy");
            
            $this->question_def_elements["report_on_unit"]["survey"] = array ("question_element_id" => "survey",
             "question_id" => "report_on_unit", "question_element_title" => "survey", "question_element_value" => "survey");
            
            $this->question_def_elements["report_on_unit"]["client_service"] = array ("question_element_id" => "client_service", "question_id" => "report_on_unit", "question_element_title" => "client_service", "question_element_value" => "client_service");
            
            $this->question_def_elements["report_on_unit"]["household_service"] = array ("question_element_id" => "household_service", "question_id" => "report_on_unit", "question_element_title" => "household_service", "question_element_value" => "household_service");
            
            $this->question_def["report_on_timeperiod"] = array ("question_id" => "report_on_timeperiod", "question_title" => "Report on timeperiod:",
             "question_type" => "radio", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "0",
              "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "no",
                "question_visible" => "yes", "question_ecma" => "");
            
            $this->question_def_elements["report_on_timeperiod"]["yes"] = array ("question_element_id" => "yes",
             "question_id" => "report_on_timeperiod", "question_element_title" => "Start and end dates", "question_element_value" => "yes");
             
            $this->question_def_elements["report_on_timeperiod"]["yes_begin_only"] = array ("question_element_id" => "yes_begin_only",
             "question_id" => "report_on_timeperiod", "question_element_title" => "Start date only", "question_element_value" =>
             "yes_begin_only"); 
             
            $this->question_def_elements["report_on_timeperiod"]["no"] = array ("question_element_id" => "no",
             "question_id" => "report_on_timeperiod", "question_element_title" => "No", "question_element_value" => "no");
             
            $this->question_def["report_that_follows"] = array ("question_id" => "report_that_follows", "question_title" => "Report that follows:",
             "question_type" => "dropdown", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "0",
              "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "",
                "question_visible" => "yes", "question_ecma" => "");
            
            $sql = "SELECT report_title, report_type_id FROM report_type_profile ORDER BY report_title";
            $result = run_query_return_array ( $sql, "Pulling list of reports" );
            
            $this->question_def_elements["report_that_follows"]["BLANK"] = array ("question_element_id" =>
                 "BLANK", "question_id" => "report_that_follows", "question_element_title" =>
                  "[SELECT ONE]", "question_element_value" => "BLANK");
            
            $this->question_def_elements["report_that_follows"]["none"] = array ("question_element_id" =>
                 "none", "question_id" => "report_that_follows", "question_element_title" =>
                  "Loopback to same report; allow editing", "question_element_value" => "none");
            
            
            foreach ( $result AS $cur_result )
            {
                $this->question_def_elements["report_that_follows"][$cur_result["report_type_id"]] = array ("question_element_id" =>
                 $cur_result["report_type_id"], "question_id" => "report_that_follows", "question_element_title" =>
                  $cur_result["report_title"], "question_element_value" => $cur_result["report_type_id"]);
            }
            
            $this->question_def["report_edit_access_levels"] = array ("question_id" => "report_edit_access_levels",
             "question_title" => "Access levels (e.g., \":10:20:30:\"):", "question_type" => "text", "question_field_size" => "10",
              "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => $question_attributes["y_axis_title"], "question_visible" => "yes",
                "question_ecma" => "", "question_display_size" => "10");
    }
}



function insert_ecma ($page_id, $form_ecma)
{
	GLOBAL $access_level;
	if($access_level != "10"){echo "Not Allowed to Edit ECMA"; exit;}
	if (!$page_id){echo "No page_id identified for ecma"; exit;}
	
	if(get_magic_quotes_runtime()){$form_ecma = addslashes ($form_ecma);}
	$sql = "SELECT * FROM validation WHERE validation_page_id LIKE '".$page_id."'";
	$result = run_query ($sql, "Pull ECMA");
	if(num_rows ($result) > 0)
    {
		$sqlq="UPDATE validation SET validation_ecma='".$form_ecma."' WHERE validation_page_id LIKE '".$page_id."'";
	}
	else
    {
		$sqlq="INSERT INTO validation (validation_ecma, validation_page_id) VALUES ('".$form_ecma."', '".$page_id."')";
	}
	
	run_query ($sqlq, "Insert ecma");
}



function ecma_input_textbox ($form_ecma)
{
	$final_html .=  "<textarea "; 
	$final_html .=  "name=\"form_ecma\"";
	$final_html .=  "rows=\"20\"";
	$final_html .=  "cols=\"75\">";
	$final_html .=  $form_ecma."</textarea><br>";
	
    return $final_html;
}



class Optional_questions
{

	var $pulled_form_answer_optional_questions;
	//var $questions;
    
	function process_edited_optional_questions ($questions, $form_answer_optional_question)
    {
		GLOBAL $access_level, $group_id, $org_id;
		
		switch($access_level)
        {
        
			case "10":
				$access_type = "group_id";
				$access_id = $group_id;
				break;
        
			case "20":
				$access_type = "group_id";
				$access_id = $group_id;
				break;
        
			case "30":
				$access_type = "org_id";
				$access_id = $org_id;
				break;
        
			default:
			echo "No access level defined in process_edited_optional_questions";
        
		}
        
		if (is_array($questions))
        {
            transaction_begin ( "Changing optional question status" );
		    
			if($access_level == "10")
            {
				foreach($questions AS $current_question)
                {
					$question_id = $current_question["question_id"];
					if($form_answer_optional_question[$question_id] == "yes")
                    {
						if(!$this->pulled_form_answer_optional_questions[$question_id])
                        {
							$sql = "UPDATE questions SET question_visible='yes' WHERE question_id = '".$question_id."'";
							run_query($sql, "Changing optional question from no to yes");
						}
					}
                    
					if($form_answer_optional_question[$question_id] == "no")
                    {
						if($this->pulled_form_answer_optional_questions[$question_id])
                        {
							$sqlx = "UPDATE questions SET question_visible='no' WHERE question_id = '".$question_id."'";
						    
							run_query($sqlx, "Changing optional question from yes to no");
							//echo "Change to No sql is: ".$sqlx."<br>";
						}
					}
				}
		}
		else
        {
			foreach($questions AS $current_question)
            {
				$question_id = $current_question["question_id"];
				if($form_answer_optional_question[$question_id] == "yes")
                {
					if(!$this->pulled_form_answer_optional_questions[$question_id])
                    {
						$sql = "INSERT INTO question_optional (question_id, ".$access_type.") VALUES ('".$question_id."', '".$access_id."')";
						//echo $sql;
						run_query($sql, "Changing optional question from no to yes");
					}
				}
                
				if($form_answer_optional_question[$question_id] == "no")
                {
					if($this->pulled_form_answer_optional_questions[$question_id])
                    {
						$sql = "DELETE FROM question_optional WHERE question_id = '".$question_id."' AND ".$access_type." = '".$access_id."'";
						//echo $sql;
						run_query($sql, "Changing optional question from yes to no");
					}
				}
			}
		}
        transaction_commit ();
	}
}//end class


	function pull_optional_questions_status ()	{
		GLOBAL $access_level, $group_id, $org_id;
		$this->pulled_form_answer_optional_questions = "";
        
		if($access_level == "10")
        {
			$sql = "SELECT * FROM questions WHERE question_owner_group_id = '1000000' AND question_visible = 'yes'";
		}
		
		if($access_level == "20")
        {
			$sql = "SELECT * FROM question_optional WHERE group_id = '".$group_id."'";
		}

		if($access_level == "30")
        {
			$sql = "SELECT * FROM question_optional WHERE org_id = '".$org_id."'";
		}
		$result = run_query($sql, "Pull answers to optional question selection");
        
		$result_count = num_rows($result);
		
		for($i = 0; $i < $result_count; $i++)
        {
			$optional_question_status = fetch_array($result, "Status of optional question", $i);
			//if($access_level == "10")	{
				$question_id = $optional_question_status["question_id"];
			//}
			//else	{
			//	$question_id = $optional_question_status["question_id"];
			//}
			
			$this->pulled_form_answer_optional_questions[$question_id] = "yes";
		}
    }



	function display_optional_questions_table ($questions, $question_elements)
    {
	    GLOBAL $access_level;
		$td_count = 0;
        
		$final_html .= "<table>";
        
		if(is_array($questions))
        {
			foreach ($questions as $current_question)
            {
				if ($td_count == 2)	{$final_html .= "</tr>"; $td_count = 0; }
				if ($td_count == 0) {$final_html .= "<tr>"; }
				$td_count++;
                
				$final_html .= "\n<td class=\"wrap\">";
				$final_html .= "<p class=\"headlinesub\">Optional Question #".$current_question["question_id"]."</p>";
                
				//Route the question to an HTML creation function based on what type it is, i.e., "text"
				switch ($current_question["question_type"])
                {
					case "text":
						$final_html .= display_text_question ($current_question, $form_answer_optional_questions);
						break;

					case "password":
						$final_html .= display_text_question ($current_question, $form_answer_optional_questions, '',TRUE);
						break;

					case "number":
						$final_html .= display_number_question ($current_question, $form_answer_optional_questions);
						break;
						
					case "radio":
						$final_html .= display_radio_buttons_question ($current_question, $question_elements[$current_question["question_id"]], $form_answer_optional_questions);
						break;

					case "checkboxes":
						$final_html .= display_checkboxes_question ($current_question, $question_elements[$current_question["question_id"]], $form_answer_optional_questions);
						break;

					case "dropdown":
						$final_html .= display_dropdown_list_question ($current_question, $question_elements[$current_question["question_id"]], $form_answer_optional_questions);
						break;

					case "textarea":
						$final_html .= display_textarea_question ($current_question, $form_answer_optional_questions);
						break;
                        
                    case "table":
						$final_html .= display_table_question ($current_question, $form_answer_optional_questions);
						break;

					default:
						echo "Question #".$tags_num[0][0]."Not Found In Question Database, or question has not type defined<br>";
				}
				
				//Add the html radio buttons to choose questions that are included 
				$final_html .= "\n<p class=\"genericsubtop\">Include This Question:<br/>\nYes<input type=\"radio\" name=\"form_answer_optional_question[".$current_question["question_id"]."]\" value=\"yes\"";
                
				if($this->pulled_form_answer_optional_questions[$current_question["question_id"]] == "yes")
                {
					$final_html .= "checked=\"checked\" ";
					$yes_check = "yes";
				}
				else {$yes_check = "no";}
                
				$final_html .= ">&nbsp;&nbsp;";
                
				$final_html .= "\nNo<input type=\"radio\" name=\"form_answer_optional_question[".$current_question["question_id"]."]\" value=\"no\" ";
				
				if($yes_check == "no")
                {
					$final_html .= "checked=\"checked\" ";
				}
                
				$final_html .= ">";
                
                if ($access_level == "10" )
                {
                    $final_html .=  "<br/><a href=\"".$system_directory."edit_specific_question.php?page_id=".$current_question["report_type_id"]."&question_edit_id=".$current_question["question_id"]."\" target=\"_blank\">Click Here to Edit</a>";
                }
                $final_html .= "</p></td>";
			}
		}
		//If there are an odd numer of questions, finish the table row and close it
		if($td_count == "1"){$final_html .= "<td>&nbsp;</td></tr>";}
		
		$final_html .= "</table>";

		return $final_html;

	}

}




class Add_report_profile
{
    var $quest_def;
    var $vetted_form_answers;
    
    
    function Add_report_profile ()
    {
        $this->load_questions();
    }



    function load_questions ()
    {
        $this->quest_def["report_title"] = array ("question_id" => "report_title",
         "question_title" => "Form title: ", "question_type" => "text", "question_field_size" => "255",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "75");
            
        $this->quest_def["report_type_id"] = array ("question_id" => "report_type_id",
         "question_title" => "Form ID: ", "question_type" => "text", "question_field_size" => "30",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "30");
    
    }



    function display_add_report_type_profile_form ()
    {
        //GLOBAL $this_page;    
        
        $ht .= form_start ();
        $ht .= "<table>"; 
        $ht .= "<tr><td class=\"generictabletop\">";
        $ht .= "Add new report by entering a Form Title and Form ID below";
        $ht .= "</td></tr>";
        
        foreach ( $this->quest_def AS $cur_quest )
        {
            $ht .= "<tr>";
            $ht .= question_display ( $cur_quest, "", $this->vetted_form_answers );
            $ht .= "</tr>";
        }
        
        $ht .= "</table>";
        $ht .= submit_button ( "new_profile_form", "Submit new form profile" );
        $ht .=  "</form>\n";
        
        return $ht;   
    }



    function process_new_report_profile($form_answers)
    {
        GLOBAL $question_validation_error;
        
        $val = new Questions_answers_validation($this->quest_def, "", $form_answers);
        $this->vetted_form_answers = $val->vetted_form_answer; 
        
        secondary_form_answer_validation ("New form ID. Only lowercase letters and the \"_\" character allowed",
         "report_type_id", $val->vetted_form_answer["report_type_id"], "", $ereg="a-z\_");
                
        if ( !$question_validation_error )
        {
            //Check for duplicates
            $sqla = "SELECT * FROM report_type_profile WHERE report_type_id LIKE '".$val->vetted_value["report_type_id"]."'";
            $resulta = run_query ($sqla, "Finding matching report ids" );
            if ( num_rows( $resulta) > 0 )  {
                question_error_marking ("report_type_id", "Form ID already exists", "Duplicate Form ID, see below. ");    
            }
            
            $sqlb = "SELECT * FROM report_type_profile WHERE report_title LIKE '".$val->vetted_value["report_title"]."'";
            $resultb = run_query ($sqlb, "Finding matching report ids" );
            if ( num_rows( $resultb) > 0 )
            {
                question_error_marking ("report_title", "Form Title already exists", "Duplicate Form Title, see below. ");    
            }
            
            //If no errors from the secondary error checks above for duplicates, insert new report 
            if ( !$question_validation_error )
            {
                $sql = "INSERT INTO report_type_profile
                       ( report_type_id, report_title, report_edit_access_levels  )
                       VALUES ( '".$val->vetted_value["report_type_id"]."', '".$val->vetted_value["report_title"]."', ':10:' )";
                
                //echo $sql;
                run_query ( $sql, "Updating form type profile values" );
            }
        }
    
    
    }



}



?>
Return current item: Homeless Mangement Information System