forum.ajax.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CForumPost;
  4. /**
  5. * Responses to AJAX calls for forum attachments.
  6. *
  7. * @package chamilo/forum
  8. *
  9. * @author Daniel Barreto Alva <daniel.barreto@beeznest.com>
  10. */
  11. require_once __DIR__.'/../global.inc.php';
  12. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  13. // First, protect this script
  14. api_protect_course_script(false);
  15. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  16. // Create a default error response
  17. $json = [
  18. 'error' => true,
  19. 'errorMessage' => 'ERROR',
  20. ];
  21. // Check if exist action
  22. if (!empty($action)) {
  23. switch ($action) {
  24. case 'upload_file':
  25. $current_forum = get_forum_information($_REQUEST['forum']);
  26. $current_forum_category = get_forumcategory_information($current_forum['forum_category']);
  27. $current_thread = get_thread_information($_REQUEST['forum'], $_REQUEST['thread']);
  28. if (!empty($_FILES) && !empty($_REQUEST['forum'])) {
  29. // The user is not allowed here if
  30. // 1. the forum category, forum or thread is invisible (visibility==0)
  31. // 2. the forum category, forum or thread is locked (locked <>0)
  32. // 3. if anonymous posts are not allowed
  33. // The only exception is the course manager
  34. // They are several pieces for clarity.
  35. if (!api_is_allowed_to_edit(null, true) &&
  36. (
  37. ($current_forum_category && $current_forum_category['visibility'] == 0) ||
  38. $current_forum['visibility'] == 0
  39. )
  40. ) {
  41. $json['errorMessage'] = '1. the forum category, forum or thread is invisible (visibility==0)';
  42. break;
  43. }
  44. if (!api_is_allowed_to_edit(null, true) &&
  45. (
  46. ($current_forum_category && $current_forum_category['locked'] != 0) ||
  47. $current_forum['locked'] != 0 || $current_thread['locked'] != 0
  48. )
  49. ) {
  50. $json['errorMessage'] = '2. the forum category, forum or thread is locked (locked <>0)';
  51. break;
  52. }
  53. if (api_is_anonymous() && $current_forum['allow_anonymous'] == 0) {
  54. $json['errorMessage'] = '3. if anonymous posts are not allowed';
  55. break;
  56. }
  57. // If pass all previous control, user can edit post
  58. $courseId = isset($_REQUEST['c_id']) ? intval($_REQUEST['c_id']) : api_get_course_int_id();
  59. $json['courseId'] = $courseId;
  60. $forumId = isset($_REQUEST['forum']) ? intval($_REQUEST['forum']) : null;
  61. $json['forum'] = $forumId;
  62. $threadId = isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : null;
  63. $json['thread'] = $threadId;
  64. $postId = isset($_REQUEST['postId']) ? intval($_REQUEST['postId']) : null;
  65. $json['postId'] = $postId;
  66. if (!empty($courseId) &&
  67. !is_null($forumId) &&
  68. !is_null($threadId) &&
  69. !is_null($postId)
  70. ) {
  71. // Save forum attachment
  72. $attachId = add_forum_attachment_file('', $postId);
  73. if ($attachId !== false) {
  74. // Get prepared array of attachment data
  75. $array = getAttachedFiles(
  76. $forumId,
  77. $threadId,
  78. $postId,
  79. $attachId,
  80. $courseId
  81. );
  82. // Check if array data is consistent
  83. if (isset($array['name'])) {
  84. $json['error'] = false;
  85. $json['errorMessage'] = 'Success';
  86. $json = array_merge($json, $array);
  87. }
  88. }
  89. }
  90. }
  91. echo json_encode($json);
  92. break;
  93. case 'delete_file':
  94. $current_forum = get_forum_information($_REQUEST['forum']);
  95. $current_forum_category = get_forumcategory_information($current_forum['forum_category']);
  96. $current_thread = get_thread_information($_REQUEST['forum'], $_REQUEST['thread']);
  97. // Check if set attachment ID and thread ID
  98. if (isset($_REQUEST['attachId']) && isset($_REQUEST['thread'])) {
  99. api_block_course_item_locked_by_gradebook($_REQUEST['thread'], LINK_FORUM_THREAD);
  100. // The user is not allowed here if
  101. // 1. the forum category, forum or thread is invisible (visibility==0)
  102. // 2. the forum category, forum or thread is locked (locked <>0)
  103. // 3. if anonymous posts are not allowed
  104. // 4. if editing of replies is not allowed
  105. // The only exception is the course manager
  106. // They are several pieces for clarity.
  107. if (!api_is_allowed_to_edit(null, true) &&
  108. (
  109. ($current_forum_category && $current_forum_category['visibility'] == 0) ||
  110. $current_forum['visibility'] == 0
  111. )
  112. ) {
  113. $json['errorMessage'] = '1. the forum category, forum or thread is invisible (visibility==0)';
  114. break;
  115. }
  116. if (!api_is_allowed_to_edit(null, true) &&
  117. (
  118. ($current_forum_category && $current_forum_category['locked'] != 0) ||
  119. $current_forum['locked'] != 0 || $current_thread['locked'] != 0
  120. )
  121. ) {
  122. $json['errorMessage'] = '2. the forum category, forum or thread is locked (locked <>0)';
  123. break;
  124. }
  125. if (api_is_anonymous() && $current_forum['allow_anonymous'] == 0) {
  126. $json['errorMessage'] = '3. if anonymous posts are not allowed';
  127. break;
  128. }
  129. $group_id = api_get_group_id();
  130. $groupInfo = GroupManager::get_group_properties($group_id);
  131. if (!api_is_allowed_to_edit(null, true) &&
  132. $current_forum['allow_edit'] == 0 &&
  133. ($group_id && !GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo))
  134. ) {
  135. $json['errorMessage'] = '4. if editing of replies is not allowed';
  136. break;
  137. }
  138. // If pass all previous control, user can edit post
  139. $attachId = $_REQUEST['attachId'];
  140. $threadId = $_REQUEST['thread'];
  141. // Delete forum attachment from database and file system
  142. $affectedRows = delete_attachment(0, $attachId, false);
  143. if ($affectedRows > 0) {
  144. $json['error'] = false;
  145. $json['errorMessage'] = 'Success';
  146. }
  147. }
  148. echo json_encode($json);
  149. break;
  150. case 'change_post_status':
  151. if (api_is_allowed_to_edit(false, true)) {
  152. $postId = isset($_GET['post_id']) ? $_GET['post_id'] : '';
  153. if (empty($postId)) {
  154. exit;
  155. }
  156. $postId = str_replace('status_post_', '', $postId);
  157. $em = Database::getManager();
  158. /** @var CForumPost $post */
  159. $post = $em->find('ChamiloCourseBundle:CForumPost', $postId);
  160. if ($post) {
  161. $forum = get_forums($post->getForumId(), api_get_course_id());
  162. $status = $post->getStatus();
  163. if (empty($status)) {
  164. $status = CForumPost::STATUS_WAITING_MODERATION;
  165. }
  166. switch ($status) {
  167. case CForumPost::STATUS_VALIDATED:
  168. $changeTo = CForumPost::STATUS_REJECTED;
  169. break;
  170. case CForumPost::STATUS_WAITING_MODERATION:
  171. $changeTo = CForumPost::STATUS_VALIDATED;
  172. break;
  173. case CForumPost::STATUS_REJECTED:
  174. $changeTo = CForumPost::STATUS_WAITING_MODERATION;
  175. break;
  176. }
  177. $post->setStatus($changeTo);
  178. $em->persist($post);
  179. $em->flush();
  180. echo getPostStatus(
  181. $forum,
  182. [
  183. 'iid' => $post->getIid(),
  184. 'status' => $post->getStatus(),
  185. ],
  186. false
  187. );
  188. }
  189. }
  190. break;
  191. }
  192. }
  193. exit;