Skip to content

Commit 93ebc20

Browse files
Catalogue: Add "Catalogue access" tab to restrict visibility by usergroup - refs #6163
1 parent cee7a1d commit 93ebc20

File tree

2 files changed

+348
-130
lines changed

2 files changed

+348
-130
lines changed

public/main/admin/course_edit.php

+111-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
/* For licensing terms, see /license.txt */
44

5+
use Chamilo\CoreBundle\Entity\Course;
6+
use Chamilo\CoreBundle\Entity\Usergroup;
7+
use Chamilo\CoreBundle\Entity\CatalogueCourseRelAccessUrlRelUsergroup;
58
use Chamilo\CoreBundle\Framework\Container;
69
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
710
use Chamilo\CoreBundle\Component\Utils\ToolIcon;
@@ -20,6 +23,7 @@
2023
$urlId = api_get_current_access_url_id();
2124

2225
$courseId = $_GET['id'] ?? null;
26+
$currentView = $_GET['view'] ?? 'general';
2327

2428
if (empty($courseId)) {
2529
api_not_allowed(true);
@@ -461,6 +465,17 @@
461465
exit;
462466
}
463467

468+
$tabs = [
469+
'general' => [
470+
'url' => 'course_edit.php?id='.$courseId,
471+
'content' => get_lang('Edit course'),
472+
],
473+
'catalogue_access' => [
474+
'url' => 'course_edit.php?id='.$courseId.'&view=catalogue_access',
475+
'content' => get_lang('Catalogue access'),
476+
],
477+
];
478+
464479
Display::display_header($tool_name);
465480

466481
$actions = Display::url(
@@ -477,6 +492,7 @@
477492
api_get_path(WEB_CODE_PATH)."admin/course_information.php?id=$courseId"
478493
);
479494

495+
echo Display::toolbarAction('toolbarCourseEdit', [Display::tabsOnlyLink($tabs, $currentView, 'course-edit-tabs')]);
480496
echo Display::toolbarAction('toolbar', [$actions]);
481497

482498
echo "<script>
@@ -527,6 +543,100 @@ function valide() {
527543
}
528544
</script>";
529545

530-
$form->display();
546+
if ('catalogue_access' === $currentView) {
547+
echo Display::div(
548+
get_lang('Select classes for which this course will be visible for subscription in the catalogue. Subscription rules still apply apart from it being visible in the catalogue.'),
549+
['class' => 'alert alert-info']
550+
);
551+
552+
$em = Database::getManager();
553+
$accessUrl = Container::getAccessUrlHelper()->getCurrent();
554+
$accessUrlId = $accessUrl->getId();
555+
556+
/** @var Course|null $course */
557+
$course = $em->getRepository(Course::class)->find($courseId);
558+
559+
if (!$accessUrl || !$course) {
560+
echo Display::return_message(get_lang('Invalid access URL or course'), 'error');
561+
return;
562+
}
563+
564+
$formCatalogue = new FormValidator(
565+
'form_catalogue_access',
566+
'post',
567+
api_get_self().'?id='.$courseId.'&view=catalogue_access'
568+
);
569+
570+
$groupEntities = $em->createQueryBuilder()
571+
->select('ug')
572+
->from(Usergroup::class, 'ug')
573+
->innerJoin('ug.urls', 'urlRel')
574+
->where('urlRel.url = :accessUrl')
575+
->setParameter('accessUrl', $accessUrl)
576+
->orderBy('ug.title', 'ASC')
577+
->getQuery()
578+
->getResult();
579+
580+
$groups = [];
581+
foreach ($groupEntities as $group) {
582+
$groups[$group->getId()] = $group->getTitle();
583+
}
584+
585+
$existing = $em->getRepository(CatalogueCourseRelAccessUrlRelUsergroup::class)->findBy([
586+
'course' => $course,
587+
'accessUrl' => $accessUrl,
588+
]);
589+
590+
$selected = [];
591+
foreach ($existing as $record) {
592+
if ($record->getUsergroup()) {
593+
$selected[] = $record->getUsergroup()->getId();
594+
}
595+
}
596+
597+
$formCatalogue->addMultiSelect(
598+
'selected_usergroups',
599+
get_lang('User groups'),
600+
$groups,
601+
['style' => 'width:100%;height:300px;']
602+
);
603+
604+
$formCatalogue->setDefaults([
605+
'selected_usergroups' => $selected,
606+
]);
607+
608+
$formCatalogue->addButtonSave(get_lang('Save'));
609+
610+
if ($formCatalogue->validate()) {
611+
$data = $formCatalogue->getSubmitValues();
612+
$newGroups = $data['selected_usergroups'] ?? [];
613+
614+
foreach ($existing as $old) {
615+
$em->remove($old);
616+
}
617+
$em->flush();
618+
619+
foreach ($newGroups as $groupId) {
620+
$group = $em->getRepository(Usergroup::class)->find((int) $groupId);
621+
if ($group) {
622+
$rel = new CatalogueCourseRelAccessUrlRelUsergroup();
623+
$rel->setCourse($course);
624+
$rel->setAccessUrl($accessUrl);
625+
$rel->setUsergroup($group);
626+
$em->persist($rel);
627+
}
628+
}
629+
630+
$em->flush();
631+
632+
Display::addFlash(Display::return_message(get_lang('Changes saved successfully'), 'confirmation'));
633+
header('Location: '.api_get_self().'?id='.$courseId.'&view=catalogue_access');
634+
exit;
635+
}
636+
637+
$formCatalogue->display();
638+
} else {
639+
$form->display();
640+
}
531641

532642
Display::display_footer();

0 commit comments

Comments
 (0)