$reportData['advanced_logic'] = $_POST['advanced_logic'];
$reportData['filter_dags'] = json_encode($filter_dags);
$reportData['orderby_field1'] = $orderby_field1;
$reportData['orderby_sort1'] = $orderby_sort1;
$reportData['orderby_field2'] = $orderby_field2;
$reportData['orderby_sort2'] = $orderby_sort2;
$reportData['orderby_field3'] = $orderby_field3;
$reportData['orderby_sort3'] = $orderby_sort3;
$reportData['update_by'] = $userid;
$reportData['update_at'] = $now->format('Y-m-d H:i:s');
$reportData['report_complete'] = '2';
$success = true;
$data = array();
$data[] = $reportData;
// Can handle multiple records - not needed here
$success = LongitudinalReports::save($data);
// If there are errors, then roll back all changes
if (!$success) {
    //$errors > 0) {
    // Errors occurred, so undo any changes made
    //	db_query("ROLLBACK");
    // Return '0' for error
    exit('0');
} else {
    // Logging
    $log_descrip = $_GET['report_id'] != 0 ? "Edit longitudinal report" : "Create longitudinal report";
    REDCap::logEvent($log_descrip, "report_id = {$report_id}: " . print_r($reportData, true));
    // Commit changes
    //	db_query("COMMIT");
    // Response
    $dialog_title = RCView::img(array('src' => 'tick.png', 'style' => 'vertical-align:middle')) . RCView::span(array('style' => 'color:green;vertical-align:middle'), $lang['report_builder_01']);
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++." 
    			where project_id = $project_id and report_id = $this_report_id";
    	db_query($sql);*/
    $rpt = array();
    $rpt['report_id'] = $this_report_id;
    $rpt['report_order'] = $report_order++;
    $import[] = $rpt;
}
// Deal with orphaned report_ids added simultaneously by other user while this user reorders
foreach ($append_report_ids as $this_report_id) {
    /*	$sql = "update redcap_reports set report_order = ".$report_order++." 
    			where project_id = $project_id and report_id = $this_report_id";
    	db_query($sql);*/
    $rpt = array();
    $rpt['report_id'] = $this_report_id;
    $rpt['report_order'] = $report_order++;
    $import[] = $rpt;
}
$success = LongitudinalReports::save($import);
if (!$success) {
    exit('0');
}
// Logging
REDCap::logEvent("Reorder longitudinal reports", "report_id = " . $_POST['report_ids']);
// Return Value: If there are some extra reports that exist that are not currently in the list, then refresh the user's page
print !empty($append_report_ids) ? '2' : '1';