<commit_before><?php

include_once ("../config.php");

if ( nbt_get_privileges_for_userid ( $_SESSION[INSTALL_HASH . '_nbt_userid'] ) >= 2 ) {

    // array_map( 'unlink', glob ( ABS_PATH . "export/*.csv" ) );

    // $filename = substr (hash('sha256', rand(0, 10000)), 0, 12);

    $filename = date("Y-m-d_His");

    switch ( $_POST['export_type'] ) {

        case "extraction":

            if ( $_POST['final'] == 0 ) {

		// Export the final copy

		// Get the columns from the reference set

		$rs_cols = nbt_get_columns_for_refset ( $_POST['refsetid'] );

		$filename = $filename . "-form_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-extractions";

		echo $filename;

		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT * FROM referenceset_" . $_POST['refsetid'] . ", extractions_" . $_POST['formid'] . " WHERE extractions_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND extractions_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id AND extractions_" . $_POST['formid'] . ".status = 2 ORDER BY extractions_" . $_POST['formid'] . ".timestamp_started ASC;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

            } else {

		// Export the extractions

		$filename = $filename . "-form_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-final";

		echo $filename;

		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT * FROM referenceset_" . $_POST['refsetid'] . ", m_extractions_" . $_POST['formid'] . " WHERE m_extractions_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND m_extractions_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id AND m_extractions_" . $_POST['formid'] . ".status = 2 ORDER BY m_extractions_" . $_POST['formid'] . ".timestamp_started ASC;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

            }

            break;

        case "sub_extraction":

	    if ( $_POST['final'] == 0 ) {

		// echo "sub";

		$filename = $filename . "-sub_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-sub-extraction";

		echo $filename;

		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT * FROM referenceset_" . $_POST['refsetid'] . ", sub_" . $_POST['formid'] . " WHERE sub_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND sub_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

	    } else {

		// echo sub final;

		$filename = $filename . "-sub_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-sub-final";
<commit_msg>Export extractions with columns in the order they are in the form<commit_after><?php

include_once ("../config.php");

if ( nbt_get_privileges_for_userid ( $_SESSION[INSTALL_HASH . '_nbt_userid'] ) >= 2 ) {

    // array_map( 'unlink', glob ( ABS_PATH . "export/*.csv" ) );

    // $filename = substr (hash('sha256', rand(0, 10000)), 0, 12);

    $filename = date("Y-m-d_His");

    switch ( $_POST['export_type'] ) {

        case "extraction":

            if ( $_POST['final'] == 0 ) {

		// Export the extractions

		// Get the columns from the reference set

		$rs_cols = nbt_get_columns_for_refset ( $_POST['refsetid'] );

		// Put those columns together into a string

		$rscols_to_export = array();
		foreach ($rs_cols as $rs_col) {
		    if ($rs_col[0] == "type") {
			$rs_col[0] = "`type`";
		    }
		    $rscols_to_export[] = $rs_col[0];
		}

		$rs_cols_string = "referenceset_" . $_POST['refsetid'] . "." . implode(", referenceset_" . $_POST['refsetid'] . ".", $rscols_to_export);

		// Get the columns for the extraction form

		$elements = nbt_get_elements_for_formid ( $_POST['formid'] );

		$fcols = array();
		foreach ($elements as $ele) {
		    switch ($ele['type']) {
			case "open_text":
			case "text_area":
			case "date_selector":
			case "single_select":
			case "country_selector":
			case "prev_select":
			    $fcols[] = $ele['columnname'];
			    break;
			case "multi_select":
			    $selectoptions = nbt_get_all_select_options_for_element ( $ele['id'] );
			    foreach ($selectoptions as $opt) {
				$fcols[] = $ele['columnname'] . "_" . $opt['dbname'];
			    }
			    break;
		    }
		}
		
		// Put those columns together into a string

		$form_cols_string = "extractions_" . $_POST['formid'] . "." . implode(", extractions_" . $_POST['formid'] . ".", $fcols);

		$select_cols = $rs_cols_string . ", " . $form_cols_string;

		// Make a filename

		$filename = $filename . "-form_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-extractions";

		echo $filename;
		
		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT " . $select_cols . " FROM referenceset_" . $_POST['refsetid'] . ", extractions_" . $_POST['formid'] . " WHERE extractions_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND extractions_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id AND extractions_" . $_POST['formid'] . ".status = 2 ORDER BY extractions_" . $_POST['formid'] . ".timestamp_started ASC;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

            } else {

		// Export the final copy

		$filename = $filename . "-form_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-final";

		echo $filename;

		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT * FROM referenceset_" . $_POST['refsetid'] . ", m_extractions_" . $_POST['formid'] . " WHERE m_extractions_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND m_extractions_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id AND m_extractions_" . $_POST['formid'] . ".status = 2 ORDER BY m_extractions_" . $_POST['formid'] . ".timestamp_started ASC;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

            }

            break;

        case "sub_extraction":

	    if ( $_POST['final'] == 0 ) {

		// echo "sub";

		$filename = $filename . "-sub_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-sub-extraction";

		echo $filename;

		exec ( "mysql -u " . DB_USER . " -p" . DB_PASS . " -h " . DB_HOST . " " . DB_NAME . " -B -e \"SELECT * FROM referenceset_" . $_POST['refsetid'] . ", sub_" . $_POST['formid'] . " WHERE sub_" . $_POST['formid'] . ".refsetid = " . $_POST['refsetid'] . " AND sub_" . $_POST['formid'] . ".referenceid = referenceset_" . $_POST['refsetid'] . ".id;\" > " . ABS_PATH . "export/" . $filename . ".tsv" );

	    } else {

		// echo sub final;

		$filename = $filename . "-sub_" . $_POST['formid'] . "-refset_" . $_POST['refsetid'] . "-sub-final";

{'fraction_non_alphanumeric': 0.21065729761381935, 'fraction_numerical': 0.005416483677353243, 'mean_word_length': 2.762114537444934, 'pattern_counts': {'":': 11, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3707142', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2364, 'n_words': 533}