Skip to content

Commit

Permalink
Merge pull request #11 from susom/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Jordan-M-Schultz authored Feb 4, 2025
2 parents 2b357df + 9f533ca commit c776e89
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 131 deletions.
71 changes: 30 additions & 41 deletions IntakeDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once "Utilities/Sanitizer.php";
//require_once "classes/ModuleCore/ModuleCore.php";
require_once("classes/Child.php");
require_once ("classes/DashboardUtil.php");

use ExternalModules;
use Exception;
Expand Down Expand Up @@ -413,10 +414,17 @@ public function handleGlobalError($e): false|string
{
$msg = $e->getMessage();
$this->emError("Error: $msg");
return json_encode([
"error" => $msg,
http_response_code(500); // Send HTTP 500 status
header('Content-Type: application/json'); // Ensure JSON response
echo json_encode([
"error" => $e->getMessage(),
"success" => false
]);
exit;
// return json_encode([
// "error" => $msg,
// "success" => false
// ]);
}

/**
Expand Down Expand Up @@ -588,28 +596,6 @@ public function checkChildDataExists($universalId, $pid){

}

/**
* @param $project
* @param $fields
* @return array
* Removes hidden fields before sending the contents of getData to client
*/
public function filterHiddenFields($project, &$fields) {
$new = [];
$excluded = ["requester_lookup", "pi_lookup", "one_lookup"];

foreach($fields as $k => $v) {
if($project->metadata[$k] && (str_contains($project->metadata[$k]['misc'],'HIDDEN') || in_array($project->metadata[$k], $excluded))) { // Hidden fields should not be shown to client
unset($fields[$k]);
} else {
$label = trim($project->metadata[$k]['element_label']);
if(!empty($label)){
$new[$label] = $v;
}
}
}
return $new;
}
/**
* @param $universalId
* @return false|string
Expand All @@ -631,8 +617,12 @@ public function fetchRequiredSurveys($payload)

$requiredChildPIDs = $this->getRequiredChildPIDs($completedIntake, $projectSettings);


//Parse fields & convert labels for UI render of submitted form
$pretty = $completedIntake[0];
$pretty = $this->filterHiddenFields($project, $pretty);
$excluded = ["requester_lookup", "pi_lookup", "one_lookup", "webauth_user"];
$du = new DashboardUtil($this, $projectSettings);
$pretty = $du->prepareFieldsForRender($parentId, $pretty, $excluded);

$childSurveys = $project->surveys;
$mutableUrl = [];
Expand Down Expand Up @@ -705,20 +695,9 @@ private function fetchParentRecordData($parentId, $universalId, $event, $form =
private function getRequiredChildPIDs($completedIntake, $projectSettings)
{
$requiredChildPIDs = [];
$firstRecord = reset($completedIntake);

foreach($projectSettings['project-id'] as $pid)
$requiredChildPIDs[] = $pid;

// foreach ($firstRecord as $key => $value) {
// if ($this->isValidServiceKey($key, $value)) {
// $mappingKey = array_search($key, $projectSettings['mapping-field']);
// if ($mappingKey !== false) {
// $requiredChildPIDs[] = $projectSettings['project-id'][$mappingKey];
// }
// }
// }

return $requiredChildPIDs;
}

Expand Down Expand Up @@ -762,18 +741,24 @@ private function newChildRequest($payload){
}
}

/**
* Given a child PID + Universal record ID: determine
* @param $payload
* @return false|string|void
*/
private function getChildSubmissions($payload){
try {
if(empty($payload['child_id']) || empty($payload['universal_id']))
if(empty($payload['child_pid']) || empty($payload['universal_id']))
throw new \Exception("Missing child ID or Universal ID");

$parent_id = $this->getSystemSetting('parent-project');
$pSettings = $this->getProjectSettings($parent_id);

$child = new Child($this, $payload['child_id'], $parent_id, $pSettings);
$child = new Child($this, $payload['child_pid'], $parent_id, $pSettings);

$submissions = $child->childRecordExists($payload['universal_id']);
// Grab all field names for first two required surveys

// Grab first form name for this child
$mainSurvey = $child->getMainSurveyFormName();

//Iterate over all submissions to determine if we need to render a return link
Expand All @@ -786,8 +771,13 @@ private function getChildSubmissions($payload){
// Grab timestamp for dashboard display
$submissions[$in]['survey_completion_ts'] = $child->getSurveyTimestamp($submission['record_id']);

// Set default field to make it easier to render completion status on dashboard (form name agnostic)
// Set default field to make it easier to render survey completion status on dashboard (form name agnostic)
$submissions[$in]['child_survey_complete'] = $submission[$completionVariable];

//Get pretty form to render submitted information
$du = new DashboardUtil($this, $pSettings);
$pretty = $du->prepareFieldsForRender($payload['child_pid'], $submissions[$in], [], $mainSurvey);
$submissions[$in]['completed_form_pretty'] = $pretty;
}

return json_encode(["data" => $submissions, "success" => true]);
Expand Down Expand Up @@ -870,5 +860,4 @@ private function preCreateChildRecord($childProjectId, $universalId)

return array_key_first($response['ids']);
}

}
1 change: 0 additions & 1 deletion classes/Child.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Stanford\IntakeDashboard;
use REDCap;
use Survey;
use Project;

class Child {
private $module;
Expand Down
99 changes: 99 additions & 0 deletions classes/DashboardUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Stanford\IntakeDashboard;
use REDCap;

class DashboardUtil
{
private $module;
private $emSettings;

public function __construct($module, $settings)
{
$this->setModule($module);
$this->setEMSettings($settings);
}

/**
* @param $projectId
* @param $fields
* @return array
* Removes hidden fields before sending the contents of getData to client
*/
public function prepareFieldsForRender($projectId, $fields, $excluded = [], $form = ""): array
{
$project = new \Project($projectId);
$new = [];

//If specific form is provided, only render that form's data
if(!empty($form)) {
$formFields = json_decode(REDCap::getDataDictionary($projectId, 'json', true, null, $form), true);
$formFieldHash = [];
foreach($formFields as $field) {
$formFieldHash[$field['field_name']] = 1;
}
foreach($fields as $variableName => $val){
if(!array_key_exists($variableName, $formFieldHash))
unset($fields[$variableName]);
}
}

foreach($fields as $k => $v) {
$label = isset($project->metadata[$k]) ? trim($project->metadata[$k]['element_label']) : null;
if(!empty($label)){
// Hidden fields should not be shown to client
if(str_contains($project->metadata[$k]['misc'],'HIDDEN') || in_array($project->metadata[$k], $excluded))
continue;

//Change key from variable to label for UI display
$new[$k]["value"] = $v;
$new[$k]["label"] = $label;
if(isset($project->metadata[$k]['element_enum'])) {
//Check if field has enum values, if so, replace (checkbox, etc)
$parsed = $this->parseEnumField($project->metadata[$k]['element_enum']);
if(array_key_exists($v, $parsed))
$new[$k]["value"] = $parsed[$v];

}
}
}
return $new;
}

public function parseEnumField($str): array
{
//REDCap uses explicit \n embedded into strings
$lines = explode("\\n", $str);

$parsedArray = [];
foreach ($lines as $line) {
$parts = array_map('trim', explode(',', $line, 2)); // Split only on first comma, trim spaces
if (count($parts) === 2) {
$parsedArray[(int)$parts[0]] = $parts[1]; // Convert key to integer
}
}

// Output the parsed array
return $parsedArray;
}

private function setModule($module)
{
$this->module = $module;
}

private function setEMSettings($settings)
{
$this->emSettings = $settings;
}

public function getModule()
{
return $this->module;
}

public function getEMSettings()
{
return $this->emSettings;
}
}
1 change: 1 addition & 0 deletions dashboard-ui/dist/assets/index-D_VeKvQr.css

Large diffs are not rendered by default.

Loading

0 comments on commit c776e89

Please sign in to comment.