viewthread_nested.inc.php 13 KB

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