Skip to content

Commit 58554ca

Browse files
Admin: Refactor and UI improvement of Access URL management pages - refs BT#22643
1 parent fdecfa6 commit 58554ca

9 files changed

+429
-190
lines changed

public/main/admin/access_url_add_usergroup_to_url.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
/* For licensing terms, see /license.txt */
3+
34
/**
4-
* This script allows platform admins to add users to urls.
5-
* It displays a list of users and a list of courses;
6-
* you can select multiple users and courses and then click on.
7-
*
8-
* @author Julio Montoya <[email protected]>
5+
* This script allows platform admins to assign user groups to multiple URLs.
6+
* It supports bulk assignment and removal of groups from URLs.
97
*/
108

119
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
@@ -22,9 +20,6 @@
2220

2321
$userGroup = new UserGroupModel();
2422
$firstLetterUserGroup = null;
25-
$courses = [];
26-
$url_list = [];
27-
2823
$tbl_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
2924
$tool_name = get_lang('Add group to URL');
3025
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
@@ -38,7 +33,7 @@
3833
api_get_path(WEB_CODE_PATH).'admin/access_urls.php'
3934
);
4035
echo Display::url(
41-
Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Add group to URL')),
36+
Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Edit groups for one URL')),
4237
api_get_path(WEB_CODE_PATH).'admin/access_url_edit_usergroup_to_url.php'
4338
);
4439
echo '</div>';
@@ -49,40 +44,35 @@
4944
<?php echo $tool_name; ?>
5045
</h2>
5146
<?php
52-
if (isset($_POST['form_sent']) && $_POST['form_sent']) {
53-
$form_sent = $_POST['form_sent'];
47+
if (!empty($_POST['form_sent'])) {
5448
$userGroups = isset($_POST['user_group_list']) && is_array($_POST['user_group_list']) ? $_POST['user_group_list'] : [];
55-
$urlList = is_array($_POST['url_list']) ? $_POST['url_list'] : [];
56-
$firstLetterUserGroup = $_POST['first_letter_user_group'] ?? null;
49+
$urlList = isset($_POST['url_list']) && is_array($_POST['url_list']) ? $_POST['url_list'] : [];
5750

58-
if (1 == $form_sent) {
59-
if (0 == count($userGroups) || 0 == count($urlList)) {
60-
echo Display::return_message(get_lang('You need to select at least one group and one site'), 'error');
61-
} else {
51+
if (empty($userGroups) || empty($urlList)) {
52+
echo Display::return_message(get_lang('You need to select at least one group and one site'), 'error');
53+
} else {
54+
if (isset($_POST['add'])) {
6255
UrlManager::addUserGroupListToUrl($userGroups, $urlList);
6356
echo Display::return_message(get_lang('The group now belongs to the selected site'), 'confirm');
57+
} elseif (isset($_POST['remove'])) {
58+
UrlManager::removeUserGroupListFromUrl($userGroups, $urlList);
59+
echo Display::return_message(get_lang('The group has been removed from the selected site'), 'confirm');
6460
}
6561
}
6662
}
6763

68-
$firstLetterUser = null;
69-
if ($userGroup->getTotalCount() > 1000) {
70-
//if there are too much num_courses to gracefully handle with the HTML select list,
71-
// assign a default filter on users names
72-
$firstLetterUser = 'A';
73-
}
74-
75-
$dbUserGroups = $userGroup->filterByFirstLetter($firstLetterUserGroup);
64+
$dbUserGroups = $userGroup->getAllUserGroups();
7665

77-
$sql = "SELECT id, url FROM $tbl_access_url WHERE active = 1 ORDER BY url";
78-
$result = Database::query($sql);
79-
$db_urls = Database::store_result($result);
66+
$sql = "SELECT id, url FROM $tbl_access_url WHERE active = 1 ORDER BY url";
67+
$db_urls = Database::store_result(Database::query($sql));
8068
?>
69+
8170
<form method="post" class="space-y-6" onsubmit="return confirmSubmission(event)">
8271
<input type="hidden" name="form_sent" value="1" />
72+
8373
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
8474
<div>
85-
<label class="block text-sm font-bold mb-2"><?php echo get_lang('User groups list'); ?></label>
75+
<label class="block text-sm font-bold text-gray-700 mb-2"><?php echo get_lang('User groups list'); ?></label>
8676
<input type="text" id="groupFilter" onkeyup="filterSelect('groupFilter', 'groupSelect')" class="mb-2 w-full p-2 border rounded" placeholder="<?php echo get_lang('Search group'); ?>">
8777
<select name="user_group_list[]" id="groupSelect" multiple size="20" class="w-full h-[400px] p-2 border rounded">
8878
<?php foreach ($dbUserGroups as $item): ?>
@@ -91,7 +81,7 @@
9181
</select>
9282
</div>
9383
<div>
94-
<label class="block text-sm font-bold mb-2"><?php echo get_lang('URL list'); ?></label>
84+
<label class="block text-sm font-bold text-gray-700 mb-2"><?php echo get_lang('URL list'); ?></label>
9585
<input type="text" id="urlFilter" onkeyup="filterSelect('urlFilter', 'urlSelect')" class="mb-2 w-full p-2 border rounded" placeholder="<?php echo get_lang('Search URL'); ?>">
9686
<select name="url_list[]" id="urlSelect" multiple size="20" class="w-full h-[400px] p-2 border rounded">
9787
<?php foreach ($db_urls as $url_obj): ?>
@@ -138,5 +128,4 @@ function confirmSubmission(event) {
138128
}
139129
</script>
140130
<?php
141-
142-
Display :: display_footer();
131+
Display::display_footer();

public/main/admin/access_url_add_users_to_url.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@
5858
get_lang('You must select at least one user and one URL'),
5959
'error'
6060
);
61-
} else {
61+
} elseif (isset($_POST['add'])) {
6262
UrlManager::add_users_to_urls($users, $url_list);
6363
echo Display::return_message(get_lang('The user accounts are now attached to the URL'), 'confirm');
64+
} elseif (isset($_POST['remove'])) {
65+
UrlManager::remove_users_from_urls($users, $url_list);
66+
echo Display::return_message(get_lang('The user accounts have been unassigned from the URL'), 'confirm');
6467
}
6568
}
6669
}
@@ -94,8 +97,8 @@
9497
unset($result);
9598
?>
9699

