forum.ajax.php 9.0 KB

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