Skip to content

Commit

Permalink
Merge pull request #5823 from christianbeeznest/batnum-22048
Browse files Browse the repository at this point in the history
Internal: Add button to show last 10 registered users in user group and session pages - refs BT#22048
  • Loading branch information
NicoDucou authored Oct 2, 2024
2 parents 814672b + de5623b commit 7cf2c49
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 9 deletions.
86 changes: 79 additions & 7 deletions main/admin/add_users_to_usergroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ function change_select(reset) {
});
}
</script>';
$htmlHeadXtra[] = '
<script>
$(document).ready(function() {
function showLastTenUsers() {
var selectedUsers = [];
$("#elements_in option").each(function() {
selectedUsers.push($(this).val());
});
var groupId = "'.$id.'";
$.ajax({
type: "POST",
url: "'.api_get_self().'",
data: {
action: "get_last_ten_users",
excludedUsers: selectedUsers,
id: groupId
},
dataType: "json",
success: function(data) {
var select = document.getElementById("elements_not_in");
select.innerHTML = "";
$.each(data, function(index, user) {
select.append(new Option(user.username + " - " + user.firstname + " " + user.lastname, user.id));
});
},
error: function(xhr, status, error) {
console.error("Error en la solicitud AJAX: " + status + " - " + error);
}
});
}
$("#show_last_ten_users_button").click(function() {
showLastTenUsers();
});
});
</script>';

$form_sent = 0;
Expand All @@ -133,6 +171,35 @@ function change_select(reset) {
api_not_allowed(true);
}

if (ChamiloApi::isAjaxRequest() && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'get_last_ten_users') {
$excludedUsers = isset($_POST['excludedUsers']) ? $_POST['excludedUsers'] : [];
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;

$excludedIds = !empty($excludedUsers) ? implode(",", array_map('intval', $excludedUsers)) : '0';
$sql = 'SELECT id, username, firstname, lastname
FROM user
WHERE status != '.ANONYMOUS.'
AND id NOT IN ('.$excludedIds.')
ORDER BY id DESC
LIMIT 10';

$result = Database::query($sql);
$users = [];

while ($user = Database::fetch_array($result)) {
$users[] = [
'id' => $user['id'],
'username' => $user['username'],
'firstname' => $user['firstname'],
'lastname' => $user['lastname']
];
}

header('Content-Type: application/json');
echo json_encode($users);
die();
}

$first_letter_user = '';

if ((isset($_POST['form_sent']) && $_POST['form_sent']) || isset($_REQUEST['firstLetterUser'])) {
Expand Down Expand Up @@ -453,13 +520,18 @@ function formatCompleteName(array $userInfo, bool $orderListByOfficialCode): str
placeholder="<?php echo get_lang('Search'); ?>"
onkeydown="return 13 !== event.keyCode;">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="change_select();">
<?php echo get_lang('Filter'); ?>
</button>
<button class="btn btn-default" type="button" onclick="change_select(true);">
<?php echo get_lang('Reset'); ?>
</button>
</span>
<button class="btn btn-default" type="button" onclick="change_select();">
<?php echo get_lang('Filter'); ?>
</button>
<button class="btn btn-default" type="button" onclick="change_select(true);">
<?php echo get_lang('Reset'); ?>
</button>
</span>
<span class="input-group-btn">
<button class="btn btn-default" type="button" id="show_last_ten_users_button" title="<?php echo get_lang('ShowLastTenUsers') ?>">
<i class="fa fa-clock-o"></i>
</button>
</span>
</div>
</div>
<?php
Expand Down
119 changes: 117 additions & 2 deletions main/session/add_users_to_session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package chamilo.admin
*/
// resetting the course id
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;

$cidReset = true;

require_once __DIR__.'/../inc/global.inc.php';
Expand All @@ -14,8 +16,8 @@
// setting the section (for the tabs)
$this_section = SECTION_PLATFORM_ADMIN;

$id_session = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
$addProcess = isset($_GET['add']) && 'true' === $_GET['add'] ? 'true' : null;
$id_session = isset($_REQUEST['id_session']) ? (int) $_REQUEST['id_session'] : 0;
$addProcess = isset($_REQUEST['add']) && 'true' === $_REQUEST['add'] ? 'true' : null;

SessionManager::protectSession($id_session);

Expand Down Expand Up @@ -65,6 +67,40 @@
}
}

