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

require "ecesar.php";

/** Runs external programs.  Escapes and puts each argument between '...'
    Author: Vladimir Támara
    Public domain. 2004. http://structio.sourceforge.net
*/
function runext($pgm,$args) {
	$cmd=$pgm;
	foreach($args as $par) {
		$pesc=escapeshellcmd($par); #str_replace("'","\'",$par);
		$cmd.=" '".$pesc."'";
	}
    if(($fh = popen($GLOBALS["AUTHPGM"], "w"))) { 
        $pass=des_ala_cesar($GLOBALS['PALABRA_SITIO'], $_SESSION['pass']);
        fputs($fh, $_SESSION['login']." ".$pass." $cmd"); 
        $r = pclose($fh); 
        if(!$r) {
            return true; 
        }
    } 
}


/**
 * This function looks into two bidimensional arrays, and find the differences 
 * returning an array suited for camval or insval of a .prc file
 * @param $program Program used to change valorations
 * @param $datepr Program used to change valorations
 * @param $pasteval The past valoration
 * @param $pathgrade Used to get the hierarchy of the .cla in the dir logro
 * @param $currenteval The current valoration
 * @param $period Time where the evaluation will be put
 * @param $subject name of the file with sequence of subject
 * @param $prc name of file with progress
 * @param $base_dir directory where the hierarchy archives reside
 * @return true if there are changes applied
 */

function ds ($program,$datepr,$pathgrade,$pasteval,$currenteval,$period,$subject,$prc, $base_dir) {
    $cantidad=0;
    $year=substr($period,1,strpos($period,"-")-1);
    $result=array();
    foreach ( $currenteval as $login => $arr2) {
         $isin=false;
         $mylog=str_replace("\\'","",$login);
         $mylog=str_replace("'","",$mylog);
         $info=explode(":",$mylog);
         $dirstud=substr($info[2],0,strrpos($info[2],"/"));
         foreach ( $arr2 as $ind => $valor) {
             $textoin=str_replace("\\'","",$ind);
             $textoin=str_replace("'","",$textoin);
             $pev=isset($pasteval[$mylog][$textoin]) ? 
                $pasteval[$mylog][$textoin] : 0;

             if ($valor!=$pev) {
                 if (strcmp($textoin,"Fallas")==0 
                    || strcmp($textoin,"Observaciones")==0) {
                     $dircla=substr($subject,0,strrpos($subject,".")).".cla";
                     if ($valor=="") {
                         $valor=" ";
                     }
                     error_log("sigue ".date()." Antes de runext $datepr, $dirstud, $prc, $textoin, $period, $valor");
                     runext($datepr,array("$base_dir/planest/$year/$dirstud/$prc",$textoin,$period,$valor));
                     error_log("sigue ".date()." Antes de runext");
                 }
                 else {
                     if (!$isin) {
                         $isin=true;
                         $dircla=substr($subject,0,strrpos($subject,".")).".cla";
                         $result[$mylog]="$program $base_dir/planest/$year/$dirstud/$prc";
                         $pgm[$mylog]="$program";
                         $parpgm[$mylog][]="$base_dir/planest/$year/$dirstud/$prc";
                     }
                     $t=substr($textoin,strrpos($textoin,":")+1);
                     $result[$mylog].=" ".$dircla." \"".$t."\" \"".$period."\" \"".$valor."\" ";
                     $parpgm[$mylog][]=$dircla;
                     $parpgm[$mylog][]=$t;
                     $parpgm[$mylog][]=$period;
                     $parpgm[$mylog][]=$valor;
                }
	            $cantidad++;
            }
        }
    }
    if(count ($result)>0) {
        foreach ($pgm as $mylog => $npgm) {
            runext($npgm,$parpgm[$mylog]);
        }
    }
    return $cantidad;
}

?>
