viewthread_flat.inc.php 16 KB

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