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

require_once "SecPeriod.php";
require_once "GroupQuery.php";
require_once "SubjectExtract.php";
require_once "StudentEval.php";

/** 
 * This class is used to extract the "valoraciones" from a level in
 * a subject, according to a year.
 * @package sigue 
 */

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

class GroupValActual {
  /**
   * associative array with login and names of users
   * @var array
   */
  var $students;

  /**
   * associative array that has all of the ids and texts of the "indicadores"
   * @var array
   */
  var $questions;

  /**
   * associative matrix that has "valoraciones" according to questions and
   * students.
   * @var [array]
   */
  var $values;

  /**
   * Holds the actual "valoracion"
   * @var String
   */
  var $selected;

 /** 
  * The class constructor
  * @param string $my_sec sequence of subject to extract from
  * @param string $my_year year when the califications are evaluated
  * @param string $my_periodo time to be evaluated
  * @param string $my_grade group file of course
  */
  function GroupValActual($base_dir,$my_sec,$my_prc,$my_year,
    $my_periodo,$my_grade) {
      $auxiliar = new GroupQuery("$base_dir/planest/$my_year/$my_grade",$my_year);
      $this->students=$auxiliar->values();
      $r=strrpos($my_sec,".");
      if ($r<0 || $r>strlen($my_sec)) {
          die("Nombre de secuencia sin extensión '$my_sec'");
      }
      $my_cla=substr($my_sec,0,$r).".cla";
      $auxiliar = new SecPeriod("$base_dir/planest/$my_year/$my_sec",$my_cla);
      $aux2=$auxiliar->values();
      $auxiliar=new SubjectExtract($base_dir,$my_year,$my_cla,
              $aux2[$my_periodo]);
      $this->questions=$auxiliar->values();
      $this->values=array();
      /* Invariant: Arrays indexed by indicador will have as identifier:
file:indicador
where file is the file of classifications referenced with respect
to the directory base_dir/planest/2004. We suppose that the
reference in the PRC is exactly that one. */


      foreach ($this->questions as $val => $total) {
          $valores[$val]=$GLOBALS['ESCALA'];
      }
      for ($i=0;$i<25;$i++){
          $fallas[]=$i;
      }

      //We extract the valoraciones for each indicador
      $max=0; //Used to calculate the longest row
      foreach ($this->students as $datstud => $name) {
          $dlogin=explode(":",$datstud);
          $dirstudent=substr($dlogin[2],0,strrpos($dlogin[2],"/"));
          $gradesStudent = new StudentEval("$base_dir/planest/$my_year/".
            $dirstudent."/$my_prc",$my_periodo,
            $GLOBALS['ESCALA'],$aux2[$my_periodo]); 
          $this->selected[$datstud]=$gradesStudent->values();
          if (isset($this->values[$datstud]) &&
            count($this->values[$datstud])>$max) {
              $max=count($this->values[$datstud]);
          }
      }

      foreach ($this->students as $datstud => $name) {
          $this->values[$datstud]=$valores;
          $this->values[$datstud]['Fallas']=$fallas;
          $this->values[$datstud]['Observaciones']="";
      }
  }

  /**
   * Private function that displays the login and name of the students
   * of a grade in a year.
   * @acces private 
   */
  function __display__students() {
    foreach ($this->students as $login=>$name) {
      print "<b>$login</b>:$name<br>\n";
    }
  }

  /**
   * Private function that displays the id and text of "indicadores"
   * of a grade in a year.
   * @acces private 
   */
  function __display__questions() {
    foreach ($this->questions as $id=>$text) {
      print "<b>$id</b>:$text.<br>\n";
    }
  }

  /**
   * Private function that displays the login and name of the students
   * of a grade in a year.
   * @acces private 
   */
  function __display__matrix() {
    foreach ($this->values as $login=>$student) {
      print "<b>$login</b><br>";
      foreach ($student as $id => $texto) {
	print "<b>$id</b>:";
	if (is_array($texto)) {
	  foreach($texto as $key => $val) {
	    print "['$key']=$val ";
	  }
	}
	else {
	  print "es $texto";
	}
	print "{".$this->selected[$login][$id]."}<br>\n";
      }
      print "<br>\n";
    }
  }

  /**
   * Function that prints the html code of the group 
   */
  function display () 
    {
      print "<h3>Lista de Estudiantes</h3>";
      $this->__display__students();
      print "<h3>Lista de Valoraciones por Estudiante</h3>";
      $this->__display__matrix();
      print "<h3>Lista de Indicadores e IDs</h3>";
      $this->__display__questions();
    }

}
?>
