viewthread_nested.inc.php 13 KB

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