viewthread_threaded.inc.php 16 KB

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