survsimp_question_display

one of the documented procedures in this installation of the ACS
Usage:
survsimp_question_display   db   question_id   { edit_previous_response_p "f" }
What it does:
Returns a string of HTML to display for a question, suitable for embedding in a form. The form variable is of the form "response_to_question_$question_id.
Defined in: /web/philip/tcl/survey-simple-defs.tcl

Source code:


    set element_name "response_to_question_$question_id"

    set selection [ns_db 1row $db "select * from survsimp_questions where question_id = $question_id"]
    set_variables_after_query

    set html $question_text
    if { $presentation_alignment == "below" } {
	append html "<br>"
    } else {
	append html " "
    }

    set user_value ""

    if {$edit_previous_response_p == "t"} {
 	set user_id [ad_get_user_id]

 	set prev_response_query "select	
	  choice_id,
	  boolean_answer,
	  clob_answer,
	  number_answer,
 	  varchar_answer,
	  date_answer
   	  from survsimp_question_responses
 	  where question_id = $question_id
             and response_id = (select max(response_id) from survsimp_responses r, survsimp_questions q
   	                       where q.question_id = $question_id
                       	         and r.user_id = $user_id
 	                         and q.survey_id = r.survey_id)"

	set selection [ns_db select $db $prev_response_query]
    
        set count 0
        while {[ns_db getrow $db $selection]} {
	    set_variables_after_query
	    incr count
	    
	    if {$presentation_type == "checkbox"} {
		set selected_choices($choice_id) "t"
	    }
	}

	if {$count == 0} {
	    set choice_id 0
	    set boolean_answer ""
	    set clob_answer ""
	    set number_answer ""
	    set varchar_answer ""
	    set date_answer ""
	}
    }

    switch -- $presentation_type {
	"textbox" {
	    if {$edit_previous_response_p == "t"} {
		if {$abstract_data_type == "number" || $abstract_data_type == "integer"} {
		    set user_value $number_answer
		} else {
		    set user_value $varchar_answer
		}
	    }

	    append html "<input type=text name=$element_name value=\"[philg_quote_double_quotes $user_value]\" [ad_decode $presentation_options "large" "size=70" "medium" "size=40" "size=10"]>"
	}
	"textarea" {
	    if {$edit_previous_response_p == "t"} {
		set user_value $clob_answer
	    }

	    append html "<textarea name=$element_name $presentation_options>$user_value</textarea>" 
	}
	"date" {
	    if {$edit_previous_response_p == "t"} {
		set user_value $date_answer
	    }

	    append html "[ad_dateentrywidget $element_name $user_value]" 
	}
	"select" {
	    if { $abstract_data_type == "boolean" } {
		if {$edit_previous_response_p == "t"} {
		    set user_value $boolean_answer
		}

		append html "<select name=$element_name>
 <option value=\"\">Select One</option>
 <option value=\"t\" [ad_decode $user_value "t" "selected" ""]>True</option>
 <option value=\"f\" [ad_decode $user_value "f" "selected" ""]>False</option>
</select>
"
	    } else {
		if {$edit_previous_response_p == "t"} {
		    set user_value $choice_id
		}

		append html "<select name=$element_name>
<option value=\"\">Select One</option>\n"
		set selection [ns_db select $db "select choice_id, label
from survsimp_question_choices
where question_id = $question_id
order by sort_order"]
		
		while { [ns_db getrow $db $selection] } {
		    set_variables_after_query
		    if { $user_value == $choice_id } {
			append html "<option value=$choice_id selected>$label</option>\n"
		    } else {
			append html "<option value=$choice_id>$label</option>\n"
		    }
		}
		append html "</select>"
	    }
	}
    
	"radio" {
	    if { $abstract_data_type == "boolean" } {
		if {$edit_previous_response_p == "t"} {
		    set user_value $boolean_answer
		}

		set choices [list "<input type=radio name=$element_name value=t [ad_decode $user_value "t" "checked" ""]> True"  "<input type=radio name=$element_name value=f [ad_decode $user_value "f" "checked" ""]> False"]
	    } else {
		if {$edit_previous_response_p == "t"} {
		    set user_value $choice_id
		}
		
		set choices [list]
		set selection [ns_db select $db "select choice_id, label
from survsimp_question_choices
where question_id = $question_id
order by sort_order"]
		while { [ns_db getrow $db $selection] } {
		    set_variables_after_query
		    if { $user_value == $choice_id } {
			lappend choices "<input type=radio name=$element_name value=$choice_id checked> $label"
		    } else {
			lappend choices "<input type=radio name=$element_name value=$choice_id> $label"
		    }
		}
	    }  
	    if { $presentation_alignment == "beside" } {
		append html [join $choices " "]
	    } else {
		append html "<blockquote>\n[join $choices "<br>\n"]\n</blockquote>"
	    }
	}

	"checkbox" {
	    set choices [list]
	    set selection [ns_db select $db "select * from survsimp_question_choices
where question_id = $question_id
order by sort_order"]
	    while { [ns_db getrow $db $selection] } {
		set_variables_after_query

		if { [info exists selected_choices($choice_id)] } {
		    lappend choices "<input type=checkbox name=$element_name value=$choice_id checked> $label"
		} else {
		    lappend choices "<input type=checkbox name=$element_name value=$choice_id> $label"
		}
	    }
	    if { $presentation_alignment == "beside" } {
		append html [join $choices " "]
	    } else {
		append html "<blockquote>\n[join $choices "<br>\n"]\n</blockquote>"
	    }
	}
    }
    return $html


philg@mit.edu