<h1> Insert Aktifitas Mahasiswa 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=3; $i<=$hasildata; $i++) // membaca data dimulai baris ke 3
{
		
		$id_sp1="id_sp='$id_sp'";
		
			$smt=trim($data->val($i,2));
		$record['id_smt']=$smt;			
			$n_smt="id_smt='$smt'";
			$result=$proxy->GetRecord($gettoken,'semester',$n_smt);
			$nm_smt=$result['result']['nm_smt'];
		$record['nm_smt']=$nm_smt;				
			$judul=trim($data->val($i,3));
		$record['judul_akt_mhs']=$judul;
			$lokasi=trim($data->val($i,4));
		$record['lokasi_kegiatan']=$lokasi;
			$sk_tgs= trim($data->val($i,5));
		$record['sk_tugas']=$sk_tgs;
			$tgl_sk=trim($data->val($i,6));
		$record['tgl_sk_tugas']=$tgl_sk;
			$ket=trim($data->val($i,7));
		$record['ket_akt']=$ket;
			$komunal=trim($data->val($i,8));
		$record['a_komunal']=$komunal;
			$jns_akt=trim($data->val($i,9));
		$record['id_jns_akt_mhs']=$jns_akt;
			$kd_prodi=trim($data->val($i,10));
			$kode_prodi="kode_prodi ILIKE '%$kd_prodi%' AND $id_sp1";
			$filter=$kode_prodi;
			//echo $filter."<br>";
			$result=$proxy->GetRecord($gettoken,'sms',$filter);
			//var_dump($result);
			//$coba=$result['result'];
			$id_sms=$result['result']['id_sms'];
			$nm_lemb=$result['result']['nm_lemb'];
	
			$nipd=trim($data->val($i,11));
			$filt="nipd like '%$nipd%'";
			//echo "<br />filter : $filt";
			$result1=$proxy->GetRecord($gettoken,'mahasiswa_pt',$filt);
			$id_reg_pd=$result1['result']['id_reg_pd'];
			$nm_pd=$result1['result']['nm_pd'];
		$record['id_reg_pd']=$id_reg_pd;
		$record['nm_pd']=$nm_pd;	
		$record['nipd']=$nipd;	
	
			$jns_peran_mhs=trim($data->val($i,12));
			//echo "<br /> jenis peran :$jns_peran_mhs <br />";
		$record['jns_peran_mhs']=$jns_peran_mhs;
			
		$record['id_sms']=$id_sms;
		$record['nm_lemb']=$nm_lemb;
		
  		$records[]=$record; //data dikumpulkan pada array records
  
  }

  //print_r($records);

  $j_encode=json_encode($records);
  $akt_je=$j_encode;
  //print_r($akm_je);
  $j_decode=json_decode($j_encode,true);
  $akt_jd=$j_decode;
  
 switch($_GET['act']){
	
	case "import":
			
			$result=$proxy->InsertRecordset($gettoken,'aktivitas_mahasiswa',$akt_je);
		    //var_dump($result);
	break;
		  
	case "update":
	
	echo "ini case update";
	
//		for($i=0;$i<1;$i++){
//				foreach($j_decode as $row){
//					$key = array('id_smt' => $row['id_smt'],'id_reg_pd' => $row['id_reg_pd']);
//					$data = array('id_stat_mhs' => $row['id_stat_mhs'],
//					   			  		   'ips'=> $row['ips'],
//					   			       'sks_smt'=> $row['sks_smt'],
//									       'ipk'=> $row['ipk'],
//									 'sks_total'=> $row['sks_total']);
//					
//		
//					$updates[] = array('key'=>$key,'data'=>$data);
//					
//				$j_upd=json_encode($updates);
//				//echo "<br> json_update ".$j_upd;
//			$result = $proxy->UpdateRecordset($gettoken,'kuliah_mahasiswa',$j_upd);
//			//var_dump($result);
//				}
//		}
	
	
	break ;
	}
	$a=0;
	echo "<pre style='background-color:#1699C9'>";
	if ($result['result']) {
		//for ($i=3; $i<=$hasildata; $i++){
			foreach ($j_decode as $row){
				$a++;
				$prodi=$row['nm_lemb'];
				$judul=$row['judul_akt_mhs'];
				$nm_smt=$row['nm_smt'];
				//$i_r_pd=$row['id_reg_pd'];
//				$nm=$row['nm_pd'];
//				$npm=$row['nipd'];
//				$jns_peran_mhs=$row['jns_peran_mhs'];
				//echo "<br>".$prodi."-".$judul."-".$nm_smt;
				//$record1=array();
				
					$filter="judul_akt_mhs LIKE '%$judul%' AND nm_smt ='$nm_smt' And nm_lemb like '%$prodi%'";
					$result=$proxy->GetRecord($gettoken,'aktivitas_mahasiswa',$filter);
					$id_akt_mhs=$result['result']['id_akt_mhs'];
					
//				$rec=array();
//				$rec['id_reg_pd']=$i_r_pd;
//				$rec['id_akt_mhs']=$id_akt_mhs;
//				$rec['nipd']=$npm;
//				$rec['nm_pd']=$nm;
//				$rec['jns_peran_mhs']=$jns_peran_mhs;
//				$recs[]=$rec;
				
				// menambahkan per record
				//foreach ($recs as $record) {
					//$result=$proxy->InsertRecord($gettoken,'anggota_aktivitas_mahasiswa',json_encode($record));
					//var_dump($result);
				//}
				
				echo $a."."."Judul : ".$judul." | Semester :".$nm_smt." | Prodi :".$row['nm_lemb']."<br>";
				//echo "id_reg_pd :".$i_r_pd." | Id aktifitas :".$id_akt_mhs."| Nama :".$nm." | NPM : ".$npm."| Jenis peran :".$jns_peran_mhs."<br>";
			
				//if ($rec['id_akt_mhs'] !='' and $row['id_reg_pd'] !='') $sukses++;
				if($row['judul_akt_mhs'] !='' )$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>