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

/** 
 * This class is used to extract the Ids and the text of "Indicadores
 * This class requires the xml parser functions in PHP4 described 
 * in the PHP Manual
 * @package sigue 
 */

/**
 * Extract "indicadores" from .cla in sigue
 * @package sigue 
 * @author Igor Támara
 * @since 5-06-2002.
 * Given to the public domain. No warranty. 2003. http://structio.sourceforge.net
 */


class SubjectExtract  {

  /** Name of cla file */
  var $cla;

  /**
   * Make the class work with xml, see function.xml-set-object.html for doc
   * @var xmlparser
   */
  var $parser;

  /**
   * array of "indicadores" with ID as keys
   * @var array
   */
  var $indicadores;

  /**
   * array of flags "recuperable"
   * @var array
   */
  var $recuperables;


  /**
   * to know if an "indicador" is being visited
   * @var boolean
   */
  var $in_indicador;

  /**
   * to know if a "logro" is being visited
   * @var boolean
   */
  var $in_logro;

  /**
   * to fill the text of "indicadores" associated to their ids
   * @var boolean
   */
  var $in_desc;

  /**
   * holds the id of the "logro"
   * @var string
   */
  var $i_logro;

  /**
   * holds the id of the "indicador"
   * @var string
   */
  var $i_indicador;

   /**
   * Its value is true if there is an array to fill.
   * @var boolean 
   */
  var $complete;

   /**
   * Contains the ids of the "Indicadores" for which we want to obtain
   * the text.
   * @var Array
   */
  var $id_array;


 /** 
  * The class constructor
  * @param string $file FileName to retrieve
  * @param string $my_periodo time to be evaluated
  * @param array $my_ids optional parameter, if not set, becomes 1.
  */
  function SubjectExtract($base_dir, $my_year, $file,$my_ids=FALSE)
    {
      $this->cla=$file;
      if ($my_ids===FALSE) {
	$this->complete=false;
      }
      else {
	$this->complete=true;
	$this->id_array=$my_ids;
      }
      $this->indicadores=array();
      $this->recuperables=array();
      $this->in_indicador=false;
      $this->in_desc=false;
      $this->in_logro=false;

      $this->parser = xml_parser_create("ISO-8859-1");

      xml_set_object($this->parser, &$this);
      xml_set_element_handler($this->parser, "tag_open", "tag_close");
      xml_set_character_data_handler($this->parser, "cdata");
	$f="$base_dir/planest/$my_year/$file";
      if (!($fp = fopen("$f", "r"))) {
	die("El archivo de clasificación ".$f." no pudo ser leído");
      }
      while ($data = fread($fp, 4096)) {
	if (!xml_parse($this->parser, $data, feof($fp))) {
	  die(sprintf("XML error: %s en la línea %d, por favor revise ".$file,
		      xml_error_string(xml_get_error_code($this->parser)),
		      xml_get_current_line_number($this->parser)));
	}
      }
      xml_parser_free($this->parser);
      fclose($fp);
    }

  /**
   * function that process the start of an element
   * @param $parser xml parser
   * @param $name name of the element
   * @param $attrs attributes of the element 
   * @access private 
   */
  function tag_open($parser, $name, $attrs)
    { 
      
      
      if (strcmp($name,'CLASIF')==0) {
	if (strcmp($attrs['TIPO'],'Logro')==0) {
	  $this->i_logro=$attrs['ID'];
	  $this->in_logro=true;
	  $this->in_indicador=false;
	}
	if ($this->in_logro&&strcmp($attrs['TIPO'],'Indicador')==0) {
	  $this->i_indicador=$attrs['ID'];
	  $this->in_indicador=true;
	}
      }
      if (strcmp($name,'ANOTA')==0 && $this->in_indicador 
          && strcmp($attrs['TIPO'],'Recuperable')==0
          && strcmp($attrs['VALOR'],'no')==0) {
              $this->recuperables[$this->cla.":".$this->i_indicador]=false;
      }
      if (strcmp($name,'DESC')==0&&$this->in_indicador){
	$this->in_desc=true;
      }
    }
  
    /**
    * function that process the content of the element
    * @param $parser xml parser
    * @param $data ASCII data of the element
    * @access private 
    */  
    function cdata($parser, $data)
    {
        if ($this->in_desc==true&&$this->in_indicador==true) {
            $ni=$this->cla.":".$this->i_indicador;
            $this->recuperables[$ni]=true;
            if ($this->complete==false ||
            is_array($this->id_array) && in_array($ni,$this->id_array)) {
                if (!isset($this->indicadores[$ni])) {
                    $this->indicadores[$ni]='';
                }
		        $this->indicadores[$ni].=trim($data);
	        }
        }
    } 
  /**
   * function that process the end of an element
   * @param $parser xml parser
   * @param $name name of the element
   * @access private 
   */
  function tag_close($parser, $name)
    {
      if (strcmp($name,'CLASIF')==0) {
	if ($this->in_indicador && $this->in_logro) {
	  $this->in_indicador=false;
	}
	else if($this->in_logro) {
	  $this->in_logro=false;
	}
      }
      if (strcmp($name,'DESC')==0 && $this->in_indicador){
	$this->in_desc=false;
      }
    }
  
  /**
   * Function that prints the html code of the students 
   */
  function display () 
    {
      foreach ($this->indicadores as $key => $logro) {
	print "<tr><td>$key :<b>$logro</b>\n";
      }
    }

  /**
   * This function returns the Associate array that contains the evaluations
   * associated by the ids of the "Logro"
   */
  function values() 
    {
      return $this->indicadores;
    }  
}

?>
