* Luke Stevens, Murdoch Childrens Research Institute https://www.mcri.edu.au
 * Version date 16-Nov-2015 
 */
require_once dirname(__FILE__) . '/config.php';
// Validate ids
if (!isset($_POST['report_ids'])) {
    exit('0');
}
// Remove comma on end
if (substr($_POST['report_ids'], -1) == ',') {
    $_POST['report_ids'] = substr($_POST['report_ids'], 0, -1);
}
// Create array of report_ids
$new_report_ids = explode(",", $_POST['report_ids']);
// Get existing list of reports to validate and compare number of items
$old_report_ids = array_keys(LongitudinalReports::getReportNames());
// Determine if any new report_ids were maliciously added
$extra_report_ids = array_diff($new_report_ids, $old_report_ids);
if (!empty($extra_report_ids)) {
    exit('0');
}
// Determine if any new reports were added by another user simultaneously and are not in this list
$append_report_ids = array_diff($old_report_ids, $new_report_ids);
/*// Set all report_orders to null
$sql = "update redcap_reports set report_order = null where project_id = $project_id";
db_query($sql);*/
// Loop through report_ids and set new report_order
$report_order = 1;
$import = array();
foreach ($new_report_ids as $this_report_id) {
    /*	$sql = "update redcap_reports set report_order = ".$report_order++." 
<?php

/* 
 * Longitudinal Reports Plugin
 * Luke Stevens, Murdoch Childrens Research Institute https://www.mcri.edu.au
 * Version date 16-Nov-2015 
 */
require_once dirname(__FILE__) . '/config.php';
// Get html report table
list($report_table, $num_results_returned) = LongitudinalReports::doReport($_POST['report_id'], 'report', 'html', false, false, false, false, false, false, false, false, false, false, false, isset($_GET['instruments']) ? explode(',', $_GET['instruments']) : array(), isset($_GET['events']) ? explode(',', $_GET['events']) : array());
// Display report and title and other text
print "<div id='report_div' style='margin:10px 0 20px;'>" . RCView::div(array('style' => ''), RCView::div(array('class' => 'hide_in_print', 'style' => 'float:left;width:350px;'), RCView::div(array('style' => 'font-weight:bold;'), $lang['custom_reports_02'] . RCView::span(array('style' => 'margin-left:5px;color:#800000;font-size:15px;'), User::number_format_user($num_results_returned))) . RCView::div(array('style' => ''), $lang['custom_reports_03'] . RCView::span(array('style' => 'margin-left:5px;'), User::number_format_user(LongitudinalRecords::getRecordCount())) . (!$longitudinal ? "" : RCView::div(array('style' => 'margin-top:3px;color:#888;font-size:11px;font-family:tahoma,arial;'), $lang['custom_reports_09'])))) . RCView::div(array('class' => 'hide_in_print', 'style' => 'float:left;'), ($user_rights['data_export_tool'] == '0' ? '' : RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "showExportFormatDialog('{$_POST['report_id']}');", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'go-down.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_12']))) . RCView::SP . RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "window.print();", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'printer.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_13'])) . RCView::SP . ($_POST['report_id'] == 'ALL' || $_POST['report_id'] == 'SELECTED' || !$user_rights['reports'] ? '' : RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "window.location.href = '" . APP_PATH_WEBROOT . LR_PATH_FROM_WEBROOT . "index.php?pid=" . PROJECT_ID . "&report_id={$_POST['report_id']}&addedit=1';", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'pencil_small.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_14'])))) . RCView::div(array('class' => 'clear'), '')) . RCView::div(array('id' => 'this_report_title', 'style' => 'margin:40px 0 8px;padding:5px 3px;color:#800000;font-size:18px;font-weight:bold;'), LongitudinalReports::getReportNames($_POST['report_id'])) . $report_table . "</div>";
}
// Place all HTML here
$html = "";
## CREATE NEW REPORT
if (isset($_GET['addedit'])) {
    // Hidden dialog for help with filters and AND/OR logic
    $html .= LongitudinalReports::renderFilterHelpDialog();
    // Hidden dialog for error popup when field name entered is not valid
    $html .= RCView::div(array('id' => 'VarEnteredNoExist_dialog', 'class' => 'simpleDialog'), $lang['report_builder_72']);
    // Add the actual "create report" table's HTML at the very bottom since we're doing a direct print. So output the buffer and disable buffering.
    ob_end_flush();
} elseif (!isset($_GET['report_id'])) {
    $html .= RCView::p(array('style' => 'max-width:810px;margin:5px 0 15px;'), $lang['report_builder_117']) . RCView::div(array('id' => 'report_list_parent_div'), LongitudinalReports::renderReportList());
} elseif (isset($_GET['report_id']) && (is_numeric($_GET['report_id']) || in_array($_GET['report_id'], array('ALL', 'SELECTED')))) {
    // Get report name
    $report_name = LongitudinalReports::getReportNames($_GET['report_id'], !$user_rights['reports']);
    // If report name is NULL, then user doesn't have Report Builder rights AND doesn't have access to this report
    if ($report_name === null) {
        $html .= RCView::div(array('class' => 'red'), $lang['global_01'] . $lang['colon'] . " " . $lang['data_export_tool_180']);
    } else {
        // Display progress while report loads via ajax
        $html .= RCView::div(array('id' => 'report_load_progress', 'style' => 'display:none;margin:5px 0 25px 20px;color:#777;font-size:18px;'), RCView::img(array('src' => 'progress_circle.gif', 'class' => 'imgfix2')) . $lang['report_builder_60'] . " \"" . RCView::span(array('style' => 'color:#800000;font-size:18px;'), $report_name) . "\"" . RCView::span(array('id' => 'report_load_progress_pagenum_text', 'style' => 'display:none;margin-left:10px;color:#777;font-size:14px;'), "({$lang['global_14']} " . RCView::span(array('id' => 'report_load_progress_pagenum'), '1') . ")")) . RCView::div(array('id' => 'report_load_progress2', 'style' => 'display:none;margin:5px 0 0 20px;color:#999;font-size:18px;'), RCView::img(array('src' => 'hourglass.png', 'class' => 'imgfix2')) . $lang['report_builder_115']);
        // Div where report will go
        $html .= RCView::div(array('id' => 'report_parent_div', 'style' => ''), '');
    }
}
// Header
include APP_PATH_DOCROOT . 'ProjectGeneral/header.php';
print RCView::div(array('style' => 'max-width:750px;margin-bottom:10px;'), RCView::div(array('style' => 'color: #800000;font-size: 16px;font-weight: bold;float:left;'), "Longitudinal Reports") . RCView::div(array('class' => 'clear'), ''));
// JavaScript files
callJSfile('jquery_tablednd.js');