viewthread_flat.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script manages the display of forum threads in flat view
  5. * @copyright Julio Montoya <gugli100@gmail.com> UI Improvements + lots of bugfixes
  6. * @package chamilo.forum
  7. */
  8. // Delete attachment file
  9. if ((isset($_GET['action']) &&
  10. $_GET['action'] == 'delete_attach') &&
  11. isset($_GET['id_attach'])
  12. ) {
  13. delete_attachment(0, $_GET['id_attach']);
  14. }
  15. $sessionId = api_get_session_id();
  16. $_user = api_get_user_info();
  17. $userId = api_get_user_id();
  18. $groupId = api_get_group_id();
  19. $sortDirection = isset($_GET['posts_order']) && $_GET['posts_order'] === 'desc' ? 'DESC' : 'ASC';
  20. if (isset($current_thread['thread_id'])) {
  21. $rows = getPosts($current_thread['thread_id'], $sortDirection);
  22. $increment = 0;
  23. $clean_forum_id = intval($_GET['forum']);
  24. $clean_thread_id = intval($_GET['thread']);
  25. $locked = api_resource_is_locked_by_gradebook(
  26. $clean_thread_id,
  27. LINK_FORUM_THREAD
  28. );
  29. $closedPost = null;
  30. if (!empty($rows)) {
  31. foreach ($rows as $row) {
  32. if ($row['user_id'] == '0') {
  33. $name = prepare4display($row['poster_name']);
  34. } else {
  35. $name = api_get_person_name($row['firstname'], $row['lastname']);
  36. }
  37. $username = sprintf(get_lang('LoginX'), $row['username']);
  38. if (($current_forum_category && $current_forum_category['locked'] == 0) &&
  39. $current_forum['locked'] == 0 &&
  40. $current_thread['locked'] == 0 ||
  41. api_is_allowed_to_edit(false, true)
  42. ) {
  43. if ($_user['user_id'] || ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])) {
  44. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  45. $buttonReply = Display::tag(
  46. 'a',
  47. '<i class="fa fa-reply"></i> ' . get_lang('ReplyToMessage'),
  48. array(
  49. 'href' => 'reply.php?' . api_get_cidreq()
  50. . "&forum=$clean_forum_id&thread=$clean_thread_id&post="
  51. . "{$row['post_id']}&action=replymessage&origin=$origin",
  52. 'class' => 'btn btn-primary'
  53. )
  54. );
  55. $buttonQuote = Display::tag(
  56. 'a',
  57. '<i class="fa fa-quote-left"></i> ' . get_lang('QuoteMessage'),
  58. array(
  59. 'href' => 'reply.php?' . api_get_cidreq()
  60. . "&forum=$clean_forum_id&thread=$clean_thread_id"
  61. . "&post={$row['post_id']}&action=quote&origin=$origin",
  62. 'class' => 'btn btn-success'
  63. )
  64. );
  65. }
  66. }
  67. } else {
  68. if (($current_forum_category && $current_forum_category['locked'] == 1)) {
  69. $closedPost = Display::tag(
  70. 'div',
  71. '<i class="fa fa-exclamation-triangle"></i> ' . get_lang('ForumcategoryLocked'),
  72. array('class' => 'alert alert-warning post-closed')
  73. );
  74. }
  75. if ($current_forum['locked'] == 1) {
  76. $closedPost = Display::tag(
  77. 'div',
  78. '<i class="fa fa-exclamation-triangle"></i> ' . get_lang('ForumLocked'),
  79. array('class' => 'alert alert-warning post-closed')
  80. );
  81. }
  82. if ($current_thread['locked'] == 1) {
  83. $closedPost = Display::tag(
  84. 'div',
  85. '<i class="fa fa-exclamation-triangle"></i> ' . get_lang('ThreadLocked'),
  86. array('class' => 'alert alert-warning post-closed')
  87. );
  88. }
  89. }
  90. $html = '';
  91. $html .= '<div class="panel panel-default forum-post">';
  92. $html .= '<div class="panel-body">';
  93. $html .= '<div class="row">';
  94. $html .= '<div class="col-md-2">';
  95. if ($origin != 'learnpath') {
  96. if (api_get_course_setting('allow_user_image_forum')) {
  97. $html .= '<div class="thumbnail">' . display_user_image($row['user_id'], $name) . '</div>';
  98. }
  99. $html .= Display::tag(
  100. 'h4',
  101. display_user_link($row['user_id'], $name),
  102. array('class' => 'title-username')
  103. );
  104. } else {
  105. $html .= Display::tag(
  106. 'span',
  107. $name,
  108. array(
  109. 'title' => api_htmlentities($username, ENT_QUOTES)
  110. )
  111. );
  112. }
  113. $html .= Display::tag(
  114. 'p',
  115. api_convert_and_format_date($row['post_date']),
  116. array('class' => 'post-date')
  117. );
  118. // get attach id
  119. $attachment_list = get_attachment($row['post_id']);
  120. $id_attach = !empty($attachment_list) ? $attachment_list['iid'] : '';
  121. $iconEdit = '';
  122. // The user who posted it can edit his thread only if the course admin allowed
  123. // this in the properties of the forum
  124. // The course admin him/herself can do this off course always
  125. if (
  126. GroupManager::is_tutor_of_group($userId, $groupId) ||
  127. ($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  128. (
  129. api_is_allowed_to_edit(false, true) &&
  130. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  131. )
  132. ) {
  133. if (api_is_allowed_to_session_edit(false, true)) {
  134. if ($locked == false) {
  135. $iconEdit .= "<a href=\"editpost.php?" . api_get_cidreq() . "&forum=" . $clean_forum_id
  136. . "&thread=" . $clean_thread_id . "&post=" . $row['post_id'] . "&origin="
  137. . $origin . "&edit=edition&id_attach=" . $id_attach . "\">"
  138. . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . "</a>";
  139. }
  140. }
  141. }
  142. if ($origin != 'learnpath') {
  143. if (GroupManager::is_tutor_of_group($userId, $groupId) ||
  144. api_is_allowed_to_edit(false, true) &&
  145. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  146. ) {
  147. if ($locked == false) {
  148. $iconEdit .= "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&forum="
  149. . $clean_forum_id . "&thread=" . $clean_thread_id
  150. . "&action=delete&content=post&id=" . $row['post_id'] . "&origin="
  151. . $origin . "\" onclick=\"javascript:if(!confirm('"
  152. . addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES))
  153. . "')) return false;\">"
  154. . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  155. . "</a>";
  156. }
  157. }
  158. if (
  159. api_is_allowed_to_edit(false, true) &&
  160. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  161. ) {
  162. $iconEdit .= return_visible_invisible_icon(
  163. 'post', $row['post_id'], $row['visible'], array(
  164. 'forum' => $clean_forum_id,
  165. 'thread' => $clean_thread_id,
  166. 'origin' => $origin,
  167. )
  168. );
  169. $iconEdit .= "";
  170. if ($increment > 0) {
  171. $iconEdit .= "<a href=\"viewthread.php?" . api_get_cidreq() . "&forum=" . $clean_forum_id
  172. . "&thread=" . $clean_thread_id . "&action=move&post=" . $row['post_id']
  173. . "&origin=" . $origin . "\">"
  174. . Display::return_icon('move.png', get_lang('MovePost'), array(), ICON_SIZE_SMALL)
  175. . "</a>";
  176. }
  177. }
  178. }
  179. $user_status = api_get_status_of_user_in_course(
  180. $row['user_id'], api_get_course_int_id()
  181. );
  182. $current_qualify_thread = showQualify(
  183. '1', $row['poster_id'], $_GET['thread']
  184. );
  185. if (
  186. (
  187. $current_thread['thread_peer_qualify'] == 1 ||
  188. api_is_allowed_to_edit(null, true)
  189. ) &&
  190. $current_thread['thread_qualify_max'] > 0 && $origin != 'learnpath'
  191. ) {
  192. $my_forum_id = $clean_forum_id;
  193. if (isset($_GET['gradebook'])) {
  194. $info_thread = get_thread_information($clean_thread_id);
  195. $my_forum_id = $info_thread['forum_id'];
  196. }
  197. $userCanEdit = $current_thread['thread_peer_qualify'] == 1 && $row['poster_id'] != $userId;
  198. if (api_is_allowed_to_edit(null, true)) {
  199. $userCanEdit = true;
  200. }
  201. if ($increment > 0 && $locked == false && $userCanEdit) {
  202. $iconEdit .= "<a href=\"forumqualify.php?" . api_get_cidreq() . "&forum=" . $my_forum_id
  203. . "&thread=" . $clean_thread_id . "&action=list&post=" . $row['post_id']
  204. . "&user=" . $row['poster_id'] . "&user_id=" . $row['poster_id']
  205. . "&origin=" . $origin . "&idtextqualify=" . $current_qualify_thread . "\" >"
  206. . Display::return_icon('quiz.gif', get_lang('Qualify'))
  207. . "</a> ";
  208. }
  209. }
  210. if ($iconEdit != '') {
  211. $html .= '<div class="tools-icons">' . $iconEdit . '</div>';
  212. }
  213. $html .= $closedPost;
  214. $html .= '</div>';
  215. $html .= '<div class="col-md-10">';
  216. $titlePost = Display::tag(
  217. 'h3', $row['post_title'],
  218. array('class' => 'forum_post_title')
  219. );
  220. $html .= Display::tag(
  221. 'div', $titlePost,
  222. array('class' => 'post-header')
  223. );
  224. // see comments inside forumfunction.inc.php to lower filtering and allow more visual changes
  225. $html .= Display::tag(
  226. 'div', $row['post_text'],
  227. array('class' => 'post-body')
  228. );
  229. $html .= '</div>';
  230. $html .= '</div>';
  231. $html .= '<div class="row">';
  232. $html .= '<div class="col-md-7">';
  233. // prepare the notification icon
  234. if (
  235. isset(
  236. $whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]
  237. ) &&
  238. !empty(
  239. $whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]
  240. ) &&
  241. !empty($whatsnew_post_info[$_GET['forum']][$row['thread_id']])
  242. ) {
  243. $post_image = Display::return_icon('forumpostnew.gif');
  244. } else {
  245. $post_image = Display::return_icon('forumpost.gif');
  246. }
  247. if ($row['post_notification'] == '1' && $row['poster_id'] == $_user['user_id']) {
  248. $post_image .= Display::return_icon('forumnotification.gif', get_lang('YouWillBeNotified'));
  249. }
  250. // The post title
  251. // The check if there is an attachment
  252. $attachment_list = getAllAttachment($row['post_id']);
  253. if (!empty($attachment_list) && is_array($attachment_list)) {
  254. foreach ($attachment_list as $attachment) {
  255. $realname = $attachment['path'];
  256. $user_filename = $attachment['filename'];
  257. $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
  258. $html .= '<a href="download.php?file=' . $realname . '"> ' . $user_filename . ' </a>';
  259. if (($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  260. (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && $current_forum['session_id'] != $sessionId))
  261. ) {
  262. $html .= '&nbsp;&nbsp;<a href="' . api_get_self() . '?' . api_get_cidreq() . '&origin='
  263. . Security::remove_XSS($_GET['origin']) . '&action=delete_attach&id_attach='
  264. . $attachment['iid'] . '&forum=' . $clean_forum_id . '&thread=' . $clean_thread_id
  265. . '" onclick="javascript:if(!confirm(\''
  266. . addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))
  267. . '\')) return false;">'
  268. . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  269. . '</a><br />';
  270. }
  271. $html .= '<span class="forum_attach_comment" >' . $attachment['comment'] . '</span>';
  272. }
  273. }
  274. $html .= '</div>';
  275. $html .= '<div class="col-md-5 text-right">';
  276. $html .= $buttonReply . ' ' . $buttonQuote;
  277. $html .= '</div>';
  278. $html .= '</div>';
  279. // The post has been displayed => it can be removed from the what's new array
  280. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  281. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']]);
  282. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  283. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']]);
  284. $increment++;
  285. $html .= '</div>';
  286. $html .= '</div>';
  287. echo $html;
  288. }
  289. }
  290. }