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

/** 
 * This class is used to extract the last evaluations from a file of
 * an student of the project sigue.  This class requires the xml
 * parser functions in PHP4 described in the PHP Manual
 * @package sigue 
 */

/**
 * Extract "valoraciones" from students in sigue
 * @package sigue 
 * @author Igor Támara
 * @since 5-06-2002.
 * @copyright Given to the public domain
 */


class StudentEval  {

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

  /**
   * to fill the text of "indicadores" associated to their ids
   * @var boolean
   */
  var $in_indicador;
  
  /**
   * Array that contains the "valoraciones" indexed by the ids
   * @var array 
   */
  var $values;

  /**
   * Array that contains the "periodos" indexed by the ids
   * @var array 
   */
  var $perval;


   /**
   * Variable to control the associative array. For optimization. Just
   * one pass through the tree.
   * @var array */
  var $id;
  
  /**
   * Variable to control the period of time being evaluated.
   * @var string
   */
  var $periodo;

  /**
   * Period of the indicadores
   * @var string
   */
  var $perind;

  /**
   * Variable to control the period of time being evaluated.
   * @var string
   */
  var $choices;


  /**
   * Holds the inassistence
   * @var int
   */
  var $fallas;

  /**
   * Indicates if there is an evaluation of fallas.
   * @var int
   */
  var $infallas;

  /**
   * Holds the observations
   * @var int
   */
  var $observaciones;

  /**
   * Indicates if there is an evaluation of observaciones
   * @var int
   */
  var $inobservaciones;


  /**
   * Ids of Indicadores to evaluate
   * @var array
   */
  var $indicadores;

  /**
   * Flags that indicate if indicadores are or not recoverable
   * @var array
   */
  var $recuperables;

   /** File to open */ 
  var $file;



 /** 
  * The class constructor
  * @param string $file FileName to retrieve
  * @param string $my_periodo time to be evaluated
  * @param Arrey $replace associative array to replace "valoraciones"
  */
  function StudentEval($file,$my_periodo,$replace,$id_indicadores, $recuperables=array(), $perind="")
    {
      $this->file=$file;
      $this->names=array();
      $this->values=array();
      $this->perval=array();
      $this->periodo=$this->perind=$my_periodo;
      if ($perind!="") {
        $this->perind=$perind;
      }
      $this->parser = xml_parser_create("ISO-8859-1");
      $this->in_indicador=false;
      $this->choices=$replace;
      $this->indicadores=$id_indicadores;
      $this->recuperables=$recuperables;
      $this->infallas=false;
      $this->observaciones="";
      $this->inobservaciones=false;
      $this->fallas=0;

      xml_set_object($this->parser, &$this);
      xml_set_element_handler($this->parser, "tag_open", "tag_close");
      xml_set_character_data_handler($this->parser, "cdata");
      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,'ANOTA')==0&&strcmp($attrs['TIPO'],'Fallas')==0&&strcmp($attrs['VALOR'],$this->periodo)==0) {
	$this->infallas=true;
      }
      if (strcmp($name,'ANOTA')==0&&strcmp($attrs['TIPO'],'Observaciones')==0&&strcmp($attrs['VALOR'],$this->periodo)==0) {
	$this->inobservaciones=true;
      }
      
      if (strcmp($name,'PROGCLA')==0) {
	$f=$attrs['IDCLA'];
	if(is_array($this->indicadores) && in_array($f,$this->indicadores)) {
	  $this->id=$f;
	}
	else {
	  $this->id=null;
	}
      }
      if ($name=='VAL' && $this->id!=null) {
        $llena=false;
      	if (!isset($this->recuperables[$this->id]) || 
		$this->recuperables[$this->id]) {
		if (!isset($this->values[$this->id]) ||
			( /* $this->perval[$this->id]!=$this->periodo &&  */
			$attrs['PERIODO']>$this->perval[$this->id])) {
				$llena=true;
		}
	} else {
		# No recuperable
		if ($attrs['PERIODO']==$this->perind) {
			$llena=true;
		}
	}
	if ($llena) {
		$this->values[$this->id]=$attrs['CONCEPTO'];
	  	$this->perval[$this->id]=$attrs['PERIODO'];
	} 
      }

    }
  

  /**
   * 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->infallas==true) {
	$this->fallas=$data;
	$this->infallas=false;
      }
      if ($this->inobservaciones==true) {
	$this->observaciones.=$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,'PROGCLA')==0) {
            $this->id=null;
        }
        if (strcmp($name,'ANOTA')==0 && $this->inobservaciones) {
            $this->inobservaciones=false;
        }
    }
  
  /**
   * Function that prints the html code of the students 
   */
  function display () 
    {
      foreach ($this->values 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() 
    {
      $this->values['Fallas']=$this->fallas;
      $this->values['Observaciones']=$this->observaciones;
      return $this->values;
    }  
  /**
   * This function returns the Associate array that contains the evaluations
   * associated by the ids of the "Logro"
   */
  function valuesarray($peract) 
    {
      foreach($this->values as $key => $value) {
	if ($value=='I') {
		if ($this->perval[$key]<$peract &&
		(!isset($this->recuperables[$key]) || 
		  $this->recuperables[$key])) {
	  		$this->values[$key]=$this->choices;
		}
	}
      }
      return $this->values;
    }  
} // end of class xml

?>
