Skip to content
Home ยป Import mahasiswa dari file excel

Import mahasiswa dari file excel

  • by
Spread the love

Berikut adalah contoh source code untuk mengimport mahasiswa dari file excel

<?php

include "./bootstrap.php";

use \UniversitySDK\Entities\SPMBStudent;

class ImportStudentFromExcel{
	function localToDbDate($var){
		$date = str_replace('/', '-', $var);
		return date('Y-m-d', strtotime($date));
	}
	function logStatus($t){
		$f = fopen(__DIR__.'/InsertStudentFromExcel-live.log','a+');
		if($f){
			$now = new \DateTime();
			$now->setTimezone(new \DateTimeZone('Asia/Jakarta'));
			$time = $now->format('Y-m-d H:i:s');   
			$text = $time.': '.$t."\n";
			echo $text;			
			fwrite($f,$text);
			fclose($f);
		}
	}
	private function findNationality($nationalityText){
		$nationalityText = strtolower(trim($nationalityText));
		foreach($this->nationalities as $rl){
			if(strtolower($rl->name) == $nationalityText){
				return $rl;
			}
		}
		return NULL;
	}
	function findReligion($religionText){
		$religionText = strtolower($religionText);
		foreach($this->religions as $rl){
			if(strtolower($rl->name) == $religionText){
				return $rl;
			}
		}
		return NULL;
	}
	public function getDataSources(){
		return [
			[
				'file'=>'/1. Udah registrasi dari 295- S2 MPI-edit1.csv',
				'progID'=>1039
			],
			[
				'file'=>'/2. Udah registrasi dari 295- S2 HES-edit1.csv',
				'progID'=>1036
			],
			[
				'file'=>'/3. Udah registrasi dari 295- S2 IAT-edit1.csv',
				'progID'=>1038
			],
			[
				'file'=>'/4. Udah registrasi dari 295- S2 PBA-edit1.csv',
				'progID'=>1041
			],
			[
				'file'=>'/5. Udah registrasi dari 295- S2 PGMI-edit1.csv',
				'progID'=>1042
			],
			[
				'file'=>'/6. Udah registrasi dari 295- S2 PAI-edit1.csv',
				'progID'=>1040
			],
			[
				'file'=>'/7. Udah registrasi dari 295- S2 AFI-edit1.csv',
				'progID'=>1034
			],
			[
				'file'=>'/8. Udah registrasi dari 295- S2 ES-edit1.csv',
				'progID'=>1035
			],
			[
				'file'=>'/9. Udah registrasi dari 295- S2 HKI-edit1.csv',
				'progID'=>1037
			],
			[
				'file'=>'/10. Udah registrasi dari 295- S2 TBI-edit1.csv',
				'progID'=>1043
			],
			
			[
				'file'=>'/home/aldo/Downloads/Yang registrasi untuk siakad/S3 edit 1/1. Yang registrasi dari 295- S3 MPI Edit1.csv',
				'progID'=>1044
			],
			[
				'file'=>'/home/aldo/Downloads/Yang registrasi untuk siakad/S3 edit 1/2. Yang registrasi dari 295- S3 SI Edit1.csv',
				'progID'=>1045
			]
		];
	}
	public function run(){
		$this->admin = new \UniversitySDK\Admin;
		//$this->admin->setHost('http://university');
		$ret = $this->admin->login(1001,'[email protected]', 'J9iFt>L,(n7d@4d');
		if(!$ret){
			echo 'failed to login';
			return FALSE;
		}
		$this->religions = $this->admin->getAllReligions();
		//get all nationalities
		$this->nationalities = $this->admin->getAllNationalities();
		
		$srcs = $this->getDataSources();
		foreach($srcs as $src){
			$programID = $src['progID'];//
			$csvPath = $src['file'];
			
			$row = 1;
			if (($handle = fopen($csvPath, "r")) !== FALSE) {
			  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
				  if($row==1){
					  $row++;
					  continue;
				  }
				$std = new SPMBStudent();
					$std->UniversityID = 1001;
					$std->unitid = $programID;
					$std->ProgramID = $programID;
					//$std->fakultas = 1001;
					//$std->kode_prodi_sia = ;
					$std->no_ident = trim($data[5]);
					$std->nim = trim($data[3]);			
					$std->nama = trim($data[2]);
					$std->email = trim($data[1]);
					if(empty($std->email)){
						$std->email = $std->nim .'@universitas.ac.id';
					}
					$std->tmpt_lahir = strtoupper(trim($data[6]));
					$std->tgl_lahir = trim($this->localToDbDate($data[7]));
					$sex = 'P';
					if(trim($data[4])=='Laki - laki'){
						$sex = 'L';
					}
					$std->sex = $sex;  
					$std->blood = '';
					$now = new \DateTime();
					$now->setTimezone(new \DateTimeZone('Asia/Jakarta'));
					$time = $now->format('Y-m-d H:i:s');   
					$std->reg_date = $time;
					//all using area id
					//Kelurahan/Desa Tempat Tinggal 10
					$std->dsn = trim($data[10]);
					//Kecamatan Tempat Tinggal 11
					$kec = trim($data[11]);
					$kec = str_replace(['kec'],'',strtolower($kec));
					if($kec=='campurdarat'){
						$kec = 'campur darat';
					}
					if($kec=='pucanglaban'){
						$kec = 'pucang laban';
					}
					$subdistrict = $this->admin->findSubDistrict($kec);
					
					if($subdistrict==NULL){
						$this->logStatus('Can not find kec:'.$kec);
					}else{
						$std->kec = $subdistrict->ID;
					}
					//Kabupaten Tempat Tinggal 12
					$kab = str_replace(['kabupaten','kota'],'',strtolower(trim($data[12])));
					//exceptions
					if($kab=='kediri'){
						$kab = 'Kota Kediri';
					}
					$city = $this->admin->findCity($kab);
					if($city==NULL){
						$this->logStatus('Can not find kab:'.$kab);
					}else{
						$std->kab = $city->ID;
					}
					//Provinsi Tempat Tinggal 13
					$prop = trim($data[13]);
					if($prop=='JAWATIMUR'){
						$prop = 'JAWA TIMUR';
					}
					
					$prov = $this->admin->findProvince($prop);
					if($prov==NULL){
						$this->logStatus('Can not find prop:'.$prop);
					}else{
						$std->prov = $prov->ID;
					}
					
					$std->kpos = '-';
					$std->no_hp = '-';
					$neg = trim($data[9]);
					$neg = str_replace('WNI','Indonesia',$neg);
					$nationality = $this->findNationality($neg);
					if($nationality==NULL){
						$this->logStatus('negara tidak ditemukan: '.$neg);										
						$std->negara = 'ID';
					}else{
						//var_dump($nationality);
						$std->negara = $nationality->code;
					}				
					$ag = trim($data[8]);
					$rel = $this->findReligion($ag);
					if($rel==NULL){
						$this->logStatus('agama tidak ditemukan: '.$ag);										
						$std->id_agama = 1;
					}else{
						//var_dump($rel)
						$std->id_agama = $rel->ID;
					}
					$std->ayah = trim($data[15]);
					$std->status = 1;
					$std->tahun = 2019;
					$std->jalur = 5;//spmb mandiri
					$std->ibu = trim($data[14]);
					
					$std->id_pendaftaran = '';
					$std->alamat = '';
					$std->nik_ayah = '';				
					$std->nik_ibu = '';				
					$std->tgl_ayah = '';
					$std->tgl_ibu = '';
					$std->pekerjaan = '';
					$std->alamatortu = '';
					$std->kotaortu = '';//code from feeder
					$std->propinsiortu = '';
					$std->kodeposortu = '';    
					$std->wali = '';
					$std->alamatwali = '';
					$std->alamatwali = '';
					$std->hp_ortu = '';
					$std->pend_ibu = '';
					$std->pend_bpk = '';
					$std->kerja_ibu = '';
					$std->kerja_bpk = '';
					$std->gaji_ibu ='';
					$std->gaji_bpk = '';
					$std->gaji_wali = '';			
					$std->pend_wali = '';
					$std->kerja_wali = '';			
					
					$this->logStatus('registering: '.$std->nim);
					$ret2 = $this->admin->registerSPMBStudent($std);	
					if(isset($ret2->error)){
						$this->logStatus('Error Student: '.$std->nim.' '.$std->nama.' '.$ret2->error->message);
					}
					if(isset($ret2->status)){
						if($ret2->status==0){
							$this->logStatus('Error Student: '.$std->nim.' '.$std->nama.' '.$ret2->message);					
						}else{
							$this->logStatus('done: '.$ret2->status);						
							//return TRUE;						
						}
					}
				$row++;	
			  }
			  fclose($handle);
			}
		}
	}
}

$r = new ImportStudentFromExcel();
$r->run();
Butuh bantuan?