<?php
ini_set('max_execution_time', 300); 
require "../../config/koneksi.php";
require "../../config/function.php";

$datetime = date('Y-m-d H:i:s');
if ($koneksi) {
    
    $token = $setting['token_api'];
	
	$data[]='mapel';
    $data[]='bank';
	$data[]='soal';
	$data[]='informasi';
	$data[]='file';
	$data[]='jadwal';
	$data[]='jenis';
    if ($token <> '' and $data <> '') {
       
        foreach ($data as $data) {
           
            if ($data == 'mapel') {
                $syncda = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncda, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table mata_pelajaran");
                    foreach ($sync['mapel'] as $mapel) {
                      
                        $sqlpel = mysqli_query($koneksi, "insert into mata_pelajaran
                                (id,kode,nama_mapel,pk) values 			
                                ('$mapel[id]','$mapel[kode]','$mapel[nama_mapel]','$mapel[pk]')");
			}	
			 }
			}
			 if ($data == 'bank') {
                $syncbank = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncbank, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table banksoal");
                    foreach ($sync['bank'] as $bank) {                    
                        
						$sqlbank = mysqli_query($koneksi, "insert into banksoal
                                (id_bank,kode,idguru,nama,level,kelas,status,soal_agama,model,groupsoal) values 			
                                ('$bank[id_bank]','$bank[kode]','$bank[idguru]','$bank[nama]','$bank[level]','$bank[kelas]','$bank[status]','$bank[soal_agama]','$bank[model]','$bank[groupsoal]')");
			      
				  }	
			 }
			}
			if ($data == 'soal') {
                $synsoal = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($synsoal, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table soal");
                    foreach ($sync['soal'] as $soal) {                    
                                    $soalx = addslashes($soal['soal']);
                                    $pilA = addslashes($soal['pilA']);
                                    $pilB = addslashes($soal['pilB']);
                                    $pilC = addslashes($soal['pilC']);
                                    $pilD = addslashes($soal['pilD']);
                                    $pilE = addslashes($soal['pilE']);					
									$perA = addslashes($soal['perA']);									
                                    $perB = addslashes($soal['perB']);								
                                    $perC = addslashes($soal['perC']);									
                                    $perD = addslashes($soal['perD']);									
                                    $perE = addslashes($soal['perE']);        
						$sqlsoal = mysqli_query($koneksi, "insert into soal
                                (id_soal,id_bank,nomor,soal,jenis,pilA,pilB,pilC,pilD,pilE,perA,perB,perC,perD,perE,jawaban,file,file1,fileA,fileB,fileC,fileD,fileE,max_skor) values 			
                                ('$soal[id_soal]','$soal[id_bank]','$soal[nomor]','$soalx','$soal[jenis]','$pilA','$pilB','$pilC','$pilD','$pilE',
								'$perA','$perB','$perC','$perD','$perE','$soal[jawaban]','$soal[file]','$soal[file1]','$soal[fileA]','$soal[fileB]','$soal[fileC]','$soal[fileD]','$soal[fileE]','$soal[max_skor]')");
			      
				  }	
			 }
			}
		
		if ($data == 'informasi') {
                $syncinfo = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncinfo, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table informasi");
                    foreach ($sync['informasi'] as $info) {
                      
                        $sqlinfo = mysqli_query($koneksi, "insert into informasi
                                (id,judul,untuk,isi,waktu) values 			
                                ('$info[id]','$info[judul]','$info[untuk]','$info[isi]','$info[waktu]')");
			}	
			 }
			}
		if ($data == 'file') {
                $syncfile = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncfile, TRUE);

                if ($sync <> null) {
				$foto = glob('../../files/*');
                 foreach ($foto as $file) {
					if (is_file($file))
						unlink($file); 
				}
					$filezip = '../../files.zip';
					 unlink($filezip); 
				 
				$web = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM aplikasi  WHERE id_aplikasi='1'"));
					$remote_file_url = $web['url_host'].'/files.zip';
					$local = '../../files.zip';
					$copyQ = copy($remote_file_url, $local);
					$local_file = '../files/files.zip';
					$copy = copy($remote_file_url, $local_file);
					$file = $local_file;
					$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

					$zip = new ZipArchive;
					$res = $zip->open($file);
					if ($res === TRUE) {
					  
					  $zip->extractTo($path);
					  $zip->close();
					 
					  unlink("../../files/files.zip");
					 
					}
                    $sql = mysqli_query($koneksi, "truncate table file_pendukung");
                    foreach ($sync['file'] as $file) {
                      
                        $sqlinfo = mysqli_query($koneksi, "insert into file_pendukung
                                (id_file,id_bank,nama_file,status_file) values 			
                                ('$file[id_file]','$file[id_bank]','$file[nama_file]','$file[status_file]')");
			}	
			 }
			}
		if ($data == 'jadwal') {
                $syncjadwal = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncjadwal, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table ujian");
                    foreach ($sync['jadwal'] as $jadwal) {
                      
                        $sqljadwal = mysqli_query($koneksi, "insert into ujian
                                (id_ujian,kode_nama,id_bank,kode_ujian,nama,jml_soal,jml_esai,jml_multi,jml_bs,jml_urut,tampil_pg,tampil_esai,tampil_multi,tampil_bs,tampil_urut,lama_ujian,tgl_ujian,tgl_selesai,waktu_ujian,level,sesi,status,soal_agama) values 			
                                ('$jadwal[id_ujian]','$jadwal[kode_nama]','$jadwal[id_bank]','$jadwal[kode_ujian]','$jadwal[nama]','$jadwal[jml_soal]','$jadwal[jml_esai]','$jadwal[jml_multi]',
								'$jadwal[jml_bs]','$jadwal[jml_urut]','$jadwal[tampil_pg]','$jadwal[tampil_esai]','$jadwal[tampil_multi]','$jadwal[tampil_bs]','$jadwal[tampil_urut]',
                                '$jadwal[lama_ujian]','$jadwal[tgl_ujian]','$jadwal[tgl_selesai]','$jadwal[waktu_ujian]','$jadwal[level]','$jadwal[sesi]','$jadwal[status]','$jadwal[soal_agama]')");
			}	
			 }
			}
		
		if ($data == 'jenis') {
                $syncjenis = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncjenis, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table jenis");
                    foreach ($sync['jenis'] as $jenis) {
                      
                        $sqljenis = mysqli_query($koneksi, "insert into jenis
                                (id_jenis,nama,status) values('$jenis[id_jenis]','$jenis[nama]','$jenis[status]')");
			}	
			 }
			}
		
		
		}
	}	
}