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

/**
 * Extract "valoraciones" from students of a grade in specific subject
 * and year in sigue 
 * @package sigue 
 * @author Igor Támara 
 * @since 5-06-2002.  
 * Given to the public domain. No warranties. 2003. http://structio.sourceforge.net/sigue
 */
class SecPeriod {
  /**
   * Make the class work with xml, see function.xml-set-object.html for doc
   * @var xmlparser
   */
  var $parser;

  /**
   * string of ids of "indicadores" of current period
   * @var array
   */
  var $actual;

  /**
   * array of ids of "indicadores" not of current period
   * @var array
   */
  var $values;

  /**
   * array [id_logro][id_ind] => periodo
   * @var array
   */
  var $logindper;


   /** Default file with classifications */
   var $cla;

 /** 
  * The class constructor
  * @param string $file name of the file to be parsed
  * @param string $my_periodo period to be extracred
  * califications
  */
  function SecPeriod($file, $mycla) {
	$this->cla=$mycla;
    $this->values=array();
    $this->parser = xml_parser_create("ISO-8859-1");
    $this->in_cp=false;

      xml_set_object($this->parser, &$this);
      xml_set_element_handler($this->parser, "tag_open", "tag_close");
      if (!($fp = fopen("$file", "r"))) {
	die("El archivo ".$file." 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,'SEC')==0&&strcmp($attrs['TIPO'],'Periodo')==0) {
	$this->actual=$attrs['ID'];
      }
      if (strcmp($name,'REFCLA')==0&&strcmp($attrs['TIPO'],'Indicador')==0) {
	$i=$attrs['IDCLA'];
	$r=strrpos($i,'.');
	$il=substr($i,0,$r);
	$ii=substr($i,$r+1);
	if (strrpos($i,":")===false) {
		$i=$this->cla.":".$i;
	}
	$this->values[$this->actual][]=$i;
	$this->logindper[$il][$ii][]=$this->actual;
      }	
    }
  
  /**
   * 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)
    {
    }
  
  /**
   * Function that prints the html code of the students 
   */
  function display () 
    {
      foreach ($this->values as $per => $indicadores) {
	foreach ($indicadores as $key => $valor) {
	  print "<tr><td>[$per][$key]:<b>$valor</b>\n";
	}
      }
    }

  /**
   * This function returns the Associate array that contains the evaluations
   * associated by the ids of the "Logro"
   */
  function values() 
    {
      return $this->values;
    }  
  /**
   * Returns array [id_logro][id_ind] => [periodo]
   */
  function logindper() 
    {
      return $this->logindper;
    }  

} // end of class xml

?>
