/*********************************************************************************************************
"Ensemble" is the proprietary property of The Regents of the University of California ("The Regents.")
Copyright (c) 2005-10 The Regents of the University of California, Davis campus. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted by
nonprofit, research institutions for research use only, provided the conditions in the included
license agreement are met.
Refer to the file "ensemble_license.txt" for the license agreement,
located in the top level directory of this distribution.
**********************************************************************************************************/
//Author: Stefan Tomic
//Janata Lab
//Center for Mind and Brain
//Developed for the Ensemble Project
include('../include/researcher_includes.php');
$enc_key = subinfo_encryption_key();
$_SESSION['editor_calling_page'] = $_SERVER['PHP_SELF'];
$_SESSION['editor_title'] = "Response Export Utility";
$export_experiment_id = (isset($_POST['export_experiment_id']))? $_POST['export_experiment_id'] : '%';
$export_session_id = (isset($_POST['export_session_id']))? $_POST['export_session_id'] : '%';
$export_subject_id = (isset($_POST['export_subject_id']))? $_POST['export_subject_id'] : '%';
$export_ticket_id = (isset($_POST['export_ticket_id']))? $_POST['export_ticket_id'] : '%';
$response_table_fields_to_export_all = array("response_id","response_order","date_time","response_text","response_enum","question_id","form_question_num",
"question_iteration","subquestion","trial_id","stimulus_id","form_id","form_order","session_id","subject_id","experiment_id",
"misc_info");
$question_table_fields_to_export_all = array("question_text","question_category");
$question_x_data_format_fields_to_export_all = array("subquestion","heading","range","default");
//get subject table column names
$sql_get_subject_fieldnames = "show columns from subject";
$get_subject_fieldnames = mysql_query($sql_get_subject_fieldnames) or report_error_form(mysql_error());
while($row_subject_fieldnames= mysql_fetch_assoc($get_subject_fieldnames)) {
//do not export the subject_id field from subject table (this is already provided from response table)
if(strcmp($row_subject_fieldnames['Field'],"subject_id") != 0)
$subject_fields_to_export_all[] = $row_subject_fieldnames['Field'];
}
//if fieldnames were posted for export, store them into export variables (contain fieldnames to export)
foreach($_POST as $key=>$value) {
if(in_array($key,$response_table_fields_to_export_all)) {
//store response table fieldnames (without encryption function) for the purpose of writing a header to the output file
$response_table_fieldnames_to_export[] = $key;
if( ($_SESSION['encrypted_response_table']) && ((strcmp($key,"response_text") == 0) || (strcmp($key,"response_enum") == 0)) ) {
$key = sprintf("aes_encrypt(`%s`,'%s') as `%s`",$key,$enc_key,$key);
}
$response_table_fields_to_export[] = $key;
}
if(in_array($key,$question_table_fields_to_export_all))
$question_table_fields_to_export[] = $key;
if(in_array($key,$question_x_data_format_fields_to_export_all))
$question_x_data_format_fields_to_export[] = $key;
if(in_array($key,$subject_fields_to_export_all))
$subject_fields_to_export[] = $key;
}
$data_format_fields_to_export = array("type","enum_values");
//this function is called for prepending the table name string to each element in the field arrays defined above
function prepend_string2array($prepend_string,$string_array) {
for($idx = 0; $idx < sizeof($string_array); $idx++)
$string_array[$idx] = $prepend_string.$string_array[$idx];
return($string_array);
}
function decextbin($decimalnumber,$bit)
{
/* decextbin function
by James Preece (j.preece@gmail.com)
http://www.lovingit.co.uk
First we find that maximum value represented by the
leftmost binary digit. For error checking purposes
we also calulate the maximum number we can display
using the number of bits requested: */
$maxval = 1;
$sumval = 1;
for($i=1;$i<$bit;$i++)
{
$maxval = $maxval * 2;
$sumval = $sumval + $maxval;
}
/* Using our sumval we now check if it is possible
to display the decimal number our function received: */
if ($sumval < $decimalnumber) return 'ERROR - Not enough bits to display this figure in binary.';
/* Then we work down through the figures, to get a
better idea of how this works remove the commenting
from the echo lines */
for($bitvalue=$maxval;$bitvalue>=1;$bitvalue=$bitvalue/2)
{
//echo 'Bit Value: '.$bitvalue.'
';
//echo 'Decimal Number: '.$decimalnumber.'
';
if (($decimalnumber/$bitvalue) >= 1) $thisbit = 1; else $thisbit = 0;
//echo 'This Bit: '.$thisbit.'
';
if ($thisbit == 1) $decimalnumber = $decimalnumber - $bitvalue;
$binarynumber .= $thisbit;
}
/* Finally we return the output... */
return $binarynumber;
}
function resolve_enum_values($mask_int) {
// convert from ^2 to bin # for each enum mask
$mask_bin = strrev(decextbin($mask_int,64));
for($mask_idx = 0; $mask_idx < strlen($mask_bin); $mask_idx++) {
$bit = substr($mask_bin,$mask_idx,1);
if($bit == 1) {
if($return_enum == NULL)
$return_enum = $mask_idx+1;
else {
$return_enum .= ",".$mask_idx+1;
}
}
}
return $return_enum;
}
function resolve_enum_text($mask_int,$enum_values) {
// resolve the enum text that goes along with an enum mask
$enum_array = enumstr2array($enum_values);
$mask_bin = strrev(decextbin($mask_int,64));
for($mask_idx = 0; $mask_idx < strlen($mask_bin); $mask_idx++) {
$bit = substr($mask_bin,$mask_idx,1);
if($bit == 1) {
if($return_enum == NULL)
$return_enum = $enum_array[$mask_idx];
else {
$return_enum .= ",".$enum_array[$mask_idx];
}
}
}
return $return_enum;
}
//unless "Export" button was pressed, send the HTTP headers
if(!isset($_POST['action']) || ($_POST['action'] != "Export")) {
?>
} if(isset($_POST['action']) && ($_POST['action'] == "Export")) { $quest_field_array = prepend_string2array("question.",$question_table_fields_to_export); $quest_fields = implode(",",$quest_field_array); $qdf_field_array = prepend_string2array("question_x_data_format.",$question_x_data_format_fields_to_export); $qdf_fields = implode(",",$qdf_field_array); $df_field_array = prepend_string2array("data_format.",$data_format_fields_to_export); $df_fields = implode(",",$df_field_array); //$sub_field_array = prepend_string2array("subject.",$subject_fields_to_export); $subflds_not_encrypted = array('date_entered','security_questions','gender','ethnic_category','racial_category','notes'); foreach($subject_fields_to_export as $subfld) { if(!in_array($subfld,$subflds_not_encrypted)) $sub_field_array[] = sprintf("aes_decrypt(`subject`.`%s`,'%s') as %s",$subfld,$enc_key,$subfld); else $sub_field_array[] = sprintf("`subject`.`%s` as %s",$subfld,$subfld); } $sub_fields = implode(",",$sub_field_array); header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: csv; filename=\"".$_POST['filename']."\""); //write fieldnames to the first row $all_response_fields = array_merge($response_table_fields_to_export,$question_table_fields_to_export,$question_x_data_format_fields_to_export,$subject_fields_to_export); print implode(",",$all_response_fields).",response_enum_text\n"; $experiment_id_array = NULL; if($export_experiment_id == '%') { $get_exp_id = "select experiment_id from experiment"; $exp_id = mysql_query($get_exp_id) or die(mysql_error()); while($row_exp_id = mysql_fetch_assoc($exp_id)) $experiment_id_array[] = $row_exp_id['experiment_id']; } if($experiment_id_array == NULL) $experiment_id_array = array($export_experiment_id); foreach($experiment_id_array as $experiment_id_resp) { $sql_get_response_table = sprintf("select response_table,encrypted_response_table from experiment where experiment_id = %d",$experiment_id_resp); $get_response_table = mysql_select($sql_get_response_table); $resp_field_array = prepend_string2array($get_response_table['response_table'].".",$response_table_fields_to_export); if($get_response_table['encrypted_response_table'] == 'T') { $enum_idx = array_search($get_response_table['response_table'].".response_enum", $resp_field_array); $text_idx = array_search($get_response_table['response_table'].".response_text", $resp_field_array); if(is_integer($enum_idx)) $resp_field_array[$enum_idx] = sprintf("aes_decrypt(`%s`.`response_enum`,'%s') as response_enum",$get_response_table['response_table'],$enc_key); if(is_integer($text_idx)) $resp_field_array[$text_idx] = sprintf("aes_decrypt(`%s`.`response_text`,'%s') as response_text",$get_response_table['response_table'],$enc_key); } $resp_fields = implode(",",$resp_field_array); $sql_get_responses = sprintf("select %s,%s,%s,%s,%s from %s left join question using (question_id) left join question_x_data_format ", $resp_fields,$quest_fields,$qdf_fields,$df_fields,$sub_fields,$get_response_table['response_table']). sprintf("on(%s.question_id = question_x_data_format.question_id and %s.subquestion=question_x_data_format.subquestion) left join data_format ", $get_response_table['response_table'],$get_response_table['response_table']). sprintf(" on (question_x_data_format.answer_format_id = data_format.data_format_id) "). sprintf(" left join subject using (subject_id) "). sprintf("where subject_id like '%s' and session_id like '%s'",$export_subject_id, $export_session_id); //echo $sql_get_responses;die(); $get_responses = mysql_query($sql_get_responses) or die(mysql_error()); while($response = mysql_fetch_assoc($get_responses)) { $row = array(); foreach($all_response_fields as $field) { if ($field =="response_enum") $data = resolve_enum_values($response[$field]); else $data = ($response[$field] != NULL)? $response[$field] : "NULL"; //add double quotes to returned fields that have commas if(preg_match("/.*,.*/",$data)) $data = "\"".preg_replace("|\"|","\"\"",$data)."\""; array_push($row,$data); } // add response enum text $data = resolve_enum_text($response['response_enum'],$response['enum_values']); array_push($row,$data); // print the row print implode(",",$row)."\n"; } } } elseif(isset($_POST['action']) && ($_POST['action'] == "Submit")) { if ($export_experiment_id != '%') { $sql_get_experiment_name = sprintf("select experiment_title from experiment where experiment_id = %d",$export_experiment_id); $experiment_name = mysql_select($sql_get_experiment_name); $descript_expid = $experiment_name['experiment_title']; } else $descript_expid = "All Experiments"; $descript_sessid = ($export_session_id != '%')? $export_session_id : "(All Sessions)"; $descript_subid = ($export_subject_id != '%')? $export_subject_id : "All Subjects"; $descript_tickid = ($export_ticket_id != '%')? $export_ticket_id : "All Tickets"; printf("Exporting responses for Experiment(s) \"%s,\" Ticket(s) \"%s,\", Subject(s) \"%s,\" and Session(s) %s