Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
- Read form-level permissions for API exports
- Fix display of empty combined checkbox field
  • Loading branch information
lsgs committed Feb 21, 2024
1 parent 00338f6 commit f7c195f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
39 changes: 38 additions & 1 deletion ExtendedReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ protected function apiReportExport() {
}

try {
list($data_content, $num_records_returned) = $report->doExtendedReport($format, $csvDelimiter, $decimalCharacter);
list($data_content, $num_records_returned) = $report->doExtendedReport($format, null, $csvDelimiter, $decimalCharacter);
} catch (\Exception $ex) {
switch ($returnFormat) {
case 'csv': $data_content = $ex->getMessage(); break;
Expand Down Expand Up @@ -624,4 +624,41 @@ public function stripTabs($str, $replace=" ") {
}
return $rtn;
}

/**
* getUserRights($username=null)
* Patched version of REDCap::getUserRights() developer method that until at least v14.1.6 does not return form-level export permissions
*/
public static function getUserRights($username=null)
{
global $data_resolution_enabled, $Proj;
// Make sure we are in the Project context
if (empty($Proj)) return; // self::checkProjectContext(__METHOD__);
// Get rights for this user or all users in project
$rights = \UserRights::getPrivileges(PROJECT_ID, $username);
$rights = $rights[PROJECT_ID];
// Loop through each user
if (!is_array($rights)) return [];
foreach ($rights as $this_user=>$attr) {
// Parse form-level rights
$allForms = explode("][", substr(trim($attr['data_entry']), 1, -1));
foreach ($allForms as $forminfo)
{
list($this_form, $this_form_rights) = explode(",", $forminfo, 2);
$rights[$this_user]['forms'][$this_form] = $this_form_rights;
unset($rights[$this_user]['data_entry']);
}
$allFormsExport = explode("][", substr(trim($attr['data_export_instruments']), 1, -1));
foreach ($allFormsExport as $forminfoExport)
{
list($this_form_export, $this_form_export_rights) = explode(",", $forminfoExport, 2);
$rights[$this_user]['forms_export'][$this_form_export] = $this_form_export_rights;
unset($rights[$this_user]['data_export_instruments']);
}
// Data resolution workflow: disable rights if module is disabled
if ($data_resolution_enabled != '2') $rights[$this_user]['data_quality_resolution'] = '0';
}
// Return rights
return $rights;
}
}
8 changes: 6 additions & 2 deletions Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ protected function doSqlReport() {
protected function doReshapedReport($format) {
global $lang, $Proj, $user_rights;

if (defined('USERID') && empty($user_rights)) { // e.g. api exports
$user_rights = $this->module::getUserRights(USERID)[USERID]; // patched version of REDCap::getUserRights()
}

$report_data = self::getReport($this->report_id, $format); // \REDCap::getReport($this->report_id, 'array', false, false); // note \REDCap::getReport() does not work for superusers

// work our what our reshaped colmns are and thereby the way to reference the report data array for each reshaped row
Expand Down Expand Up @@ -1279,14 +1283,14 @@ protected function makeChoiceDisplay($val, $fieldName, $format, $decimalCharacte
$selLbls[] = $this->module->escape($choices[$valkey]);
}
}
$valstr = implode(', ',$selVals);
$valstr = (count($selVals)==0) ? '' : ' <span class="text-muted">('.implode(', ',$selVals).')</span>';
$lblstr = implode(', ',$selLbls);
switch ($format){
case 'html': // return single value: labels and values of selected checkboxes "Choice 1, Choice 3 (1,3)"
switch ($this->report_attr['report_display_data']) {
case 'LABEL': $outValue = $lblstr; break;
case 'RAW': $outValue = $valstr; break;
default: $outValue = $lblstr.' <span class="text-muted">('.$valstr.')</span>'; // BOTH
default: $outValue = $lblstr.$valstr; // BOTH
}
break;
case 'csv':
Expand Down

0 comments on commit f7c195f

Please sign in to comment.