Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/Questions_into_array_class.inc
<?php
//This class puts questions (and their elements) into arrays for use in other functions

class Questions_into_array	{

   	var $page_id;
	var $group_id;
	var $org_id;
	var $user_access_level;
	var $retrieve_questions_sql;
	var $questions;
	var $question_elements;
    var $rp_id; //Defines the report_type_id associated with a question
 
	function Questions_into_array ( $page_id, $user_access_level, $org_id, $group_id, $display_or_edit, $rp_id = '' )
	{
		include_once($include_root.'services_functions.inc');
	    //echo "<br>Questions_into_array ( $page_id, $user_access_level, $org_id, $group_id, $display_or_edit, $rp_id )<br>";
		$this->page_id = $page_id;
		$this->user_access_level = $user_access_level;
		$this->org_id = $org_id;
		$this->group_id = $group_id;
        $this->display_or_edit = $display_or_edit;
        $this->rp_id = $rp_id;
        
        //if ( strlen ( $this->rp_id ) > 0 )  {
        //    $this->rp_id = $this->rp_id.'__';
        //} 
        /* bugbug: This is unnecessary; handled by dynamic question attribute. TK 3-16-2004
        if ($page_id == 'client_service' || $page_id == 'household_service' )
		{
            $active_services = get_services_by_org_id_array($org_id);
            if ( $active_services )
			{
                foreach ($active_services as $current_service)
				{
                    $this->question_elements['99'][$current_service['service_id']] = array ('question_element_id' => $current_service["service_id"], "question_id" => "99", "question_element_title" => $current_service["service_display_term"], "question_element_value" => $current_service["service_id"]);
                }
            }
        }
        */
		switch ( $display_or_edit )	{
			case 'display':
				$this->questions_for_display();
				//echo 'leaving Questions_into_array: '.var_dump($this->questions);
				//echo "<font color=red>this->questions is going out...:".pretty_var_dump($this->questions)."</font><hr color=green><br>";
				break;

			case 'edit':
				$this->questions_for_editing();
				break;

			case 'optional_only':
				$this->questions_optional_only();
				break;

			default:
			    echo 'Query type not defined for questions into arrray'; exit;

		}
	}