97-
<form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" class="space-y-6">
98-
<input type="hidden" name="form_sent" value="1" />
100+
<form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" class="space-y-6" onsubmit="return confirmSubmission(event)">
101+
<input type="hidden" name="form_sent" value="1" />
99102
<div class="flex flex-col sm:flex-row items-center justify-between gap-4">
100103
<div>
101104
<label class="block text-sm font-medium text-gray-700 mb-1">
@@ -112,7 +115,7 @@ class="rounded-md border border-gray-300 p-2 shadow-sm focus:border-primary focu
112115
</div>
113116
</div>
114117

115-
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
118+
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
116119
<div>
117120
<label class="block text-sm font-bold text-gray-700 mb-2"><?php echo get_lang('User list'); ?></label>
118121
<input
@@ -137,14 +140,7 @@ class="w-full h-[400px] rounded-md border border-gray-300 p-2 text-sm focus:outl
137140
<?php endforeach; ?>
138141
</select>
139142
</div>
140-
<div class="flex flex-col items-center justify-center">
141-
<button
142-
type="submit"
143-
class="inline-flex items-center justify-center rounded-lg bg-primary px-6 py-2 text-white shadow hover:bg-primary/90 focus:outline-none focus:ring"
144-
>
145-
<?php echo get_lang('Add users to that URL'); ?>
146-
</button>
147-
</div>
143+
148144
<div>
149145
<label class="block text-sm font-bold text-gray-700 mb-2"><?php echo get_lang('URL list'); ?></label>
150146
<input
@@ -169,6 +165,24 @@ class="w-full h-[400px] rounded-md border border-gray-300 p-2 text-sm focus:outl
169165
</select>
170166
</div>
171167
</div>
168+
<div class="flex justify-center gap-4 mt-6 flex-wrap">
169+
<button
170+
type="submit"
171+
name="add"
172+
class="rounded-lg px-6 py-2 shadow focus:outline-none focus:ring btn--info"
173+
>
174+
<?php echo get_lang('Add users to selected URLs'); ?>
175+
</button>
176+
177+
<button
178+
type="submit"
179+
name="remove"
180+
class="rounded-lg px-6 py-2 shadow focus:outline-none focus:ring"
181+
style="background: rgb(var(--color-danger-base)); color: rgb(var(--color-danger-button-text));"
182+
>
183+
<?php echo get_lang('Remove users from selected URLs'); ?>
184+
</button>
185+
</div>
172186
</form>
173187
<script>
174188
function filterSelect(inputId, selectId) {
@@ -182,6 +196,19 @@ function filterSelect(inputId, selectId) {
182196
options[i].style.display = txt.includes(filter) ? '' : 'none';
183197
}
184198
}
199+
function confirmSubmission(event) {
200+
const form = event.target;
201+
const addClicked = form.querySelector('[name="add"]')?.matches(':focus');
202+
const removeClicked = form.querySelector('[name="remove"]')?.matches(':focus');
203+
204+
if (addClicked) {
205+
return confirm("<?php echo get_lang('Are you sure you want to assign the selected users to the selected URLs?'); ?>");
206+
}
207+
if (removeClicked) {
208+
return confirm("<?php echo get_lang('Are you sure you want to unassign the selected users from the selected URLs?'); ?>");
209+
}
210+
return true;
211+
}
185212
</script>
186213
<?php
187214

