<?php
//include 'session.php';
include "module/excel_reader2.php";
@$data = new Spreadsheet_Excel_Reader($_FILES['fileexcel']['tmp_name']);
@$hasildata = $data->rowcount($sheet_index=0);
//echo $hasildata;
$sukses = 0;
$gagal = 0;
for ($i=3; $i<=$hasildata; $i++)
{
		/* mencari id_sdm dosen ybs */
		$nidn=trim($data->val($i,2));
		$nodos="nidn ILIKE '%$nidn%'";
		//echo "<br>".$nodos."<br>";
		$result=$proxy->GetRecord($gettoken,'dosen',$nodos);
		$id_sdm=$result['result']['id_sdm'];
		//echo "id_sdm ='".$id_sdm."'<br>";
		
		/*mencari id_reg_pt dosen yang bersangkutan  */
		$id_sdm_pt="p.id_sdm='".$id_sdm."'";
		//echo "<br>".$id_sdm_pt."<br>";
		$result=$proxy->GetRecord($gettoken,'dosen_pt',$id_sdm_pt);
		$id_reg_ptk=$result['result']['id_reg_ptk'];
		//echo "id_reg_ptk ='".$id_reg_ptk."<br>";
		
		
		$kd_mk=trim($data->val($i,4));
		$smt=trim($data->val($i,3));
		$kelas=trim($data->val($i,6));
		$filt="p.id_smt='".$smt."' AND nm_kls='".$kelas."' AND kode_mk='".$kd_mk."'";
		//echo $filt."<br>";
		$result=$proxy->GetRecord($gettoken,'kelas_kuliah',$filt);
		$id_kls=$result['result']['id_kls'];
		$sks_mk=$result['result']['sks_mk'];
		//echo "id kelas : ".$id_kls. "<br>";
	
	$record['id_kls'] = $id_kls; 
	$record['id_reg_ptk'] = $id_reg_ptk;
	  $sks_subst_tot= $data->val($i,7);
	  $sks_tm_subst=$data->val($i,8);
	  $sks_prak_subst= $data->val($i,9);
	  $sks_prak_lap_subst=$data->val($i,10);
	  $sks_sim_subst= $data->val($i,11);
	  $jml_tm_renc= $data->val($i,12);
	  $tot_subt=$sks_tm_subst+$sks_prak_subst+$sks_prak_lap_subst+$sks_sim_subst;
	  if($tot_subt==0){$sks_ajar=$sks_mk;} else {$sks_ajar=$tot_subt;}
	  //echo "Nilai =".$nilai."dan Indeks =".$indeks;
	$record['sks_mk'] = $sks_mk;   
	$record['id_jns_eval'] =1;	
	$record['sks_tm_subst']=(integer)$sks_tm_subst;
	$record['sks_prak_subst'] =(integer)$sks_prak_subst;
	$record['sks_prak_lap_subst'] =(integer)$sks_prak_lap_subst;
	$record['sks_sim_subst'] =(integer)$sks_sim_subst;
	$record['jml_tm_renc'] =(integer)$jml_tm_renc;
	$record['sks_subst_tot'] =$sks_ajar;
	
  	$records[]=$record;
	
}
	$je_ajar=json_encode($records);
	$jd_ajar=json_decode($je_ajar,true);
	//print_r($jd_ajar);
switch($_GET['act']){
	
	case "import":	
	
		$result=$proxy->InsertRecordset($gettoken,'ajar_dosen',$je_ajar);    // fix insert banyak records
		//var_dump($result);
	
	break;
		  
	case "update":
	
	for($i=0;$i<1;$i++){
				foreach($jd_ajar as $row){
					$key = array('id_kls' => $row['id_kls'],'id_reg_ptk' => $row['id_reg_ptk']);
					$data = array('sks_mk' => $row['sks_mk'],
					   		'sks_subst_tot'=> $row['sks_subst_tot'],
					   		 'sks_tm_subst'=> $row['sks_tm_subst'],
					   'sks_prak_lap_subst'=> $row['sks_prak_lap_subst'],
							'sks_sim_subst'=> $row['sks_sim_subst'],
							 'jml_tm_renc' => $row['jml_tm_renc']);
					
		
					$updates[] = array('key'=>$key,'data'=>$data);
					
				$j_upd=json_encode($updates);
				//echo "<br> json_update ".$j_upd;
			$result = $proxy->UpdateRecordset($gettoken,'ajar_dosen',$j_upd);
			//var_dump($result);
				}
		}
	
	break;	
}
	echo "<pre style='background-color:#1699C9'>";
	if ($result['result']) {
		for($i=0;$i<1;$i++){
			foreach($jd_ajar as $row){
				$kdmk=$kd_mk;
				$id_kls=$row['id_kls'];
				$id_reg_ptk=$row['id_reg_ptk'];
				$id_jns_eval=$row['id_jns_eval'];
			echo "<br>Id Kelas : ".$id_kls." | Id reg ptk : ".$id_reg_ptk." | Id Jenis Eval = ".$id_jns_eval;
			if ($id_kls !='') $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>";
?>
