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

class Edit_question
{
    //The elements making up a question are put into $question_def
    var $question_def;
    var $question_def_elements;
    var $question_table_axis_def;
    var $question_table_axis_def_elements;
    var $new_element;
    var $new_element_element;
    var $vetted_form_answers;
    
    var $question_attribute;
    var $questions_defining_table;
    var $final_html;
    
    
    
    function Edit_question ($question_attributes, $question_elements_attributes, $form_answers)
    {
        //echo "<br>Edit_question ($question_attributes, $question_elements_attributes, $form_answers)";
        GLOBAL $form_submitted;
        $this->question_attribute = $question_attributes;
        $this->load_question_defining_questions ($question_attributes);
        $new_element["new"]["question_element_title"] = "[NEW ELEMENT]";
        $new_element["new"]["question_element_id"] = "new";
        
        //If no form has been submitted, use the question type in the database....
        if(!$form_submitted)
        {
            $load_this_quest_type = $question_attributes["question_type"];
            $this->vetted_form_answers = $question_attributes;
        }
        else
        {
            $load_this_quest_type = $form_answers["question_type"];
            //echo "Coming from a submitted form<p/>";
        }
        //display_value($form_answers);
        //echo "The load question type is: ".$load_this_quest_type."<p/>";
        switch ($load_this_quest_type)
        {
            case "text":
                $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], 
                  $this->question_def["question_title"],
                   $this->question_def["question_required"], 
                    $this->question_def["question_visible"],
                     $this->question_def["question_field_size"],
                      $this->question_def["question_display_size"], 
                       $this->question_def["question_default_value"],
                        $this->question_def['question_encrypted'],
                         $this->question_def['question_reason_blank_dont_know'],
                          $this->question_def['question_reason_blank_not_applicable'],
                           $this->question_def['question_reason_blank_refused'] );
                
                break;
            
