lp_subscribe_users_to_category.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Entity\CLpCategoryUser;
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. api_protect_course_script();
  8. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false);
  9. if (!$is_allowed_to_edit) {
  10. api_not_allowed(true);
  11. }
  12. $categoryId = isset($_GET['id']) ? intval($_GET['id']) : 0;
  13. if (empty($categoryId)) {
  14. api_not_allowed(true);
  15. }
  16. $courseId = api_get_course_int_id();
  17. $courseCode = api_get_course_id();
  18. $em = Database::getManager();
  19. /** @var \Chamilo\CourseBundle\Entity\CLpCategory $category */
  20. $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId);
  21. if (!$category) {
  22. api_not_allowed(true);
  23. }
  24. $interbreadcrumb[] = array(
  25. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  26. 'name' => get_lang('LearningPaths')
  27. );
  28. $interbreadcrumb[] = array('url' => '#', 'name' => strip_tags($category->getName()));
  29. // Find course.
  30. $course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId);
  31. // Getting subscribe users to the course.
  32. $subscribedUsers = $em->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course);
  33. $subscribedUsers = $subscribedUsers->getQuery();
  34. $subscribedUsers = $subscribedUsers->execute();
  35. // Getting all users in a nice format.
  36. $choices = array();
  37. /** @var User $user */
  38. foreach ($subscribedUsers as $user) {
  39. $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
  40. }
  41. // Getting subscribed users to a category.
  42. $subscribedUsersInCategory = $category->getUsers();
  43. $selectedChoices = array();
  44. foreach ($subscribedUsersInCategory as $item) {
  45. $selectedChoices[] = $item->getUser()->getId();
  46. }
  47. $url = api_get_self().'?'.api_get_cidreq().'&action=add_users_to_category&id='.$categoryId;
  48. //Building the form for Users
  49. $formUsers = new \FormValidator('lp_edit', 'post', $url);
  50. $formUsers->addElement('hidden', 'user_form', 1);
  51. $userMultiSelect = $formUsers->addElement(
  52. 'advmultiselect',
  53. 'users',
  54. get_lang('Users'),
  55. $choices
  56. );
  57. $formUsers->addButtonSave(get_lang('Save'));
  58. $defaults = array();
  59. if (!empty($selectedChoices)) {
  60. $defaults['users'] = $selectedChoices;
  61. }
  62. $formUsers->setDefaults($defaults);
  63. // Building the form for Groups
  64. $tpl = new Template();
  65. $currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id());
  66. if ($formUsers->validate()) {
  67. $values = $formUsers->getSubmitValues();
  68. // Subscribing users
  69. $users = isset($values['users']) ? $values['users'] : [];
  70. $deleteUsers = [];
  71. if ($subscribedUsersInCategory) {
  72. /** @var CLpCategoryUser $user */
  73. foreach ($subscribedUsersInCategory as $user) {
  74. $userId = $user->getUser()->getId();
  75. if (!in_array($userId, $users)) {
  76. $category->removeUsers($user);
  77. }
  78. }
  79. }
  80. foreach ($users as $userId) {
  81. $categoryUser = new CLpCategoryUser();
  82. $user = UserManager::getRepository()->find($userId);
  83. $categoryUser->setUser($user);
  84. $category->addUser($categoryUser);
  85. }
  86. $em->merge($category);
  87. $em->flush();
  88. Display::addFlash(Display::return_message(get_lang('Updated')));
  89. header("Location: $url");
  90. exit;
  91. } else {
  92. $headers = [get_lang('SubscribeUsersToLp'), get_lang('SubscribeGroupsToLp')];
  93. $tabs = $formUsers->toHtml();
  94. $tpl->assign('tabs', $tabs);
  95. }
  96. $layout = $tpl->get_template('learnpath/subscribe_users.tpl');
  97. $tpl->display($layout);