viewthread_flat.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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'] || ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])) {
  48. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  49. $buttonReply = Display::toolbarButton(
  50. get_lang('ReplyToMessage'),
  51. 'reply.php?'.api_get_cidreq().'&'.http_build_query([
  52. 'forum' => $clean_forum_id,
  53. 'thread' => $clean_thread_id,
  54. 'post' => $row['post_id'],
  55. 'action' => 'replymessage'
  56. ]),
  57. 'reply',
  58. 'primary',
  59. ['id' => "reply-to-post-{$row['post_id']}"]
  60. );
  61. $buttonQuote = Display::toolbarButton(
  62. get_lang('QuoteMessage'),
  63. 'reply.php?'.api_get_cidreq().'&'.http_build_query([
  64. 'forum' => $clean_forum_id,
  65. 'thread' => $clean_thread_id,
  66. 'post' => $row['post_id'],
  67. 'action' => 'quote'
  68. ]),
  69. 'quote-left',
  70. 'success',
  71. ['id' => "quote-post-{$row['post_id']}"]
  72. );
  73. }
  74. }
  75. } else {
  76. if (($current_forum_category && $current_forum_category['locked'] == 1)) {
  77. $closedPost = Display::tag(
  78. 'div',
  79. '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ForumcategoryLocked'),
  80. array('class' => 'alert alert-warning post-closed')
  81. );
  82. }
  83. if ($current_forum['locked'] == 1) {
  84. $closedPost = Display::tag(
  85. 'div',
  86. '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ForumLocked'),
  87. array('class' => 'alert alert-warning post-closed')
  88. );
  89. }
  90. if ($current_thread['locked'] == 1) {
  91. $closedPost = Display::tag(
  92. 'div',
  93. '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ThreadLocked'),
  94. array('class' => 'alert alert-warning post-closed')
  95. );
  96. }
  97. }
  98. $html = '';
  99. $html .= '<div class="panel panel-default forum-post">';
  100. $html .= '<div class="panel-body">';
  101. $html .= '<div class="row">';
  102. $html .= '<div class="col-md-2">';
  103. if ($origin != 'learnpath') {
  104. if (api_get_course_setting('allow_user_image_forum')) {
  105. $html .= '<div class="thumbnail">'.display_user_image($row['user_id'], $name, $origin).'</div>';
  106. }
  107. $html .= Display::tag(
  108. 'h4',
  109. display_user_link($row['user_id'], $name),
  110. array('class' => 'title-username')
  111. );
  112. } else {
  113. if (api_get_course_setting('allow_user_image_forum')) {
  114. $html .= '<div class="thumbnail">'.display_user_image($row['user_id'], $name, $origin).'</div>';
  115. }
  116. $name = Display::tag('strong', "#".$postCount--, ['class' => 'text-info'])." | $name";
  117. $html .= Display::tag(
  118. 'p',
  119. $name,
  120. array(
  121. 'title' => api_htmlentities($username, ENT_QUOTES),
  122. 'class' => 'lead'
  123. )
  124. );
  125. }
  126. if ($origin != 'learnpath') {
  127. $html .= Display::tag(
  128. 'p',
  129. Display::dateToStringAgoAndLongDate($row['post_date']),
  130. array('class' => 'post-date')
  131. );
  132. } else {
  133. $html .= Display::tag(
  134. 'p',
  135. Display::dateToStringAgoAndLongDate($row['post_date']),
  136. array('class' => 'text-muted')
  137. );
  138. }
  139. // get attach id
  140. $attachment_list = get_attachment($row['post_id']);
  141. $id_attach = !empty($attachment_list) ? $attachment_list['iid'] : '';
  142. $iconEdit = '';
  143. $statusIcon = '';
  144. // The user who posted it can edit his thread only if the course admin allowed
  145. // this in the properties of the forum
  146. // The course admin him/herself can do this off course always
  147. $groupInfo = GroupManager::get_group_properties($groupId);
  148. if ((isset($groupInfo['iid']) && GroupManager::is_tutor_of_group($userId, $groupInfo)) ||
  149. ($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  150. (
  151. api_is_allowed_to_edit(false, true) &&
  152. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  153. )
  154. ) {
  155. if (api_is_allowed_to_session_edit(false, true)) {
  156. if ($locked == false && postIsEditableByStudent($current_forum, $row)) {
  157. $iconEdit .= "<a href=\"editpost.php?".api_get_cidreq()."&forum=".$clean_forum_id
  158. . "&thread=".$clean_thread_id."&post=".$row['post_id']
  159. . "&edit=edition&id_attach=".$id_attach."\">"
  160. . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)."</a>";
  161. }
  162. }
  163. }
  164. if ($origin != 'learnpath') {
  165. if (GroupManager::is_tutor_of_group($userId, $groupInfo) ||
  166. api_is_allowed_to_edit(false, true) &&
  167. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  168. ) {
  169. if ($locked === false) {
  170. $deleteUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  171. 'forum' => $clean_forum_id,
  172. 'thread' => $clean_thread_id,
  173. 'action' => 'delete',
  174. 'content' => 'post',
  175. 'id' => $row['post_id']
  176. ]);
  177. $iconEdit .= Display::url(
  178. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  179. $deleteUrl,
  180. [
  181. 'onclick' => "javascript:if(!confirm('"
  182. . addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES))
  183. . "')) return false;",
  184. 'id' => "delete-post-{$row['post_id']}"
  185. ]
  186. );
  187. }
  188. }
  189. $statusIcon = getPostStatus($current_forum, $row);
  190. if (GroupManager::is_tutor_of_group($userId, $groupInfo) ||
  191. (api_is_allowed_to_edit(false, true) &&
  192. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  193. )
  194. ) {
  195. $iconEdit .= return_visible_invisible_icon(
  196. 'post',
  197. $row['post_id'],
  198. $row['visible'],
  199. array(
  200. 'forum' => $clean_forum_id,
  201. 'thread' => $clean_thread_id,
  202. 'origin' => $origin
  203. )
  204. );
  205. if ($increment > 0) {
  206. $iconEdit .= "<a href=\"viewthread.php?".api_get_cidreq()."&forum=".$clean_forum_id
  207. . "&thread=".$clean_thread_id."&action=move&post=".$row['post_id']."\">"
  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($row['user_id'], api_get_course_int_id());
  214. $current_qualify_thread = showQualify('1', $row['poster_id'], $_GET['thread']);
  215. if (($current_thread['thread_peer_qualify'] == 1 || api_is_allowed_to_edit(null, true)) &&
  216. $current_thread['thread_qualify_max'] > 0 && $origin != 'learnpath'
  217. ) {
  218. $my_forum_id = $clean_forum_id;
  219. if (isset($_GET['gradebook'])) {
  220. $info_thread = get_thread_information($clean_forum_id, $clean_thread_id);
  221. $my_forum_id = $info_thread['forum_id'];
  222. }
  223. $userCanEdit = $current_thread['thread_peer_qualify'] == 1 && $row['poster_id'] != $userId;
  224. /*if ($row['poster_id'] != $userId && $current_forum['moderated'] == 1 && $row['status']) {
  225. }*/
  226. if (api_is_allowed_to_edit(null, true)) {
  227. $userCanEdit = true;
  228. }
  229. if ($increment > 0 && $locked == false && $userCanEdit) {
  230. $iconEdit .= "<a href=\"forumqualify.php?".api_get_cidreq()."&forum=".$my_forum_id
  231. . "&thread=".$clean_thread_id."&action=list&post=".$row['post_id']
  232. . "&user=".$row['poster_id']."&user_id=".$row['poster_id']
  233. . "&idtextqualify=".$current_qualify_thread."\" >"
  234. . Display::return_icon('quiz.png', get_lang('Qualify'))
  235. . "</a> ";
  236. }
  237. }
  238. if (!empty($iconEdit)) {
  239. $html .= '<div class="tools-icons">'.$iconEdit.' '.$statusIcon.'</div>';
  240. } else {
  241. if (!empty(strip_tags($statusIcon))) {
  242. $html .= '<div class="tools-icons">'.$statusIcon.'</div>';
  243. }
  244. }
  245. $html .= $closedPost;
  246. $html .= '</div>';
  247. $html .= '<div class="col-md-10">';
  248. $titlePost = Display::tag(
  249. 'h3',
  250. $row['post_title'],
  251. array('class' => 'forum_post_title')
  252. );
  253. $html .= Display::tag(
  254. 'div',
  255. $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',
  261. $row['post_text'],
  262. array('class' => 'post-body')
  263. );
  264. $html .= '</div>';
  265. $html .= '</div>';
  266. $html .= '<div class="row">';
  267. $html .= '<div class="col-md-7">';
  268. // prepare the notification icon
  269. if (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]) &&
  270. !empty(
  271. $whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]
  272. ) &&
  273. !empty($whatsnew_post_info[$_GET['forum']][$row['thread_id']])
  274. ) {
  275. $post_image = Display::return_icon('forumpostnew.gif');
  276. } else {
  277. $post_image = Display::return_icon('forumpost.gif');
  278. }
  279. if ($row['post_notification'] == '1' && $row['poster_id'] == $_user['user_id']) {
  280. $post_image .= Display::return_icon('forumnotification.gif', get_lang('YouWillBeNotified'));
  281. }
  282. // The post title
  283. // The check if there is an attachment
  284. $attachment_list = getAllAttachment($row['post_id']);
  285. if (!empty($attachment_list) && is_array($attachment_list)) {
  286. foreach ($attachment_list as $attachment) {
  287. $realname = $attachment['path'];
  288. $user_filename = $attachment['filename'];
  289. $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
  290. $html .= '<a href="download.php?file='.$realname.'"> '.$user_filename.' </a>';
  291. if (($current_forum['allow_edit'] == 1 && $row['user_id'] == $_user['user_id']) ||
  292. (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && $current_forum['session_id'] != $sessionId))
  293. ) {
  294. $html .= '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&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. }