/////////////////////////////////////////////
//
// Distribution of "home" local grades among students with "host" local grades.
//
// Written in Matlab by Per Warfvinge per.warfvinge@chemeng.lth.se
// Translated into PHP by Karim Andersson karim.andersson@kansli.lth.se
// Lund University, LTH (Faculty of Engineering)
//
// Web (Swedish): http://www.ceq.lth.se/ECTScalc/
// Web (English): http://www.ceq.lth.se/ECTScalc/?lang=en
//
//////////////////////////////////////////////


$ECTSmatrix = ectsdistribution($Cdata,$Ldata,$Pdata);

function ectsdistribution($Cdata,$Ldata,$Pdata) {
  // $Cdata = array of number of students in each "host" local grade cohort
  // $Ldata = array of long term percentages for each "host" local grade, the sum should be 100.
  // $Pdata = array of long term percentages for each "home" local grade, the sum should be 100.
  
  $N=array_sum($Cdata);
  $n=count($Cdata);       // $n= number of cohorts in "host" grading scale
  $np=count($Pdata);      // $np= number of steps in "home" grading scale
  
  for ($i=0;$i<$n;$i++) {
    // $C($i) = current percentiles
    $C[$i]=0;
    foreach (range(0, $i) as $j) {
      $C[$i]= $C[$i]+$Cdata[$j];
    }
    $C[$i]= $C[$i]/$N;
    
    // $L($i) = long term percentiles "host"
    $L[$i]=0;
      foreach (range(0, $i) as $j) {
        $L[$i]= $L[$i]+$Ldata[$j];
      }
    $L[$i]= $L[$i]/100;
  }
  
  for ($i=0;$i<$np-1;$i++) {
    
    // $P($i) = long term percentiles "home"
    $P[$i]=0;
      foreach (range(0, $i) as $j) {
        $P[$i]= $P[$i]+$Pdata[$j];
      }
    $P[$i]= $P[$i]/100;
  }
  
  
  // mapping of current data on long-term data
  for ($i=0;$i<($np-1);$i++) {
    $Q[$i]=percentiles($P[$i],$C,$L)+0.0000001; 
  }
  $Q[$np-1]=1.0;
  
  $ECTS[0]=floor(($N+0.5)*$Q[0]);
  for ($i=1;$i<$np;$i++) {
    $ECTS[$i]=($N+0.5)*$Q[$i];
    foreach (range(0, ($i-1)) as $j) {
      $ECTS[$i]= $ECTS[$i] - $ECTS[$j];
    }
    $ECTS[$i]=floor($ECTS[$i]);
    
  }
  
  // spreading out of the students on the ECTS-grades
  for ($j=0;$j<$n;$j++) {
    for ($i=0;$i<$np;$i++) {
      $ECTSmatrix[$i][$j]=min(array($ECTS[$i],$Cdata[$j])); // Number in current group j that should have ECTS-grade i
      $Cdata[$j]=$Cdata[$j]-$ECTSmatrix[$i][$j]; // Reduce the current group j with those that got  ECTS-grade i
      $ECTS[$i]=$ECTS[$i]-$ECTSmatrix[$i][$j];
    }
  }
  //ECTSmatrix is the output from the function
  return $ECTSmatrix;

}

function percentiles($P,$C,$L) {
  // Calculate the normal grade index corresponding to an ECTS-percentile
  $n=count($C);
  
  for ($i=0 ; $i<$n ; $i++) {
    //find right segment index "seg" by checking if P>L(i)
    if ($P < $L[$i]) {
      $seg = $i+1;
      break;
    }
  }
  
  if ($seg<2) {
    $j=$P/$L[0];
  }
  else {
    $j=($P-($seg*$L[$seg-2]-($seg-1)*$L[$seg-1]))/($L[$seg-1]-$L[$seg-2]);
  }
  //Calculuate the corresponding percentile within the current student group
  $js=floor($j);
  if ($js<1) {
    $Q=$j*$C[0];
  }
  else {
    $Q=$j*($C[$js]-$C[$js-1])+(1+$js)*$C[$js-1]-($js)*$C[$js];
  }
  return $Q;
}