            case "password":
                $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"],
                  $this->question_def["question_title"],
                   $this->question_def["question_required"],
                    $this->question_def["question_visible"],
                     $this->question_def["question_field_size"],
                      $this->question_def["question_display_size"],
                       $this->question_def["question_default_value"],
                        $this->question_def['question_encrypted'],
                         $this->question_def['question_reason_blank_dont_know'],
                          $this->question_def['question_reason_blank_not_applicable'],
                           $this->question_def['question_reason_blank_refused'] );
                
                break;
            
            case "textarea":
                $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], 
                  $this->question_def["question_title"],
                   $this->question_def["question_required"], 
                    $this->question_def["question_visible"],
                     $this->question_def["question_field_size"],
                      $this->question_def["question_display_size"],
                       $this->question_def["question_special_attribute"], 
                        $this->question_def["question_default_value"],
                         $this->question_def['question_encrypted'],
                          $this->question_def['question_reason_blank_dont_know'],
                           $this->question_def['question_reason_blank_not_applicable'],
                            $this->question_def['question_reason_blank_refused'] );
                
                break;
            
            
            case "number":
               
               $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], 
                  $this->question_def["question_title"],
                   $this->question_def["question_required"],
                    $this->question_def["question_visible"],
                     $this->question_def["question_ecma"], 
                      $this->question_def["question_range_bottom"],
                       $this->question_def["question_range_top"], 
                        $this->question_def["question_field_size"],
                         $this->question_def["question_display_size"], 
                          $this->question_def["question_default_value"],
                           $this->question_def['question_encrypted'],
                            $this->question_def['question_reason_blank_dont_know'],
                             $this->question_def['question_reason_blank_not_applicable'],
                              $this->question_def['question_reason_blank_refused'] );
                
                break;
            
            
            case "checkboxes":
               
               $this->questions_defining_table = array ($this->question_def["question_type"],
                $this->question_def["question_display_order"], $this->question_def["question_title"],
                 $this->question_def["question_required"],
                  $this->question_def["question_visible"], $this->question_def["question_ecma"],
                   $this->question_def['question_encrypted'],
                    $this->question_def['question_reason_blank_dont_know'],
                     $this->question_def['question_reason_blank_not_applicable'],
                      $this->question_def['question_reason_blank_refused'] );
               
               $pull_element_questions = $this->load_elements_defining_questions ($question_elements_attributes);
               $this->question_table_axis_def = $pull_element_questions[0];
               $this->question_table_axis_def_elements = $pull_element_questions[1];
               
               $pull_new_element_questions = $this->load_elements_defining_questions ($new_element);
               $this->new_element = $pull_new_element_questions[0];
               $this->new_element_element = $pull_new_element_questions[1];
               
               break;
            
            
            case "radio":
               
               $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], $this->question_def["question_title"],
                  $this->question_def["question_required"],
                   $this->question_def["question_visible"], $this->question_def["question_ecma"],
                    $this->question_def["question_default_value"],
                     $this->question_def['question_encrypted'],
                      $this->question_def['question_reason_blank_dont_know'],
                       $this->question_def['question_reason_blank_not_applicable'],
                        $this->question_def['question_reason_blank_refused'] );
               
               $pull_element_questions = $this->load_elements_defining_questions ($question_elements_attributes);
               $this->question_table_axis_def = $pull_element_questions[0];
               $this->question_table_axis_def_elements = $pull_element_questions[1];
               
               $pull_new_element_questions = $this->load_elements_defining_questions ($new_element);
               $this->new_element = $pull_new_element_questions[0];
               $this->new_element_element = $pull_new_element_questions[1];
               
               break;
            
            
            case "dropdown":
               
               $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], $this->question_def["question_title"],
                  $this->question_def["question_required"],
                   $this->question_def["question_visible"], $this->question_def["question_ecma"],
                    $this->question_def["question_default_value"],
                     $this->question_def['question_encrypted'],
                      $this->question_def['question_reason_blank_dont_know'],
                       $this->question_def['question_reason_blank_not_applicable'],
                        $this->question_def['question_reason_blank_refused'] );
               
               $pull_element_questions = $this->load_elements_defining_questions ($question_elements_attributes);
               $this->question_table_axis_def = $pull_element_questions[0];
               $this->question_table_axis_def_elements = $pull_element_questions[1];
               
               $pull_new_element_questions = $this->load_elements_defining_questions ($new_element);
               $this->new_element = $pull_new_element_questions[0];
               $this->new_element_element = $pull_new_element_questions[1];
               
               break;
            
            
            case "table":
                
                $this->questions_defining_table = array ($this->question_def["question_type"], $this->question_def["table_type"],
                 $this->question_def["question_display_order"], $this->question_def["question_title"],
                  $this->question_def["question_required"],
                   $this->question_def["question_visible"], $this->question_def["question_ecma"], $this->question_def["x_axis_title"],
                    $this->question_def["y_axis_title"], $this->question_def["question_field_size"],
                     $this->question_def["question_display_size"],
                      $this->question_def['question_reason_blank_dont_know'],
                       $this->question_def['question_reason_blank_not_applicable'],
                        $this->question_def['question_reason_blank_refused']
                     );
               
               $pull_element_questions = $this->load_question_table_defining_elements ($question_elements_attributes);
               $this->question_table_axis_def = $pull_element_questions[0];
               $this->question_table_axis_def_elements = $pull_element_questions[1]; 
               
               $new_table_element["x"]["new"]["axis_label"] = "[NEW AXIS]";
               $new_table_element["x"]["new"]["axis_id"] = "new";
                
               $pull_new_element_questions = $this->load_question_table_defining_elements ($new_table_element);
               $this->new_element = $pull_new_element_questions[0];
               $this->new_element_element = $pull_new_element_questions[1]; 
               
               break;
               
            case "date":
               
               $this->questions_defining_table = array ($this->question_def["question_type"],
                 $this->question_def["question_display_order"], $this->question_def["question_title"],
                  $this->question_def["question_required"],
                    $this->question_def["question_visible"], $this->question_def["question_ecma"],
                      $this->question_def["question_special_attribute"],
                        $this->question_def["question_default_value"],
                          $this->question_def['question_encrypted'],
                            $this->question_def['question_reason_blank_dont_know'],
                              $this->question_def['question_reason_blank_not_applicable'],
                                $this->question_def['question_reason_blank_refused']
                     );
               
               break;
            
            default:
                echo "Unknown question type, cannot edit"; exit;
        }
        
        $this->add_administrator_only_questions();
        
        //echo 'calling edit_question_form...';
        $this->edit_question_form ($question_attributes, $question_elements_attributes, $form_answers);
        //Output from the resulting HTML
        //return $this->final_html; WILL NOT WORK, RETURNS "OBJECT"
    }
   
   
    
    function add_administrator_only_questions ()
    {
        GLOBAL $access_level;
        if ($access_level == "10")
        {
            array_push($this->questions_defining_table, $this->question_def["question_owner_org_id"],
             $this->question_def["question_ecma"]);
        }
    }
    
    
    
    function edit_question_form ($question_attributes, $question_elements_attributes, $form_answers)
    {
        GLOBAL $form_submitted, $this_page, $question_validation_error;
        
        //echo "<br>edit_question_form ($question_attributes, $question_elements_attributes, $form_answers)";
        
        //Put the existing answers in if form is freshly loaded...
        if($form_submitted)
        {
            //Validate new element if it has been altered
            if ($form_answers["question_element_titlenew"] != "[NEW ELEMENT]"  && $form_answers["axis_labelnew"] != "[NEW AXIS]" )
            {
                $validation_new_element = new Questions_answers_validation($this->new_element,
                 $this->new_element_element, $form_answers);
                 //echo "Validating the new element<p>";
            }
            
            //If we are switching betweeen types of questions...
            //if ($form_answers["question_type"] != $question_attributes["question_type"])   {
            
                //..switch the question type
             //   $this->vetted_form_answers["question_type"] = $form_answers["question_type"];
            //}
            
            $validation_question = new Questions_answers_validation( $this->questions_defining_table,
                                                                     $this->question_def_elements, 
                                                                     $form_answers);
            
            $validation_table_elements = new Questions_answers_validation( $this->question_table_axis_def,
                                                                           $this->question_table_axis_def_elements, 
                                                                           $form_answers );
            
            //Merge the form elements for display
            $this->vetted_form_answers = array_merge ($validation_question->vetted_form_answer,
                                                      $validation_table_elements->vetted_form_answer, 
                                                      $validation_new_element->vetted_value );
            
            /*echo "validation:".$question_validation_error.'validated_answers:'.pretty_var_dump($this->vetted_form_answers);
            echo '<table border=1 bordercolor=blue><tr><td>'.pretty_var_dump($validation_question->vetted_value).
                 '</td><td>'.pretty_var_dump($validation_table_elements->vetted_value).'</td></tr></table>'; */
            
            if(!$question_validation_error)
            {
                //Merge the values to be inserted into database
                $vetted_values = array_merge ($validation_question->vetted_value, $validation_table_elements->vetted_value);
                
                transaction_begin ( 'Insert new/edited question' );
                
                $this->insert_edited_question_definition($question_attributes, $question_elements_attributes, $vetted_values);
                //$vetted_form_answers["question_element_titlenew"] = "[NEW ELEMENT]";
                
                //If there is a new element, insert it
                if($validation_new_element->vetted_value)
                {
                    //echo "There is a new element<p>";
                    if($this->question_attribute["question_type"] == "table")
                    {
                       $this->insert_new_axis ($validation_new_element->vetted_value);
                        $this->vetted_form_answers["axis_labelnew"] = "[NEW AXIS]";
                        
                        //echo "Inserting new tablwe axis<p>";
                    }
                    else
                    {
                        $this->insert_new_question_element ($question_attributes, $validation_new_element->vetted_value);
                        $this->vetted_form_answers["question_element_titlenew"] = "[NEW ELEMENT]";
                        $this->vetted_form_answers["question_element_valuenew"] = "";
                        $this->vetted_form_answers["question_element_checkednew"] = "";
                        
                        //echo "Inserting new quest element<p>";
                    }
                }
                
                transaction_commit ();
            }
       }
       
       //echo "vetted_form_answers['question_no_answer_options'] =: ".$this->vetted_form_answers['question_no_answer_options']."<p>";
       
       if ( $this->vetted_form_answers['question_no_answer_options'] & 1 )
       {
            $this->vetted_form_answers['question_reason_blank_dont_know'] = '1';
       }
       if ( $this->vetted_form_answers['question_no_answer_options'] & 2 )
       {
            $this->vetted_form_answers['question_reason_blank_not_applicable'] = '2';
       }
       if ( $this->vetted_form_answers['question_no_answer_options'] & 4 )
       {
            $this->vetted_form_answers['question_reason_blank_refused'] = '4';
       }
       //if ( $this->vetted_form_answers['question_no_answer_options'] & 8 ) {
       //    $this->vetted_form_answers['question_reason_blank_done_have'] = '8';
       //}
       
       $this->final_html .= form_start( $this_page."?page_id=".
                                         $question_attributes["report_type_id"]."&question_edit_id=".
                                          $question_attributes["question_id"] );
       
       $this->final_html .= "<table>";
       
       //Loop through the questions that define how the question being edited is displayed
       foreach ($this->questions_defining_table AS $question_id => $question_array)
       {
            //echo "question id is: ".$question_array["question_id"]."<br/>"; 
            $this->final_html .= "<tr>";
            $this->final_html .= question_display($question_array,
                                                   $this->question_def_elements[$question_array["question_id"]], 
                                                    $this->vetted_form_answers);
            $this->final_html .= "</tr>";
       }
       
       $this->final_html .= "</table>";
       
       
       $this->final_html .= "<table>";
       
       //If this question has elements, loop through and display them
       if ($this->question_table_axis_def)
       {
           //Loop through the table elements that define how the axis being edited is displayed
           foreach($this->question_table_axis_def AS $axis_question_id => $current_axis_question)
           {
               //Find the axis id for display in title bar
               preg_match_all( '/\d+/', $axis_question_id, $axis_num);
           	   $display_axis_num = $axis_num[0][0]; 
               
               //echo "axis_question_id: ".$axis_question_id."<br/>";
                
               if($current_axis_question["question_title"] == "Axis Label:")
               {
                   $this->final_html .= "<tr><td class=\"headlinesub\">Axis ID: ".$display_axis_num."</td></tr>";
               }
               
               if($current_axis_question["question_title"] == "Element Label:")
               {
                   $this->final_html .= "<tr><td class=\"headlinesub\">Element ID: ".$display_axis_num."</td></tr>";
               }
               
               //display text question
               $this->final_html .= "<tr>";
               
               //echo ' yo '.pretty_var_dump($this->vetted_form_answers);
               $this->final_html .= question_display( $current_axis_question,
                                                       $this->question_table_axis_def_elements[$axis_question_id], 
                                                        $this->vetted_form_answers,
                                                         true );
               $this->final_html .= "</tr>";
           }
          $this->final_html .= "<table>";
      }
      
      $this->final_html .= "<table>";
      
      //Loop through the new element questions
      if ($this->new_element)
      {
           //Loop through the table elements that define how the axis being edited is displayed
           foreach($this->new_element AS $axis_question_id => $current_axis_question)
           {
               //echo "axis_question_id: ".$axis_question_id."<br/>";
               
               if ( $current_axis_question["question_title"] == "Axis Label:"
                    || $current_axis_question["question_title"] == "Element Label:" )
                {
                   $this->final_html .= "<tr><td class=\"headlinesub\">New Element</td></tr>";
               }
               
               $this->final_html .= "<tr>";
               $this->final_html .= question_display( $current_axis_question,
                                                       $this->new_element_element[$axis_question_id], 
                                                        $this->vetted_form_answers );
               $this->final_html .= "</tr>";
           }
          $this->final_html .= "</table>";
      }
      
      
      $this->final_html .= form_end("Press to Submit Edited Question");
    }



    function insert_edited_question_definition ($question_attributes, $question_elements_attributes, $vetted_values)
    {
        GLOBAL $date_stamp, $unix_date, $message, $message_type, $org_id, $group_id;
        
        //echo "<br>insert_edited_question_definition ($question_attributes, $question_elements_attributes, $vetted_values)";
        
        //Pull out the old questions definition information, put it in the backup table, then insert the new answers
        
        //Pull out the existing question attributes
        $sql = "SELECT * FROM questions WHERE question_id = ".$question_attributes["question_id"];
        $old_question_definition_result = run_query($sql, "Pulling out old question definition");
        $old_question_definition_array =  fetch_array($old_question_definition_result, "Old question variables", 0);
        
        //Replace values for insertion
        foreach ( $question_attributes AS $key => $val )
        {
            if ( $val == "" )
            {
                $question_attributes_i[$key] = 'NULL';
            }
            else
            {
                $question_attributes_i[$key] = "'".$val."'";
            }
        }
        
        //Put existing question attributes into the backup table, so we can revert to them later if needed
        $sql = "
        INSERT INTO questions_previous_versions (
            question_id, 
            question_title, 
            question_type, 
            question_owner_group_id,
            question_owner_org_id, 
            question_field_size,  
            question_range_bottom, 
            question_range_top, 
            question_display_order,
            question_required, 
            question_default_value, 
            question_visible, 
            question_encrypted, 
            question_no_answer_options, 
            question_ecma,
            question_display_size, 
            question_special_attribute, 
            question_edit_date_stamp, 
            question_edit_unix_date
        ) 
        VALUES (
            ".$question_attributes_i["question_id"].", 
            ".$question_attributes_i["question_title"].", 
            ".$question_attributes_i["question_type"].",
            ".$question_attributes_i["question_owner_group_id"].",
            ".$question_attributes_i["question_owner_org_id"].",
            ".$question_attributes_i["question_field_size"].",
            ".$question_attributes_i["question_range_bottom"].",
            ".$question_attributes_i["question_range_top"].",
            ".$question_attributes_i["question_display_order"].",
            ".$question_attributes_i["question_required"].",
            ".$question_attributes_i["question_default_value"].",
            ".$question_attributes_i["question_visible"].",
            ".$question_attributes_i["question_encrypted"].",
            ".$question_attributes_i["question_no_answer_options"].",
            ".$question_attributes_i["question_ecma"].",
            ".$question_attributes_i["question_display_size"].",
            ".$question_attributes_i["question_special_attribute"].", 
            ".$date_stamp.",
            ".$unix_date."
        )";
               
            run_query($sql, "Inserting existing question definitions"); 
        
        //echo "We are updating ".$question_attributes["question_type"]."<p>";
        if ( $question_attributes['question_type'] == 'table' )   {
            //echo "We are updating table profile<p>";    
            $this->save_existing_table_profile( $question_attributes, $question_elements_attributes );
            //var_dump ( $question_elements_attributes );
            //echo "We save existing table elements<p>"; 
            $this->save_existing_table_elements( $question_attributes, $question_elements_attributes );
            //echo "We update edited table profile and elements<p>"; 
            $this->update_edited_table_profile_and_elements ( $question_attributes, $question_elements_attributes, $vetted_values );
        
        }
        elseif ( $question_attributes["question_type"] == "checkboxes"
                    || $question_attributes["question_type"] == "radio"
                    || $question_attributes["question_type"] == "dropdown")
        {
            $this->save_existing_element_values($question_attributes, $question_elements_attributes);
            $this->update_edited_question_elements($question_elements_attributes, $vetted_values);
            $this->insert_new_question_element($question_attributes, $vetted_values);
        }
        
        ////Insert the edited question_attributes
        //If the system administrtator selects the question owner that denotes an "Optional" question, change the owner
        if ( $vetted_values["question_owner_org_id"] == "1000000" )
        {
            $insert_org_id = "1000000";
            $insert_group_id = "1000000";
        }
        else
        {
            $insert_org_id = $org_id;
            $insert_group_id = $question_attributes["question_owner_group_id"];
        }
        
        //Determine the 'no answer options' code
        $vetted_values['question_no_answer_options'] = 
            0 + 
            $vetted_values['question_reason_blank_dont_know'] +
            $vetted_values['question_reason_blank_not_applicable'] +
            $vetted_values['question_reason_blank_refused'];
            
        
        /*
        if ( $vetted_values['question_reason_blank_dont_know'] == 'yes' )   {
            $vetted_values['question_no_answer_options'] = 1;
        }
        if ( $vetted_values['question_reason_blank_not_applicable'] == 'yes' )   {
            $vetted_values['question_no_answer_options'] = $vetted_values['question_no_answer_options'] + 2;
        }
        if ( $vetted_values['question_reason_blank_refused'] == 'yes' )   {
            $vetted_values['question_no_answer_options'] = $vetted_values['question_no_answer_options'] + 4;
        }
        */
        
        $q_keys = array ( "question_title", "question_type", "question_field_size", "question_range_bottom", "question_range_top",
         "question_display_order", "question_required", "question_default_value", "question_visible", "question_encrypted",
         "question_no_answer_options", "question_ecma",
             "question_display_size", "question_special_attribute" );
        
        foreach ( $q_keys AS $cur_key )
        {
            if ( strlen( $vetted_values[$cur_key] ) < 1 )
            {
                $vetted_values[$cur_key] = 'NULL';
            }
            else
            {
                $vetted_values[$cur_key] = "'".$vetted_values[$cur_key]."'";
            }
        }
        
        
        
        $sqlb = "
        UPDATE questions 
            SET 
            question_title=".$vetted_values["question_title"].",
         question_type=".$vetted_values["question_type"].",
          question_owner_group_id='".$insert_group_id."',
           question_owner_org_id='".$insert_org_id."',
            question_field_size=".$vetted_values["question_field_size"].",
             question_range_bottom=".$vetted_values["question_range_bottom"].",
              question_range_top=".$vetted_values["question_range_top"].",
               question_display_order=".$vetted_values["question_display_order"].",
                question_required=".$vetted_values["question_required"].",
                 question_default_value=".$vetted_values["question_default_value"].", 
                  question_visible=".$vetted_values["question_visible"].",
                   question_encrypted=".$vetted_values["question_encrypted"].",
                    question_no_answer_options=".$vetted_values["question_no_answer_options"].",
                     question_ecma=".$vetted_values["question_ecma"].",
                      question_display_size=".$vetted_values["question_display_size"].",
                       question_special_attribute=".$vetted_values["question_special_attribute"]."
            WHERE question_id = ".$question_attributes["question_id"]."";
            //echo $sqlb."<br/>";  
            run_query($sqlb, "Updating edited question attributes");
            
            $message .= "Sucessfully submitted edited question. ";
            $message_type = "ok";
    }




    function save_existing_element_values ($question_attributes, $question_elements_attributes)
    {
        GLOBAL $date_stamp, $unix_date;
        
        //echo "<br>save_existing_element_values ($question_attributes, $question_elements_attributes)";
        
        //echo pretty_var_dump($question_attributes);
        //Put existing question elements attributes into the backup table, so we can revert if necessary
        if( is_array($question_elements_attributes) )
        {
            foreach ( $question_elements_attributes AS $key => $current_value )
            {
                //Replace values for insertion
                foreach ( $current_value AS $key => $val )
                {
                    if ( $val == '' )
                    {
                        $current_value[$key] = 'NULL';
                    }
                    else
                    {
                        $current_value[$key] = "'".addslashes($val)."'";
                    }
                }
                
                if ( !$current_value["question_element_checked"] )
                {
                    $current_value["question_element_checked"] = 'NULL';
                }
                
                if ( !$current_value["question_element_attribute"] )
                {
                
                    $current_value["question_element_attribute"] = 'NULL';
                }
                
                //bugbug: todo: is this correct or are noan questions supposed to go into the question_elements_previous_version table?
                if (strstr($current_value["question_element_value"],'#'))
                {
                   //these special code options are not saved into the question_elements table
                }
                else
                {
                    //display_value($current_value);
                    $sqlc = "INSERT INTO question_elements_previous_version (question_element_id, question_id, question_element_title,
                     question_element_value, question_element_checked, question_element_attribute,
                      question_edit_date_stamp, question_edit_unix_date) 
                        VALUES (".
                        $current_value["question_element_id"].",
                         ".$current_value["question_id"].",
                          ".$current_value["question_element_title"].",
                           ".$current_value["question_element_value"].",
                            ".$current_value["question_element_checked"].",
                             ".$current_value["question_element_attribute"].",
                              ".$date_stamp.",
                               ".$unix_date.")";
                    
                    //echo $sqlc."<br/>";
                    run_query($sqlc, "Inserting existing question attributes definitions");
                }
                
            }
        }
    }



    function save_existing_table_profile ($question_attributes, $question_elements_attributes)
    {
        GLOBAL $date_stamp, $unix_date;
        
        //echo "<br>save_existing_table_profile ($question_attributes, $question_elements_attributes)";
        
        //Replace values for insertion
        foreach ( $question_attributes AS $key => $val )
        {
            if ( $val == "" )
            {
                $question_attributes[$key] = 'NULL';
            }
            else
            {
                $question_attributes[$key] = "'".$val."'";
            }
        }
        
        $sqld = "INSERT INTO table_profiles_previous_version (question_id, x_axis_title, y_axis_title, table_type,
          question_edit_unix_date) 
            VALUES (
            ".$question_attributes["question_id"].", 
             ".$question_attributes["x_axis_title"].", 
              ".$question_attributes["y_axis_title"].", 
               ".$question_attributes["table_type"].", 
                ".$unix_date." )";
        //echo $sqld."<br/>";
        run_query($sqld, "Inserting existing table profile attributes");
    
    }



    function save_existing_table_elements ($question_attributes, $question_elements_attributes)
    {
        GLOBAL $date_stamp, $unix_date;
        
        //echo "<br>save_existing_table_elements ($question_attributes, $question_elements_attributes)";
        
        //Loop through x, then y elements
        foreach ($question_elements_attributes AS $key_x_or_y => $current_value_x_or_y)  {
            //Loop through each of the elements in the x and y sets
            foreach ($current_value_x_or_y AS $key => $current_value)
            {
                //Replace values for insertion
                foreach ( $current_value AS $key => $val ) {
                    if ( $val == "" )
                    {
                        $current_value[$key] = 'NULL';
                    }
                    else {
                        $current_value[$key] = "'".$val."'";
                    }
                }
               
               
               $sqle = "INSERT INTO table_elements_previous_version (
                axis_id, question_id, axis_type, axis_label, axis_special_type, axis_display_order,
                axis_owner_group_id, axis_owner_org_id, axis_required, axis_range_bottom, axis_range_top, axis_visible,
                 question_edit_unix_date ) 
                 VALUES (
                 ".$current_value["axis_id"].", 
                  ".$current_value["question_id"].", 
                   ".$current_value["axis_type"].", 
                    ".$current_value["axis_label"].", 
                     ".$current_value["axis_special_type"].", 
                      ".$current_value["axis_display_order"].", 
                       ".$current_value["axis_owner_group_id"].", 
                        ".$current_value["axis_owner_org_id"].", 
                         ".$current_value["axis_required"].", 
                          ".$current_value["axis_range_bottom"].", 
                           ".$current_value["axis_range_top"].", 
                            ".$current_value["axis_visible"].", 
                              ".$unix_date.")"; 
            }
            //echo $sqle."<br/>";
            run_query($sqle, "Inserting existing table elements attributes");
        }
    
    }



    function update_edited_table_profile_and_elements ($question_attributes, $question_elements_attributes, $vetted_values)
    {
        GLOBAL $date_stamp, $unix_date;
        
        //echo "<br>update_edited_table_profile_and_elements ($question_attributes, $question_elements_attributes, $vetted_values)";
        
        if ( strlen ( $vetted_values["x_axis_title"] ) < 1 )
        {
            $vetted_values["x_axis_title"] = 'NULL';
        }
        else
        {
            $vetted_values["x_axis_title"] = "'".$vetted_values["x_axis_title"]."'";
        }
        
        if ( strlen ( $vetted_values["y_axis_title"] ) < 1 )
        {
            $vetted_values["y_axis_title"] = 'NULL';
        }
        else
        {
            $vetted_values["y_axis_title"] = "'".$vetted_values["y_axis_title"]."'";
        }
        
        
        $sqlf = "UPDATE table_profiles SET  
          x_axis_title=".$vetted_values["x_axis_title"].",
           y_axis_title=".$vetted_values["y_axis_title"].",
            table_type='".$question_attributes["table_type"]."' 
              WHERE question_id='".$question_attributes["question_id"]."'";
        
        //echo $sqlf."<br/>";
        run_query($sqlf, "Inserting existing table elements attributes");
        
        //Loop through x, then y elements
        foreach ($question_elements_attributes AS $key_x_or_y => $current_value_x_or_y)
        {
            //Loop through each of the elements in the x and y sets
            foreach ($current_value_x_or_y AS $key => $current_value)
            {
                $id = $current_value["axis_id"];
                $sqlg = "UPDATE table_elements SET
                   axis_type='".$vetted_values["axis_type".$id]."',
                    axis_label='".$vetted_values["axis_label".$id]."',
                     axis_special_type='".$vetted_values["axis_special_type".$id]."',
                      axis_display_order='".$vetted_values["axis_display_order".$id]."',
                       axis_required='".$vetted_values["axis_required".$id]."',
                        axis_range_bottom='".$vetted_values["axis_range_bottom".$id]."',
                         axis_range_top='".$vetted_values["axis_range_top".$id]."',
                          axis_visible='".$vetted_values["axis_visible".$id]."'
                WHERE axis_id='".$id."'";
                //echo $sqlg."<p/>";
                run_query($sqlg, "Updating table elements attributes");
            }
        }
    }



    function update_edited_question_elements ($question_elements_attributes, $vetted_values)
    {
        //echo "<br>update_edited_question_elements ($question_elements_attributes, $vetted_values)";
        if ( is_array($question_elements_attributes) )
        {
            foreach ($question_elements_attributes AS $key => $current_value)
            {
                $id = $current_value["question_element_id"];
                //assume that if # present then #77# #88# #98# #99# or other code present.
                if ( strstr($vetted_values["question_element_value".$id],'#')  )
                {
                    //do nothing... todo? bugbug?
                }
                else
                {
                   $sqlh = "UPDATE question_elements SET
                            question_element_title='".$vetted_values["question_element_title".$id]."',
                             question_element_value='".$vetted_values["question_element_value".$id]."',
                              question_element_checked='".$vetted_values["question_element_checked".$id]["yes"]."'
                               WHERE question_element_id='".$id."'";
                   //echo $sqlh."<p/>"; 
                   run_query($sqlh, "Updating table elements attributes"); 
                }
            }
        }
    }



    function insert_new_question_element ($question_attributes, $vetted_value)
	{
		GLOBAL $db_file;
        //echo "<br>insert_new_question_element($question_attributes, $vetted_value) ";
		
        //If it is not the default answer (meaning no new input) insert the new element
        if ( $vetted_value["question_element_titlenew"] )
		{
		   $new_question_element_id = '';
		   $mssql_question_element_id_column_include = '';
		   //for mysql both the question_id and question_element_id autoincrement but we have to special case MSSQL
		   //as we can't have two autoincrement (i.e. IDENTITY columns) in an MSSQL table.  So we have to make our own q_element index.
           if ( $db_file == 'db_connection_mssql' )
		   {
                 //Pull out the largest question element id...DEPRECIATED, now auto increments    
                 $sqlj = "SELECT question_element_id FROM question_elements ORDER BY question_element_id DESC";
		         $resultj = run_query ($sqlj, "Updating questions");
                 $largest_question_element_id = fetch_result( $resultj, "Finding largest element." );
                 $new_question_element_id =  $largest_question_element_id + 1;
				 $new_question_element_id = "'$new_question_element_id', ";
				 $mssql_question_element_id_column_include = 'question_element_id, ';
		   }
           
           //question_element_id, //'".$new_question_element_id."',
            $sqli = "INSERT INTO question_elements (
               ".$mssql_question_element_id_column_include."question_id, question_element_title,
               question_element_value, question_element_checked, question_element_attribute
               )
               VALUES (
                ".$new_question_element_id."
                '".$question_attributes["question_id"]."',
                 '".$vetted_value["question_element_titlenew"]."',
                  '".$vetted_value["question_element_valuenew"]."',
				   '".$vetted_value["question_element_checkednew"]["yes"]."',
				    '".$current_value["question_element_attribute"]."')";
            
            //echo $sqli."<p/>";
            run_query($sqli, "Updating elements attributes");
            //$this->question_table_axis_def
            
            //Pull out the largest question element id
            $sqlj = "SELECT question_element_rowid FROM question_elements ORDER BY question_element_rowid DESC";
		    $resultj = run_query ($sqlj, "Updating questions");
            $new_question_element_id = fetch_result( $resultj, "Finding largest element." );
            
            $sqlk = "
            UPDATE question_elements 
                SET question_element_id = ".$new_question_element_id ."
                WHERE question_element_rowid = ".$new_question_element_id;
            run_query ($sqlk, "Updating questions");
            
            $new_question_element_attributes[$question_attributes["question_id"]] = array (
            "question_element_id" => $new_question_element_id,
              "question_id" =>  $question_attributes["question_id"],
                "question_element_title" => $vetted_value["question_element_titlenew"],
                  "question_element_value" => $vetted_value["question_element_valuenew"],
                   "question_element_checked" => $vetted_value["question_element_checkednew"]["yes"],
                     "question_element_attribute" => $current_value["question_element_attribute"]);
            
            $form_string = "question_element_checked".$new_question_element_id;
            $this->vetted_form_answers[$form_string]["yes"] = $vetted_value["question_element_checkednew"]["yes"];
            
            //echo $vetted_value["question_element_checkednew"]["yes"]."IS THIS YES?<p/>";
            
            //Merge the just defined element into the list of question elements
            $pull_element_questions = $this->load_elements_defining_questions ($new_question_element_attributes);
            $this->question_table_axis_def = array_merge($this->question_table_axis_def, $pull_element_questions[0]);
            $this->question_table_axis_def_elements = array_merge($this->question_table_axis_def_elements,
                                                                   $pull_element_questions[1] );
        }
    }



    function insert_new_axis ($vetted_value)
    {
        GLOBAL $org_id, $group_id;
        
        //If it is not the default answer (meaning no new input) insert the new element
        //display_value($vetted_form_answer);
        if ( $vetted_value["axis_labelnew"] )
        {
           //Pull out the largest question element id...    
            $sqlj = "SELECT axis_id FROM table_elements ORDER BY axis_id DESC";
		    $resultj = run_query ($sqlj, "Updating questions");
            $largest_question_element_id = fetch_result( $resultj, "Finding largest element id." );
            $new_question_element_id =  $largest_question_element_id + 1;   
            
            $sqli = "INSERT INTO table_elements (
             axis_id, question_id, axis_type, axis_label, axis_special_type, axis_display_order,
              axis_owner_group_id, axis_owner_org_id, axis_required, axis_range_bottom, axis_range_top, axis_visible
               ) 
               VALUES (
               '".$new_question_element_id."',
                '".$this->question_attribute["question_id"]."',
                 '".$vetted_value["axis_typenew"]."',
                  '".$vetted_value["axis_labelnew"]."',
                   '".$vetted_value["axis_special_typenew"]."',
                    '".$vetted_value["axis_display_ordernew"]."',
                     '".$group_id."',
                       '".$org_id."',
                        '".$vetted_value["axis_requirednew"]."',
                         '".$vetted_value["axis_range_bottomdnew"]."',
                          '".$vetted_value["axis_range_topnew"]."',
                           '".$vetted_value["axis_visiblenew"]."'
                    )";
            
            //echo $sqli."<p/>";
            run_query($sqli, "Updating table elements attributes");
            //$this->question_table_axis_def
            $new_question_element_attributes[$vetted_value["axis_typenew"]][$question_attributes["question_id"]] = array (
            "axis_id" => $new_question_element_id,
              "question_id" =>  $this->question_attribute["question_id"],
                "axis_type" => $vetted_value["axis_typenew"],
                  "axis_label" => $vetted_value["axis_labelnew"],
                   "axis_special_type" => $vetted_value["axis_special_typenew"],
                     "axis_display_order" => $vetted_value["axis_display_ordernew"],
                       "axis_display_order" => $vetted_value["axis_display_ordernew"],
                         "axis_display_order" => $vetted_value["axis_display_ordernew"],
                           "axis_required" => $vetted_value["axis_requirednew"],
                             "axis_range_bottom" => $vetted_value["axis_range_bottomnew"],
                               "axis_range_top" => $vetted_value["axis_range_topnew"],
                                 "axis_visible" => $vetted_value["axis_visiblenew"]
                     );
            
            //Merge the just defined element into the list of question elements
            $pull_element_questions = $this->load_question_table_defining_elements ($new_question_element_attributes);
               $this->question_table_axis_def = array_merge($this->question_table_axis_def, $pull_element_questions[0]);
                 $this->question_table_axis_def_elements = array_merge($this->question_table_axis_def_elements,
                   $pull_element_questions[1]);
        }
    }



    function load_question_defining_questions ($question_attributes)
    {
        $this->question_def["question_title"] = array ("question_id" => "question_title",
         "question_title" => "Question Title: ", "question_type" => "text", "question_field_size" => "500",
          "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" => "40");
        
        $this->question_def["x_axis_title"] = array ("question_id" => "x_axis_title",
         "question_title" => "Axis X Title: ", "question_type" => "text", "question_field_size" => "200",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "no", "question_default_value" => $question_attributes["x_axis_title"], "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "40");
        
        $this->question_def["y_axis_title"] = array ("question_id" => "y_axis_title",
         "question_title" => "Axis Y Title: ", "question_type" => "text", "question_field_size" => "200",
          "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
           "question_required" => "no", "question_default_value" => $question_attributes["y_axis_title"], "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "40");
        
        $this->question_def["table_type"] = array ("question_id" => "table_type", "question_title" => "Table Type:",
         "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_attributes["table_type"],
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements["table_type"]["numbers"] = array ("question_element_id" => "element_question_type",
             "question_id" => "question_type", "question_element_title" => "Numbers", "question_element_value" => "numbers");
        
		$this->question_def_elements["table_type"]["text"] = array ("question_element_id" => "element_question_type",
             "question_id" => "question_type", "question_element_title" => "Text", "question_element_value" => "text");
        
        $this->question_def_elements["table_type"]["checkboxes"] = array ("question_element_id" => "element_question_type",
             "question_id" => "question_type", "question_element_title" => "Checkboxes", "question_element_value" => "checkboxes");
        
        $this->question_def['question_encrypted'] = array ('question_id' => 'question_encrypted', 'question_title' => 'Encrypted: ',
         '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" => "no", "question_default_value" => 'encrypted_no',
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements['question_encrypted']['1'] = array ('question_element_id' => 'encrypted_yes',
             'question_id' => 'question_encrypted', 'question_element_title' => 'Yes', 'question_element_value' => '1');
        $this->question_def_elements['question_encrypted']['0'] = array ('question_element_id' => 'encrypted_no',
             'question_id' => 'question_encrypted', 'question_element_title' => 'No', 'question_element_value' => '0');
        
        
        $this->question_def['question_reason_blank_dont_know'] = array ('question_id' => 'question_reason_blank_dont_know',
        'question_title' => "Add \"Don't know\" answer option: ",
         '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" => "0", "question_default_value" => '0',
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements['question_reason_blank_dont_know']['1'] = array ('question_element_id' => 'dont_know_yes',
             'question_id' => 'question_reason_blank_dont_know', 'question_element_title' => 'Yes', 'question_element_value' => '1');
        $this->question_def_elements['question_reason_blank_dont_know']['0'] = array ('question_element_id' => 'dont_know_no',
             'question_id' => 'question_reason_blank_dont_know', 'question_element_title' => 'No', 'question_element_value' => '0');
        
        $this->question_def['question_reason_blank_not_applicable'] = array ('question_id' => 'question_reason_blank_not_applicable',
        'question_title' => "Add \"Not applicable\" answer option: ",
         '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" => "no", "question_default_value" => '0',
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements['question_reason_blank_not_applicable']['2'] = array ('question_element_id' => 'not_app_yes',
             'question_id' => 'question_reason_blank_not_applicable', 'question_element_title' => 'Yes', 'question_element_value' => '2');
        $this->question_def_elements['question_reason_blank_not_applicable']['0'] = array ('question_element_id' => 'dnot_app_no',
             'question_id' => 'question_reason_blank_not_applicable', 'question_element_title' => 'No', 'question_element_value' => '0');
        
        $this->question_def['question_reason_blank_refused'] = array ('question_id' => 'question_reason_blank_refused',
        'question_title' => "Add \"Refused\" answer option: ",
         '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" => "no", "question_default_value" => '0',
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements['question_reason_blank_refused']['4'] = array ('question_element_id' => 'refused_yes',
             'question_id' => 'question_reason_blank_not_applicable', 'question_element_title' => 'Yes', 'question_element_value' => '4');
        $this->question_def_elements['question_reason_blank_refused']['0'] = array ('question_element_id' => 'refused_no',
             'question_id' => 'question_reason_blank_not_applicable', 'question_element_title' => 'No', 'question_element_value' => '0');
        
        
        $this->question_def["question_type"] = array ("question_id" => "question_type", "question_title" => "Question Type:",
         "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_attributes["question_type"],
            "question_visible" => "yes", "question_ecma" => "");
        
        
        //Only load in options to change to other question types if conversion makes sense (i.e., no "number" to "checkboxes")
        if ($this->question_attribute["question_type"] == "text" || 
             $this->question_attribute["question_type"] == "number" || 
              $this->question_attribute["question_type"] == "textarea" )
        {
            $this->question_def_elements["question_type"]["text"] = array ("question_element_id" => "element_question_type",
               "question_id" => "question_type", "question_element_title" => "Text", "question_element_value" => "text");
            
            $this->question_def_elements["question_type"]["number"] = array ("question_element_id" => "element_question_type",
              "question_id" => "question_type", "question_element_title" => "Number", "question_element_value" => "number");
            
            $this->question_def_elements["question_type"]["textarea"] = array ("question_element_id" => "element_question_type",
              "question_id" => "question_type", "question_element_title" => "Text Area", "question_element_value" => "textarea");
        }
        
        if ($this->question_attribute["question_type"] == "checkboxes" || 
             $this->question_attribute["question_type"] == "radio" || 
              $this->question_attribute["question_type"] == "dropdown" )
        {
            $this->question_def_elements["question_type"]["radio"] = array ("question_element_id" => "element_question_type",
              "question_id" => "question_type", "question_element_title" => "Radio", "question_element_value" => "radio");
            
            $this->question_def_elements["question_type"]["checkboxes"] = array ("question_element_id" => "element_question_type",
              "question_id" => "question_type", "question_element_title" => "Checkboxes", "question_element_value" => "checkboxes");
            
            $this->question_def_elements["question_type"]["dropdown"] = array ("question_element_id" => "element_question_type",
              "question_id" => "question_type", "question_element_title" => "Dropdown", "question_element_value" => "dropdown");
        }
        
        if ($this->question_attribute["question_type"] == "table" )
        {
            //Can't easily convert a table question, so don't allow option
			$this->question_def_elements["question_type"]["table"] = array ("question_element_id" => "element_question_type",
                "question_id" => "question_type", "question_element_title" => "Table", "question_element_value" => "table");
        }
        if ($this->question_attribute["question_type"] == "date" )
        {
            //Can't easily convert a date question, so don't allow option
			$this->question_def_elements["question_type"]["date"] = array ("question_element_id" => "element_question_type",
                "question_id" => "question_type", "question_element_title" => "Date", "question_element_value" => "date");
        }
        
        if ( $this->question_attribute["question_type"] == "textarea" )
        {
            $this->question_def["question_display_size"] = array ("question_id" => "question_display_size",
             "question_title" => "Table width (columns):", "question_type" => "number", "question_field_size" => "3",
              "question_range_bottom" => "1", "question_range_top" => "300", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "4", "question_visible" => "yes",
                "question_ecma" => "", "question_display_size" => "3");
            
            $this->question_def["question_field_size"] = array ("question_id" => "question_field_size",
             "question_title" => "Table height (rows):", "question_type" => "number", "question_field_size" => "3",
              "question_range_bottom" => "1", "question_range_top" => "300", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "",
                "question_visible" => "yes",
                "question_ecma" => "", "question_display_size" => "3");
        }
        else
        {
            $this->question_def["question_field_size"] = array ("question_id" => "question_field_size",
             "question_title" => "Question Field Size:", "question_type" => "number", "question_field_size" => "3",
              "question_range_bottom" => "1", "question_range_top" => "300", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => $question_attributes["question_field_size"],
                "question_visible" => "yes",
                "question_ecma" => "", "question_display_size" => "3");
            
            $this->question_def["question_display_size"] = array ("question_id" => "question_display_size",
             "question_title" => "Question Display Size:", "question_type" => "number", "question_field_size" => "3",
              "question_range_bottom" => "1", "question_range_top" => "300", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "4", "question_visible" => "yes",
                "question_ecma" => "", "question_display_size" => "3");
        }
        
        $this->question_def["question_range_bottom"] = array ("question_id" => "question_range_bottom",
         "question_title" => "Range Bottom:", "question_type" => "number", "question_field_size" => "10",
          "question_range_bottom" => "-999999999", "question_range_top" => "999999999", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "-1", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "8");
        
        $this->question_def["question_range_top"] = array ("question_id" => "question_range_top",
         "question_title" => "Range Top:", "question_type" => "number", "question_field_size" => "10",
          "question_range_bottom" => "-999999999", "question_range_top" => "999999999", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "10000000", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "8");
        
        $this->question_def["question_display_order"] = array ("question_id" => "question_display_order",
         "question_title" => "Display Order:", "question_type" => "number", "question_field_size" => "10",
          "question_range_bottom" => "-999999999", "question_range_top" => "999999999", "question_display_order" => "1019",
           "question_required" => "yes", "question_default_value" => "1000", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "5");
        
        $this->question_def["question_required"] = array ("question_id" => "question_required",
         "question_title" => "Answer Required:", "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" => "yes",
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements["question_required"]["yes"] = array ("question_element_id" => "element_question_required",
             "question_id" => "question_required", "question_element_title" => "Yes", "question_element_value" => "yes");
        
        $this->question_def_elements["question_required"]["no"] = array ("question_element_id" => "element_question_required",
             "question_id" => "question_required", "question_element_title" => "No", "question_element_value" => "no");
        
        $this->question_def["question_default_value"] = array ("question_id" => "question_default_value",
         "question_title" => "Default Value:", "question_type" => "text", "question_field_size" => "35",
          "question_range_bottom" => "", "question_range_top" => "", "question_display_order" => "1019",
           "question_required" => "no", "question_default_value" => "", "question_visible" => "yes",
            "question_ecma" => "", "question_display_size" => "10");
        
        $this->question_def["question_visible"] = array ("question_id" => "question_visible",
         "question_title" => "Question Visible:", "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" => "yes",
            "question_visible" => "yes", "question_ecma" => "");
        
        $this->question_def_elements["question_visible"]["yes"] = array ("question_element_id" => "element_question_visible",
             "question_id" => "question_visible", "question_element_title" => "Yes", "question_element_value" => "yes");
        
        $this->question_def_elements["question_visible"]["no"] = array ("question_element_id" => "element_question_visible",
             "question_id" => "question_visible", "question_element_title" => "No", "question_element_value" => "no");
        
        $this->question_def["question_ecma"] = array ("question_id" => "question_ecma",
         "question_title" => "Question ECMA:", "question_type" => "text", "question_owner_group_id" => "1",
          "question_owner_org_id" => "1", "question_field_size" => "50",
          "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" => "10");
        
        $this->question_def["question_owner_org_id"] = array ("question_id" => "question_owner_org_id",
         "question_title" => "Question Optional:", "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["question_owner_org_id"]["1"] = array ("question_element_id" => "element_question_owner",
             "question_id" => "question_owner_org_id", "question_element_title" => "Required", "question_element_value" => "1");
        
        $this->question_def_elements["question_owner_org_id"]["1000000"] = array ("question_element_id" => "element_question_owner",
             "question_id" => "question_owner_org_id", "question_element_title" => "Optional", "question_element_value" => "1000000");
        
        
        if ($this->question_attribute["question_type"] == "date" )
        {
            $this->question_def["question_special_attribute"] = array ("question_id" => "question_special_attribute", "question_title" =>
            "Date elements (\"F\" = text month, \"m\" = number month, \"j\" = day of month, \"Y\" = year):",
             "question_type" => "text", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "50",
             "question_display_size" => "25", "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
               "question_required" => "yes", "question_default_value" => "FjY", "question_visible" => "yes", "question_ecma" => "");
            
            /*
            $this->question_def_elements["question_special_attribute"]["month"] = array ("question_element_id" => "element_question_type",
                "question_id" => "question_type", "question_element_title" => "Month", "question_element_value" => "radio",
                "question_element_checked" => "yes");
            
            $this->question_def_elements["question_type"]["day_of_month"] = array ("question_element_id" => "element_question_type",
                "question_id" => "question_type", "question_element_title" => "Day of Month", "question_element_value" => "checkboxes",
                "question_element_checked" => "yes");
            
            $this->question_def_elements["question_type"]["year"] = array ("question_element_id" => "element_question_type",
                "question_id" => "question_type", "question_element_title" => "Year", "question_element_value" => "dropdown",
                "question_element_checked" => "yes");
            */
            
        }
        
        if ($this->question_attribute["question_type"] == "textarea" )
        {
            $this->question_def["question_special_attribute"] = array ("question_id" => "question_special_attribute", "question_title" =>
            "Total characters allowed:",
             "question_type" => "number", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "10",
             "question_display_size" => "25", "question_range_bottom" => "10", "question_range_top" => "1000000", "question_display_order" => "1019",
               "question_required" => "no", "question_default_value" => "", "question_visible" => "yes", "question_ecma" => "");
        }
    }





    function load_question_table_defining_elements ($question_elements_attributes)
    {
        //There are both "x" and "y" sets of elements; this loops through x then y
        if (is_array($question_elements_attributes) )
        {
            foreach ($question_elements_attributes AS $question_elements_attributes_x_or_y)
            {
                foreach ($question_elements_attributes_x_or_y AS $current_question_element)
                {
                    $axis_id = $current_question_element["axis_id"];
                    //echo "Axis id is: ".$axis_id."<br/>";
                    
                    $table_axis_def["axis_label".$axis_id] = array (
                      "question_id" => "axis_label".$axis_id, "question_title" => "Axis Label:","question_type" => "text",
                       "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "150",
                        "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
                         "question_required" => "yes", "question_default_value" => $current_question_element["axis_label"],
                          "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "15");
                    
                    $table_axis_def["axis_type".$axis_id] = array (
                      "question_id" => "axis_type".$axis_id, "question_title" => "Axis Type:","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" => $current_question_element["axis_type"],
                          "question_visible" => "yes", "question_ecma" => "");
                   
                   //"question_element_id" => "element_axis_type",  UNUSED IN QUESTIONS NOT PULLED FROM DB AS FAR AS I KNOW
                   
                   $table_axis_def_elements["axis_type".$axis_id]["x"] = array ( "question_id" =>
                         "axis_type".$axis_id, "question_element_title" => "X Axis", "question_element_value" => "x" );
                   $table_axis_def_elements["axis_type".$axis_id]["y"] = array ( "question_id" =>
                         "axis_type".$axis_id, "question_element_title" => "Y Axis", "question_element_value" => "y" );
                   
                   if ($this->question_attribute["table_type"] == "numbers")
                   {
                          $table_axis_def["axis_range_bottom".$axis_id] = array (
                           "question_id" => "axis_range_bottom".$axis_id, "question_title" => "Axis Range Bottom:","question_type" => "number",
                            "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "7",
                             "question_range_bottom" => "-999999", "question_range_top" => "999999", "question_display_order" => "1019",
                              "question_required" => "no", "question_default_value" => $current_question_element["axis_range_bottom"],
                               "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "10");
                          
                          $table_axis_def["axis_range_top".$axis_id] = array (
                           "question_id" => "axis_range_top".$axis_id, "question_title" => "Axis Range Top:","question_type" => "number",
                            "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "7",
                             "question_range_bottom" => "-999999", "question_range_top" => "999999", "question_display_order" => "1019",
                              "question_required" => "no", "question_default_value" => $current_question_element["axis_range_top"],
                               "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "10");
                   }
                   
                   $table_axis_def["axis_special_type".$axis_id] = array (
                        "question_id" => "axis_special_type".$axis_id, "question_title" => "Axis Format:","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" => $current_question_element["axis_special_type"],
                            "question_visible" => "yes", "question_ecma" => "");
                   
                   $table_axis_def_elements["axis_special_type".$axis_id]["default"] = array (
                            "question_id" => "axis_special_type".$axis_id, "question_element_title" => "Default",
                             "question_element_value" => "default" );
                   
                   $table_axis_def_elements["axis_special_type".$axis_id]["subtotal"] = array ( 
                           "question_id" => "axis_special_type".$axis_id, "question_element_title" => "Subtotal",
                            "question_element_value" => "subtotal" );
                   $table_axis_def_elements["axis_special_type".$axis_id]["total"] = array ( 
                           "question_id" => "axis_special_type".$axis_id, "question_element_title" => "Total",
                            "question_element_value" => "total" );
                   $table_axis_def_elements["axis_special_type".$axis_id]["text_axis"] = array ( 
                           "question_id" => "axis_special_type".$axis_id, "question_element_title" => "Text Axis",
                            "question_element_value" => "text_axis" );
                   
                   $table_axis_def["axis_display_order".$axis_id] = array (
                        "question_id" => "axis_display_order".$axis_id, "question_title" => "Axis Display Order:","question_type" => "number",
                         "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "4",
                          "question_range_bottom" => "-999", "question_range_top" => "999", "question_display_order" => "1019",
                           "question_required" => "yes", "question_default_value" => $current_question_element["axis_display_order"],
                            "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "4");
                   
                   $table_axis_def["axis_required".$axis_id] = array (
                        "question_id" => "axis_required".$axis_id, "question_title" => "Axis Required:","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" => $current_question_element["axis_required"],
                            "question_visible" => "yes", "question_ecma" => "");
                   
                   $table_axis_def_elements["axis_required".$axis_id]["all"] = array ( "question_id" =>
                            "axis_required".$axis_id, "question_element_title" => "All", "question_element_value" => "all" );
                   $table_axis_def_elements["axis_required".$axis_id]["one"] = array ( "question_id" =>
                            "axis_required".$axis_id, "question_element_title" => "One", "question_element_value" => "one" );
                   $table_axis_def_elements["axis_required".$axis_id]["none"] = array ( "question_id" =>
                            "axis_required".$axis_id, "question_element_title" => "None", "question_element_value" => "none" );
                   
                   
                   $table_axis_def["axis_visible".$axis_id] = array (
                        "question_id" => "axis_visible".$axis_id, "question_title" => "Axis Visible:","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" => $current_question_element["axis_visible"],
                            "question_visible" => "yes", "question_ecma" => "");
                   
                   $table_axis_def_elements["axis_visible".$axis_id]["visible"] = array ( "question_id" =>
                            "axis_visible".$axis_id, "question_element_title" => "Visible", "question_element_value" => "visible" );
                   $table_axis_def_elements["axis_visible".$axis_id]["hidden"] = array ( "question_id" =>
                            "axis_visible".$axis_id, "question_element_title" => "Hidden", "question_element_value" => "hidden" );
                }
            }
        }
        
        $return_value[0] = $table_axis_def;
        $return_value[1] = $table_axis_def_elements;
        return $return_value;
    }



    //Defines the questions elements
    function load_elements_defining_questions ($question_elements_attributes)
    {
        //question_element_title VARCHAR(60), question_element_value VARCHAR(70), question_element_checked VARCHAR(10)
        if ( is_array($question_elements_attributes) )
        {
            foreach ($question_elements_attributes AS $current_question_element)
            {
                $axis_id = $current_question_element["question_element_id"];
                
                $question_element_def[0]["question_element_title".$axis_id] = array (
                 "question_id" => "question_element_title".$axis_id, "question_title" => "Element Label:","question_type" => "text",
                  "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "60",
                   "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
                    "question_required" => "yes", "question_default_value" => $current_question_element["question_element_title"],
                     "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "30");
                
                $question_element_def[0]["question_element_value".$axis_id] = array (
                 "question_id" => "question_element_value".$axis_id, "question_title" => "Value:","question_type" => "text",
                  "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "70",
                   "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => "1019",
                    "question_required" => "yes", "question_default_value" => $current_question_element["question_element_value"],
                     "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "20");
                
                if($this->question_attribute["question_type"] == "checkboxes")
                {
                    $question_element_def[0]["question_element_checked".$axis_id] = array (
                     "question_id" => "question_element_checked".$axis_id, "question_title" => "Default:", 
                      "question_type" => "checkboxes",
                       "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" => "no", 
                          "question_visible" => "yes", "question_ecma" => "");
                          //echo "in the load_elements ".$axis_id.$current_question_element["question_element_checked"]."<p/>";
                          $this->vetted_form_answer = $current_question_element["question_element_checked"];
                    
                    $question_element_def[1]["question_element_checked".$axis_id]["yes"] = array ( "question_id" =>
                        "question_element_checked".$axis_id, "question_element_title" => "Checked", "question_element_value" => "yes",
                        "question_element_checked" => $current_question_element["question_element_checked"]
                         );
                }
            }
        }
        return $question_element_def;
    }



}
?>
Return current item: Homeless Mangement Information System