<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */

/**
 * Represents a cell in a table survey
 * @package Survey
 * @author Jaime Irving Davila
 * @since 3-06-2002. 
 * @copyright Given to the public domain
 */

class SurveyField
{
  /** 
   * Name of the field in the survey
   * @var string*/
  var $name;
  /**
   * Options for the field of the Survey, could be an array of the form:
   * ["Descriptive answer 1" => "value_1", ....] or a fixed value like
   * "value_1", if this is the case it won't be presented like a selection
   * @var mixed */
  var $options; 
  /**
   * Value which indicates the selection of the field
   * @var string
  */
  var $is_fixed;

  /** 
   * The class constructor
   * @param string $my_name Name of the field
   * @param mixed $my_options Available options for the field
   * @param mixed $my_selection Actual selected option
   */
  function SurveyField ($my_name, $my_options, $my_selection, $is_input=false)
    {
      $this->name = $my_name;
      $this->options = $my_options;
      $this->selection = $my_selection;
      $this->is_fixed = !is_array($my_options);
      $this->is_input = $my_options=="";
    }

  function get_value ()
    {
      if ($this->is_fixed) 
	return $this->options;
      else 
	return $GLOBALS[$this->name];
    }

  /** 
   * Displays the cell on the HTML output
   */ 
  function display ()
    {
      print "<td align=\"center\">\n";
      /* We display the cell in case it's fixed */
      if ($this->is_input) {
	  print "<input type=\"text\" name=\"$this->name\" value=\"$this->selection\"></input>\n";
      }
      else if ($this->is_fixed) {
	print $this->options;
      } else
	{
	  /* We display the cell in case it's a selection */
	  print "<select name=\"$this->name\">\n";
	  foreach ($this->options as $longanswer=>$valanswer)
	    {
	      if ($this->selection != $valanswer)
		print "<option value=\"".$valanswer."\">";
	      else
		print "<option selected value=\"".$valanswer."\">";
	      print "$longanswer";
	      print "</option>\n";
	    }
	  print "</select>";
	}
      print "</td>\n";
    }

}
?>
