public function __construct($current_project)
 {
     $this->runner_type = $_GET['runner_type'];
     $this->action = $_GET['action'];
     $this->action_value = $_GET['action_value'];
     $this->list_table = $this->runner_type . "_list";
     $this->attribs_table = $this->runner_type . "_attrib";
     $this->output_table = $this->runner_type . "_output:";
     $this->current_project = $current_project;
     $this->name_to_edit = $_GET['name'];
     $this->name = $_GET['name'];
     // select the first ppt for use as a template for input etc.
     //$participant= $this->get_single_participant_id();
     // now build the arrays for the form
     $this->column_list = @header_array_builder("" . $this->current_project . ".datasets");
     // Computation list
     $this->computation_list = array("AVG", "SUM", "MAX", "MIN", "COUNT", "COUNT DISTINCT");
     // Calculation list - for custom columns. Defaults to multiply
     $this->calculation_list = array("*", "+", "-", "/");
     // Time period list
     $this->time_period_list = single_column_array_builder($this->current_project . ".time_periods_list", "name");
     // Response list
     $this->response_list = single_column_array_builder($this->current_project . ".responses_list", "name");
     // Outcome list
     $this->outcome_list = array("CORRECT", "INCORRECT");
 }
function create_excel($database)
{
    // import global references
    global $current_project;
    // build data for output
    $spreadsheet = '';
    // this contains all of the stuff we are going to add
    // build headers
    $headers = header_array_builder($database);
    foreach ($headers as $header) {
        $spreadsheet = $spreadsheet . $header . "\t";
    }
    $spreadsheet = $spreadsheet . "\n";
    // get the actual data and add it in
    $result = mysql_query("SELECT * FROM " . $database);
    while ($row = mysql_fetch_row($result)) {
        foreach ($row as $cell) {
            $spreadsheet = $spreadsheet . $cell . "\t";
        }
        $spreadsheet = $spreadsheet . "\n";
    }
    // output file to appropriate location
    $file_name = "../projects/" . $current_project . "/excel_output/" . $database . ".xls";
    $file_handler = fopen($file_name, "w");
    fwrite($file_handler, $spreadsheet);
}
function select_column_builder($database, $list = array())
{
    $array = header_array_builder($database);
    echo "<select multiple id='available' name='available'>";
    foreach ($array as $head) {
        echo "<option value='" . $head . "'>" . $head . "</option>";
    }
    echo "</select>";
    echo "<select multiple id='selected' name='selected'></select>";
    echo "<script>\n\t\t\t\$().ready(function() {\n\t\t\t\t\$('#available').click(add_column);\n\t\t\t\t\$('#selected').click(remove_column);\t\t\n \t\t\t});\n\t\t  </script>";
}
 function selection_query()
 {
     //////////////
     //
     // 	THIS HANDLES BOTH NORMAL AND CUSTOM ANALYSES. THEY HAVE A LOT OF OVERLAP.
     //
     //////////////
     //$this->dummy_run=true;
     if ($this->runner_interface->runner_type == 'analyses') {
         $this->computation = $this->runner_data['computation'];
         $this->input_column = $this->runner_data['input_column'];
         $this->group_by = $this->runner_data['group_by'];
     }
     if ($this->runner_interface->runner_type == 'custom') {
         $this->generate_output = false;
         $this->custom_label = $this->runner_data['custom_label'];
         $this->output_type = $this->runner_data['output_type'];
         $this->computation = $this->runner_data['computation'];
         $this->input_column = $this->runner_data['input_column'];
         $this->output_value = $this->runner_data['output_value'];
         $this->output_text = $this->runner_data['output_text'];
         $this->output_manual = $this->runner_data['output_manual'];
     }
     // set up info for time periods, inc full list and temporal order
     $this->get_time_periods_info();
     // set up info for responses
     $this->get_responses_info();
     $this->restriction_columns = array();
     $this->restriction_symbols = array();
     $this->restriction_texts = array();
     $this->response_list = array();
     $this->outcome_list = array();
     $this->time_period_list = array();
     // sort out temporary table - create if it doesnt exist already
     $this->aggregated_table_exists = $this->table_exists();
     if ($this->aggregated_table_exists == false) {
         $this->aggregated_table("create");
     }
     if ($this->runner_interface->runner_type == 'analyses') {
         // these are for standard computations
         if (!strstr($this->computation, 'COUNT DISTINCT') && $this->input_column != '') {
             $this->computation_string = $this->computation . "(" . $this->input_column . ")";
         }
         if (strstr($this->computation, 'COUNT DISTINCT') && $this->input_column != '') {
             $this->computation_string = "COUNT(DISTINCT(" . $this->input_column . "))";
         }
         // this is used to shave times off fixation durations that occur as part of multiple time periods
         if (count($this->time_period_list) > 0 && strtolower($this->input_column) == 'current_fix_duration' && $this->computation == 'avg') {
             $this->computation_string = "AVG(\n\t\t\t  CASE\n\t\t\t\tWHEN (cast(p.current_fix_start as signed) \n\t\t\t\t\tbetween cast(a." . $this->first_time_period_selected . " as signed) \n\t\t  \t\t\t\tand cast(a." . $this->final_time_period_selected . " as signed)) \n\t\t  \t\t    and (cast(p.current_fix_end as signed) between\n\t\t  \t\t    \tcast(a." . $this->first_time_period_selected . " as signed) \n\t\t   \t\t\t\tand cast(a." . $this->final_time_period_selected . " as signed)) \n\t\t   \t\tTHEN p.current_fix_duration\n\t\n\t\t\t\tWHEN (cast(p.current_fix_start as signed) \n\t\t\t\t\tbetween cast(a." . $this->first_time_period_selected . " as signed) \n\t\t  \t\t\t\tand cast(a." . $this->final_time_period_selected . " as signed)) \n\t\t  \t\t\tand (cast(p.current_fix_end as signed) > \n\t\t  \t\t\t\tcast(a." . $this->final_time_period_selected . " as signed)) \n\t\t  \t\tTHEN cast(a." . $this->final_time_period_selected . " as signed) - cast(p.current_fix_start as signed)\n\t\n\t\t\t\tWHEN (cast(p.current_fix_start as signed) \n\t\t\t\t\t< cast(a." . $this->first_time_period_selected . " as signed)) \n\t\t  \t\t\tand (cast(p.current_fix_end as signed) \n\t\t  \t\t\t\tbetween cast(a." . $this->first_time_period_selected . " as signed) and \n\t\t  \t\t\t\tcast(a." . $this->final_time_period_selected . " as signed))\n\t\t  \t\tTHEN cast(p.current_fix_end as signed) - cast(a." . $this->first_time_period_selected . " as signed)\n\t\n\t\t\t\tWHEN (cast(p.current_fix_start as signed) \n\t\t\t\t\t< cast(a." . $this->first_time_period_selected . " as signed)) \n\t\t   \t\t\tand (cast(p.current_fix_end as signed) \n\t\t   \t\t\t\t> cast(a." . $this->final_time_period_selected . " as signed)) \n\t\t   \t\tTHEN cast(a." . $this->final_time_period_selected . " as signed)- cast(a." . $this->first_time_period_selected . " as signed)\n\t\n\t\t\t END\n\t\t\t)";
         }
         $this->restriction_query = "INSERT INTO \n\t\t     " . $this->runner_interface->current_project . "." . $this->runner_interface->runner_type . "_output \n\t\t     (runner, ppt_id, participant, session_id, trial, value)\n\t\t     SELECT '" . $this->runner_interface->name . "', p.INPSYTE__PPT_ID,\n\t\t     p.INPSYTE__PPT_TRUE_ID, p.INPSYTE__SESSION_ID, p." . $this->group_by . ",\n\t\t     " . $this->computation_string . " \n\t\t     FROM " . $this->runner_interface->current_project . ".datasets p\n\t\t     JOIN " . $this->runner_interface->current_project . ".datasets_aggregated as a on \n\t\t     p.INPSYTE__PPT_ID = a.INPSYTE__PPT_ID AND p.TRIAL_INDEX = a.trial_index\n\t\t     ";
     }
     ////// build query trunk for custom
     if ($this->runner_interface->runner_type == 'custom') {
         if ($this->output_type == 'calculation') {
             $output_string = $this->input_column . " " . $this->computation . " " . $this->output_value;
         }
         if ($this->output_type == 'text') {
             $output_string = " '" . $this->output_text . "' ";
         }
         if ($this->output_type == 'manual') {
             $output_string = " " . $this->output_manual . " ";
         }
         $datasets_columns = header_array_builder($this->runner_interface->current_project . ".datasets");
         if (!in_array($this->custom_label, $datasets_columns)) {
             $add = mysql_query("ALTER TABLE " . $this->runner_interface->current_project . ".datasets\n\t\t\t\t ADD COLUMN " . $this->custom_label . " VARCHAR(50) DEFAULT NULL");
         }
         $this->restriction_query = "UPDATE " . $this->runner_interface->current_project . ".datasets p\n\t\tLEFT JOIN " . $this->runner_interface->current_project . ".datasets_aggregated AS a \n\t\tON p.INPSYTE__PPT_ID = a.INPSYTE__PPT_ID AND p.TRIAL_INDEX = a.trial_index\n\t\tSET " . $this->custom_label . " = " . $output_string . "";
     }
     // this is used for dynamic query generation
     $this->where_written = false;
     // handle various monstrous arrays - restrictions, responses, time periods, outcomes
     for ($i = 1; $i <= count($this->runner_data); $i += 1) {
         // handle infinite number of restrictions
         if (array_key_exists('restriction_' . $i . '_type', $this->runner_data)) {
             $this->add_where_check();
             // equals comparison
             if ($this->runner_data['restriction_' . $i . '_symbol'] == '=') {
                 $this->restriction_query .= "p." . $this->runner_data['restriction_' . $i . '_column'] . " " . $this->runner_data['restriction_' . $i . '_symbol'] . "'" . $this->runner_data['restriction_' . $i . '_text'] . "' ";
             }
             // like COMPARISON
             if ($this->runner_data['restriction_' . $i . '_symbol'] == 'LIKE') {
                 $this->restriction_query .= "p." . $this->runner_data['restriction_' . $i . '_column'] . " " . $this->runner_data['restriction_' . $i . '_symbol'] . "'%" . $this->runner_data['restriction_' . $i . '_text'] . "%' ";
             }
             // numerical comparison
             if ($this->runner_data['restriction_' . $i . '_symbol'] == '>' || $this->runner_data['restriction_' . $i . '_symbol'] == '<' || $this->runner_data['restriction_' . $i . '_symbol'] == '<=' || $this->runner_data['restriction_' . $i . '_symbol'] == '>=') {
                 $this->restriction_query .= " CAST(" . "p." . $this->runner_data['restriction_' . $i . '_column'] . " AS SIGNED) " . $this->runner_data['restriction_' . $i . '_symbol'] . " CAST(" . $this->runner_data['restriction_' . $i . '_text'] . " AS SIGNED) ";
             }
             // manual restrictions
             if ($this->runner_data['restriction_' . $i . '_type'] == 'manual') {
                 $this->restriction_query .= $this->runner_data['restriction_' . $i . '_manual'];
             }
         }
         // if we have selected specific response(s) to look at
         if (array_key_exists('response_' . $i, $this->runner_data)) {
             $this->response_list[] = $this->runner_data['response_' . $i];
         }
         // if we have selected specific outcome(s) to look at
         if (array_key_exists('outcome_' . $i, $this->runner_data)) {
             $this->outcome_list[] = $this->runner_data['outcome_' . $i];
         }
         // HANDLES time periods
         if (array_key_exists('time_period_' . $i, $this->runner_data)) {
             $this->time_period_list[] = $this->runner_data['time_period_' . $i];
         }
     }
     // do user-selected aspects of the joinery
     $this->join_lister($this->response_list, "response");
     $this->join_lister($this->outcome_list, "outcome");
     $this->time_period_join_lister($this->time_period_list);
     // now select only trials where all events occurred
     // note this only counts for analyses where the user asked to look at events in some shape or form
     $this->select_time_period_trials_join_lister($this->time_period_list);
     // close up and group by - not for custom analyses though
     if ($this->runner_interface->runner_type == 'analyses') {
         $this->restriction_query .= " GROUP BY p.INPSYTE__PPT_ID, p.TRIAL_INDEX ";
     }
     echo "<BR>";
     echo $this->restriction_query;
     echo "<BR>";
 }