Skip to content

Commit b8db71a

Browse files
committed
Merge branch 'master' of github.com:chamilo/chamilo-lms
2 parents 8df5375 + b67c2bc commit b8db71a

File tree

2 files changed

+352
-130
lines changed

2 files changed

+352
-130
lines changed

public/main/admin/course_edit.php

Lines changed: 113 additions & 1 deletion
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,102 @@ 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+
$formCatalogue->addElement('header', get_lang('Course').' #'.$courseInfo['real_id'].' '.$course_code);
571+
572+
$groupEntities = $em->createQueryBuilder()
573+
->select('ug')
574+
->from(Usergroup::class, 'ug')
575+
->innerJoin('ug.urls', 'urlRel')
576+
->where('urlRel.url = :accessUrl')
577+
->setParameter('accessUrl', $accessUrl)
578+
->orderBy('ug.title', 'ASC')
579+
->getQuery()
580+
->getResult();
581+
582+
$groups = [];
583+
foreach ($groupEntities as $group) {
584+
$groups[$group->getId()] = $group->getTitle();
585+
}
586+
587+
$existing = $em->getRepository(CatalogueCourseRelAccessUrlRelUsergroup::class)->findBy([
588+
'course' => $course,
589+
'accessUrl' => $accessUrl,
590+
]);
591+
592+
$selected = [];
593+
foreach ($existing as $record) {
594+
if ($record->getUsergroup()) {
595+
$selected[] = $record->getUsergroup()->getId();
596+
}
597+
}
598+
599+
$formCatalogue->addMultiSelect(
600+
'selected_usergroups',
601+
get_lang('User groups'),
602+
$groups,
603+
['style' => 'width:100%;height:300px;']
604+
);
605+
606+
$formCatalogue->setDefaults([
607+
'selected_usergroups' => $selected,
608+
]);
609+
610+
$formCatalogue->addButtonSave(get_lang('Save'));
611+
612+
if ($formCatalogue->validate()) {
613+
$data = $formCatalogue->getSubmitValues();
614+
$newGroups = $data['selected_usergroups'] ?? [];
615+
616+
foreach ($existing as $old) {
617+
$em->remove($old);
618+
}
619+
$em->flush();
620+
621+
foreach ($newGroups as $groupId) {
622+
$group = $em->getRepository(Usergroup::class)->find((int) $groupId);
623+
if ($group) {
624+
$rel = new CatalogueCourseRelAccessUrlRelUsergroup();
625+
$rel->setCourse($course);
626+
$rel->setAccessUrl($accessUrl);
627+
$rel->setUsergroup($group);
628+
$em->persist($rel);
629+
}
630+
}
631+
632+
$em->flush();
633+
634+
Display::addFlash(Display::return_message(get_lang('Changes saved successfully'), 'confirmation'));
635+
header('Location: '.api_get_self().'?id='.$courseId.'&view=catalogue_access');
636+
exit;
637+
}
638+
639+
$formCatalogue->display();
640+
} else {
641+
$form->display();
642+
}
531643

532644
Display::display_footer();

0 commit comments

Comments
 (0)