add_drh_to_user.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User,
  4. Chamilo\CoreBundle\Entity\UserRelUser;
  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. $relationsRepo = $em->getRepository('ChamiloCoreBundle:UserRelUser');
  13. /** @var User $user */
  14. $user = UserManager::getManager()->find($_REQUEST['u']);
  15. if (!$user) {
  16. api_not_allowed(true);
  17. }
  18. $subscribedUsers = $user->getHrm();
  19. $hrmOptions = [];
  20. /** @var UserRelUser $subscribedUser */
  21. foreach ($subscribedUsers as $subscribedUser) {
  22. /** @var User $hrm */
  23. $hrm = UserManager::getManager()->find($subscribedUser->getFriendUserId());
  24. if (!$hrm) {
  25. continue;
  26. }
  27. $hrmOptions[$hrm->getId()] = $hrm->getCompleteNameWithUsername();
  28. }
  29. $form = new FormValidator('assign_hrm');
  30. $form->addUserAvatar('u', get_lang('User'), 'medium');
  31. $form->addSelectAjax(
  32. 'hrm',
  33. get_lang('HrmList'),
  34. $hrmOptions,
  35. ['multiple' => 'multiple', 'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role']
  36. );
  37. $form->addButtonSave(get_lang('Send'));
  38. $form->setDefaults([
  39. 'u' => $user,
  40. 'hrm' => array_keys($hrmOptions)
  41. ]);
  42. if ($form->validate()) {
  43. /** @var UserRelUser $subscribedUser */
  44. foreach ($subscribedUsers as $subscribedUser) {
  45. $em->remove($subscribedUser);
  46. };
  47. $em->flush();
  48. $values = $form->exportValues();
  49. foreach ($values['hrm'] as $hrmId) {
  50. /** @var User $hrm */
  51. $hrm = UserManager::getManager()->find($hrmId);
  52. if (!$hrm) {
  53. continue;
  54. }
  55. if ($hrm->getStatus() !== DRH) {
  56. continue;
  57. }
  58. UserManager::subscribeUsersToHRManager($hrm->getId(), [$user->getId()], false);
  59. }
  60. Display::addFlash(
  61. Display::return_message(get_lang('AssignedUsersHaveBeenUpdatedSuccessfully'), 'success')
  62. );
  63. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user->getId());
  64. exit;
  65. }
  66. $interbreadcrumb[] = ['name' => get_lang('PlatformAdmin'), 'url' => 'index.php'];
  67. $interbreadcrumb[] = ['name' => get_lang('UserList'), 'url' => 'user_list.php'];
  68. $interbreadcrumb[] = ['name' => $user->getCompleteName(), 'url' => 'user_information.php?user_id='.$user->getId()];
  69. $toolName = get_lang('AssignHrmToUser');
  70. $view = new Template($toolName);
  71. $view->assign('header', $toolName);
  72. $view->assign('content', $form->returnForm());
  73. $view->display_one_col_template();