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($_GET['thread'], $sortDirection, true);
  26. $sessionId = api_get_session_id();
  27. $currentThread = get_thread_information($_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></div>';
  83. $prev_next_array[] = $post['post_id'];
  84. } else {
  85. if ($post['visible'] == '0') {
  86. $class = ' class="invisible"';
  87. } else {
  88. $class = '';
  89. }
  90. $count_loop = ($count == 0) ? '&id=1' : '';
  91. $thread_structure .= "<a href=\"viewthread.php?" . api_get_cidreq() .
  92. "&forum=" . $forumId . "&thread=" . $threadId .
  93. "&post=" . $post['post_id'] . "&origin=$origin$count_loop\"" .
  94. "$class>" . prepare4display($post['post_title']) . "</a></div>";
  95. $prev_next_array[] = $post['post_id'];
  96. }
  97. $count++;
  98. }
  99. $locked = api_resource_is_locked_by_gradebook($threadId, LINK_FORUM_THREAD);
  100. /* NAVIGATION CONTROLS */
  101. $current_id = array_search($display_post_id, $prev_next_array);
  102. $max = count($prev_next_array);
  103. $next_id = $current_id + 1;
  104. $prev_id = $current_id - 1;
  105. // Text
  106. $first_message = get_lang('FirstMessage');
  107. $last_message = get_lang('LastMessage');
  108. $next_message = get_lang('NextMessage');
  109. $prev_message = get_lang('PrevMessage');
  110. // Images
  111. $first_img = Display::return_icon(
  112. 'action_first.png',
  113. get_lang('FirstMessage'),
  114. array('style' => 'vertical-align: middle;')
  115. );
  116. $last_img = Display::return_icon(
  117. 'action_last.png',
  118. get_lang('LastMessage'),
  119. array('style' => 'vertical-align: middle;')
  120. );
  121. $prev_img = Display::return_icon(
  122. 'action_prev.png',
  123. get_lang('PrevMessage'),
  124. array('style' => 'vertical-align: middle;')
  125. );
  126. $next_img = Display::return_icon(
  127. 'action_next.png',
  128. get_lang('NextMessage'),
  129. array('style' => 'vertical-align: middle;')
  130. );
  131. $class_prev = '';
  132. $class_next = '';
  133. // Links
  134. $first_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  135. '&forum=' . $forumId . '&thread=' . $threadId .
  136. '&gradebook=' . $gradebook . '&id=1&post=' . $prev_next_array[0];
  137. $last_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  138. '&forum=' . $forumId . '&thread=' . $threadId .
  139. '&gradebook=' . $gradebook . '&post=' . $prev_next_array[$max-1];
  140. $prev_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  141. '&forum=' . $forumId . '&thread=' . $threadId .
  142. '&gradebook=' . $gradebook . '&post=' . $prev_next_array[$prev_id];
  143. $next_href = $forumUrl . 'viewthread.php?' . api_get_cidreq() .
  144. '&forum=' . $forumId . '&thread=' . $threadId .
  145. '&post=' . $prev_next_array[$next_id];
  146. echo '<center style="margin-top: 10px; margin-bottom: 10px;">';
  147. // Go to: first and previous
  148. if (((int) $current_id) > 0) {
  149. echo '<a href="' . $first_href . '" ' . $class . ' title=' .
  150. $first_message . '>' . $first_img . ' ' . $first_message .'</a>';
  151. echo '<a href="' . $prev_href . '" ' . $class_prev . ' title=' .
  152. $prev_message . '>' . $prev_img . ' ' . $prev_message . '</a>';
  153. } else {
  154. echo '<b><span class="invisible">' .
  155. $first_img . ' ' . $first_message . '</b></span>';
  156. echo '<b><span class="invisible">' .
  157. $prev_img . ' ' . $prev_message . '</b></span>';
  158. }
  159. // Current counter
  160. echo ' [ ' . ($current_id + 1) . ' / ' . $max . ' ] ';
  161. // Go to: next and last
  162. if (($current_id + 1) < $max) {
  163. echo '<a href="' . $next_href . '" ' . $class_next . ' title=' . $next_message . '>' . $next_message . ' ' . $next_img . '</a>';
  164. echo '<a href="' . $last_href . '" ' . $class . ' title=' . $last_message . '>' . $last_message . ' ' . $last_img . '</a>';
  165. } else {
  166. echo '<b><span class="invisible">' . $next_message . ' ' . $next_img . '</b></span>';
  167. echo '<b><span class="invisible">' . $last_message . ' ' . $last_img . '</b></span>';
  168. }
  169. echo '</center>';
  170. // The style depends on the status of the message: approved or not
  171. if ($rows[$display_post_id]['visible'] == '0') {
  172. $titleclass = 'forum_message_post_title_2_be_approved';
  173. $messageclass = 'forum_message_post_text_2_be_approved';
  174. $leftclass = 'forum_message_left_2_be_approved';
  175. } else {
  176. $titleclass = 'forum_message_post_title';
  177. $messageclass = 'forum_message_post_text';
  178. $leftclass = 'forum_message_left';
  179. }
  180. // Displaying the message
  181. // We mark the image we are displaying as set
  182. unset($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]);
  183. echo "<table width=\"100%\" class=\"forum_table\" cellspacing=\"5\" border=\"0\">";
  184. echo "<tr>";
  185. echo "<td rowspan=\"3\" class=\"$leftclass\">";
  186. $username = sprintf(get_lang('LoginX'), $rows[$display_post_id]['username']);
  187. if ($rows[$display_post_id]['user_id'] == '0') {
  188. $name = prepare4display($rows[$display_post_id]['poster_name']);
  189. } else {
  190. $name = api_get_person_name(
  191. $rows[$display_post_id]['firstname'],
  192. $rows[$display_post_id]['lastname']
  193. );
  194. }
  195. if (api_get_course_setting('allow_user_image_forum')) {
  196. echo '<br />' . display_user_image($rows[$display_post_id]['user_id'], $name, $origin) . '<br />';
  197. }
  198. echo display_user_link(
  199. $rows[$display_post_id]['user_id'],
  200. $name,
  201. $origin,
  202. $username
  203. ) . "<br />";
  204. echo api_convert_and_format_date(
  205. $rows[$display_post_id]['post_date']
  206. ) . '<br /><br />';
  207. // Get attach id
  208. $attachment_list = get_attachment($display_post_id);
  209. $id_attach = !empty($attachment_list) ? $attachment_list['id'] : '';
  210. // The user who posted it can edit his thread only if the course admin allowed this in the properties of the forum
  211. // The course admin him/herself can do this off course always
  212. if (
  213. GroupManager::is_tutor_of_group(api_get_user_id(), $groupId) || (
  214. $current_forum['allow_edit'] == 1 &&
  215. $row['user_id'] == $_user['user_id']
  216. ) || (
  217. api_is_allowed_to_edit(false, true) && !(
  218. api_is_course_coach() &&
  219. $current_forum['session_id'] != $sessionId
  220. )
  221. )
  222. ) {
  223. if ($locked == false) {
  224. echo "<a href=\"editpost.php?" . api_get_cidreq() .
  225. "&forum=" . $forumId . "&thread=" . $threadId .
  226. "&post=" . $rows[$display_post_id]['post_id'] .
  227. "&id_attach=" . $id_attach . "\">" .
  228. Display::return_icon(
  229. 'edit.png',
  230. get_lang('Edit'),
  231. array(),
  232. ICON_SIZE_SMALL
  233. ) . '</a>';
  234. }
  235. }
  236. // Verified the post minor
  237. $my_post = getPosts($_GET['thread']);
  238. $id_posts = array();
  239. if (!empty($my_post) && is_array($my_post)) {
  240. foreach ($my_post as $post_value) {
  241. $id_posts[] = $post_value['post_id'];
  242. }
  243. sort($id_posts, SORT_NUMERIC);
  244. reset($id_posts);
  245. // The post minor
  246. $post_minor = (int)$id_posts[0];
  247. }
  248. if (
  249. GroupManager::is_tutor_of_group(api_get_user_id(), $groupId) ||
  250. api_is_allowed_to_edit(false, true) &&
  251. !(api_is_course_coach() &&$current_forum['session_id'] != $sessionId)
  252. ) {
  253. if ($locked == false) {
  254. echo "<a href=\"" . api_get_self() . "?" . api_get_cidreq() .
  255. "&forum=" . $forumId . "&thread=" . $threadId .
  256. "&action=delete&content=post&id=" .
  257. $rows[$display_post_id]['post_id'] .
  258. "\" onclick=\"javascript:if(!confirm('" .
  259. addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES)) .
  260. "')) return false;\">" . Display::return_icon(
  261. 'delete.png',
  262. get_lang('Delete'),
  263. array(),
  264. ICON_SIZE_SMALL
  265. )."</a>";
  266. }
  267. display_visible_invisible_icon(
  268. 'post',
  269. $rows[$display_post_id]['post_id'],
  270. $rows[$display_post_id]['visible'],
  271. array(
  272. 'forum' => $forumId,
  273. 'thread' => $threadId,
  274. 'post' => Security::remove_XSS($_GET['post'])
  275. )
  276. );
  277. if (!isset($_GET['id']) && $post_id > $post_minor) {
  278. echo "<a href=\"viewthread.php?" . api_get_cidreq() .
  279. "&forum=" . $forumId . "&thread=" . $threadId .
  280. "&origin=" . $origin . "&action=move&post=" .
  281. $rows[$display_post_id]['post_id'] . "\">" .
  282. Display::return_icon(
  283. 'move.png',
  284. get_lang('MovePost'),
  285. array(),
  286. ICON_SIZE_SMALL
  287. ) . "</a>";
  288. }
  289. }
  290. $userCanQualify = $currentThread['thread_peer_qualify'] == 1 && $rows[$display_post_id]['poster_id'] != $userId;
  291. if (api_is_allowed_to_edit(null, true)) {
  292. $userCanQualify = true;
  293. }
  294. if (empty($currentThread['thread_qualify_max'])) {
  295. $userCanQualify = false;
  296. }
  297. if ($userCanQualify) {
  298. if ($post_id > $post_minor) {
  299. $current_qualify_thread = showQualify(
  300. '1',
  301. $rows[$display_post_id]['user_id'],
  302. $_GET['thread']
  303. );
  304. if ($locked == false) {
  305. echo "<a href=\"forumqualify.php?" . api_get_cidreq() .
  306. "&forum=" . $forumId . "&thread=" . $threadId .
  307. "&action=list&post=" . $rows[$display_post_id]['post_id'] .
  308. "&user=" . $rows[$display_post_id]['user_id'] . "&user_id=" .
  309. $rows[$display_post_id]['user_id'] . "&origin=" . $origin .
  310. "&idtextqualify=" . $current_qualify_thread .
  311. "\" >" . Display::return_icon(
  312. 'quiz.gif',
  313. get_lang('Qualify')
  314. ) . "</a>";
  315. }
  316. }
  317. }
  318. if (($current_forum_category && $current_forum_category['locked'] == 0) &&
  319. $current_forum['locked'] == 0 &&
  320. $current_thread['locked'] == 0 || api_is_allowed_to_edit(false, true)
  321. ) {
  322. if ($_user['user_id'] ||
  323. ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])
  324. ) {
  325. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  326. echo '<a href="reply.php?' . api_get_cidreq() .
  327. '&forum=' . $forumId . '&thread=' . $threadId .
  328. '&post=' . $rows[$display_post_id]['post_id'] .
  329. '&action=replymessage&origin=' . $origin . '">' .
  330. Display::return_icon(
  331. 'message_reply_forum.png',
  332. get_lang('ReplyToMessage')
  333. ) . "</a>";
  334. echo '<a href="reply.php?' . api_get_cidreq() .
  335. '&forum=' . $forumId . '&thread=' . $threadId .
  336. '&post=' . $rows[$display_post_id]['post_id'] .
  337. '&action=quote&origin=' . $origin . '">' .
  338. Display::return_icon(
  339. 'quote.gif',
  340. get_lang('QuoteMessage')
  341. ) . "</a>";
  342. }
  343. }
  344. } else {
  345. if ($current_forum_category && $current_forum_category['locked'] == 1) {
  346. echo get_lang('ForumcategoryLocked') . '<br />';
  347. }
  348. if ($current_forum['locked'] == 1) {
  349. echo get_lang('ForumLocked') . '<br />';
  350. }
  351. if ($current_thread['locked'] == 1) {
  352. echo get_lang('ThreadLocked') . '<br />';
  353. }
  354. }
  355. echo "</td>";
  356. // Note: this can be removed here because it will be displayed in the tree
  357. if (
  358. isset($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]) AND
  359. !empty($whatsnew_post_info[$forumId][$threadId][$rows[$display_post_id]['post_id']]) AND
  360. !empty($whatsnew_post_info[$_GET['forum']][$rows[$display_post_id]['thread_id']])
  361. ) {
  362. $post_image = Display::return_icon('forumpostnew.gif');
  363. } else {
  364. $post_image = Display::return_icon('forumpost.gif');
  365. }
  366. if (
  367. $rows[$display_post_id]['post_notification'] == '1' AND
  368. $rows[$display_post_id]['poster_id'] == $_user['user_id']
  369. ) {
  370. $post_image .= Display::return_icon('forumnotification.gif',get_lang('YouWillBeNotified'));
  371. }
  372. // The post title
  373. echo "<td class=\"$titleclass\">" .
  374. prepare4display($rows[$display_post_id]['post_title']) . "</td>";
  375. echo "</tr>";
  376. // The post message
  377. echo "<tr>";
  378. echo "<td class=\"$messageclass\">" .
  379. prepare4display($rows[$display_post_id]['post_text']) . "</td>";
  380. echo "</tr>";
  381. // The check if there is an attachment
  382. $attachment_list = getAllAttachment($display_post_id);
  383. if (!empty($attachment_list) && is_array($attachment_list)) {
  384. foreach ($attachment_list as $attachment) {
  385. echo '<tr><td height="50%">';
  386. $realname = $attachment['path'];
  387. $user_filename = $attachment['filename'];
  388. echo Display::return_icon('attachment.gif', get_lang('Attachment'));
  389. echo '<a href="download.php?file=';
  390. echo $realname;
  391. echo ' "> ' . $user_filename . ' </a>';
  392. echo '<span class="forum_attach_comment">' .
  393. Security::remove_XSS($attachment['comment'], STUDENT) . '</span>';
  394. if (
  395. ($current_forum['allow_edit'] == 1 &&$rows[$display_post_id]['user_id'] == $_user['user_id']) ||
  396. (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && $current_forum['session_id'] != $sessionId))
  397. ) {
  398. echo '&nbsp;&nbsp;<a href="' . api_get_self() . '?' .
  399. api_get_cidreq() . '&origin=' .
  400. Security::remove_XSS($_GET['origin']) .
  401. '&action=delete_attach&id_attach=' .
  402. $attachment['id'] . '&forum=' . $forumId .
  403. '&thread=' . $threadId .
  404. '" onclick="javascript:if(!confirm(\'' .
  405. addslashes(api_htmlentities(
  406. get_lang('ConfirmYourChoice'), ENT_QUOTES)
  407. ) . '\')) return false;">' . Display::return_icon(
  408. 'delete.gif',
  409. get_lang('Delete')
  410. ).'</a><br />';
  411. }
  412. echo '</td></tr>';
  413. }
  414. }
  415. // The post has been displayed => it can be removed from the what's new array
  416. if (isset($whatsnew_post_info[$forumId][$threadId][$row['post_id']])) {
  417. unset($whatsnew_post_info[$forumId][$threadId][$row['post_id']]);
  418. unset($_SESSION['whatsnew_post_info'][$forumId][$threadId][$row['post_id']]);
  419. }
  420. echo "</table>";
  421. echo $thread_structure;