Skip to content

Commit b1df01a

Browse files
Merge pull request #7001 from christianbeeznest/GH-5200
Announcements: Show to non-logged users - refs #5200
2 parents 15993a6 + ae9c682 commit b1df01a

File tree

3 files changed

+66
-56
lines changed

3 files changed

+66
-56
lines changed

assets/vue/components/systemannouncement/SystemAnnouncementCard.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
2-
<BaseCard plain>
2+
<BaseCard
3+
plain
4+
class="mb-2 last:mb-0"
5+
>
36
<template #header>
47
<div class="-mb-2 flex items-center justify-between gap-2 bg-gray-15 px-4 py-2">
58
<h6 v-text="announcement.title" />

public/main/admin/system_announcements.php

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@
3535

3636
$repo = Container::getSysAnnouncementRepository();
3737

38-
$visibleList = api_get_user_roles();
38+
$roleOptions = api_get_roles();
3939

40-
if (!array_key_exists('ROLE_ANONYMOUS', $visibleList)) {
41-
// Prefix to make it appear at the top; adapt the label if needed
42-
$visibleList = array_merge(['ROLE_ANONYMOUS' => get_lang('Anonymous')], $visibleList);
40+
if (!isset($roleOptions['ROLE_ANONYMOUS'])) {
41+
$roleOptions = ['ROLE_ANONYMOUS' => get_lang('Anonymous')] + $roleOptions;
42+
} else {
43+
$roleOptions['ROLE_ANONYMOUS'] = get_lang('Anonymous');
44+
$roleOptions = ['ROLE_ANONYMOUS' => $roleOptions['ROLE_ANONYMOUS']] + array_diff_key($roleOptions, ['ROLE_ANONYMOUS' => true]);
45+
}
46+
47+
$optionKeyByCanon = [];
48+
foreach ($roleOptions as $optKey => $label) {
49+
$optionKeyByCanon[api_normalize_role_code((string) $optKey)] = $optKey;
4350
}
4451

52+
$visibleList = $roleOptions;
53+
4554
$tool_name = null;
4655
if (empty($_GET['lang'])) {
4756
$_GET['lang'] = $_SESSION['user_language_choice'] ?? null;
@@ -158,7 +167,14 @@ function showCareer() {
158167
$values['range'] = substr(api_get_local_time($announcement->getDateStart()), 0, 16).' / '.
159168
substr(api_get_local_time($announcement->getDateEnd()), 0, 16);
160169

161-
$values['roles'] = $announcement->getRoles();
170+
$userCanonRoles = array_map('api_normalize_role_code', (array) $announcement->getRoles());
171+
$selectedOptionKeys = [];
172+
foreach ($userCanonRoles as $canon) {
173+
if (isset($optionKeyByCanon[$canon])) {
174+
$selectedOptionKeys[] = $optionKeyByCanon[$canon];
175+
}
176+
}
177+
$values['roles'] = array_values(array_unique($selectedOptionKeys));
162178

163179
if ($allowCareers) {
164180
$values['career_id'] = $announcement->getCareer() ? $announcement->getCareer()->getId() : 0;
@@ -250,44 +266,22 @@ function showCareer() {
250266
$form->addHtml('</div>');
251267
}
252268

253-
// Add Announcement picture (new feature, correct paths and lang vars)
254-
try {
255-
/*$form->addFile(
256-
'picture',
257-
[
258-
get_lang('Add a picture'),
259-
get_lang('The image must have a maximum dimension of 950 x 712 pixels'),
260-
],
261-
['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '4/3']
262-
);
263-
264-
$allowed_picture_types = api_get_supported_image_extensions(false);
265-
266-
$form->addRule(
267-
'picture',
268-
get_lang('Only PNG, JPG or GIF images allowed').' ('.implode(',', $allowed_picture_types).')',
269-
'filetype',
270-
$allowed_picture_types
271-
);
269+
$form->addElement(
270+
'select',
271+
'roles',
272+
get_lang('Roles'),
273+
$roleOptions,
274+
[
275+
'multiple' => 'multiple',
276+
'size' => 8,
277+
]
278+
);
279+
$form->addRule('roles', get_lang('Required field'), 'required');
272280

273-
$image = '';
274-
// Display announcements picture
275-
$store_path = api_get_path(SYS_UPLOAD_PATH).'announcements'; // course path
276-
if (file_exists($store_path.'/announcement_'.$announcement->id.'.png')) {
277-
$announcementsPath = api_get_path(WEB_UPLOAD_PATH).'announcements'; // announcement web path
278-
$announcementsImage = $announcementsPath.'/announcement_'.$announcement->id.'_100x100.png?'.rand(1, 1000); // redimensioned image 85x85
279-
$image = '<div class="row"><label class="col-md-2 control-label">'.get_lang('Image').'</label>
280-
<div class="col-md-8"><img class="img-thumbnail" src="'.$announcementsImage.'" /></div></div>';
281-
282-
$form->addHtml($image);
283-
$form->addElement('checkbox', 'delete_picture', null, get_lang('Delete picture'));
284-
}*/
285-
} catch (Exception $e) {
286-
error_log($e);
281+
if (!empty($values['roles'])) {
282+
$form->getElement('roles')->setSelected($values['roles']);
287283
}
288284

289-
$form->addSelect('roles', get_lang('Visible'), $visibleList, ['multiple' => 'multiple']);
290-
291285
$form->addElement('hidden', 'id');
292286
$userGroup = new UserGroupModel();
293287
$group_list = $userGroup->get_all();
@@ -325,6 +319,12 @@ function showCareer() {
325319
$values['lang'] = null;
326320
}
327321

322+
$rolesNormalized = array_values(
323+
array_unique(
324+
array_map('api_normalize_role_code', (array) ($values['roles'] ?? []))
325+
)
326+
);
327+
328328
$sendMail = $values['send_mail'] ?? null;
329329

330330
switch ($values['action']) {
@@ -334,11 +334,11 @@ function showCareer() {
334334
$values['content'],
335335
$values['range_start'],
336336
$values['range_end'],
337-
$values['roles'] ?? [],
337+
$rolesNormalized,
338338
$values['lang'],
339339
$sendMail,
340-
empty($values['add_to_calendar']) ? false : true,
341-
empty($values['send_email_test']) ? false : true
340+
!empty($values['add_to_calendar']),
341+
!empty($values['send_email_test'])
342342
);
343343

344344
if (false !== $announcement_id) {
@@ -379,7 +379,7 @@ function showCareer() {
379379
$values['content'],
380380
$values['range_start'],
381381
$values['range_end'],
382-
$values['roles'] ?? [],
382+
$rolesNormalized,
383383
$values['lang'],
384384
$sendMail,
385385
$sendMailTest
@@ -436,17 +436,20 @@ function showCareer() {
436436
$row = [];
437437
$row[] = $announcement->getId();
438438
if ($announcement->isVisible()) {
439-
$row[] =Display::getMdiIcon(StateIcon::COMPLETE, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('The announcement is available'));
439+
$row[] = Display::getMdiIcon(StateIcon::COMPLETE, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('The announcement is available'));
440440
} else {
441-
$row[] =Display::getMdiIcon(StateIcon::WARNING, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('The announcement is not available'));
441+
$row[] = Display::getMdiIcon(StateIcon::WARNING, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('The announcement is not available'));
442442
}
443443
$row[] = $announcement->getTitle();
444444
$row[] = api_convert_and_format_date($announcement->getDateStart());
445445
$row[] = api_convert_and_format_date($announcement->getDateEnd());
446-
$announcementRoles = $announcement->getRoles(); // tableau d'identifiants
446+
447+
$announcementRoles = (array) $announcement->getRoles();
447448
$displayRoles = [];
448449
foreach ($announcementRoles as $r) {
449-
$displayRoles[] = $visibleList[$r] ?? $r;
450+
$canon = api_normalize_role_code((string) $r);
451+
$key = $optionKeyByCanon[$canon] ?? null;
452+
$displayRoles[] = $key ? ($roleOptions[$key] ?? (string) $r) : (string) $r;
450453
}
451454
$row[] = implode(', ', $displayRoles);
452455

src/CoreBundle/Controller/NewsController.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Chamilo\CoreBundle\Helpers\UserHelper;
1111
use Chamilo\CoreBundle\Repository\SysAnnouncementRepository;
1212
use Chamilo\CoreBundle\Traits\ControllerTrait;
13+
use Chamilo\CoreBundle\Entity\User;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Routing\Attribute\Route;
@@ -32,15 +33,18 @@ public function index(SysAnnouncementRepository $sysAnnouncementRepository): Res
3233
{
3334
$user = $this->userHelper->getCurrent();
3435

35-
$list = [];
36-
if (null !== $user) {
37-
$list = $sysAnnouncementRepository->getAnnouncements(
38-
$user,
39-
$this->accessUrlHelper->getCurrent(),
40-
$this->getRequest()->getLocale()
41-
);
36+
if (null === $user) {
37+
$anon = new User();
38+
$anon->setRoles(['ROLE_ANONYMOUS']);
39+
$user = $anon;
4240
}
4341

42+
$list = $sysAnnouncementRepository->getAnnouncements(
43+
$user,
44+
$this->accessUrlHelper->getCurrent(),
45+
$this->getRequest()->getLocale()
46+
);
47+
4448
return new JsonResponse($list);
4549
}
4650
}

0 commit comments

Comments
 (0)