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

include "SurveyField.php";

/**
 * Represents a table survey, with SurveyField on each cell
 * @author Jaime Irving Davila
 * @since 3-06-2002
 * Given to the public domain. 2003. No warranties. http://structio.sourceforge.net/sigue 
 **/

/**
 * The method wich will be used in the form
 * @var string */
define ("FORMMETHOD", "post");

/**
 * The name of the button of submit
 * @var string */               
define ("SUBMITBUT", "submit");

/**
 * The value in case of sucessful sending
 * @var string */
define ("SUBMITVAL", "Enviar");

/**
 * The prefix wich will be used for all the fields of the survey
 * @var string */
define ("PREFIELD", "question"); 

class TableSelSurvey
{
  /**
   * label wich will be presented before the survey 
   * @var string */
  var $label; 

  /**
   * Array of the form ["id0" => "Question0", ...], representing the questions
   * The keys of the array will be presented on the first row of the table 
   * @var array */
  var $questions;

  /**
   * Array of the form [0 => "Item0", ...], representing items
   * The values will be presented on the first column of the table
   * @var array */
  var $items;

  /**
   * Name of the php file wich will take control in case of submit 
   * @var string */
  var $control;   

  /** 
   * Double array, which contains the kind of cell
   * @var array */
  var $cells;
  
  /** 
   * The class constructor
   * @param string $my_label The label which will be presented before the survey
   * @param array $my_questions The array which contains the question
   * @param array $my_items  The array which contains the items
   * @param array $my_options An array wich contains the options
   * @param array $my_selection An array wich contains the selected option
   * @param string $mycontrol Name of the php file, in case of submit */
  function TableSelSurvey ($my_label, $my_questions, $my_items, $my_options, $my_selection, $my_control)
    {
      $this->label = $my_label;
      $this->control = $my_control;
      $this->items = $my_items;
      $this->questions = $my_questions;
      $this->selected = $my_selection;
      
      if (!$this->issubmit ())
	{
	  /* We init the cells in case of the first call */
      	  foreach ($my_options as $i => $question)
	    foreach ($question as $j => $item)
	    {
	      $field_name = PREFIELD."['".$i."']['".$j."']";	      
	      $sel=isset($this->selected[$i][$j]) ?
	      	$this->selected[$i][$j] : '';
	      $this->cells[$i][$j] = new SurveyField ($field_name,
						      $item,$sel);
	    }
	}
      else
	{
	  /* We set the value of the cells, in case of a further call */
	  foreach ($my_options as $i => $question)
	    foreach ($question as $j => $item)
	    {
	      $field_name = PREFIELD."['".$i."']['".$j."']";
	      $this->cells[$i][$j] = new SurveyField ($field_name,
						      $item);
	    }
	} 
    }

  /**
   * Function wich prints the html code of the form of the survey */
  function display ()
    {

      /* We display the header and the ids of the answers */
      print "<h3>$this->label</h3>"; 
      print "<form method=\"".FORMMETHOD."\" action=\"".$this->control."\"";
      print "<table>\n";
      print "<tr>\n";
            $this->__display_id_questions();

      /* We display the table */
	if (is_array($this->cells)) {
      		foreach ($this->cells as $j => $item) {
	  		print "<tr>\n";
	  		$this->__display_name ($this->items[$j]);
	  		foreach ($item as $i => $question) {
	      			$my_cell = $question;
	      			$my_cell->display (); 
	    		}
	  		print "</tr>\n";
		}
	}
      print "</table>\n";
      print "<div align=\"center\">\n";
      print "<input type=\"submit\" name=\"".SUBMITBUT."\" value=\"".
	SUBMITVAL."\"></input>";	
      print "</div>\n";
      print "</form>\n";

      /* We display the questions on large format */
      $this->__display_questions();
    } 

  /**
   * Function which tells if the information of the form has been sent or no
   * @return boolean if the information has been sent returns true, else returns false */
  function issubmit () {
	  if (!isset($GLOBALS[SUBMITBUT]) || $GLOBALS[SUBMITBUT]!= SUBMITVAL)
		  return false;
	  else
		  return true;
  }
  
  /**
   * Function which returns the answer of a given question
   * @param string $my_question The id of the question
   * @param integer $my_item The key of the item
   * @return string The value of the answer */
  function get_answer ($my_question, $my_item)
    {
      $my_cell = $this->cells[$my_question][$my_item];
           
      return $my_cell->get_value();
    }

  /**
   * Private function which displays a given name in a table
   * @param $name The name wich will be displayed 
   * @access private */
  function __display_name ($name)
    {
      print "<th>";
      print "$name\n";
      print "</th>";
    }

  /**
   * Private function which displays the id's of the questions in a row
   * @acces private */
  function __display_id_questions ()
    {
      $i=1;
      print "<td></td>\n";
      foreach ($this->questions as $id => $question) 
	{
	  print "<th>\n";
	  $j=substr($id,strrpos($id,":")+1);
	  print "<a href=\"#$j\"><b>$i</b></a>";
	  print "</th>\n";
	  $i++;
	}
	print "<th>Fallas</th>";
	print "<th>Observaciones</th>";
      print "</tr>\n";
    }

  /**
   * Private function which displays the questions and their id's in a table 
   * @acces private*/
  function __display_questions ()
    {
      print "<table border=\"1\">\n";
      $i=1;
      foreach ($this->questions as $id => $question) 
	{
	  print "<tr>\n";
	  $j=substr($id,strrpos($id,":")+1);
	  print "<td><b>$i.</b> <a name=\"$j\">$j</a></td>\n";
	  print "<td>$question</td>\n";
	  print "</tr>\n";
	  $i++;
	}
      print "</table>\n";
    }

}
?>
