user_linking_requests.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\UserRelUser;
  4. use Chamilo\UserBundle\Entity\User;
  5. $cidReset = true;
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. api_protect_admin_script();
  8. $action = isset($_GET['action']) ? $_GET['action'] : null;
  9. $hrmId = isset($_GET['hrm']) ? intval($_GET['hrm']) : 0;
  10. $assignedId = isset($_GET['u']) ? intval($_GET['u']) : 0;
  11. $hrm = $hrmId ? api_get_user_entity($hrmId) : null;
  12. $em = Database::getManager();
  13. if (!empty($action) && $hrm && $assignedId) {
  14. switch ($action) {
  15. case 'accept':
  16. /** @var UserRelUser $request */
  17. $request = $em->getRepository('ChamiloCoreBundle:UserRelUser')
  18. ->findOneBy([
  19. 'userId' => $assignedId,
  20. 'friendUserId' => $hrm->getId(),
  21. 'relationType' => USER_RELATION_TYPE_HRM_REQUEST,
  22. ]);
  23. if ($request) {
  24. $request->setRelationType(USER_RELATION_TYPE_RRHH);
  25. $request->setLastEdit(api_get_utc_datetime(null, null, true));
  26. $em->persist($request);
  27. $em->flush();
  28. Display::addFlash(
  29. Display::return_message(get_lang('UserLinkingRequestAccepted'), 'success')
  30. );
  31. }
  32. header('Location: '.api_get_self().'?hrm='.$hrm->getId());
  33. exit;
  34. case 'reject':
  35. /** @var UserRelUser $request */
  36. $request = $em->getRepository('ChamiloCoreBundle:UserRelUser')
  37. ->findOneBy([
  38. 'userId' => $assignedId,
  39. 'friendUserId' => $hrm->getId(),
  40. 'relationType' => USER_RELATION_TYPE_HRM_REQUEST,
  41. ]);
  42. if ($request) {
  43. $em->remove($request);
  44. $em->flush();
  45. Display::addFlash(
  46. Display::return_message(get_lang('UserLinkingRequestRejected'), 'success')
  47. );
  48. }
  49. /** Todo: notify the HRM that the request was rejected */
  50. header('Location: '.api_get_self().'?hrm='.$hrm->getId());
  51. exit;
  52. case 'remove':
  53. /** @var UserRelUser $request */
  54. $request = $em->getRepository('ChamiloCoreBundle:UserRelUser')
  55. ->findOneBy([
  56. 'userId' => $assignedId,
  57. 'friendUserId' => $hrm->getId(),
  58. 'relationType' => USER_RELATION_TYPE_RRHH,
  59. ]);
  60. if ($request) {
  61. $em->remove($request);
  62. $em->flush();
  63. Display::addFlash(
  64. Display::return_message(get_lang('UserLinkRemoved'), 'success')
  65. );
  66. }
  67. /** Todo: notify the HRM that the request was rejected */
  68. header('Location: '.api_get_self().'?hrm='.$hrm->getId());
  69. exit;
  70. }
  71. }
  72. /**
  73. * Get the data to fill the tables on screen.
  74. *
  75. * @param User $hrm
  76. * @param int $status
  77. *
  78. * @return array
  79. */
  80. function getData(User $hrm, $status = HRM_REQUEST)
  81. {
  82. $requests = UserManager::getUsersFollowedByUser(
  83. $hrm->getId(),
  84. null,
  85. false,
  86. false,
  87. false,
  88. null,
  89. null,
  90. null,
  91. null,
  92. null,
  93. null,
  94. $status
  95. );
  96. $result = [];
  97. $iconAccept = Display::return_icon('accept.png', get_lang('Accept'));
  98. $urlAccept = api_get_self().'?action=accept&hrm='.$hrm->getId().'&u=';
  99. $iconReject = Display::return_icon('delete.png', get_lang('Reject'));
  100. $urlReject = api_get_self().'?action=reject&hrm='.$hrm->getId().'&u=';
  101. $iconRemove = Display::return_icon('delete.png', get_lang('Remove'));
  102. $urlRemove = api_get_self().'?action=remove&hrm='.$hrm->getId().'&u=';
  103. foreach ($requests as $request) {
  104. $line = [];
  105. $studentLink = api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$request['user_id'];
  106. $line[] = '<a href="'.$studentLink.'">'.api_get_person_name($request['firstname'], $request['lastname']).'</a>';
  107. if ($status == HRM_REQUEST) {
  108. $line[] = Display::url(
  109. $iconAccept,
  110. $urlAccept.$request['user_id']
  111. ).
  112. Display::url(
  113. $iconReject,
  114. $urlReject.$request['user_id']
  115. );
  116. } else {
  117. $line[] = Display::url(
  118. $iconRemove,
  119. $urlRemove.$request['user_id']
  120. );
  121. }
  122. $result[] = $line;
  123. }
  124. return $result;
  125. }
  126. $form = new FormValidator('user_linking_requests', 'get');
  127. $form->addSelectAjax(
  128. 'hrm',
  129. get_lang('DRH'),
  130. $hrm ? [$hrm->getId() => UserManager::formatUserFullName($hrm)] : [],
  131. ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role']
  132. );
  133. $form->addButtonFilter(get_lang('Filter'));
  134. $content = $form->returnForm();
  135. if ($hrm) {
  136. $requests = getData($hrm);
  137. if ($requests) {
  138. $content .= Display::table(
  139. [get_lang('UserLinkingRequests'), get_lang('Actions')],
  140. $requests
  141. );
  142. } else {
  143. $content .= Display::table(
  144. [get_lang('UserLinkingRequests')],
  145. [get_lang('NoResults')]
  146. );
  147. }
  148. $approved = getData($hrm, DRH);
  149. if ($approved) {
  150. $content .= Display::table(
  151. [get_lang('UserLinkingTo'), get_lang('Actions')],
  152. $approved
  153. );
  154. } else {
  155. $content .= Display::table(
  156. [get_lang('UserLinkingTo')],
  157. [get_lang('NoResults')]
  158. );
  159. }
  160. }
  161. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  162. $toolName = get_lang('UserLinkingRequests');
  163. $view = new Template($toolName);
  164. $view->assign('header', $toolName);
  165. $view->assign('content', $content);
  166. $view->display_one_col_template();