viewthread_threaded.inc.php 16 KB

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