|
2 | 2 |
|
3 | 3 | /* For licensing terms, see /license.txt */
|
4 | 4 |
|
| 5 | +use Chamilo\CoreBundle\Entity\Course; |
| 6 | +use Chamilo\CoreBundle\Entity\Usergroup; |
| 7 | +use Chamilo\CoreBundle\Entity\CatalogueCourseRelAccessUrlRelUsergroup; |
5 | 8 | use Chamilo\CoreBundle\Framework\Container;
|
6 | 9 | use Chamilo\CoreBundle\Component\Utils\ActionIcon;
|
7 | 10 | use Chamilo\CoreBundle\Component\Utils\ToolIcon;
|
|
20 | 23 | $urlId = api_get_current_access_url_id();
|
21 | 24 |
|
22 | 25 | $courseId = $_GET['id'] ?? null;
|
| 26 | +$currentView = $_GET['view'] ?? 'general'; |
23 | 27 |
|
24 | 28 | if (empty($courseId)) {
|
25 | 29 | api_not_allowed(true);
|
|
461 | 465 | exit;
|
462 | 466 | }
|
463 | 467 |
|
| 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 | + |
464 | 479 | Display::display_header($tool_name);
|
465 | 480 |
|
466 | 481 | $actions = Display::url(
|
|
477 | 492 | api_get_path(WEB_CODE_PATH)."admin/course_information.php?id=$courseId"
|
478 | 493 | );
|
479 | 494 |
|
| 495 | +echo Display::toolbarAction('toolbarCourseEdit', [Display::tabsOnlyLink($tabs, $currentView, 'course-edit-tabs')]); |
480 | 496 | echo Display::toolbarAction('toolbar', [$actions]);
|
481 | 497 |
|
482 | 498 | echo "<script>
|
@@ -527,6 +543,100 @@ function valide() {
|
527 | 543 | }
|
528 | 544 | </script>";
|
529 | 545 |
|
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 | +} |
531 | 641 |
|
532 | 642 | Display::display_footer();
|
0 commit comments