viewthread_flat.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = api_get_origin();
  17. $sessionId = api_get_session_id();
  18. $_user = api_get_user_info();
  19. $userId = api_get_user_id();
  20. $groupId = api_get_group_id();
  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. if (isset($current_thread['thread_id'])) {
  24. $rows = getPosts($current_forum, $current_thread['thread_id'], $sortDirection);
  25. $increment = 0;
  26. $clean_forum_id = intval($_GET['forum']);
  27. $clean_thread_id = intval($_GET['thread']);
  28. $locked = api_resource_is_locked_by_gradebook(
  29. $clean_thread_id,
  30. LINK_FORUM_THREAD
  31. );
  32. $closedPost = null;
  33. if (!empty($rows)) {
  34. $postCount = count($rows);
  35. foreach ($rows as $row) {
  36. if ($row['user_id'] == '0') {
  37. $name = prepare4display($row['poster_name']);
  38. } else {
  39. $name = api_get_person_name($row['firstname'], $row['lastname']);
  40. }
  41. $username = sprintf(get_lang('LoginX'), $row['username']);
  42. if (($current_forum_category && $current_forum_category['locked'] == 0) &&
  43. $current_forum['locked'] == 0 &&
  44. $current_thread['locked'] == 0 ||
  45. api_is_allowed_to_edit(false, true)
  46. ) {
  47. if ($_user['user_id'] ||
  48. ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])
  49. ) {
  50. if (!api_is_anonymous() &&
  51. api_is_allowed_to_session_edit(false, true)
  52. ) {
  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. Display::dateToStringAgoAndLongDate($row['post_date']),
  134. array('class' => 'post-date')
  135. );
  136. } else {
  137. $html .= Display::tag(
  138. 'p',
  139. Display::dateToStringAgoAndLongDate($row['post_date']),
  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. $statusIcon = '';
  148. // The user who posted it can edit his thread only if the course admin allowed
  149. // this in the properties of the forum
  150. // The course admin him/herself can do this off course always
  151. $groupInfo = GroupManager::get_group_properties($groupId);
  152. if ((isset($groupInfo['iid']) && GroupManager::is_tutor_of_group($userId, $groupInfo)) ||
  153. ($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  154. (
  155. api_is_allowed_to_edit(false, true) &&
  156. !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)
  157. )
  158. ) {
  159. if (api_is_allowed_to_session_edit(false, true)) {
  160. if ($locked == false && postIsEditableByStudent($current_forum, $row)) {
  161. $iconEdit .= "<a href=\"editpost.php?".api_get_cidreq()."&forum=".$clean_forum_id
  162. . "&thread=".$clean_thread_id."&post=".$row['post_id']
  163. . "&edit=edition&id_attach=".$id_attach."\">"
  164. . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)."</a>";
  165. }
  166. }
  167. }
  168. if ($origin != 'learnpath') {
  169. if (GroupManager::is_tutor_of_group($userId, $groupInfo) ||
  170. api_is_allowed_to_edit(false, true) &&
  171. !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)
  172. ) {
  173. if ($locked === false) {
  174. $deleteUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  175. 'forum' => $clean_forum_id,
  176. 'thread' => $clean_thread_id,
  177. 'action' => 'delete',
  178. 'content' => 'post',
  179. 'id' => $row['post_id']
  180. ]);
  181. $iconEdit .= Display::url(
  182. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  183. $deleteUrl,
  184. [
  185. 'onclick' => "javascript:if(!confirm('"
  186. . addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES))
  187. . "')) return false;",
  188. 'id' => "delete-post-{$row['post_id']}"
  189. ]
  190. );
  191. }
  192. }
  193. $statusIcon = getPostStatus($current_forum, $row);
  194. if (GroupManager::is_tutor_of_group($userId, $groupInfo) ||
  195. (api_is_allowed_to_edit(false, true) &&
  196. !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)
  197. )
  198. ) {
  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. . Display::return_icon('move.png', get_lang('MovePost'), array(), ICON_SIZE_SMALL)
  213. . "</a>";
  214. }
  215. }
  216. }
  217. $user_status = api_get_status_of_user_in_course($row['user_id'], api_get_course_int_id());
  218. $current_qualify_thread = showQualify('1', $row['poster_id'], $_GET['thread']);
  219. if (($current_thread['thread_peer_qualify'] == 1 || api_is_allowed_to_edit(null, true)) &&
  220. $current_thread['thread_qualify_max'] > 0 && $origin != 'learnpath'
  221. ) {
  222. $my_forum_id = $clean_forum_id;
  223. $info_thread = get_thread_information($clean_forum_id, $clean_thread_id);
  224. $my_forum_id = $info_thread['forum_id'];
  225. $userCanEdit = $current_thread['thread_peer_qualify'] == 1 && $row['poster_id'] != $userId;
  226. /*if ($row['poster_id'] != $userId && $current_forum['moderated'] == 1 && $row['status']) {
  227. }*/
  228. if (api_is_allowed_to_edit(null, true)) {
  229. $userCanEdit = true;
  230. }
  231. if ($increment > 0 && $locked == false && $userCanEdit) {
  232. $iconEdit .= "<a href=\"forumqualify.php?".api_get_cidreq()."&forum=".$my_forum_id
  233. . "&thread=".$clean_thread_id."&action=list&post=".$row['post_id']
  234. . "&user=".$row['poster_id']."&user_id=".$row['poster_id']
  235. . "&idtextqualify=".$current_qualify_thread."\" >"
  236. . Display::return_icon('quiz.png', get_lang('Qualify'))
  237. . "</a> ";
  238. }
  239. }
  240. if (!empty($iconEdit)) {
  241. $html .= '<div class="tools-icons">'.$iconEdit.' '.$statusIcon.'</div>';
  242. } else {
  243. if (!empty(strip_tags($statusIcon))) {
  244. $html .= '<div class="tools-icons">'.$statusIcon.'</div>';
  245. }
  246. }
  247. $html .= $closedPost;
  248. $html .= '</div>';
  249. $html .= '<div class="col-md-10">';
  250. $titlePost = Display::tag(
  251. 'h3',
  252. $row['post_title'],
  253. array('class' => 'forum_post_title')
  254. );
  255. $html .= Display::tag(
  256. 'div',
  257. $titlePost,
  258. array('class' => 'post-header')
  259. );
  260. // see comments inside forumfunction.inc.php to lower filtering and allow more visual changes
  261. $html .= Display::tag(
  262. 'div',
  263. $row['post_text'],
  264. array('class' => 'post-body')
  265. );
  266. $html .= '</div>';
  267. $html .= '</div>';
  268. $html .= '<div class="row">';
  269. $html .= '<div class="col-md-7">';
  270. // prepare the notification icon
  271. if (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]) &&
  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_session_general_coach() && $current_forum['session_id'] != $sessionId))
  295. ) {
  296. $html .= '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_attach&id_attach='
  297. . $attachment['iid'].'&forum='.$clean_forum_id.'&thread='.$clean_thread_id
  298. . '" onclick="javascript:if(!confirm(\''
  299. . addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))
  300. . '\')) return false;">'
  301. . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  302. . '</a><br />';
  303. }
  304. $html .= '<span class="forum_attach_comment" >'.$attachment['comment'].'</span>';
  305. }
  306. }
  307. $html .= '</div>';
  308. $html .= '<div class="col-md-5 text-right">';
  309. $html .= $buttonReply.' '.$buttonQuote;
  310. $html .= '</div>';
  311. $html .= '</div>';
  312. // The post has been displayed => it can be removed from the what's new array
  313. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  314. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']]);
  315. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  316. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']]);
  317. $increment++;
  318. $html .= '</div>';
  319. $html .= '</div>';
  320. echo $html;
  321. }
  322. }
  323. }