public/main/admin/access_url_edit.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @author Julio Montoya <[email protected]>
77
*/
88

9+
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
910
use Chamilo\CoreBundle\Framework\Container;
1011
use Symfony\Component\HttpFoundation\Request as HttpRequest;
1112

@@ -31,8 +32,6 @@
3132

3233
$defaults['url'] = 'http://';
3334
$form->setDefaults($defaults);
34-
35-
$submit_name = get_lang('Add URL');
3635
if ($httpRequest->query->has('url_id')) {
3736
$url_id = $httpRequest->query->getInt('url_id');
3837
$num_url_id = UrlManager::url_id_exist($url_id);
@@ -50,14 +49,13 @@
5049
$url_data['url'] = $https.$_SERVER['HTTP_HOST'].'/';
5150
}
5251
$form->setDefaults($url_data);
53-
$submit_name = get_lang('Add URL');
5452
}
5553

5654
$form->addHidden(
5755
'parentResourceNodeId',
5856
Container::getAccessUrlHelper()->getFirstAccessUrl()->resourceNode->getId()
5957
);
60-
$form->addButtonCreate($submit_name);
58+
$form->addButtonCreate(get_lang('Save'));
6159

6260
//the first url with id = 1 will be always active
6361
if ($httpRequest->query->has('url_id') && 1 !== $httpRequest->query->getInt('url_id')) {
@@ -120,10 +118,21 @@
120118
$form->setConstants(['sec_token' => $token]);
121119
}
122120

123-
$tool_name = get_lang('Add URL');
121+
$tool_name = isset($url_id) && $url_id > 0
122+
? get_lang('Edit URL')
123+
: get_lang('Add URL');
124124
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
125125
$interbreadcrumb[] = ['url' => 'access_urls.php', 'name' => get_lang('Multiple access URL / Branding')];
126126

127127
Display::display_header($tool_name);
128+
echo '<div class="flex gap-2 items-center mb-4 mt-4">';
129+
echo Display::url(
130+
Display::getMdiIcon(ActionIcon::BACK, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Back to URL list')),
131+
api_get_path(WEB_CODE_PATH).'admin/access_urls.php'
132+
);
133+
echo '</div>';
134+
echo '<h2 class="text-xl font-semibold text-gray-800 mt-6 mb-6">';
135+
echo $tool_name;
136+
echo '</h2>';
128137
$form->display();
129138
Display::display_footer();

public/main/admin/access_url_edit_course_category_to_url.php

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ function remove_item(origin) {
8787
if (0 == $access_url_id) {
8888
Display::addFlash(Display::return_message(get_lang('Select a URL')));
8989
header('Location: access_url_edit_users_to_url.php?');
90+
exit;
9091
} elseif (is_array($list)) {
9192
UrlManager::updateUrlRelCourseCategory($list, $access_url_id);
9293
Display::addFlash(Display::return_message(get_lang('Update successful')));
93-
header('Location: access_url_edit_users_to_url.php');
9494
}
95-
exit;
9695
}
9796
}
9897