	function questions_for_editing ()	{

		switch ($this->user_access_level)	{
			case '40':
				echo "No Access Allowed"; exit;
				break;
		
			//For the org administrator	
			case '30':
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id  
                    FROM 
                        questions 
                        LEFT JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id )
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND question_owner_org_id = '".$this->org_id."' 
                    ORDER BY question_display_order"; 
				break;
            
			//For the group administrator
			case '20':
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id  
                    FROM 
                        questions                        
                        LEFT JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND question_owner_org_id = '".$this->org_id."' 
                    ORDER BY question_display_order"; 
				break;
			
			//For the system administrtor
			case '10': 
				$this->retrieve_questions_sql = "
                SELECT DISTINCT questions.*, question_report_type_ids.report_type_id  
                    FROM 
                        questions
                        LEFT JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND 
                            ( question_owner_org_id = '".$this->org_id."' 
                                OR question_owner_org_id = '1000000') 
                    ORDER BY question_display_order";
				break;

				default:
				echo 'Failure to identify access level: '.$access_level[0];
				exit;
		}
				$this->questions_extract('edit');
	}
    


	function questions_for_display ()
	{
		preg_match ( "/^\d{1}/", $this->user_access_level, $access_level ); 
	
		switch ($access_level[0])	{
			case '4':
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id 
                    FROM 
                        questions
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                        LEFT JOIN question_optional ON (questions.question_id = question_optional.question_id)                          
                    WHERE question_report_type_ids.report_type_id = '".$this->page_id."' AND questions.question_visible LIKE 'yes' AND ((questions.question_owner_group_id = '1') OR (questions.question_owner_group_id = '".$this->group_id."') OR (questions.question_owner_org_id = '".$this->org_id."') OR (questions.question_owner_group_id = '1000000' AND questions.question_id = question_optional.question_id AND question_optional.group_id = '".$this->group_id."') OR (questions.question_owner_org_id = '1000000' AND questions.question_id = question_optional.question_id AND question_optional.org_id = '".$this->org_id."')) ORDER BY questions.question_display_order";
				
				break;
				
			case '3':
            $this->retrieve_questions_sql = "
            SELECT questions.*, question_report_type_ids.report_type_id
                FROM 
                    questions
                    INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    LEFT JOIN question_optional ON questions.question_id = question_optional.question_id 
                WHERE question_report_type_ids.report_type_id = '".$this->page_id."' 
                AND questions.question_visible LIKE 'yes' 
                AND (
                    ( questions.question_owner_group_id = '1' ) 
                    OR ( questions.question_owner_group_id = '".$this->group_id."' ) 
                    OR ( questions.question_owner_org_id = '".$this->org_id."' ) 
                    OR ( 
                        questions.question_owner_group_id = '1000000' 
                        AND questions.question_id = question_optional.question_id AND question_optional.group_id = '".$this->group_id."'
                    ) 
                    OR ( 
                        questions.question_owner_org_id = '1000000' 
                        AND questions.question_id = question_optional.question_id AND question_optional.org_id = '".$this->org_id."'
                    )
                ) 
                ORDER BY question_display_order";
//				$this->retrieve_questions_sql = "SELECT * FROM questions, question_optional WHERE questions.report_type_id = '".$this->page_id."' AND questions.question_visible LIKE 'yes' AND ((questions.question_owner_group_id = '1') OR (questions.question_owner_group_id = '".$this->group_id."') OR (questions.question_owner_org_id = '".$this->org_id."') OR (questions.question_owner_group_id = '1000000' AND questions.question_id = question_optional.question_id AND question_optional.group_id = '".$this->group_id."') OR (questions.question_owner_org_id = '1000000' AND questions.question_id = question_optional.question_id AND question_optional.org_id = '".$this->org_id."')) ORDER BY question_display_order";
				break;

			case '2':
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id
                    FROM 
                        questions 
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id )
                        LEFT JOIN question_optional ON questions.question_id = question_optional.question_id 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND questions.question_visible LIKE 'yes' 
                        AND (
                            questions.question_owner_group_id = '1' 
                            OR ( questions.question_owner_group_id = '".$this->group_id."' ) 
                            OR ( questions.question_owner_group_id = '1000000' 
                                AND questions.question_id = question_optional.question_id 
                                AND question_optional.group_id = '".$this->group_id."'
                            )
                        ) 
                    ORDER BY question_display_order";
				break;

			case '1':
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id 
                    FROM 
                        questions
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND question_visible LIKE 'yes' 
                        AND question_owner_group_id = '1' 
                ORDER BY question_display_order";
				break;

			default:
				echo 'Failure to identify access level: '.$access_level[0];
				exit;
		}
				//echo "<br>questions_for_display(): ".$this->retrieve_questions_sql;
				$this->questions_extract('');
	}



    function questions_optional_only ()
    {
        preg_match ("/^\d{1}/", $this->user_access_level, $access_level); 
	    
		switch ($access_level[0])
        {
			case "4":
				echo "No Access Allowed"; exit;
				break;
				
			case "3":
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id 
                    FROM 
                        questions
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND questions.question_visible LIKE 'yes' 
                        AND questions.question_owner_group_id = '1000000' 
                ORDER BY question_display_order";
				break;
			
			case "2":
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id
                    FROM 
                        questions
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND questions.question_visible LIKE 'yes' 
                        AND questions.question_owner_group_id = '1000000' 
                ORDER BY question_display_order";
				break;
			
			case "1":
				$this->retrieve_questions_sql = "
                SELECT questions.*, question_report_type_ids.report_type_id
                    FROM 
                        questions
                        INNER JOIN question_report_type_ids ON ( questions.question_id = question_report_type_ids.question_id ) 
                    WHERE 
                        question_report_type_ids.report_type_id = '".$this->page_id."' 
                        AND questions.question_owner_group_id = '1000000' 
                ORDER BY question_display_order";
				
				//echo "No Access Allowed for optional questions.";
			    break;
                
			default:
				echo "Failure to identify access level: ".$access_level[0];
				exit;
		}
				$this->questions_extract('');
	}



	function dynamic_default_element_value ($question_default_value_function, $question_id)
    {
		GLOBAL $unix_date;
        
		switch($question_default_value_function)
        {
			case '#lastMonth':
				
				$last_month_unix = time_add ( 'm', -1, $unix_date );
				$last_month = safe_date( 'm', $last_month_unix );
				foreach ( $this->question_elements[$question_id] AS $element_key => $current_element )
                {
					if ( $current_element['question_element_value'] == $last_month )
                    {
						return $current_element['question_element_id'];
						break 1;
					}
				}
				break;
			
			case '#lastYear':
				
				$last_month_unix = time_add ( 'Y', -1, $unix_date ); 
				$last_month = safe_date( 'm', $last_month_unix );
				foreach ( $this->question_elements[$question_id] AS $element_key => $current_element )
                {
					if ( $current_element['question_element_value'] == $last_month )
                    {
						return $current_element['question_element_id'];
						break 1;
					}
				}
				break;
			
			case '#currentDayMonth':
				
				$current_month = safe_date( 'm', $unix_date );
				foreach ( $this->question_elements[$question_id] AS $element_key => $current_element )
                {
					if ( $current_element['question_element_value'] == $current_month )
                    {
						return $current_element['question_element_id'];
						break 1;
					}
				}
				break;
			
			case '#nextDayMonth':
				
				$next_day_unix = time_add ( 'd', 1, $unix_date );
				$next_day_month = safe_date( 'm', $next_day_unix );
				foreach ( $this->question_elements[$question_id] AS $element_key => $current_element )
                {
					if ( $current_element['question_element_value'] == $next_day_month )
                    {
						return $current_element['question_element_id'];
						break 1;
					}
				}
				break;
			
			case '#currentDayDay':
				
				$this_day = safe_date( 'd', $unix_date );
				return $this_day;
                
				break;
			
			case '#nextDayDay':
				
				$next_day_unix = time_add ('d', 1, $unix_date);
				$this_day = safe_date( 'd', $next_day_unix );
				return $this_day;
                
				break;
			
			case '#currentDayYear':
				
				$this_day = safe_date( 'Y', $unix_date );
				return $this_day;
                
				break;
			
			case '#nextDayYear':
				
				$next_day_unix = time_add ( 'd', 1, $unix_date);
				$this_day = safe_date( 'Y', $next_day_unix );
				return $this_day;
                
				break;
            
            case '#currentDayUnix':
                return $unix_date;
                
                break;
            
			default:
				echo 'No dynamic_default_value function defined in Questions_into_array_class';
		}
}



    function pull_matching_table_elements ( $question_id, $mod_qid, $x_or_y )
    {
        if ( $this->display_or_edit == 'edit' )
        {
            $pull_visible_status = ", table_elements.axis_visible "; 
        }
        else
        {
            $exclude_hidden = " AND table_elements.axis_visible LIKE 'visible' ";
        }
        
        $axis_sql = "SELECT table_elements.axis_id, table_elements.question_id, table_elements.axis_type, table_elements.axis_label,
         table_elements.axis_special_type, table_elements.axis_display_order, table_elements.axis_owner_group_id,
          table_elements.axis_owner_org_id, table_elements.axis_required, table_elements.axis_range_bottom,
           table_elements.axis_range_top".$pull_visible_status."
           FROM table_elements 
                LEFT JOIN table_elements_optional ON table_elements.axis_id = table_elements_optional.axis_id
                WHERE table_elements.question_id = '".$question_id."' 
                AND table_elements.axis_type LIKE '".$x_or_y."'
                ".$exclude_hidden."                
                AND ( (table_elements.axis_owner_group_id = '1')
                    OR (table_elements.axis_owner_group_id = '".$this->group_id."') 
                    OR (table_elements.axis_owner_org_id = '".$this->org_id."') 
                    OR (table_elements.axis_owner_org_id = '1000000' 
                        AND table_elements.axis_id = table_elements_optional.axis_id 
                        AND table_elements_optional.org_id = '".$this->org_id."'
                    )
                    OR (table_elements.axis_owner_group_id = '1000000' 
                        AND table_elements.axis_id = table_elements_optional.axis_id 
                        AND table_elements_optional.group_id = '".$this->group_id."'
                    )
                ) 
                ORDER BY table_elements.axis_display_order";
        
        //echo $axis_sql."<p/>";
        $axis_result = run_query ($axis_sql, "Error pulling y_axis elements");
        $num_rows = num_rows ($axis_result);
        //echo "There are the following number of related table elements:".$num_rows."<br/>";
        if($num_rows > 0)
        {
            for($i = 0; $i < $num_rows; $i++)
            {
              $element_array = fetch_array ($axis_result, "Axis result", $i);
              $element_array['question_id'] = $mod_qid;
              
              //display_value ($element_array, "Element array:");
              /*foreach($element_array AS $key => $value) {
                echo $key." => ".$value."  ;  ";
              }
              echo "<br/>";*/
              
              $this->question_elements[$mod_qid][$x_or_y][$element_array['axis_id']] = $element_array;  
                //echo $this->question_elements[$question_id][$x_or_y][$element_array["axis_id"]]["axis_label"]."<br/>";
                //echo "[question id: ".$question_id."][x_or_y: ".$x_or_y."][axis_id: ".$element_array["axis_id"]."]<br/>";
            }
            //display_value($this->question_elements[$question_id]);
        }
        
        return $retrieve_axis_sql;
    }







	function questions_extract ($edit_or_display)
	{
		$retrieved_questions = run_query ($this->retrieve_questions_sql, "Questions Not Retrieved"); 
		//echo "<hr color=green>"; //echo "<font color=green>".$this->retrieve_questions_sql."</font><p/>";
        $questions_total_number = num_rows($retrieved_questions, "Could Not Count Num of Questions");
        //echo "The total number of questions is: ".$questions_total_number."</p>";
        
		//Pull out all of the relevant questions
		for ( $i = 0; $i < $questions_total_number; $i++ )
		{
            //echo $i." count of questions<br/>";
			//echo "Title is: ".$question["question_title"]."<p/>";
            $question = fetch_array ( $retrieved_questions, 'Could Not Fetch Array of Questions', $i );
			//echo "<b>fetch_array() questions: ".pretty_var_dump($question);
            
            //Join of multiple tables with conflicting names (quesiton_id) returns a NULL of title referenced array values in PostgreSQL
            $real_qid = $question[0];
            $mod_qid = $this->rp_id.$real_qid; 
            $question['question_id'] = $mod_qid;
            
            $this->questions[$mod_qid] = $question;
			
			//echo "<font color=green>this->questions:".pretty_var_dump($this->questions)."</font><p>";
			
			//If there are question elements, put them in an array
			if ( $question['question_type'] == 'checkboxes' || 
			     $question['question_type'] == 'radio' || 
			     $question['question_type'] == 'dropdown' )
			{
					$retrieve_question_elements_sql = "SELECT * FROM question_elements 
                    WHERE question_id = '".$real_qid."'";
					//echo $retrieve_question_elements_sql."<p/>";
                    $retrieved_question_elements = run_query ( $retrieve_question_elements_sql, 'Did Not Retrieve Question Elements' );
					$question_elements_total_number = num_rows ( $retrieved_question_elements );
					
					//Loop through the question elements
					for ( $b = 0; $b < $question_elements_total_number; $b++ )
					{
                        //echo $b." count of subelements<br/>";
						$question_element = fetch_array ( $retrieved_question_elements, 'Array of question elements', $b );
						$question_element['question_id'] = $mod_qid;
                        $this->question_elements[$mod_qid][$question_element['question_element_id']] = $question_element;
                        
                        
						//Debug
						//echo "&nbsp;&nbsp;".$question_elements[$i][$b]["question_element"]."<br>";
				    }
                    if ( $question['question_no_answer_options'] > 0 )
					{
                        $this->add_no_answer_elements ( $mod_qid, $question['question_no_answer_options'] );         
                        //echo $question['question_title'].' -- No answer option is'.$question['question_no_answer_options'].'<p>';
                    }
            }
            
            elseif ($question['question_type'] == 'table')
			{
                //First pull out the "table_profile" info and merge it into the "$this->questions" array
                $table_profile_sql = "SELECT * FROM table_profiles WHERE question_id = '".$real_qid."'";
                $table_profile_result = run_query ( $table_profile_sql, 'Table profile' );
                if ( num_rows( $table_profile_result ) > 0)
				{
                    $table_profile_array = fetch_array ( $table_profile_result, 'Pulling table profile', '0' );
                    $table_profile_array['question_id'] = $mod_qid;
                    $this->questions[$mod_qid] = array_merge ( $this->questions[$mod_qid], $table_profile_array );
                    
                    //Table questions are returned in the format: question_elements[question_id][x or y][axis_id][..axis_table_fields..]
                    //Example: $question_elements[110]["x"][2]["axis_label"]
                    
                    $x_sql = $this->pull_matching_table_elements ( $real_qid, $mod_qid, 'x' );
                    $y_sql = $this->pull_matching_table_elements ( $real_qid, $mod_qid, 'y' );
                }
            }
            
			//If the question has a default value defined by a function, denoted by "#", replace the "default value" 
            //with a dynamically generated default element number that relates to the default
			if ( substr ( $question['question_default_value'], 0, 1) == '#' && $edit_or_display != 'edit' )
			{
                //echo $question["question_default_value"]." is the dynamic default value<br/>";
				$this->questions[$mod_qid]['question_default_value'] =
                 $this->dynamic_default_element_value( $question['question_default_value'], $mod_qid );
			}
            
            
            //If a question special attribute starts with a "#," peform a special action
            if ( substr ( $question['question_special_attribute'], 0, 1 ) == '#' && $edit_or_display != 'edit' )
			{
                $this->dynamic_question_attribute ( $mod_qid, $question['question_special_attribute'] );
            }
        
	    }
        
		//echo "<font color=red>this->questions:".pretty_var_dump($this->questions)."</font><hr color=green><br>";
    }








    function dynamic_question_attribute ( $mod_qid, $question_special_attribute )
	{
        //GLOBAL $org_id;
		include_once($include_root."services_functions.inc");
        
        switch ( $question_special_attribute )  {
            
            case '#services_list':
                //echo "in services list<p>";
            
                //$this->questions["org_services"] = array ("question_id" => "99", "question_title" => $organization_term." Services:", "question_type" => "dropdown", "question_field_size" => "", "question_range_bottom" => "", "question_range_top" => "", "question_display_order" => "1019", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", "question_ecma" => "", "question_display_size" => "");

                $active_services = get_services_by_org_id_array ( $this->org_id );
                if ( $active_services ) {
                    foreach ( $active_services AS $current_service )  {

                        $this->question_elements[$mod_qid][$current_service['service_id']] = 
                         array ( 'question_element_id' =>
                          $current_service['service_id'], 'question_id' => $mod_qid, 'question_element_title' =>
                          $current_service['service_display_term'], 'question_element_value' => $current_service['service_id'] );
                    }
                }
            
                break;
            default:
                echo 'Unknown dynamic question attribute';
        
        
        }
    
    
    
    }


    
    function add_no_answer_elements ( $q_id, $no_answer_options ) {
        //echo "No answer option val is: ".$question_attributes['question_no_answer_options']."<p>";
        
        if ( $no_answer_options == 1 ) {
            $this->question_elements[$q_id]['dont_know'] = array ( 'question_element_id' => 'dont_know',
             'question_id' => $q_id, 'question_element_title' => "Don't know", 'question_element_value' => '#88#' );                 
        }        
        elseif ( $no_answer_options == 2 ) {
            $this->question_elements[$q_id]['not_app'] = array ( 'question_element_id' => 'not_app',
             'question_id' => $q_id, 'question_element_title' => 'Not applicable', 'question_element_value' => '#98#' );       
        }
        elseif ( $no_answer_options == 3 ) {
            $this->question_elements[$q_id]['dont_know'] = array ( 'question_element_id' => 'dont_know',
             'question_id' => $q_id, 'question_element_title' => "Don't know", 'question_element_value' => '#88#' );
             $this->question_elements[$q_id]['not_app'] = array ( 'question_element_id' => 'not_app',
             'question_id' => $q_id, 'question_element_title' => 'Not applicable', 'question_element_value' => '#98#' );       
        }
        elseif ( $no_answer_options == 4 ) {
            $this->question_elements[$q_id]['refused'] = array ( 'question_element_id' => 'refused',
             'question_id' => $q_id, 'question_element_title' => 'Refused', 'question_element_value' => '#99#' );
        }
        elseif ( $no_answer_options == 5 ) {
            $this->question_elements[$q_id]['dont_know'] = array ( 'question_element_id' => 'dont_know',
             'question_id' => $q_id, 'question_element_title' => "Don't know", 'question_element_value' => '#88#' );
            $this->question_elements[$q_id]['refused'] = array ( 'question_element_id' => 'refused',
             'question_id' => $q_id, 'question_element_title' => 'Refused', 'question_element_value' => '#99#' );            
        }
        elseif ( $no_answer_options == 6 ) {
            $this->question_elements[$q_id]['not_app'] = array ( 'question_element_id' => 'not_app',
             'question_id' => $q_id, 'question_element_title' => 'Not applicable', 'question_element_value' => '#98#' );
            $this->question_elements[$q_id]['refused'] = array ( 'question_element_id' => 'refused',
             'question_id' => $q_id, 'question_element_title' => 'Refused', 'question_element_value' => '#99#' );
        }
        elseif ( $no_answer_options == 7 ) {
            $this->question_elements[$q_id]['dont_know'] = array ( 'question_element_id' => 'dont_know',
             'question_id' => $q_id, 'question_element_title' => "Don't know", 'question_element_value' => '#88#' );
            $this->question_elements[$q_id]['not_app'] = array ( 'question_element_id' => 'not_app',
             'question_id' => $q_id, 'question_element_title' => 'Not applicable', 'question_element_value' => '#98#' );
            $this->question_elements[$q_id]['refused'] = array ( 'question_element_id' => 'refused',
             'question_id' => $q_id, 'question_element_title' => 'Refused', 'question_element_value' => '#99#' );
            
        }
     
    }
  
 }

?>
Return current item: Homeless Mangement Information System