<?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';
// Create array of all field validation types and their attributes
$allValTypes = getValTypes();
// Operator drop-down list (>, <, =, etc.)
print LongitudinalReports::outputLimiterOperatorDropdown($_POST['field_name'], '', $allValTypes);
// Value text box OR drop-down list (if multiple choice)
print LongitudinalReports::outputLimiterValueTextboxOrDropdown($_POST['field_name'], '');
 public static function getReports($report_id = null)
 {
     global $Proj, $lang, $double_data_entry, $user_rights;
     // Get REDCap validation types
     $valTypes = getValTypes();
     // Array to place report attributes
     $reports = array();
     // If report_id is 0 (report doesn't exist), then return field defaults from tables
     if ($report_id === 0) {
         // || $report_id == 'ALL' || $report_id == 'SELECTED') {
         // Add to reports array
         $reports[$report_id] = getTableColumns('redcap_reports');
         // Pre-fill empty slots for limiters and fields
         $reports[$report_id]['fields'] = array();
         $reports[$report_id]['limiter_fields'] = array();
         $reports[$report_id]['filter_dags'] = array();
         //$reports[$report_id]['limiter_events'] = array();
         $reports[$report_id]['limiter_logic'] = "";
         $reports[$report_id]['user_access_users'] = array();
         $reports[$report_id]['user_access_roles'] = array();
         $reports[$report_id]['user_access_dags'] = array();
         $reports[$report_id]['output_dags'] = 0;
         $reports[$report_id]['output_survey_fields'] = 0;
         $reports[$report_id]['output_schedule_dates'] = array();
         $reports[$report_id]['output_survey_urls'] = array();
         // For "new" (to-be created) reports, set Record ID field as first field and first sorting field in report
         $reports[$report_id]['fields'] = array($Proj->table_pk);
         $reports[$report_id]['orderby_field1'] = $Proj->table_pk;
         $reports[$report_id]['orderby_sort1'] = 'ASC';
         // DDE: If user is DDE person 1 or 2, then limit to ONLY their records
         if ($double_data_entry && is_array($user_rights) && $user_rights['double_data'] != 0) {
             if ($reports[$report_id]['limiter_logic'] == '') {
                 $reports[$report_id]['limiter_logic'] = "ends_with([{$Proj->table_pk}], \"--{$user_rights['double_data']}\")";
             } else {
                 $reports[$report_id]['limiter_logic'] = "({$reports[$report_id]['limiter_logic']}) and ends_with([{$Proj->table_pk}], \"--{$user_rights['double_data']}\")";
             }
         }
         // Return array
         return $reports[$report_id];
     }
     $lrProjectData = array();
     $filterExpr = '[project_id] = ' . $Proj->project_id;
     if (is_numeric($report_id)) {
         $filterExpr .= " and [report_id] = {$report_id}";
     }
     $lrProjectData = REDCap::getData(LR_REPORT_DATA_PROJECT_ID, 'array', null, null, null, null, false, false, false, $filterExpr, false, false);
     // If no reports, then return empty array
     if (empty($lrProjectData)) {
         return array();
     }
     foreach ($lrProjectData as $eventId => $reportRecords) {
         foreach ($reportRecords as $rptRec) {
             $rId = $rptRec['report_id'];
             $reports[$rId]['report_id'] = $rId;
             $reports[$rId]['project_id'] = $rptRec['project_id'];
             $reports[$rId]['title'] = $rptRec['title'];
             $reports[$rId]['report_order'] = $rptRec['report_order'];
             $reports[$rId]['user_access'] = $rptRec['user_access'];
             $reports[$rId]['user_access_dags'] = json_decode($rptRec['user_access_dags'], true);
             $reports[$rId]['user_access_roles'] = json_decode($rptRec['user_access_roles'], true);
             $reports[$rId]['user_access_users'] = json_decode($rptRec['user_access_users'], true);
             $reports[$rId]['fields'] = json_decode($rptRec['fields'], true);
             $reports[$rId]['output_dags'] = $rptRec['output_dags'] === "on" ? "1" : "0";
             $reports[$rId]['output_survey_fields'] = $rptRec['output_survey_fields'] === "on" ? "1" : "0";
             $reports[$rId]['output_schedule_dates'] = json_decode($rptRec['output_schedule_dates'], true);
             $reports[$rId]['output_survey_urls'] = json_decode($rptRec['output_survey_urls'], true);
             $reports[$rId]['limiter_fields'] = json_decode($rptRec['limiter_fields'], true);
             $reports[$rId]['filter_dags'] = json_decode($rptRec['filter_dags'], true);
             $reports[$rId]['advanced_logic'] = $rptRec['advanced_logic'];
             $reports[$rId]['orderby_field1'] = $rptRec['orderby_field1'];
             $reports[$rId]['orderby_sort1'] = $rptRec['orderby_sort1'];
             $reports[$rId]['orderby_field2'] = $rptRec['orderby_field2'];
             $reports[$rId]['orderby_sort2'] = $rptRec['orderby_sort2'];
             $reports[$rId]['orderby_field3'] = $rptRec['orderby_field3'];
             $reports[$rId]['orderby_sort3'] = $rptRec['orderby_sort3'];
             $reports[$rId]['limiter_logic'] = "";
             // Will build below
         }
     }
     // Loop through all reports and build the filter logic into a single string
     foreach ($reports as $this_report_id => $rattr) {
         // Advanced logic
         if ($rattr['advanced_logic'] != '') {
             $reports[$this_report_id]['limiter_logic'] = $rattr['advanced_logic'];
         } elseif (!empty($rattr['limiter_fields'])) {
             foreach ($rattr['limiter_fields'] as $i => $attr) {
                 // Translate the limiter item into logic
                 $reports[$this_report_id]['limiter_logic'] .= ($attr['limiter_group_operator'] == 'AND' ? $i == 0 ? "(" : ") AND (" : " OR ") . self::translateLimiterItem($attr);
             }
             // Finish with ending parenthesis
             $reports[$this_report_id]['limiter_logic'] .= ")";
         }
         // DDE: If user is DDE person 1 or 2, then limit to ONLY their records by appending ends_with() onto limiter_logic
         if ($double_data_entry && is_array($user_rights) && $user_rights['double_data'] != 0) {
             if ($reports[$this_report_id]['limiter_logic'] == '') {
                 $reports[$this_report_id]['limiter_logic'] = "ends_with([{$Proj->table_pk}], \"--{$user_rights['double_data']}\")";
             } else {
                 $reports[$this_report_id]['limiter_logic'] = "({$reports[$this_report_id]['limiter_logic']}) and ends_with([{$Proj->table_pk}], \"--{$user_rights['double_data']}\")";
             }
         }
         // Double check to make sure that it truly has SELECTED user access
         if ($rattr['user_access'] == 'SELECTED' && empty($rattr['user_access_users']) && empty($rattr['user_access_roles']) && empty($rattr['user_access_dags'])) {
             $reports[$this_report_id]['user_access'] = 'ALL';
         }
         // Make sure that Order By fields are NOT checkboxes (because that doesn't make sense)
         if ($Proj->isCheckbox($reports[$this_report_id]['orderby_field3'])) {
             $reports[$this_report_id]['orderby_field3'] = $reports[$this_report_id]['orderby_sort3'] = '';
         }
         if ($Proj->isCheckbox($reports[$this_report_id]['orderby_field2'])) {
             $reports[$this_report_id]['orderby_field2'] = $reports[$this_report_id]['orderby_field3'];
             $reports[$this_report_id]['orderby_sort2'] = $reports[$this_report_id]['orderby_sort3'];
             $reports[$this_report_id]['orderby_field3'] = $reports[$this_report_id]['orderby_sort3'] = '';
         }
         if ($Proj->isCheckbox($reports[$this_report_id]['orderby_field1'])) {
             $reports[$this_report_id]['orderby_field1'] = $reports[$this_report_id]['orderby_field2'];
             $reports[$this_report_id]['orderby_sort1'] = $reports[$this_report_id]['orderby_sort2'];
             $reports[$this_report_id]['orderby_field2'] = $reports[$this_report_id]['orderby_field3'];
             $reports[$this_report_id]['orderby_sort2'] = $reports[$this_report_id]['orderby_sort3'];
             $reports[$this_report_id]['orderby_field3'] = $reports[$this_report_id]['orderby_sort3'] = '';
         }
     }
     // Return array of report(s) attributes
     if ($report_id == null) {
         return $reports;
     } else {
         return $reports[$report_id];
     }
 }