<html>
<body>
<h1> Insert mk_kurikulum 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;
//echo $hasildata."<br>";
for ($i=2; $i<=$hasildata; $i++) // membaca data dimulai baris ke 2
{
		//echo "<br>";
		// mencari id_kurikulum_sp pada tabel kurikulum
		$nm_kur=trim($data->val($i,2));
		$kur="nm_kurikulum_sp ILIKE '%$nm_kur%'";
		//echo $kur."<br>";
		$result=$proxy->GetRecord($gettoken,'kurikulum',$kur);
		$id_kurikulum_sp=$result['result']['id_kurikulum_sp'];
		$id_sms=$result['result']['id_sms'];
		//echo "id kurikulum : $id_kurikulum_sp <br>";
		
		
		// mencari id_mk pada tabel mata_kuliah
		$kd_mk=trim($data->val($i,3)); 
		$kd="kode_mk ILIKE '%$kd_mk%' AND p.id_sms='$id_sms'";
		$result=$proxy->GetRecord($gettoken,'mata_kuliah',$kd);
		@$id_mk=$result['result']['id_mk'];
		@$sks_mk=$result['result']['sks_mk'];
		@$sks_tm=$result['result']['sks_tm'];
		@$sks_prak=$result['result']['sks_prak'];
		@$sks_prak_lap=$result['result']['sks_prak_lap'];
		@$sks_sim=$result['result']['sks_sim'];
		//echo "id MK : $id_mk <br>";
				
			$smt=$data->val($i,5); 	
			//$sks_tm=$data->val($i,7);
			//$sks_prak=$data->val($i,8);
			//$sks_prak_lap=$data->val($i,9);
			//$sks_sim=$data->val($i,10);
			//$sks_mk=$data->val($i,7)+$data->val($i,8);
			$a_wajib=$data->val($i,11);
  $record['kode_mk']=$kd_mk;		
  $record['id_kurikulum_sp']=$id_kurikulum_sp;
  $record['id_mk']=$id_mk;
  $record['smt']=$smt;                       // inti
  $record['sks_mk']= $sks_mk;                         // inti
  $record['sks_tm']=$sks_tm;
  $record['sks_prak']=$sks_prak;
  $record['sks_prak_lap']=$sks_prak_lap;
  $record['sks_sim']=$sks_sim;
  $record['a_wajib']=$a_wajib;
  
  //$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);
  $mk_kur_je=$j_encode;
  //echo "SKS TOTAL= ".$sks_lls;
  //print_r($mk_kur_je);
  $j_decode=json_decode($j_encode,true);
  $mk_kur_jd=$j_decode;
  //print_r($kel_kul_jsdec=$j_decode);
  /* kirim all */
  $result=$proxy->InsertRecordset($gettoken,'mata_kuliah_kurikulum',$mk_kur_je);
  //print_r($result);
  //var_dump($result);
    echo "<pre style='background-color:#1699C9'>";
	if ($result['result']) {
		$x=0;
		//for ($i=2; $i<=$hasildata; $i++){
			foreach ($j_decode as $row){
				$x++;
				$id_kurikulum_sp=$row['id_kurikulum_sp'];
				$id_mk=$row['id_mk'];
				$filter="id_mk='$id_mk' AND p.id_sms='$id_sms'";
				//echo "<br />".$filter;
					$mk=$proxy->GetRecord($gettoken,'mata_kuliah',$filter);
					@$ada_mk=$mk['result']['kode_mk'];
					$smt=$row['smt'];
					$filt1="p.id_mk='$id_mk' AND p.id_kurikulum_sp='$id_kurikulum_sp'";
					//echo "<br />".$filt1;
					$mk_kur=$proxy->GetRecord($gettoken,'mata_kuliah_kurikulum',$filt1);
					@$idmk=$mk_kur['result']['id_mk'];
					echo "<br />".@$idmk;
					@$idkur=$mk_kur['result']['id_kurikulum_sp'];
					
					if(@$idmk)$sukses++;
						else $gagal++;
						
					if($ada_mk =='') @$kode_mk= "<span style='color: #FFFF00'>".$row['kode_mk']." belum ada di tabel Matakuliah </span>";
					else $kode_mk=$ada_mk;
					
					@$na_mk=$mk['result']['nm_mk'];
					@$sks=$mk['result']['sks_mk'];
				
				echo "<br>$x.  ID Kurikulum : ".$id_kurikulum_sp." | Kode MK : ".$kode_mk." | Nama MK : ".$na_mk." | SMT = ".$smt." | SKS = ".$sks;
				
			}
		//}
	}
	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>
