Browse Source

Merge branch 'feature/scripts' into develop

master
Dslak 6 years ago
parent
commit
c237098166
  1. 41
      scripts/export.php
  2. 70
      scripts/import.php
  3. 24
      scripts/replace.sh

41
scripts/export.php

@ -0,0 +1,41 @@
<?php
$DATAhst="localhost";
$DATAusr="root";
$DATApwd="root";
$DATAdtb="vds";
$conn=@mysqli_connect($DATAhst, $DATAusr, $DATApwd, $DATAdtb)or die("CONNECTION ERROR");
mysqli_set_charset($conn, "utf8");
$query="SELECT * FROM questions";
$q=mysqli_query($conn, $query);
$data=array();
$answers=array();
class Answer {}
while($r=mysqli_fetch_array($q)) {
$answers=[];
$answers_unserializes = unserialize($r['answers']);
foreach ($answers_unserializes as $key => $value) {
$answer = new Answer();
$answer->id = trim($key);
$answer->text = trim($value);
if($key == $r['correct']){$answer->correct = true;}
$answers[] = $answer;
}
$data[] = array(
"id" => trim($r['id']),
"question" => trim($r['question']),
"answers" => $answers
);
}
header('Content-type: application/json');
echo json_encode( $data );
?>

70
scripts/import.php

@ -0,0 +1,70 @@
<?php
$DATAhst="localhost";
$DATAusr="root";
$DATApwd="root";
$DATAdtb="vds";
$file = "1_NormativaELegislazione";
// $file = "2_Aerodinamica";
// $file = "3_ProntoSoccorso";
// $file = "4_FisiopatologiaDelVolo";
// $file = "5_MeteorologiaEAerologia";
// $file = "6_Strumentazione";
// $file = "7_TecnicheDiPilotaggio";
// $file = "8_Materiali";
// $file = "9_SicurezzaDelVolo";
$conn=@mysqli_connect($DATAhst, $DATAusr, $DATApwd, $DATAdtb)or die("CONNECTION ERROR");
mysqli_set_charset($conn, "utf8");
$myfile = fopen("data/".$file."RES", "r") or die("Unable to open file!");
$responses=array();
while(!feof($myfile)) {
$row = explode("|",fgets($myfile));
$responses[$row[0]] = $row[1];
}
fclose($myfile);
$myfile = fopen("data/$file", "r") or die("Unable to open file!");
$answers = array();
$query="DELETE FROM questions";
mysqli_query($conn,$query);
while(!feof($myfile)) {
$row = explode("|",fgets($myfile));
if(substr($row[0], 0, 1) == "#") {
if(count($answers)) {
/*
echo $id."<br>";
echo $title."<br>";
echo $responses[$id]."<br>";
print_r($answers);
echo "<br><br>";
*/
$query="INSERT questions VALUES($id, '$title', '".serialize($answers)."', ".$responses[$id].")";
mysqli_query($conn,$query);
}
$id = substr($row[0], 1, 4);
$title = $row[1];
$answers = [];
} else {
$answers[$row[0]] = $row[1];
}
}
fclose($myfile);
?>

24
scripts/replace.sh

@ -0,0 +1,24 @@
#!/bin/bash
echo "" > tmp
while read p; do
INIT=${p:0:2}
if [[ "${p:0:4}" =~ ^[0-9]+$ ]] && [ "${p:0:1}" == "${1:0:1}" ]; then
echo $TMP_ROW >> tmp
TMP_ROW="#${p:0:4} | "$'\n'"${p:4}"
elif [ $INIT == "1." ] || [ $INIT == "2." ] || [ $INIT == "3." ]; then
echo $TMP_ROW >> tmp
TMP_ROW="${p:0:1} | "$'\n'"${p:2}"
else
TMP_ROW="$TMP_ROW $p"
fi
done < $1
echo $TMP_ROW >> tmp
sed -i -e 's/- //g' tmp
mv tmp ../data/$1
Loading…
Cancel
Save