Skip to content

Instantly share code, notes, and snippets.

@hugosolar
Created October 23, 2015 18:09
Show Gist options
  • Save hugosolar/bd09a17f77ef31604d08 to your computer and use it in GitHub Desktop.
Save hugosolar/bd09a17f77ef31604d08 to your computer and use it in GitHub Desktop.
Simple simple exporter to fsqm-pro plugin
<?php
error_reporting(0);
//FORM ID
$id_form = 8;
//manual translate of smileyrate
function smilley_translate($smile) {
switch ($smile) {
case 'frown': return 'molesto'; break;
case 'sad': return 'Muy insatisfecho(a)'; break;
case 'neutral': return 'Insatisfecho(a)'; break;
case 'happy': return 'Satisfecho(a)'; break;
case 'excited': return 'Muy Satisfecho(a)'; break;
}
}
try {
// DATA MYSQL
$host = 'localhost';
$dbname = 'dbname';
$user = 'user';
$pass = 'password';
//Mysql PDO connection
$mbd = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass);
//stdClass to store all the form data
$the_form = new stdClass;
//query form table to get the item data
foreach ($mbd->query('SELECT * from wp_fsq_form where id='.$id_form) as $form) {
$the_form->name = $form['name'];
//i have to fix the serialized data cause the 'unserialize' function throw an error
$fixed_mcq = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$form['mcq'] );
$fixed_freetype = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$form['freetype'] );
$fixed_pinfo = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$form['pinfo'] );
//have to make this exception with freetype field don't undestand why happen this
$fixed_freetype = unserialize($fixed_freetype);
$fixed_freetype = current($fixed_freetype);
$questions = unserialize($fixed_pinfo) + unserialize($fixed_mcq);
array_push($questions,$fixed_freetype);
//store questions in the stdClass
$the_form->preguntas = $questions;
}
$x = 0;
//query the answer data with the form ID
foreach($mbd->query('SELECT * from wp_fsq_data where form_id='.$id_form) as $fila) {
//get the significant data from answer like date and id (just in case)
$the_form->respuestas[$x]['id'] = $fila['id'];
$the_form->respuestas[$x]['date'] = $fila['date'];
//make the same fix to the serialized data
$fixed_mcq = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$fila['mcq'] );
$fixed_freetype = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$fila['freetype'] );
$fixed_pinfo = preg_replace_callback ( '!s:(\d+):"(.*?)";!',
function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},
$fila['pinfo'] );
$fixed_freetype = unserialize($fixed_freetype);
$fixed_freetype = current($fixed_freetype);
$questions = unserialize($fixed_pinfo) + unserialize($fixed_mcq);
array_push($questions,$fixed_freetype);
//store the answer in "items"
$the_form->respuestas[$x]['items'][] = $questions;
$x++;
}
// GENERATE CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
$columns = array('ID', 'Fecha');
$preguntas_csv = array();
foreach ($the_form->preguntas as $q) {
$preguntas_csv[] = $q['title'];
}
$columns = array_merge($columns, $preguntas_csv);
fputcsv($output, $columns);
// ID - Date - Name - Questions[] - Feedback
$responses = array();
foreach ($the_form->respuestas as $r) {
$item_response = array();
$item_response[] = $r['id'];
$item_response[] = $r['date'];
foreach ( $r['items'][0] as $item ) {
if ($item['m_type'] == 'pinfo') {
$item_response[] = $item['value'];
} else if ($item['m_type'] == 'mcq' ) {
$item_response[] = smilley_translate($item['option']);
} else if ($item['m_type'] == 'freetype') {
$item_response[] = $item['value'];
}
}
$responses[] = $item_response;
//Download CSV
fputcsv($output, $item_response);
}
$mbd = null;
} catch (PDOException $e) {
print "¡Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment