Skip to content
Home ยป Import Staf atau Dosen dari File Excel

Import Staf atau Dosen dari File Excel

  • by
Spread the love

berikut adalah contoh code untuk mengimport dosen / staf dari file excel

<?php
include "./bootstrap.php";

use \UniversitySDK\Entities\Staff;

class ConvertStaff{
	private $admin;
	private $pdo;
	
	function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$charactersLength = strlen($characters);
		$randomString = '';
		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, $charactersLength - 1)];
		}
		return $randomString;
	}
	private function logStatus($t){
		$f = fopen(__DIR__.'/ConvertStaff.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 unixTimestampToDateString($t){
		$jkt = new \DateTimeZone('Asia/Jakarta');
		$date = DateTime::createFromFormat('U', $t);
		$date->setTimeZone($jkt);
		$date = $date->format('Y-m-d');
		return $date;
	}
	public function run(){
		$this->admin = new \UniversitySDK\Admin;

		
		$ret = $this->admin->login('[email protected]', 'P@ssw0rd');
		if(!$ret){
			echo 'failed to login';
			return;
		}
		$file = file("./staffs.csv",FILE_SKIP_EMPTY_LINES); 
		$csv = array_map("str_getcsv",$file, array_fill(0, count($file), ';'));
		$keys = array_shift($csv);
		foreach ($csv as $i=>$row) {
			$csv[$i] = array_combine($keys, $row);
			//var_dump($csv[$i]);		
			$staff = (object)$csv[$i];
			
			$this->logStatus('Staff: '.$staff->NIP_BARU.' '.$staff->NAMA_LENGKAP);
			$ret = $this->addStaff($staff);
			if(isset($ret->error)){
				$this->logStatus('Error Staff: '.$staff->NIP_BARU.' '.$staff->NAMA_LENGKAP.' '.$ret->error->message);
			}
			if(isset($ret->status)){
				if($ret->status==0){
					$this->logStatus('Error Staff: '.$staff->NIP_BARU.' '.$staff->NAMA_LENGKAP.' '.$ret->message);					
				}else{										
				}
			}
		}

	}
	function convertDate($datex){
		if(empty($datex)){
			return NULL;
		}
		$date = \DateTime::createFromFormat('m/d/Y H:i A', $datex);
		var_dump(DateTime::getLastErrors());
		return $date->format('Y-m-d');
	}
	private function addStaff($staff){
		
		$idcardno = $this->generateRandomString(17);//invalid so he can fix it later
		$email = $staff->NIP_BARU.'@university.ac.id';
       
		$sex = 'm';
		
		$data = array (
		   'UniversityID' => $this->admin->getUniversity()->ID,
          //'ID' => '',
          'staffId' => $staff->NIP_BARU,
          'username' => $staff->NIP_BARU,
          'NIDN' => '',
          'fullName' => $staff->NAMA_LENGKAP,
          'email' => $email,
          'IDCardNo' => $staff->NIP_BARU,
          'suffix1' => '',
          'title1' => '',
          'title2' => '',
          'dateOfBirth' => $this->convertDate($staff->TGL_LAHIR),          
          'EmploymentTypeID' => '',
          'jobTitle' => $staff->KET_JABATAN,
          'lastEducation' => $staff->JURUSAN,
          'MaritalStatus' => 
          array (
            'ID' => '2',
            'name' => 'Kawin',
          ),
          'MaritalStatusID' => '2',
          'nextRankPromotionDate' => '',
          'nextSalaryIncreasementDate' => '',
          'prevStaffId' => $staff->NIP,
          'placeOfBirth' => $staff->TEMPAT_LAHIR,
          'rank' => '',
          'Religion' => 
          array (
            'ID' => '1',
            'name' => 'Islam',
          ),
          'ReligionID' => '1',
          'retirementDate' => $this->convertDate($staff->TMT_PENSIUN),
          'gender' => $sex,
          'startDateCPNS' => '',
          'startDatePNS' => '',
          'EmploymentStatus' => 
          array (
            'ID' => '1002',
            'name' => 'PNS',
          ),
          'EmploymentStatusID' => '1002',
          'UnitOfWork' => 
          array (
            'ID' => '1001',
            'name' => 'Unit 1',
          ),
          'UnitOfWorkID' => '1001',
          'unitOfWorkDescription' => '',
          'workingTime' => '',
          'StaffCharacteristic' => '',
          'StaffCharacteristicID' => '',
          'Address' => '',
          'AddressID' => '',
          'password' => $this->generateRandomString(),
        );
        
        $newstaff = new Staff();
		$newstaff->bind($data);
		if(!$newstaff->validate()){
			$this->logStatus('invalid staff: '.$newstaff->error_messages());
		}
		//var_dump($newstaff);exit(0);
		return $this->admin->registerStaff($newstaff);
	}
	
	
}
$p = new ConvertStaff;
$p->run();
Butuh bantuan?