Skip to content

Commit

Permalink
Store user-dag mapping in modules settings, emove config default values
Browse files Browse the repository at this point in the history
1. Change so user-dag mappings stored in module project setting instead of redcap_log_event table so can get copied when copying a project.
2. Remove default values from config.json as deprecated (instead set defaults when enabling module).
  • Loading branch information
lsgs committed Mar 15, 2019
1 parent 6a84a5e commit 4de19fe
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 73 deletions.
105 changes: 55 additions & 50 deletions DAGSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class DAGSwitcher extends AbstractExternalModule
private $Proj;

public static $IgnorePages = array('FileRepository','ProjectSetup','ExternalModules','UserRights','DataAccessGroups','SendItController');

private static $SettingDefaults = array(
"dag-switcher-block-text-pre" => "Current Data Access Group: ",
"dag-switcher-block-text-post" => " ",
"dag-switcher-dialog-title" => "Switch Data Access Group",
"dag-switcher-dialog-text" => "Select the DAG to switch to: ",
"dag-switcher-dialog-change-button-text" => "Switch",
"dag-switcher-table-block-title" => "DAG Switcher: Enable Multiple DAGs for Users",
"dag-switcher-table-block-info" => "Let users switch between the DAGs enabled for them below.<br>Note: <strong>this does not override a user's <u>current</u> DAG allocation</strong>, as set above or on the User Rights page.",
"dag-switcher-table-row-option-dags" => "Rows are DAGs",
"dag-switcher-table-row-option-users" => "Rows are Users"
);

public function __construct() {
parent::__construct();
Expand Down Expand Up @@ -98,7 +110,8 @@ protected function getPageRoute() {

/**
* Read the current configuration of users and enabled DAGs from the
* most recent DAG Switcher record in redcap_log_event
* user-dag-mapping project setting (or fall back to most recent DAG
* Switcher record in redcap_log_event where stored until v1.2.1)
* @return array
* keys: Usernames
* values: Array of DAGids user may switch to
Expand All @@ -109,38 +122,49 @@ protected function getPageRoute() {
* ]
*/
public function getUserDAGs() {
$userDags = array();

$sql = "select data_values ".
"from redcap_log_event ".
"where project_id = ".db_escape($this->project_id)." ".
"and description = '".db_escape($this->getModuleName())."' ".
"order by log_event_id desc limit 1 ";
$r = db_query($sql);
if ($r->num_rows > 0) {
while ($row = $r->fetch_assoc()) {
$userDags = json_decode($row['data_values'], true);
}
$updateConfig = false;
$userDags = json_decode($this->getProjectSetting('user-dag-mapping'), true);

if (!is_array($userDags)) {
$userDags = array();

$sql = "select data_values ".
"from redcap_log_event ".
"where project_id = ".db_escape($this->project_id)." ".
"and description = '".db_escape($this->getModuleName())."' ".
"order by log_event_id desc limit 1 ";
$r = db_query($sql);
if ($r->num_rows > 0) {
while ($row = $r->fetch_assoc()) {
$userDags = json_decode($row['data_values'], true);
}
$updateConfig = true;
}
}

// return only valid group_id values (remove any DAGs that have been deleted)
$sql = "select group_id from redcap_data_access_groups where project_id = ".db_escape($this->project_id)." ";
$r = db_query($sql);
if ($r->num_rows > 0) {
$currentDagIds = array();
while ($row = $r->fetch_assoc()) {
$currentDagIds[] = $row['group_id'];
}
$currentDagIds = array();
while ($row = $r->fetch_assoc()) {
$currentDagIds[] = $row['group_id'];
}
}

foreach ($userDags as $user => $dags) {
foreach ($dags as $dagKey => $dagId) {
if ($dagId!=0 && !in_array($dagId, $currentDagIds)) {
unset($userDags[$user][$dagKey]);
$updateConfig = true;
}
}
}

if ($updateConfig) {
$this->setProjectSetting('user-dag-mapping', json_encode($userDags, JSON_PRETTY_PRINT));
}

return $userDags;
}

Expand Down Expand Up @@ -370,8 +394,9 @@ public function saveUserDAG($user, $dag, $enabled) {
}
sort($userDags[$user], SORT_NUMERIC);

// save to new log_event record
REDCap::logEvent(db_escape($this->getModuleName()), json_encode($userDags));
// save to module config
$this->setProjectSetting('user-dag-mapping', json_encode($userDags, JSON_PRETTY_PRINT));
REDCap::logEvent(db_escape($this->getModuleName()), json_encode($userDags, JSON_PRETTY_PRINT));
return '1';
}

Expand Down Expand Up @@ -575,37 +600,6 @@ protected function includeEveryPageJs() {
}
}

// TODO Remove these UI state config methods once they are implemented in AbstractExternalModule (min redcap 8.3.0)

/**
* Return a value from the UI state config. Return null if key doesn't exist.
* @param int/string $key key
* @return mixed - value if exists, else return null
*/
public function getUserSetting($key)
{
return \UIState::getUIStateValue($this->project_id, self::UI_STATE_OBJECT_PREFIX . $this->PREFIX, $key);
}

/**
* Save a value in the UI state config (e.g., $object = 'sidebar')
* @param int/string $key key
* @param mixed $value value for key
*/
public function setUserSetting($key, $value)
{
\UIState::saveUIStateValue($this->project_id, self::UI_STATE_OBJECT_PREFIX . $this->PREFIX, $key, $value);
}

/**
* Remove key-value from the UI state config
* @param int/string $key key
*/
public function removeUserSetting($key)
{
\UIState::removeUIStateValue($this->project_id, self::UI_STATE_OBJECT_PREFIX . $this->PREFIX, $key);
}

public function apiDagSwitch($RestUtility, $newDag) {
global $Proj;

Expand All @@ -622,4 +616,15 @@ public function apiDagSwitch($RestUtility, $newDag) {

return $this->switchToDAG($newDag);
}

/**
* Set default parameter values (using "default":"xyz" in config.json is deprecated)
* @param type $version
* @param type $project_id
*/
public function redcap_module_project_enable($version, $project_id) {
foreach (self::$SettingDefaults as $settingKey => $settingValue) {
$this->setProjectSetting($settingKey, $settingValue);
}
}
}
50 changes: 27 additions & 23 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
"institution": "Murdoch Children's Research Institute"
}
],

