<html>
<head>
</head>
<body>
<h1> Insert Matakuliah to Feeder </h1>
         
 <!-- Database sisfo -->
<?php
//include 'session.php';
include "module/excel_reader2.php";
@$data = new Spreadsheet_Excel_Reader($_FILES['fileexcel']['tmp_name']);
@$hasildata = $data->rowcount($sheet_index=0);
$sukses = 0;
$gagal = 0;

for ($i=2; $i<=$hasildata; $i++) // membaca data dimulai baris ke 2
{
  //echo "<br>";
  //echo "id_sp='".$id_sp."'<br>";
  $id_sp1="id_sp='".$id_sp."'";
  //echo $id_sp1."<br>";
  $prodi=trim($data->val($i,2));      
  //echo "kode prodi : ".trim($data->val($i,2)."<br>";
  $kode_prodi="kode_prodi ILIKE '%$prodi%'";
  $filter=$kode_prodi." AND ".$id_sp1;
  //echo $filter."<br>";
  //$tbl_sms='sms';
  //$result=$proxy->GetRecord($gettoken,$tbl_sms,$kode_prodi);
  $result=$proxy->GetRecord($gettoken,'sms',$filter);
  //var_dump($result);
  //$coba=$result['result'];
  $id_sms=$result['result']['id_sms'];
  $jen=$result['result']['id_jenj_didik']; 
  //echo "id_sms = '".$id_sms."'<br>";
  //echo "id_jenj_didik = '".$jen."'<br>";
   //$sks=trim($data->val($i,1);  
   $tm=trim($data->val($i,7));  
   $prak=trim($data->val($i,8));  
   $lap=trim($data->val($i,9));
   $a_sap=trim($data->val($i,11));
   $a_silabus=trim($data->val($i,12));
   $a_bhn_ajar=trim($data->val($i,13));
   $acara_prak=trim($data->val($i,14));
   $a_diktat=trim($data->val($i,14));
  $record['id_sms']=$id_sms;
  $record['id_jenj_didik']=$jen;
  $record['kode_mk']=trim($data->val($i,3));                       // inti
  $record['nm_mk']= trim($data->val($i,4));                         // inti
  $record['jns_mk']= trim($data->val($i,6));                       // inti
  $record['sks_mk']=(integer)$tm+(integer)$prak;
  $record['sks_tm']=$tm;
  $record['sks_prak']=$prak;
  $record['sks_prak_lap']=$lap;
  $record['a_sap']=$a_sap;
  $record['a_silabus']=$a_silabus;
  $record['a_bhn_ajar']=$a_bhn_ajar;
  $record['acara_prak']=$acara_prak;
  $record['a_diktat']=$a_diktat;
  
  //$record['tgl_mulai_efektif']='2014-11-21';
  //$record['tgl_akhir_efektif']='2018-11-21';

  $records[]=$record; //data dikumpulkan pada array records
  
  }

  //print_r($records);

  $j_encode=json_encode($records);
  $makul_json=$j_encode;
  //echo "SKS TOTAL= ".$sks_lls;
  //print_r($makul_json);
  $j_decode=json_decode($j_encode,true);
  $makul_jsdec=$j_decode;
  //print_r($makul_jsdec);
  $result=$proxy->InsertRecordset($gettoken,'mata_kuliah',$makul_json);
  //print_r($result);
  //var_dump($result);
 echo "<pre style='background-color:#1699C9'>";
 if ($result['result']) {
  //for ($i=2; $i<=$hasildata; $i++){
   foreach ($j_decode as $row){
	   $kd_mk=$row['kode_mk'];
	   $filt="kode_mk ILIKE '%$kd_mk%'";
	   $result=$proxy->GetRecord($gettoken,'mata_kuliah',$filt);
	$id_mk=$result['result']['id_mk'];
    $jenjang=$row['id_jenj_didik'];
    $sks=$row['sks_mk'];
    
    echo "<br>Jenjang : ".$jenjang." | Kode MK : ".$kd_mk." | SKS = ".$sks;
    
    if ($id_mk !='') $sukses++;
    else $gagal++;
   }
  //}
 }
 echo "</pre>";

echo "<b>import data selesai.</b> <br>";
echo "Data yang berhasil di import : " . $sukses .  "<br>";
echo "Data yang gagal diimport : ".$gagal .  "<br>";
echo "kembali ke <a href='?module=home'>depan</a>";
?>

</tr>
</tr>
</body>
</html>