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

/**
 * Extracts logros and indicadores of a .cla file
 * @package sigue 
 * @author Vladimir Támara (based on SubjectExtract.php)
 * @since 30-01-2004.
 * Given to the public domain. No warranty. 2004. http://structio.sourceforge.net
 */


class LogIndExtract {

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

    /**  xmlparser */
    var $parser;

    /**  array of "logros" with ID as key
    * @var array
    */
    var $logros;

    /**  array of "indicadores" with [id_logro][id_ind] as keys 
    * @var array
    */
    var $indicadores;

    /**  array of "recuperables" with [id_logro][id_ind] as keys 
    * @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;

    /** 
    * 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 LogIndExtract($base_dir, $my_year, $file)
    {
        $this->cla=$file;
        $this->logros=array();
        $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=substr($attrs['ID'],
                    strrpos($attrs['ID'],'.')+1);
                $this->in_indicador=true;
            }
        }
        if (strcmp($name,'ANOTA')==0) {
            if ($this->in_indicador 
                && strcmp($attrs['TIPO'],'Recuperable')==0
                && strcmp($attrs['VALOR'],'no')==0) {
                $this->recuperables[$this->i_logro][$this->i_indicador]=false;
            }
        }
        if (strcmp($name,'DESC')==0){
            $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) {
            if ($this->in_indicador==false && $this->in_logro==true) {
                if (!isset($this->logros[$this->i_logro])) {
                    $this->logros[$this->i_logro]='';
                }
                //$this->logros[$this->i_logro].=trim($data);
                $this->logros[$this->i_logro].=$data;
            }
            if ($this->in_indicador==true) {

                if (!isset($this->indicadores[$this->i_logro][$this->i_indicador])) {
                    $this->indicadores[$this->i_logro][$this->i_indicador]='';
                }
                //$this->indicadores[$this->i_logro][$this->i_indicador].=trim($data);
                $this->indicadores[$this->i_logro][$this->i_indicador].=$data;
                $this->recuperables[$this->i_logro][$this->i_indicador]=true;
            }
            #	  $this->in_desc=false;
        }
    }

    /**
    * 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;
                if (isset($this->indicadores[$this->i_logro][$this->i_indicador])) {
                    $this->indicadores[$this->i_logro][$this->i_indicador]=
                    trim($this->indicadores[$this->i_logro][$this->i_indicador]);
                }
            }
            else if($this->in_logro) {
                $this->in_logro=false;
                if (isset($this->logros[$this->i_logro])) {
                    $this->logros[$this->i_logro]=
                    trim($this->logros[$this->i_logro]);
                }
           }
        }
        if (strcmp($name,'DESC')==0) {
            $this->in_desc=false;
        }
    }

    /**
    * Function that prints html code of logros and indicadores
    */
    function display () 
    {
        echo "<table border=\"1\">";
        foreach ($this->logros as $il => $logro) {
            print "<tr><td>$il</td><td>$logro</td></tr>\n";
            echo "<tr><td></td><td><table>";
            foreach ($this->indicadores[$il] as $ii => $ind) {
                echo "<tr><td>$ii :<b>$ind</b>\n";
            }
            echo "</table></td></tr>";	
        }
        echo "</table>";
    }

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

    function indicadores() 
    {
        return $this->indicadores;
    }  

    function recuperables() 
    {
        return $this->recuperables;
    }  

}


?>
