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

if (!isset($GLOBALS['DIRFUENTES'])) { 
    die("Debe incluir confcol.php");
}
require $GLOBALS['DIRFUENTES']."DataInd.php";

/**
 * Extracts grade and subject of a teacher.
 * @package sigue
 * @author Igor Támara, Vladimir Támara 
 * @since 31-12-2003.  
 * Public domain.  No warranties. http://structio.sourceforge.net/sigue
 */
class TeacherCourses {
   /** 
   * @var xmlparser
   */
  var $parser;


	/** @var last_tag_opened Name of last tag opened */
	var $last_tag_opened;

	/** @var dataind Name of file with data of teacher */
	var $dataind;
	
	var $idgrupo;
	var $refgrupo;
	var $descgrupo;
	var $idasig;
	var $refsectemas;
	var $prc;
	var $descasig;

	var $take_descgrupo;
	var $take_descasig;
  /**
   * course and subject comma-separated
   * @var array
   */
  var $data;

 /** 
   * @param string $my_year year being revised
   * @param string $my_dataind  file with data of teacher
   * @param string $base_dir name of the dir base where sigue lives.

  */
  function TeacherCourses($base_dir,$my_year,$my_dataind) {

      $this->data=array();
	$this->dataind=$my_dataind;
	$this->take_descgrupo='';
	$this->take_descasig='';
	$this->last_tag_opened='';

      	$cmd="find $base_dir/planest/$my_year -name \"*.planest\"";
	$f=`$cmd`;
	$setpl=explode("\n",$f);
	foreach ($setpl as $file) {
		if ($file=='') {
			continue;
		}
		$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");

      		if (!($fp = fopen($file, "r"))) {
			die("El plan de estudios ".$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,'CURSO')==0) {
		if (strcmp($attrs['TIPO'],"Grado")==0) {
			$this->refgrupo=$attrs['REFGRUPO'];
			$this->idgrupo=$attrs['ID'];
			$this->descgrupo='';
		}
		else {
			$this->refgrupo='';
			$this->idgrupo='';
			$this->descgrupo='';
		}
	}
	else if (strcmp($name,'DESC')==0 && 
		strcmp($this->last_tag_opened,'CURSO')==0 &&
		$this->refgrupo!='') {
		$this->take_descgrupo=1;
		$this->descgrupo='';
	}
	else if (strcmp($name,'ASIGNATURA')==0) {
		if (isset($attrs['REFMODERADOR']) &&
			$attrs['REFMODERADOR']==$this->dataind) {
			$this->idasig=$attrs['ID'];
			$this->refsectemas=$attrs['REFSECTEMAS'];
			$this->prc=$attrs['PRC'];
		}
		else {
			$this->idasig='';
			$this->refsectemas='';
			$this->prc='';
		}
	}
	else if (strcmp($name,'DESC')==0 &&
		strcmp($this->last_tag_opened,'ASIGNATURA')==0 &&
		$this->idasig!='') {
		$this->take_descasig=1;
		$this->descasig='';
	}
	$this->last_tag_opened=$name;
    }
  
  /**
   * 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 ($this->take_descgrupo!='') {
		$this->take_descgrupo='';
        }
        else if ($this->take_descasig!='') {
		$this->take_descasig='';
		$this->data[$this->idgrupo.
			":".$this->refgrupo.
			":".$this->descgrupo.
			":".$this->idasig.
			":".$this->refsectemas.
			":".$this->prc.
			":".$this->descasig]=$this->descgrupo.":".$this->descasig;
	}	
    }

  /**
   * 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->take_descgrupo!='') {
            $this->descgrupo .= $data;
        }
        else if ($this->take_descasig!='') {
            	$this->descasig .= $data;
	}
  }  
  
  /**
   * Function that prints the data of this class as HTML
   */
  function display () {
	foreach ($this->data as $key => $value) {
        	print "<tr><td>$key: $value\n";
	} 
  }

  /**
   * Returns data organized by constructor.
   */
  function values() 
    {
      return $this->data;
    }  
} ?>
