viewthread_flat.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_forum, $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. foreach ($rows as $row) {
  39. if ($row['user_id'] == '0') {
  40. $name = prepare4display($row['poster_name']);
  41. } else {
  42. $name = api_get_person_name($row['firstname'], $row['lastname']);
  43. }
  44. $username = sprintf(get_lang('LoginX'), $row['username']);
  45. if (($current_forum_category && $current_forum_category['locked'] == 0) &&
  46. $current_forum['locked'] == 0 &&
  47. $current_thread['locked'] == 0 ||
  48. api_is_allowed_to_edit(false, true)
  49. ) {
  50. if ($_user['user_id'] || ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])) {
  51. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  52. $buttonReply = Display::toolbarButton(
  53. get_lang('ReplyToMessage'),
  54. 'reply.php?' . api_get_cidreq() . '&' . http_build_query([
  55. 'forum' => $clean_forum_id,
  56. 'thread' => $clean_thread_id,
  57. 'post' => $row['post_id'],
  58. 'action' => 'replymessage'
  59. ]),
  60. 'reply',
  61. 'primary',
  62. ['id' => "reply-to-post-{$row['post_id']}"]
  63. );
  64. $buttonQuote = Display::toolbarButton(
  65. get_lang('QuoteMessage'),
  66. 'reply.php?' . api_get_cidreq() . '&' . http_build_query([
  67. 'forum' => $clean_forum_id,
  68. 'thread' => $clean_thread_id,
  69. 'post' => $row['post_id'],
  70. 'action' => 'quote'
  71. ]),
  72. 'quote-left',
  73. 'success',
  74. ['id' => "quote-post-{$row['post_id']}"]
  75. );
  76. }
  77. }
  78. } else {
  79. if (($current_forum_category && $current_forum_category['locked'] == 1)) {
  80. $closedPost = Display::tag(
  81. 'div',
  82. '<em class="fa fa-exclamation-triangle"></em> ' . get_lang('ForumcategoryLocked'),
  83. array('class' => 'alert alert-warning post-closed')
  84. );
  85. }
  86. if ($current_forum['locked'] == 1) {
  87. $closedPost = Display::tag(
  88. 'div',
  89. '<em class="fa fa-exclamation-triangle"></em> ' . get_lang('ForumLocked'),
  90. array('class' => 'alert alert-warning post-closed')
  91. );
  92. }
  93. if ($current_thread['locked'] == 1) {
  94. $closedPost = Display::tag(
  95. 'div',
  96. '<em class="fa fa-exclamation-triangle"></em> ' . get_lang('ThreadLocked'),
  97. array('class' => 'alert alert-warning post-closed')
  98. );
  99. }
  100. }
  101. $html = '';
  102. $html .= '<div class="panel panel-default forum-post">';
  103. $html .= '<div class="panel-body">';
  104. $html .= '<div class="row">';
  105. $html .= '<div class="col-md-2">';
  106. if ($origin != 'learnpath') {
  107. if (api_get_course_setting('allow_user_image_forum')) {
  108. $html .= '<div class="thumbnail">' . display_user_image($row['user_id'], $name, $origin) . '</div>';
  109. }
  110. $html .= Display::tag(
  111. 'h4',
  112. display_user_link($row['user_id'], $name),
  113. array('class' => 'title-username')
  114. );
  115. } else {
  116. if (api_get_course_setting('allow_user_image_forum')) {
  117. $html .= '<div class="thumbnail">' . display_user_image($row['user_id'], $name, $origin) . '</div>';
  118. }
  119. $name = Display::tag('strong', "#" . $postCount--, ['class' => 'text-info']) . " | $name";
  120. $html .= Display::tag(
  121. 'p',
  122. $name,
  123. array(
  124. 'title' => api_htmlentities($username, ENT_QUOTES),
  125. 'class' => 'lead'
  126. )
  127. );
  128. }
  129. if ($origin != 'learnpath') {
  130. $html .= Display::tag(
  131. 'p',
  132. api_convert_and_format_date($row['post_date']),
  133. array('class' => 'post-date')
  134. );
  135. } else {
  136. $html .= Display::tag(
  137. 'p',
  138. api_convert_and_format_date($row['post_date'], DATE_TIME_FORMAT_SHORT),
  139. array('class' => 'text-muted')
  140. );
  141. }
  142. // get attach id
  143. $attachment_list = get_attachment($row['post_id']);
  144. $id_attach = !empty($attachment_list) ? $attachment_list['iid'] : '';
  145. $iconEdit = '';
  146. $statusIcon = '';
  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. $groupInfo = GroupManager::get_group_properties($groupId);
  151. if ((isset($groupInfo['iid']) && GroupManager::is_tutor_of_group($userId, $groupInfo['iid'])) ||
  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, $groupInfo['iid']) ||
  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. GroupManager::is_tutor_of_group($userId, $groupInfo['iid']) ||
  194. (api_is_allowed_to_edit(false, true) &&
  195. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  196. )
  197. ) {
  198. $statusIcon = getPostStatus($current_forum, $row);
  199. $iconEdit .= return_visible_invisible_icon(
  200. 'post',
  201. $row['post_id'],
  202. $row['visible'],
  203. array(
  204. 'forum' => $clean_forum_id,
  205. 'thread' => $clean_thread_id,
  206. 'origin' => $origin
  207. )
  208. );
  209. if ($increment > 0) {
  210. $iconEdit .= "<a href=\"viewthread.php?" . api_get_cidreq() . "&forum=" . $clean_forum_id
  211. . "&thread=" . $clean_thread_id . "&action=move&post=" . $row['post_id']
  212. . "&origin=" . $origin . "\">"
  213. . Display::return_icon('move.png', get_lang('MovePost'), array(), ICON_SIZE_SMALL)
  214. . "</a>";
  215. }
  216. }
  217. }
  218. $user_status = api_get_status_of_user_in_course($row['user_id'], api_get_course_int_id());
  219. $current_qualify_thread = showQualify('1', $row['poster_id'], $_GET['thread']);
  220. if (($current_thread['thread_peer_qualify'] == 1 || api_is_allowed_to_edit(null, true)) &&
  221. $current_thread['thread_qualify_max'] > 0 && $origin != 'learnpath'
  222. ) {
  223. $my_forum_id = $clean_forum_id;
  224. if (isset($_GET['gradebook'])) {
  225. $info_thread = get_thread_information($clean_forum_id, $clean_thread_id);
  226. $my_forum_id = $info_thread['forum_id'];
  227. }
  228. $userCanEdit = $current_thread['thread_peer_qualify'] == 1 && $row['poster_id'] != $userId;
  229. if (api_is_allowed_to_edit(null, true)) {
  230. $userCanEdit = true;
  231. }
  232. if ($increment > 0 && $locked == false && $userCanEdit) {
  233. $iconEdit .= "<a href=\"forumqualify.php?" . api_get_cidreq() . "&forum=" . $my_forum_id
  234. . "&thread=" . $clean_thread_id . "&action=list&post=" . $row['post_id']
  235. . "&user=" . $row['poster_id'] . "&user_id=" . $row['poster_id']
  236. . "&origin=" . $origin . "&idtextqualify=" . $current_qualify_thread . "\" >"
  237. . Display::return_icon('quiz.gif', get_lang('Qualify'))
  238. . "</a> ";
  239. }
  240. }
  241. if ($iconEdit != '') {
  242. $html .= '<div class="tools-icons">' . $iconEdit . $statusIcon.'</div>';
  243. }
  244. $html .= $closedPost;
  245. $html .= '</div>';
  246. $html .= '<div class="col-md-10">';
  247. $titlePost = Display::tag(
  248. 'h3',
  249. $row['post_title'],
  250. array('class' => 'forum_post_title')
  251. );
  252. $html .= Display::tag(
  253. 'div',
  254. $titlePost,
  255. array('class' => 'post-header')
  256. );
  257. // see comments inside forumfunction.inc.php to lower filtering and allow more visual changes
  258. $html .= Display::tag(
  259. 'div',
  260. $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 (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]) &&
  269. !empty(
  270. $whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]
  271. ) &&
  272. !empty($whatsnew_post_info[$_GET['forum']][$row['thread_id']])
  273. ) {
  274. $post_image = Display::return_icon('forumpostnew.gif');
  275. } else {
  276. $post_image = Display::return_icon('forumpost.gif');
  277. }
  278. if ($row['post_notification'] == '1' && $row['poster_id'] == $_user['user_id']) {
  279. $post_image .= Display::return_icon('forumnotification.gif', get_lang('YouWillBeNotified'));
  280. }
  281. // The post title
  282. // The check if there is an attachment
  283. $attachment_list = getAllAttachment($row['post_id']);
  284. if (!empty($attachment_list) && is_array($attachment_list)) {
  285. foreach ($attachment_list as $attachment) {
  286. $realname = $attachment['path'];
  287. $user_filename = $attachment['filename'];
  288. $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
  289. $html .= '<a href="download.php?file=' . $realname . '"> ' . $user_filename . ' </a>';
  290. if (($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  291. (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && $current_forum['session_id'] != $sessionId))
  292. ) {
  293. $html .= '&nbsp;&nbsp;<a href="' . api_get_self() . '?' . api_get_cidreq() . '&origin='
  294. . Security::remove_XSS($origin) . '&action=delete_attach&id_attach='
  295. . $attachment['iid'] . '&forum=' . $clean_forum_id . '&thread=' . $clean_thread_id
  296. . '" onclick="javascript:if(!confirm(\''
  297. . addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))
  298. . '\')) return false;">'
  299. . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  300. . '</a><br />';
  301. }
  302. $html .= '<span class="forum_attach_comment" >' . $attachment['comment'] . '</span>';
  303. }
  304. }
  305. $html .= '</div>';
  306. $html .= '<div class="col-md-5 text-right">';
  307. $html .= $buttonReply . ' ' . $buttonQuote;
  308. $html .= '</div>';
  309. $html .= '</div>';
  310. // The post has been displayed => it can be removed from the what's new array
  311. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  312. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']]);
  313. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  314. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']]);
  315. $increment++;
  316. $html .= '</div>';
  317. $html .= '</div>';
  318. echo $html;
  319. }
  320. }
  321. }