viewthread_threaded.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * These files are a complete rework of the forum. The database structure is
  5. * based on phpBB but all the code is rewritten. A lot of new functionalities
  6. * are added:
  7. * - forum categories and forums can be sorted up or down, locked or made invisible
  8. * - consistent and integrated forum administration
  9. * - forum options: are students allowed to edit their post?
  10. * moderation of posts (approval)
  11. * reply only forums (students cannot create new threads)
  12. * multiple forums per group
  13. * - sticky messages
  14. * - new view option: nested view
  15. * - quoting a message
  16. *
  17. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  18. * @author Julio Montoya <gugli100@gmail.com> UI Improvements + lots of bugfixes
  19. *
  20. * @package chamilo.forum
  21. */
  22. $forumUrl = api_get_path(WEB_CODE_PATH) . 'forum/';
  23. $_user = api_get_user_info();
  24. $sortDirection = isset($_GET['posts_order']) && $_GET['posts_order'] === 'desc' ? 'DESC' : 'ASC';
  25. $rows = getPosts($current_forum, $_GET['thread'], $sortDirection, true);
  26. $sessionId = api_get_session_id();
  27. $currentThread = get_thread_information($current_forum['forum_id'], $_GET['thread']);
  28. $post_id = isset($_GET['post']) ? (int) $_GET['post'] : 0;
  29. $userId = api_get_user_id();
  30. if (isset($_GET['post']) && $_GET['post']) {
  31. $display_post_id = intval($_GET['post']);
  32. } else {
  33. // We need to display the first post
  34. reset($rows);
  35. $current = current($rows);
  36. $display_post_id = $current['post_id'];
  37. }
  38. // Are we in a lp ?
  39. $origin = '';
  40. if (isset($_GET['origin'])) {
  41. $origin = Security::remove_XSS($_GET['origin']);
  42. }
  43. // Delete attachment file.
  44. if (
  45. isset($_GET['action']) &&
  46. $_GET['action']=='delete_attach' &&
  47. isset($_GET['id_attach'])
  48. ) {
  49. delete_attachment(0, $_GET['id_attach']);
  50. if (!isset($_GET['thread'])) {
  51. exit;
  52. }
  53. }
  54. // Displaying the thread (structure)
  55. $thread_structure = "<div class=\"structure\">" .get_lang('Structure')."</div>";
  56. $counter = 0;
  57. $count = 0;
  58. $prev_next_array = array();
  59. $forumId = intval($_GET['forum']);
  60. $threadId = intval($_GET['thread']);
  61. $groupId = api_get_group_id();
  62. foreach ($rows as $post) {
  63. $counter++;
  64. $indent = $post['indent_cnt'] * '20';
  65. $thread_structure .= "<div style=\"margin-left: " . $indent . "px;\">";
  66. if (
  67. !empty($whatsnew_post_info[$forumId][$post['thread_id']]) &&
  68. isset($whatsnew_post_info[$forumId][$threadId][$post['post_id']]) &&
  69. !empty($whatsnew_post_info[$forumId][$threadId][$post['post_id']])
  70. ) {
  71. $post_image = Display::return_icon('forumpostnew.gif');
  72. } else {
  73. $post_image = Display::return_icon('forumpost.gif');
  74. }
  75. $thread_structure .= $post_image;
  76. if (
  77. isset($_GET['post']) &&
  78. $_GET['post'] == $post['post_id'] || (
  79. $counter == 1 AND !isset($_GET['post'])
  80. )
  81. ) {
  82. $thread_structure .= '<strong>' .prepare4display($post['post_title']) . '</strong>';
  83. $prev_next_array[] = $post['post_id'];
  84. } else {
  85. $count_loop = ($count == 0) ? '&id=1' : '';
  86. $thread_structure .= Display::url(
  87. prepare4display($post['post_title']),
  88. 'viewthread.php?' . api_get_cidreq() . "$count_loop&" . http_build_query([
  89. 'forum' => $forumId,
  90. 'thread' => $threadId,
  91. 'post' => $post['post_id']
  92. ]),
  93. ['class' => empty($post['visible']) ? 'text-muted' : null]
  94. );
  95. $prev_next_array[] = $post['post_id'];
  96. }
  97. $thread_structure .= '</div>';
  98. $count++;
  99. }
  100. $locked = api_resource_is_locked_by_gradebook($threadId, LINK_FORUM_THREAD);
  101. /* NAVIGATION CONTROLS */
  102. $current_id = array_search($display_post_id, $prev_next_array);
  103. $max = count($prev_next_array);
  104. $next_id = $current_id + 1;
  105. $prev_id = $current_id - 1;
  106. // Text
  107. $first_message = get_lang('FirstMessage');
  108. $last_message = get_lang('LastMessage');
  109. $next_message = get_lang('NextMessage');
  110. $prev_message = get_lang('PrevMessage');
  111. // Images
  112. $first_img = Display::return_icon(
  113. 'action_first.png',
  114. get_lang('FirstMessage'),
  115. array('style' => 'vertical-align: middle;')
  116. );
  117. $last_img = Display::return_icon(
  118. 'action_last.png',
  119. get_lang('LastMessage'),
  120. array('style' => 'vertical-align: middle;')
  121. );
  122. $prev_img = Display::return_icon(
  123. 'action_prev.png',
  124. get_lang('PrevMessage'),
  125. array('style' => 'vertical-align: middle;')
  126. );
  127. $next_img = Display::return_icon(
  128. 'action_next.png',
  129. get_lang('NextMessage'),
  130. array('style' => 'vertical-align: middle;')
  131. );
  132. $class_prev = '';
  133. $class_next = '';
  134. // Links
  135. $first_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  136. '&forum=' . $forumId . '&thread=' . $threadId .
  137. '&gradebook=' . $gradebook . '&id=1&post=' . $prev_next_array[0];
  138. $last_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  139. '&forum=' . $forumId . '&thread=' . $threadId .
  140. '&gradebook=' . $gradebook . '&post=' . $prev_next_array[$max-1];
  141. $prev_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  142. '&forum=' . $forumId . '&thread=' . $threadId .
  143. '&gradebook=' . $gradebook . '&post=' . $prev_next_array[$prev_id];
  144. $next_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  145. '&forum=' . $forumId . '&thread=' . $threadId .
  146. '&post=' . $prev_next_array[$next_id];
  147. echo '<center style="margin-top: 10px; margin-bottom: 10px;">';
  148. // Go to: first and previous
  149. if (((int) $current_id) > 0) {
  150. echo '<a href="' . $first_href . '" ' . $class . ' title=' .
  151. $first_message . '>' . $first_img . ' ' . $first_message .'</a>';
  152. echo '<a href="' . $prev_href . '" ' . $class_prev . ' title=' .
  153. $prev_message . '>' . $prev_img . ' ' . $prev_message . '</a>';
  154. } else {
  155. echo '<strong class="text-muted">' .
  156. $first_img . ' ' . $first_message . '</strong>';
  157. echo '<strong class="text-muted">' .
  158. $prev_img . ' ' . $prev_message . '</strong>';
  159. }
  160. // Current counter
  161. echo ' [ ' . ($current_id + 1) . ' / ' . $max . ' ] ';
  162. // Go to: next and last
  163. if (($current_id + 1) < $max) {
  164. echo '<a href="' . $next_href . '" ' . $class_next . ' title=' . $next_message . '>' . $next_message . ' ' . $next_img . '</a>';
  165. echo '<a href="' . $last_href . '" ' . $class . ' title=' . $last_message . '>' . $last_message . ' ' . $last_img . '</a>';
  166. } else {
  167. echo '<strong class="text-muted">' . $next_message . ' ' . $next_img . '</strong>';
  168. echo '<strong class="text-muted">' . $last_message . ' ' . $last_img . '</strong>';
  169. }
  170. echo '</center>';
  171. // The style depends on the status of the message: approved or not
  172. if ($rows[$display_post_id]['visible'] == '0') {
  173. $titleclass = 'forum_message_post_title_2_be_approved';
  174. $messageclass = 'forum_message_post_text_2_be_approved';
  175. $leftclass = 'forum_message_left_2_be_approved';
  176. } else {
  177. $titleclass = 'forum_message_post_title';
  178. $messageclass = 'forum_message_post_text';
  179. $leftclass = 'forum_message_left';
  180. }
  181. // Displaying the message
  182. // We mark the image we are displaying as set
  183. unset($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]);
  184. echo "<table width=\"100%\" class=\"forum_table\" cellspacing=\"5\" border=\"0\">";
  185. echo "<tr>";
  186. echo "<td rowspan=\"3\" class=\"$leftclass\">";
  187. $username = sprintf(get_lang('LoginX'), $rows[$display_post_id]['username']);
  188. if ($rows[$display_post_id]['user_id'] == '0') {
  189. $name = prepare4display($rows[$display_post_id]['poster_name']);
  190. } else {
  191. $name = api_get_person_name(
  192. $rows[$display_post_id]['firstname'],
  193. $rows[$display_post_id]['lastname']
  194. );
  195. }
  196. if (api_get_course_setting('allow_user_image_forum')) {
  197. echo '<br />' . display_user_image($rows[$display_post_id]['user_id'], $name, $origin) . '<br />';
  198. }
  199. echo display_user_link(
  200. $rows[$display_post_id]['user_id'],
  201. $name,
  202. $origin,
  203. $username
  204. ) . "<br />";
  205. echo api_convert_and_format_date(
  206. $rows[$display_post_id]['post_date']
  207. ) . '<br /><br />';
  208. // Get attach id
  209. $attachment_list = get_attachment($display_post_id);
  210. $id_attach = !empty($attachment_list) ? $attachment_list['id'] : '';
  211. $groupInfo = GroupManager::get_group_properties($groupId);
  212. // The user who posted it can edit his thread only if the course admin allowed this in the properties of the forum
  213. // The course admin him/herself can do this off course always
  214. if (
  215. (isset($groupInfo['iid']) && GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo['iid'])) || (
  216. $current_forum['allow_edit'] == 1 &&
  217. $row['user_id'] == $_user['user_id']
  218. ) || (
  219. api_is_allowed_to_edit(false, true) && !(
  220. api_is_course_coach() &&
  221. $current_forum['session_id'] != $sessionId
  222. )
  223. )
  224. ) {
  225. if ($locked == false) {
  226. echo "<a href=\"editpost.php?" . api_get_cidreq() .
  227. "&forum=" . $forumId . "&thread=" . $threadId .
  228. "&post=" . $rows[$display_post_id]['post_id'] .
  229. "&id_attach=" . $id_attach . "\">" .
  230. Display::return_icon(
  231. 'edit.png',
  232. get_lang('Edit'),
  233. array(),
  234. ICON_SIZE_SMALL
  235. ) . '</a>';
  236. }
  237. }
  238. // Verified the post minor
  239. $my_post = getPosts($current_forum, $_GET['thread']);
  240. $id_posts = array();
  241. if (!empty($my_post) && is_array($my_post)) {
  242. foreach ($my_post as $post_value) {
  243. $id_posts[] = $post_value['post_id'];
  244. }
  245. sort($id_posts, SORT_NUMERIC);
  246. reset($id_posts);
  247. // The post minor
  248. $post_minor = (int)$id_posts[0];
  249. }
  250. if (
  251. (isset($groupInfo['iid']) && GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo['iid'])) ||
  252. api_is_allowed_to_edit(false, true) &&
  253. !(api_is_course_coach() && $current_forum['session_id'] != $sessionId)
  254. ) {
  255. if ($locked == false) {
  256. echo "<a href=\"" . api_get_self() . "?" . api_get_cidreq() .
  257. "&forum=" . $forumId . "&thread=" . $threadId .
  258. "&action=delete&content=post&id=" .
  259. $rows[$display_post_id]['post_id'] .
  260. "\" onclick=\"javascript:if(!confirm('" .
  261. addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES)) .
  262. "')) return false;\">" . Display::return_icon(
  263. 'delete.png',
  264. get_lang('Delete'),
  265. array(),
  266. ICON_SIZE_SMALL
  267. )."</a>";
  268. }
  269. echo return_visible_invisible_icon(
  270. 'post',
  271. $rows[$display_post_id]['post_id'],
  272. $rows[$display_post_id]['visible'],
  273. array(
  274. 'forum' => $forumId,
  275. 'thread' => $threadId,
  276. 'post' => Security::remove_XSS($_GET['post'])
  277. )
  278. );
  279. if (!isset($_GET['id']) && $post_id > $post_minor) {
  280. echo "<a href=\"viewthread.php?" . api_get_cidreq() .
  281. "&forum=" . $forumId . "&thread=" . $threadId .
  282. "&action=move&post=" .
  283. $rows[$display_post_id]['post_id'] . "\">" .
  284. Display::return_icon(
  285. 'move.png',
  286. get_lang('MovePost'),
  287. array(),
  288. ICON_SIZE_SMALL
  289. ) . "</a>";
  290. }
  291. }
  292. $userCanQualify = $currentThread['thread_peer_qualify'] == 1 && $rows[$display_post_id]['poster_id'] != $userId;
  293. if (api_is_allowed_to_edit(null, true)) {
  294. $userCanQualify = true;
  295. }
  296. if (empty($currentThread['thread_qualify_max'])) {
  297. $userCanQualify = false;
  298. }
  299. if ($userCanQualify) {
  300. if ($post_id > $post_minor) {
  301. $current_qualify_thread = showQualify(
  302. '1',
  303. $rows[$display_post_id]['user_id'],
  304. $_GET['thread']
  305. );
  306. if ($locked == false) {
  307. echo "<a href=\"forumqualify.php?" . api_get_cidreq() .
  308. "&forum=" . $forumId . "&thread=" . $threadId .
  309. "&action=list&post=" . $rows[$display_post_id]['post_id'] .
  310. "&user=" . $rows[$display_post_id]['user_id'] . "&user_id=" .
  311. $rows[$display_post_id]['user_id'] . "&origin=" . $origin .
  312. "&idtextqualify=" . $current_qualify_thread .
  313. "\" >" . Display::return_icon(
  314. 'quiz.gif',
  315. get_lang('Qualify')
  316. ) . "</a>";
  317. }
  318. }
  319. }
  320. if (($current_forum_category && $current_forum_category['locked'] == 0) &&
  321. $current_forum['locked'] == 0 &&
  322. $current_thread['locked'] == 0 || api_is_allowed_to_edit(false, true)
  323. ) {
  324. if ($_user['user_id'] ||
  325. ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])
  326. ) {
  327. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  328. echo '<a href="reply.php?' . api_get_cidreq() .
  329. '&forum=' . $forumId . '&thread=' . $threadId .
  330. '&post=' . $rows[$display_post_id]['post_id'] .
  331. '&action=replymessage">' .
  332. Display::return_icon(
  333. 'message_reply_forum.png',
  334. get_lang('ReplyToMessage')
  335. ) . "</a>";
  336. echo '<a href="reply.php?' . api_get_cidreq() .
  337. '&forum=' . $forumId . '&thread=' . $threadId .
  338. '&post=' . $rows[$display_post_id]['post_id'] .
  339. '&action=quote">' .
  340. Display::return_icon(
  341. 'quote.gif',
  342. get_lang('QuoteMessage')
  343. ) . "</a>";
  344. }
  345. }
  346. } else {
  347. if ($current_forum_category && $current_forum_category['locked'] == 1) {
  348. echo get_lang('ForumcategoryLocked') . '<br />';
  349. }
  350. if ($current_forum['locked'] == 1) {
  351. echo get_lang('ForumLocked') . '<br />';
  352. }
  353. if ($current_thread['locked'] == 1) {
  354. echo get_lang('ThreadLocked') . '<br />';
  355. }
  356. }
  357. echo "</td>";
  358. // Note: this can be removed here because it will be displayed in the tree
  359. if (
  360. isset($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]) AND
  361. !empty($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]) AND
  362. !empty($whatsnew_post_info[$_GET['forum']][$rows[$display_post_id]['thread_id']])
  363. ) {
  364. $post_image = Display::return_icon('forumpostnew.gif');
  365. } else {
  366. $post_image = Display::return_icon('forumpost.gif');
  367. }
  368. if (
  369. $rows[$display_post_id]['post_notification'] == '1' AND
  370. $rows[$display_post_id]['poster_id'] == $_user['user_id']
  371. ) {
  372. $post_image .= Display::return_icon('forumnotification.gif',get_lang('YouWillBeNotified'));
  373. }
  374. // The post title
  375. echo "<td class=\"$titleclass\">" .
  376. prepare4display($rows[$display_post_id]['post_title']) . "</td>";
  377. echo "</tr>";
  378. // The post message
  379. echo "<tr>";
  380. echo "<td class=\"$messageclass\">" .
  381. prepare4display($rows[$display_post_id]['post_text']) . "</td>";
  382. echo "</tr>";
  383. // The check if there is an attachment
  384. $attachment_list = getAllAttachment($display_post_id);
  385. if (!empty($attachment_list) && is_array($attachment_list)) {
  386. foreach ($attachment_list as $attachment) {
  387. echo '<tr><td height="50%">';
  388. $realname = $attachment['path'];
  389. $user_filename = $attachment['filename'];
  390. echo Display::return_icon('attachment.gif', get_lang('Attachment'));
  391. echo '<a href="download.php?file=';
  392. echo $realname;
  393. echo ' "> ' . $user_filename . ' </a>';
  394. echo '<span class="forum_attach_comment">' .
  395. Security::remove_XSS($attachment['comment'], STUDENT) . '</span>';
  396. if (
  397. ($current_forum['allow_edit'] == 1 &&$rows[$display_post_id]['user_id'] == $_user['user_id']) ||
  398. (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && $current_forum['session_id'] != $sessionId))
  399. ) {
  400. echo '&nbsp;&nbsp;<a href="' . api_get_self() . '?' .
  401. api_get_cidreq() . '&action=delete_attach&id_attach=' .$attachment['id'] . '&forum=' . $forumId .
  402. '&thread=' . $threadId .
  403. '" onclick="javascript:if(!confirm(\'' .
  404. addslashes(api_htmlentities(
  405. get_lang('ConfirmYourChoice'), ENT_QUOTES)
  406. ) . '\')) return false;">' . Display::return_icon(
  407. 'delete.gif',
  408. get_lang('Delete')
  409. ).'</a><br />';
  410. }
  411. echo '</td></tr>';
  412. }
  413. }
  414. // The post has been displayed => it can be removed from the what's new array
  415. if (isset($whatsnew_post_info[$forumId][$threadId][$row['post_id']])) {
  416. unset($whatsnew_post_info[$forumId][$threadId][$row['post_id']]);
  417. unset($_SESSION['whatsnew_post_info'][$forumId][$threadId][$row['post_id']]);
  418. }
  419. echo "</table>";
  420. echo $thread_structure;