@@ -206,17 +205,32 @@ class="w-full h-[400px] p-2 border border-gray-300 rounded-md">
206205
</div>
207206

208207
<div class="flex flex-col items-center justify-center gap-4 my-6">
209-
<button type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))"
210-
class="btn btn--plain">
211-
<i class="mdi mdi-fast-forward-outline ch-tool-icon"></i>
212-
</button>
213-
<button type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))"
214-
class="btn btn--plain">
215-
<i class="mdi mdi-rewind-outline ch-tool-icon"></i>
216-
</button>
208+
<?php if (!$ajax_search): ?>
209+
<button type="button"
210+
onclick="moveItem(
211+
document.getElementById('origin_users'),
212+
document.getElementById('destination_users'))"
213+
class="rounded-full bg-primary p-2 hover:bg-primary/80 focus:outline-none focus:ring">
214+
<i class="mdi mdi-fast-forward-outline text-white text-2xl"></i>
215+
</button>
216+
217+
<button type="button"
218+
onclick="moveItem(
219+
document.getElementById('destination_users'),
220+
document.getElementById('origin_users'))"
221+
class="rounded-full bg-secondary p-2 hover:bg-secondary/80 focus:outline-none focus:ring">
222+
<i class="mdi mdi-rewind-outline text-white text-2xl"></i>
223+
</button>
224+
<?php else: ?>
225+
<button type="button"
226+
onclick="remove_item(document.getElementById('destination_users'))"
227+
class="rounded-full bg-danger p-2 hover:bg-danger/80 focus:outline-none focus:ring"
228+
title="<?php echo get_lang('Remove from selection') ?>">
229+
<i class="mdi mdi-close text-white text-2xl"></i>
230+
</button>
231+
<?php endif; ?>
217232
</div>
218233

219-
220234
<div>
221235
<label class="block text-sm font-semibold text-gray-700 mb-2">
222236
<?php printf(get_lang('Course categories in %s site:'), $url_selected); ?>
@@ -234,24 +248,25 @@ class="w-full h-[400px] p-2 border border-gray-300 rounded-md">
234248

235249
<div class="mt-6 text-center">
236250
<button type="submit" class="rounded-lg px-6 py-2 shadow focus:outline-none focus:ring btn--primary">
237-
<i class="mdi mdi-content-save ch-tool-icon mr-2"></i>
238251
<?php echo get_lang('Save'); ?>
239252
</button>
240253
</div>
241254
</form>
242255

243256
<script>
244257

245-
function moveItem(origin , destination) {
246-
for(var i = 0 ; i<origin.options.length ; i++) {
247-
if(origin.options[i].selected) {
248-
destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
249-
origin.options[i]=null;
250-
i = i-1;
251-
}
252-
}
253-
destination.selectedIndex = -1;
254-
sortOptions(destination.options);
258+
function moveItem(origin, destination) {
259+
if (!origin || !destination) return;
260+
for (let i = 0; i < origin.options.length; i++) {
261+
if (origin.options[i].selected) {
262+
destination.options[destination.length] =
263+
new Option(origin.options[i].text, origin.options[i].value);
264+
origin.options[i] = null;
265+
i--;
266+
}
267+
}
268+
destination.selectedIndex = -1;
269+
sortOptions(destination.options);
255270
}
256271

257272
function sortOptions(options) {
@@ -300,7 +315,7 @@ function loadUsersInSelect(select) {
300315
sessionClasses = makepost(document.getElementById('destination_classes'));
301316
xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
302317
xhr_object.onreadystatechange = function() {
303-
if(xhr_object.readyState == 4) {
318+
if(xhr_object.readyState === 4) {
304319
document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
305320
}
306321
}
@@ -313,6 +328,15 @@ function makepost(select){
313328
ret = ret + options[i].value +'::'+options[i].text+";;";
314329
return ret;
315330
}
331+
332+
function remove_item(origin) {
333+
for(var i = 0 ; i<origin.options.length ; i++) {
334+
if(origin.options[i].selected) {
335+
origin.options[i]=null;
336+
i = i-1;
337+
}
338+
}
339+
}
316340
</script>
317341
<?php
318342
Display::display_footer();

0 commit comments

Comments
 (0)