if (ChamiloApi::isAjaxRequest() && isset($_POST['action'])) {
$id_session = isset($_POST['id_session']) ? (int) $_POST['id_session'] : 0;
$excludedUsers = isset($_POST['excludedUsers']) ? $_POST['excludedUsers'] : [];

$excludedUsersList = count($excludedUsers) > 0 ? implode(",", array_map('intval', $excludedUsers)) : '0';

if ($_POST['action'] == 'get_last_ten_users') {
$sql = "SELECT u.id, u.username, u.firstname, u.lastname
FROM $tbl_user u
LEFT JOIN $tbl_session_rel_user sru ON (u.id = sru.user_id AND sru.session_id = $id_session)
WHERE sru.user_id IS NULL
AND u.id NOT IN ($excludedUsersList)
ORDER BY u.id DESC
LIMIT 10";
} elseif ($_POST['action'] == 'get_all_users') {
$sql = "SELECT u.id, u.username, u.firstname, u.lastname
FROM $tbl_user u
LEFT JOIN $tbl_session_rel_user sru ON (u.id = sru.user_id AND sru.session_id = $id_session)
WHERE sru.user_id IS NULL
AND u.id NOT IN ($excludedUsersList)
ORDER BY u.lastname ASC, u.firstname ASC";
}

$result = Database::query($sql);
$users = [];
while ($row = Database::fetch_assoc($result)) {
$users[] = $row;
}

header('Content-Type: application/json');
echo json_encode($users);
die();
}

function search_users($needle, $type)
{
global $id_session;
Expand Down Expand Up @@ -304,6 +340,77 @@ function change_select(val) {
xajax_search_users(val,"multiple");
}
</script>';
$htmlHeadXtra[] = '
<script>
function showLastTenUsers() {
var selectedUsers = [];
$("#destination_users option").each(function() {
selectedUsers.push($(this).val());
});
if (selectedUsers.length === 0) {
selectedUsers.push(0);
}
var idSession = "'.(int) $id_session.'";
$.post("'.api_get_self().'",
{
action: "get_last_ten_users",
excludedUsers: selectedUsers,
id_session: idSession,
add: "",
add_type: "multiple"
}, function(data) {
console.log(data);
var select = document.getElementById("origin_users");
select.innerHTML = "";
$.each(data, function(index, user) {
select.append(new Option(user.username + " - " + user.firstname + " " + user.lastname, user.id));
});
}, "json").fail(function(xhr, status, error) {
console.error("Error en la solicitud AJAX: " + error);
console.log(xhr.responseText);
});
}
function loadAllUsers() {
var selectedUsers = [];
$("#destination_users option").each(function() {
selectedUsers.push($(this).val());
});
if (selectedUsers.length === 0) {
selectedUsers.push(0);
}
var idSession = "'.(int) $id_session.'";
$.post("'.api_get_self().'",
{
action: "get_all_users",
excludedUsers: selectedUsers,
id_session: idSession,
add: "",
add_type: "multiple"
}, function(data) {
var select = document.getElementById("origin_users");
select.innerHTML = "";
$.each(data, function(index, user) {
select.append(new Option(user.username + " - " + user.firstname + " " + user.lastname, user.id));
});
}, "json").fail(function(xhr, status, error) {
console.error("Error en la solicitud AJAX: " + error);
});
}
$(document).ready(function() {
loadAllUsers();
$("#show_last_ten_users_button").on("click", showLastTenUsers);
$("#reset_users_button").on("click", loadAllUsers);
});
</script>
';

$form_sent = 0;
$errorMsg = $firstLetterUser = $firstLetterSession = '';
Expand Down Expand Up @@ -691,6 +798,14 @@ class="form-control">
<?php
echo Display::get_alphabet_options(); ?>
</select>
<span class="input-group-btn">
<button class="btn btn-default" type="button" id="show_last_ten_users_button" title="<?php echo get_lang('ShowLastTenUsers') ?>">
<i class="fa fa-clock-o"></i>
</button>
<button class="btn btn-default" type="button" id="reset_users_button" title="<?php echo get_lang('Reset') ?>">
<i class="fa fa-refresh"></i>
</button>
</span>
<br/>
<br/>
<?php
Expand Down

0 comments on commit 7cf2c49

Please sign in to comment.