"compatibility": {
"redcap-version-min": "8.7.0"
},

"permissions": [
"hook_every_page_top",
"redcap_every_page_top",
"redcap_module_project_enable",
"read redcap_log_event",
"read redcap_user_rights",
"write redcap_user_rights"
Expand All @@ -24,13 +29,19 @@
"user_dag_switch_api"
],

"links": {
"links": {
},

"system-settings": [
],

"project-settings": [
{
"key": "user-dag-mapping",
"name": "User-DAG Mapping (updated by user/DAG selections on project DAGs page)",
"required": false,
"type": "textarea"
},
{
"key": "dag-switcher-block-text-pre",
"name": "Project page current DAG display text (before current DAG name)",
Expand All @@ -48,51 +59,44 @@
{
"key": "dag-switcher-dialog-title",
"name": "DAG switch dialog title",
"required": true,
"type": "text",
"default": "Switch Data Access Group"
"required": false,
"type": "text"
},
{
"key": "dag-switcher-dialog-text",
"name": "DAG switch dialog text",
"required": true,
"type": "text",
"default": "Select the DAG to switch to:"
"required": false,
"type": "text"
},
{
"key": "dag-switcher-dialog-change-button-text",
"name": "DAG switch dialog Change button text",
"required": true,
"type": "text",
"default": "Switch"
"required": false,
"type": "text"
},
{
"key": "dag-switcher-table-block-title",
"name": "DAG switcher DAG page block title",
"required": true,
"type": "text",
"default": "DAG Switcher: Enable Multiple DAGs for Users"
"required": false,
"type": "text"
},
{
"key": "dag-switcher-table-block-info",
"name": "DAG switcher DAG page block information text",
"required": true,
"type": "text",
"default": "Let users switch between the DAGs enabled for them below.<br>Note: <strong>this does not override a user's <u>current</u> DAG allocation</strong>, as set above or on the User Rights page."
"required": false,
"type": "text"
},
{
"key": "dag-switcher-table-row-option-dags",
"name": "DAG switcher table row option label (DAGs)",
"required": true,
"type": "text",
"default": "Rows are DAGs"
"required": false,
"type": "text"
},
{
"key": "dag-switcher-table-row-option-users",
"name": "DAG switcher table row option label (Users)",
"required": true,
"type": "text",
"default": "Rows are Users"
"required": false,
"type": "text"
}
]
}

0 comments on commit 4de19fe

Please sign in to comment.