add_drh_to_user.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\UserRelUser;
  4. use Chamilo\UserBundle\Entity\User as UserEntity;
  5. $cidReset = true;
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. api_protect_admin_script();
  8. if (!isset($_REQUEST['u'])) {
  9. api_not_allowed(true);
  10. }
  11. $em = Database::getManager();
  12. $userRepository = UserManager::getRepository();
  13. /** @var UserEntity $user */
  14. $user = UserManager::getManager()->find($_REQUEST['u']);
  15. if ($user === null) {
  16. api_not_allowed(true);
  17. }
  18. $subscribedUsers = $userRepository->getAssignedHrmUserList(
  19. $user->getId(),
  20. api_get_current_access_url_id()
  21. );
  22. $hrmOptions = [];
  23. /** @var UserRelUser $subscribedUser */
  24. foreach ($subscribedUsers as $subscribedUser) {
  25. /** @var UserEntity $hrm */
  26. $hrm = UserManager::getManager()->find($subscribedUser->getFriendUserId());
  27. if ($hrm === null) {
  28. continue;
  29. }
  30. $hrmOptions[$hrm->getId()] = UserManager::formatUserFullName($hrm, true);
  31. }
  32. $form = new FormValidator('assign_hrm');
  33. $form->addUserAvatar('u', get_lang('User'), 'medium');
  34. $form->addSelectAjax(
  35. 'hrm',
  36. get_lang('Human Resource Managers list'),
  37. $hrmOptions,
  38. ['multiple' => 'multiple', 'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role']
  39. );
  40. $form->addButtonSave(get_lang('Send message'));
  41. $form->setDefaults([
  42. 'u' => $user,
  43. 'hrm' => array_keys($hrmOptions),
  44. ]);
  45. if ($form->validate()) {
  46. /** @var UserRelUser $subscribedUser */
  47. foreach ($subscribedUsers as $subscribedUser) {
  48. $em->remove($subscribedUser);
  49. }
  50. $em->flush();
  51. $values = $form->exportValues();
  52. foreach ($values['hrm'] as $hrmId) {
  53. /** @var UserEntity $hrm */
  54. $hrm = UserManager::getManager()->find($hrmId);
  55. if ($hrm === null) {
  56. continue;
  57. }
  58. if ($hrm->getStatus() !== DRH) {
  59. continue;
  60. }
  61. UserManager::subscribeUsersToHRManager($hrm->getId(), [$user->getId()], false);
  62. }
  63. Display::addFlash(
  64. Display::return_message(get_lang('The assigned users have been updated'), 'success')
  65. );
  66. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user->getId());
  67. exit;
  68. }
  69. $interbreadcrumb[] = ['name' => get_lang('Administration'), 'url' => 'index.php'];
  70. $interbreadcrumb[] = ['name' => get_lang('User list'), 'url' => 'user_list.php'];
  71. $interbreadcrumb[] = [
  72. 'name' => UserManager::formatUserFullName($user),
  73. 'url' => 'user_information.php?user_id='.$user->getId(),
  74. ];
  75. $toolName = get_lang('Assign Human Resources Manager to user');
  76. $view = new Template($toolName);
  77. $view->assign('header', $toolName);
  78. $view->assign('content', $form->returnForm());
  79. $view->display_one_col_template();