lp_subscribe_users_to_category.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 '../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('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths'));
  25. $interbreadcrumb[] = array('url' => '#', 'name' => $category->getName());
  26. // Find course.
  27. $course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId);
  28. // Getting subscribe users to the course.
  29. $subscribedUsers = $em->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course);
  30. $subscribedUsers = $subscribedUsers->getQuery();
  31. $subscribedUsers = $subscribedUsers->execute();
  32. // Getting all users in a nice format.
  33. $choices = array();
  34. /** @var User $user */
  35. foreach ($subscribedUsers as $user) {
  36. $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
  37. }
  38. // Getting subscribed users to a category.
  39. $subscribedUsersInCategory = $category->getUsers();
  40. $selectedChoices = array();
  41. foreach ($subscribedUsersInCategory as $item) {
  42. $selectedChoices[] = $item->getUser()->getId();
  43. }
  44. $url = api_get_self().'?'.api_get_cidreq().'&action=add_users_to_category&id='.$categoryId;
  45. //Building the form for Users
  46. $formUsers = new \FormValidator('lp_edit', 'post', $url);
  47. $formUsers->addElement('hidden', 'user_form', 1);
  48. $userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices);
  49. $formUsers->addButtonSave(get_lang('Save'));
  50. $defaults = array();
  51. if (!empty($selectedChoices)) {
  52. $defaults['users'] = $selectedChoices;
  53. }
  54. $formUsers->setDefaults($defaults);
  55. //Building the form for Groups
  56. $tpl = new Template();
  57. $currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id());
  58. if ($formUsers->validate()) {
  59. $values = $formUsers->getSubmitValues();
  60. // Subscribing users
  61. $users = isset($values['users']) ? $values['users'] : [];
  62. if (!empty($users)) {
  63. $deleteUsers = [];
  64. if ($subscribedUsersInCategory) {
  65. /** @var CLpCategoryUser $user */
  66. foreach ($subscribedUsersInCategory as $user) {
  67. $userId = $user->getUser()->getId();
  68. if (!in_array($userId, $users)) {
  69. $category->removeUsers($user);
  70. }
  71. }
  72. }
  73. foreach ($users as $userId) {
  74. $categoryUser = new CLpCategoryUser();
  75. $user = UserManager::getRepository()->find($userId);
  76. $categoryUser->setUser($user);
  77. $category->addUser($categoryUser);
  78. }
  79. $em->merge($category);
  80. $em->flush();
  81. Display::addFlash(Display::return_message(get_lang('Updated')));
  82. }
  83. header("Location: $url");
  84. exit;
  85. } else {
  86. $headers = [get_lang('SubscribeUsersToLp'), get_lang('SubscribeGroupsToLp')];
  87. $tabs = $formUsers->toHtml();
  88. $tpl->assign('tabs', $tabs);
  89. }
  90. $layout = $tpl->get_template('learnpath/subscribe_users.tpl');
  91. $tpl->display($layout);