announcement.ajax.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  8. $isAllowedToEdit = api_is_allowed_to_edit();
  9. $courseInfo = api_get_course_info();
  10. $courseId = api_get_course_int_id();
  11. $groupId = api_get_group_id();
  12. $sessionId = api_get_session_id();
  13. $isTutor = false;
  14. if (!empty($groupId)) {
  15. $groupInfo = GroupManager::get_group_properties($groupId);
  16. $isTutor = GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo);
  17. if ($isTutor) {
  18. $isAllowedToEdit = true;
  19. }
  20. }
  21. switch ($action) {
  22. case 'preview':
  23. $allowToEdit = (
  24. api_is_allowed_to_edit(false, true) ||
  25. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  26. );
  27. $drhHasAccessToSessionContent = api_drh_can_access_all_session_content();
  28. if (!empty($sessionId) && $drhHasAccessToSessionContent) {
  29. $allowToEdit = $allowToEdit || api_is_drh();
  30. }
  31. if ($allowToEdit === false && !empty($groupId)) {
  32. $groupProperties = GroupManager::get_group_properties($groupId);
  33. // Check if user is tutor group
  34. $isTutor = GroupManager::is_tutor_of_group(api_get_user_id(), $groupProperties, $courseId);
  35. if ($isTutor) {
  36. $allowToEdit = true;
  37. }
  38. // Last chance ... students can send announcements.
  39. if ($groupProperties['announcements_state'] == GroupManager::TOOL_PRIVATE_BETWEEN_USERS) {
  40. $allowToEdit = true;
  41. }
  42. }
  43. if ($allowToEdit === false) {
  44. exit;
  45. }
  46. $users = isset($_REQUEST['users']) ? json_decode($_REQUEST['users']) : '';
  47. $formParams = [];
  48. if (isset($_REQUEST['form'])) {
  49. parse_str($_REQUEST['form'], $formParams);
  50. }
  51. $previewGroups = [];
  52. $previewUsers = [];
  53. $previewTotal = [];
  54. if (empty($groupId)) {
  55. if (empty($users) ||
  56. (!empty($users) && isset($users[0]) && $users[0] == 'everyone')
  57. ) {
  58. // All users in course session
  59. if (empty($sessionId)) {
  60. $students = CourseManager::get_user_list_from_course_code($courseInfo['code']);
  61. } else {
  62. $students = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId);
  63. }
  64. foreach ($students as $student) {
  65. $previewUsers[] = $student['user_id'];
  66. }
  67. $groupList = GroupManager::get_group_list(null, $courseInfo, null, $sessionId);
  68. foreach ($groupList as $group) {
  69. $previewGroups[] = $group['iid'];
  70. }
  71. } else {
  72. $send_to = CourseManager::separateUsersGroups($users);
  73. // Storing the selected groups
  74. if (is_array($send_to['groups']) &&
  75. !empty($send_to['groups'])
  76. ) {
  77. $counter = 1;
  78. foreach ($send_to['groups'] as $group) {
  79. $previewGroups[] = $group;
  80. }
  81. }
  82. // Storing the selected users
  83. if (is_array($send_to['users'])) {
  84. $counter = 1;
  85. foreach ($send_to['users'] as $user) {
  86. $previewUsers[] = $user;
  87. }
  88. }
  89. }
  90. } else {
  91. $send_to_users = CourseManager::separateUsersGroups($users);
  92. $sentToAllGroup = false;
  93. if (empty($send_to_users['groups']) && empty($send_to_users['users'])) {
  94. $previewGroups[] = $groupId;
  95. $sentToAllGroup = true;
  96. }
  97. if ($sentToAllGroup === false) {
  98. if (!empty($send_to_users['groups'])) {
  99. foreach ($send_to_users['groups'] as $group) {
  100. $previewGroups[] = $group;
  101. }
  102. }
  103. if (!empty($send_to_users['users'])) {
  104. foreach ($send_to_users['users'] as $user) {
  105. $previewUsers[] = $user;
  106. }
  107. }
  108. }
  109. }
  110. if (isset($formParams['send_to_users_in_session']) && $formParams['send_to_users_in_session'] == 1) {
  111. $sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
  112. if (!empty($sessionList)) {
  113. foreach ($sessionList as $sessionInfo) {
  114. $sessionId = $sessionInfo['id'];
  115. $userList = CourseManager::get_user_list_from_course_code(
  116. $courseCode,
  117. $sessionId
  118. );
  119. if (!empty($userList)) {
  120. foreach ($userList as $user) {
  121. $previewUsers[] = $user;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. if (isset($formParams['send_to_hrm_users']) && $formParams['send_to_hrm_users'] == 1) {
  128. foreach ($previewUsers as $userId) {
  129. $userInfo = api_get_user_info($userId);
  130. $drhList = UserManager::getDrhListFromUser($userId);
  131. if (!empty($drhList)) {
  132. foreach ($drhList as $drhInfo) {
  133. $previewUsers[] = $drhInfo['id'];
  134. }
  135. }
  136. }
  137. }
  138. if (isset($formParams['send_me_a_copy_by_email']) && $formParams['send_me_a_copy_by_email'] == 1) {
  139. $previewUsers[] = api_get_user_id();
  140. }
  141. $previewUserNames = [];
  142. $previewGroupNames = [];
  143. if (!empty($previewGroups)) {
  144. $previewGroups = array_unique($previewGroups);
  145. foreach ($previewGroups as $groupId) {
  146. $groupInfo = GroupManager::get_group_properties($groupId);
  147. $previewGroupNames[] = Display::label($groupInfo['name'], 'info');
  148. }
  149. $previewTotal = $previewGroupNames;
  150. }
  151. if (!empty($previewUsers)) {
  152. $previewUsers = array_unique($previewUsers);
  153. foreach ($previewUsers as $userId) {
  154. $userInfo = api_get_user_info($userId);
  155. $previewUserNames[] = Display::label($userInfo['complete_name']);
  156. }
  157. $previewTotal = array_merge($previewTotal, $previewUserNames);
  158. }
  159. $previewTotal = array_map(function ($value) { return ''.$value; }, $previewTotal);
  160. echo json_encode($previewTotal);
  161. break;
  162. case 'delete_item':
  163. if ($isAllowedToEdit) {
  164. if (empty($_REQUEST['id'])) {
  165. return false;
  166. }
  167. if (!empty($sessionId) && api_is_allowed_to_session_edit(false, true) == false && empty($groupId)) {
  168. return false;
  169. }
  170. $list = explode(',', $_REQUEST['id']);
  171. foreach ($list as $itemId) {
  172. if (!api_is_session_general_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $itemId)) {
  173. $result = AnnouncementManager::get_by_id(
  174. api_get_course_int_id(),
  175. $itemId
  176. );
  177. if (!empty($result)) {
  178. $delete = true;
  179. if (!empty($groupId) && $isTutor) {
  180. if ($groupId != $result['to_group_id']) {
  181. $delete = false;
  182. }
  183. }
  184. if ($delete) {
  185. AnnouncementManager::delete_announcement($courseInfo, $itemId);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. break;
  192. default:
  193. echo '';
  194. break;
  195. }
  196. exit;