Skip to content

Commit

Permalink
Merge pull request #105 from AndrewPoppe/logging_page_bug
Browse files Browse the repository at this point in the history
Address #104 Logging page bug
  • Loading branch information
AndrewPoppe authored Aug 15, 2024
2 parents 0c1619f + 85ac9ad commit ab86db5
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 69 deletions.
98 changes: 86 additions & 12 deletions REDCapPRO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,29 +1028,57 @@ public function getUserRole(string $username)
public function getAllUsers()
{
$projects = $this->framework->getProjectsWithModuleEnabled();
$staff = $this->getStaff();
$allUsers = $this->getAllUserInfo();
$users = array();
foreach ( $projects as $pid ) {
$project = new Project($this, $pid);
$staff_arr = $project->getStaff();
$all_staff = $staff_arr["allStaff"];
$all_staff = $staff[$pid]["allStaff"];
foreach ( $all_staff as $user ) {
if ( isset($users[$user]) ) {
array_push($users[$user]['projects'], $pid);
} else {
$newUser = $this->framework->getUser($user);
$newUserArr = [
"username" => $user,
"email" => $newUser->getEmail(),
"name" => $this->getUserFullname($user),
"projects" => [ $pid ]
];
$users[$user] = $newUserArr;
$newUser = $allUsers[$user];
$newUser['projects'] = [ $pid ];
$users[$user] = $newUser;
}
}
}
return $users;
}

public function getAllUserInfo() {
$sql = 'SELECT username, user_email AS email, CONCAT(user_firstname, " ", user_lastname) AS name FROM redcap_user_information';
$result = $this->framework->query($sql, []);
$userInfo = [];
while ($row = $result->fetch_assoc()) {
$userInfo[$row['username']] = $row;
}
return $userInfo;
}

public function getStaff()
{
try {
$sql = "SELECT * FROM redcap_external_module_settings
WHERE external_module_id = (SELECT external_module_id FROM redcap_external_modules WHERE directory_prefix = ?)
AND `key` in ('managers', 'users', 'monitors')";
$result = $this->framework->query($sql, [ $this->framework->getModuleInstance()->PREFIX ]);
$staff = [];
while ($row = $result->fetch_assoc()) {
$thisStaff = json_decode($row['value']);
$staff[$row['project_id']][$row['key']] = $thisStaff;
if (!isset($staff[$row['project_id']]['allStaff'])) {
$staff[$row['project_id']]['allStaff'] = [];
}
$staff[$row['project_id']]['allStaff'] = array_merge($staff[$row['project_id']]['allStaff'], $thisStaff);
}

return $staff;
} catch (\Exception $e) {
$this->module->logError("Error getting staff", $e);
}
}

public function safeGetUsername() : string
{
try {
Expand Down Expand Up @@ -1137,7 +1165,7 @@ public function logEvent(string $message, array $parameters)
*
* @return string
*/
private function getModuleToken()
public function getModuleToken()
{
$moduleToken = $this->getSystemSetting("module_token");
if ( !isset($moduleToken) ) {
Expand Down Expand Up @@ -1281,4 +1309,50 @@ public function getProjectlessUrl(string $path, bool $noAuth, bool $useApiEndpoi
$_GET['pid'] = $pid;
return $result;
}

/**
* To create test logs in the database. Mostly for testing/debugging
* @param int $numberToCreate number of logs to create
* @return bool success
*/
public function createTestLogs(int $numberToCreate) : bool
{
$message = "TEST";
$projects = [ NULL, 51 ];
$redcap_users = [ "test_user", "test" ];
$fname = "Test";
$lname = "Person";
$rcpro_participant_ids = [ 1, 2, 3, 4, 5 ];
$rcpro_usernames = [ 'test1', 'test2' ];
try {
for ( $i = 0; $i < $numberToCreate; $i++ ) {
$parameters = [
"project_id" => $projects[array_rand($projects)],
"pid" => $projects[array_rand($projects)],
"redcap_user" => $redcap_users[array_rand($redcap_users)],
"rcpro_participant_id" => $rcpro_participant_ids[array_rand($rcpro_participant_ids)],
"rcpro_username" => $rcpro_usernames[array_rand($rcpro_usernames)],
"fname" => $fname,
"lname" => $lname
];
$this->logEvent($message, $parameters);
}
return true;
} catch (\Throwable $e) {
$this->logError("Error Creating Test Logs", $e);
return false;
}
}

public function removeTestLogs() : bool
{
try {
$token = $this->getModuleToken();
$this->framework->removeLogs("module_token = ? AND message = 'TEST'", [ $token ]);
return true;
} catch (\Throwable $e) {
$this->logError('Error Removing Test Logs', $e);
return false;
}
}
}
96 changes: 74 additions & 22 deletions src/cc_logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.25/b-1.7.1/b-colvis-1.7.1/b-html5-1.7.1/cr-1.5.4/date-1.1.0/sb-1.1.0/sp-1.3.0/sl-1.3.3/datatables.min.css" />
<script type="text/javascript"
src="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.25/b-1.7.1/b-colvis-1.7.1/b-html5-1.7.1/cr-1.5.4/date-1.1.0/sb-1.1.0/sp-1.3.0/sl-1.3.3/datatables.min.js"
defer></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" defer></script>
<link href="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/date-1.5.3/sr-1.4.1/datatables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/date-1.5.3/sr-1.4.1/datatables.min.js" integrity="sha512-Dp027lG1kw+OxJjCYvBw8jT9Buw65dP8HYTtfUQ8DAeBUcYn/urzTlQ8jbWXENZaYnujGvQlgJT21m+GsaCH5w==" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.14.0/jquery-ui.js" integrity="sha512-cxRn/7O+JWSqvBOGeODaC+v9wr7RFllEwd6SBAub5ZuBPEzBtYZXTx46KNnlzvEfG42CzbwfscbBYZYcgrXKNA==" crossorigin="anonymous" defer></script>
<link rel="stylesheet" type="text/css" href="<?= $module->getUrl("src/css/rcpro_cc.php") ?>">
<?php

Expand Down Expand Up @@ -62,10 +59,26 @@ function logExport(type) {
$(document).ready(function () {
let dataTable = $('#RCPRO_TABLE').DataTable({
deferRender: true,
serverSide: true,
processing: true,
searchDelay: 1000,
order: [[0, 'desc']],
ajax: function (data, callback, settings) {
RCPRO_module.ajax('getLogs', { cc: true })
const payload = {
draw: data.draw,
search: data.search,
start: data.start,
length: data.length,
order: data.order,
columns: data.columns,
minDate: $('#min').val(),
maxDate: $('#max').val(),
cc: true
}
tHalf = performance.now();
RCPRO_module.ajax('getLogs', payload)
.then(response => {
callback({ data: response });
callback(response);
})
.catch(error => {
console.error(error);
Expand Down Expand Up @@ -97,25 +110,19 @@ className: "dt-center"
});
});
},
dom: 'lBfrtip',
layout: {
topStart: ['pageLength', 'buttons'],
topEnd: 'search'
},
stateSave: true,
stateSaveCallback: function (settings, data) {
localStorage.setItem('DataTables_cclogs_' + settings.sInstance, JSON.stringify(data))
},
stateLoadCallback: function (settings) {
return JSON.parse(localStorage.getItem('DataTables_cclogs_' + settings.sInstance))
},
colReorder: true,
buttons: [{
extend: 'searchPanes',
config: {
cascadePanes: true,
}

},
{
extend: 'searchBuilder',
},
colReorder: false,
buttons: [
'colvis',
{
text: 'Restore Default',
Expand Down Expand Up @@ -147,7 +154,53 @@ className: "dt-center"
],
scrollX: true,
scrollY: '60vh',
scrollCollapse: true
scrollCollapse: true,
initComplete: function() {
this.api()
.columns()
.every(function () {
var column = this;
var title = column.header().textContent;

if (title.toLowerCase() === 'timestamp') {
$('<br><input type="text" class="form-control form-control-sm" style="min-width:140px;" id="min" placeholder="Min timestamp" />')
.val(column.search())
.appendTo($(column.header()))
.on('click', function (e) {
e.stopPropagation();
})
.on('change clear', function (e) {
$('#RCPRO_TABLE').DataTable().search('').draw();
});
$('<input type="text" class="form-control form-control-sm" style="min-width:140px;" id="max" placeholder="Max timestamp" />')
.val(column.search())
.appendTo($(column.header()))
.on('click', function (e) {
e.stopPropagation();
})
.on('change clear', function (e) {
$('#RCPRO_TABLE').DataTable().search('').draw();
});
var minDate = new DateTime('#min');
var maxDate = new DateTime('#max');
return;
}

// Create input element and add event listener
$('<br><input type="text" class="form-control form-control-sm" style="min-width:140px;" placeholder="Search ' + title + '" />')
.val(column.search())
.appendTo($(column.header()))
.on('click', function (e) {
e.stopPropagation();
})
.on('click keyup change clear', function (e) {
if (column.search() !== this.value) {
column.search(this.value).draw();
}
});
});
dataTable.columns.adjust();
}
});

$('#logs').removeClass('dataTableParentHidden');
Expand All @@ -160,7 +213,6 @@ className: "dt-center"
$('.dt-button-collection').draggable();
}
});
dataTable.columns.adjust().draw();
});
}(window.jQuery, window, document));
</script>
Expand Down
24 changes: 22 additions & 2 deletions src/cc_participants.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
$ui = new UI($module);
$ui->ShowControlCenterHeader("Participants");

?>
<link href="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.js" integrity="sha512-tQIUNMCB0+K4nlOn4FRg/hco5B1sf4yWGpnj+V2MxRSDSVNPD84yzoWogPL58QRlluuXkjvuDD5bzCUTMi6MDw==" crossorigin="anonymous"></script>
<?php
$participantHelper = new ParticipantHelper($module);

if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
Expand Down Expand Up @@ -266,7 +270,9 @@
}

$(document).ready(function () {

var t0 = performance.now();
var t1,t2;
console.log('start: ',t0);
// Function for resetting manage-form values
window.clearForm = function () {
$("#toReset").val("");
Expand All @@ -281,12 +287,21 @@
}

let dataTable = $('#RCPRO_TABLE').DataTable({
dom: 'lBfrtip',
// dom: 'lBfrtip',
// layout: {
// topStart: ['pageLength'],
// topEnd: 'search'
// },
stateSave: true,
deferRender: true,
processing: true,
ajax: function (data, callback, settings) {
t0 = performance.now();
RCPRO_module.ajax('getParticipantsCC', {})
.then(response => {
t1 = performance.now();
//console.log('Got data: ', t1);
console.log('Processing: ', t1-t0);
callback({ data: response });
})
.catch(error => {
Expand Down Expand Up @@ -416,6 +431,11 @@ className: "dt-center",
sScrollX: '100%',
scrollCollapse: true,
pageLength: 100,
initComplete: function() {
t2 = performance.now();
console.log('Render: ', t2-t1);
console.log('End: ', t2);
}
});
$('#participants-form').removeClass('dataTableParentHidden');
$('#loading-container').hide();
Expand Down
6 changes: 5 additions & 1 deletion src/cc_projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<!DOCTYPE html>
<html lang="en">
<title>REDCapPRO Projects</title>
<link href="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.js" integrity="sha512-tQIUNMCB0+K4nlOn4FRg/hco5B1sf4yWGpnj+V2MxRSDSVNPD84yzoWogPL58QRlluuXkjvuDD5bzCUTMi6MDw==" crossorigin="anonymous"></script>

<link rel="stylesheet" type="text/css" href="<?= $module->getUrl("src/css/rcpro_cc.php") ?>">

<?php
Expand Down Expand Up @@ -49,8 +52,9 @@
const RCPRO_module = <?= $module->getJavascriptModuleObjectName() ?>;
$(document).ready(function () {
let dataTable = $('#RCPRO_TABLE').DataTable({
dom: 'lftip',
// dom: 'lftip',
deferRender: true,
processing: true,
ajax: function (data, callback, settings) {
RCPRO_module.ajax('getProjectsCC', {})
.then(response => {
Expand Down
6 changes: 5 additions & 1 deletion src/cc_staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
echo '<link rel="stylesheet" type="text/css" href="' . $module->getUrl("src/css/rcpro_cc.php") . '">';
$module->initializeJavascriptModuleObject();
?>
<link href="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/v/bs5/jszip-3.10.1/dt-2.1.3/b-3.1.1/b-colvis-3.1.1/b-html5-3.1.1/sr-1.4.1/datatables.min.js" integrity="sha512-tQIUNMCB0+K4nlOn4FRg/hco5B1sf4yWGpnj+V2MxRSDSVNPD84yzoWogPL58QRlluuXkjvuDD5bzCUTMi6MDw==" crossorigin="anonymous"></script>

<div id="loading-container" class="loader-container">
<div id="loading" class="loader"></div>
</div>
Expand Down Expand Up @@ -44,6 +47,7 @@
$(document).ready(function () {
let dataTable = $('#RCPRO_TABLE').DataTable({
deferRender: true,
processing: true,
ajax: function (data, callback, settings) {
RCPRO_module.ajax('getStaffCC', {})
.then(response => {
Expand Down Expand Up @@ -92,7 +96,7 @@ className: "dt-center",
}
},
],
dom: 'lBfrtip',
// dom: 'lBfrtip',
stateSave: true,
stateSaveCallback: function (settings, data) {
localStorage.setItem('DataTables_ccstaff_' + settings.sInstance, JSON.stringify(data))
Expand Down
Loading

0 comments on commit ab86db5

Please sign in to comment.