viewthread_threaded.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2006-2008 Dokeos SPRL
  6. Copyright (c) 2006 Ghent University (UGent)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 108 rue du Corbeau, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. * These files are a complete rework of the forum. The database structure is
  20. * based on phpBB but all the code is rewritten. A lot of new functionalities
  21. * are added:
  22. * - forum categories and forums can be sorted up or down, locked or made invisible
  23. * - consistent and integrated forum administration
  24. * - forum options: are students allowed to edit their post?
  25. * moderation of posts (approval)
  26. * reply only forums (students cannot create new threads)
  27. * multiple forums per group
  28. * - sticky messages
  29. * - new view option: nested view
  30. * - quoting a message
  31. *
  32. * @Author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  33. * @Copyright Ghent University
  34. * @Copyright Patrick Cool
  35. *
  36. * @package dokeos.forum
  37. */
  38. /**
  39. **************************************************************************
  40. * IMPORTANT NOTICE
  41. * Please do not change anything is this code yet because there are still
  42. * some significant code that need to happen and I do not have the time to
  43. * merge files and test it all over again. So for the moment, please do not
  44. * touch the code
  45. * -- Patrick Cool <patrick.cool@UGent.be>
  46. **************************************************************************
  47. */
  48. $rows=get_posts($_GET['thread']); // note: this has to be cleaned first
  49. $rows=calculate_children($rows);
  50. if ($_GET['post'])
  51. {
  52. $display_post_id=Security::remove_XSS($_GET['post']); // note: this has to be cleaned first
  53. }
  54. else
  55. {
  56. // we need to display the first post
  57. reset($rows);
  58. $current=current($rows);
  59. $display_post_id=$current['post_id'];
  60. }
  61. //are we in a lp ?
  62. $origin = '';
  63. if(isset($_GET['origin']))
  64. {
  65. $origin = Security::remove_XSS($_GET['origin']);
  66. }
  67. //delete attachment file
  68. if ((isset($_GET['action']) && $_GET['action']=='delete_attach') && isset($_GET['id_attach'])) {
  69. delete_attachment(0,$_GET['id_attach']);
  70. }
  71. // --------------------------------------
  72. // Displaying the thread (structure)
  73. // --------------------------------------
  74. $thread_structure="<div class=\"structure\">".get_lang('Structure')."</div>";
  75. $counter=0;
  76. $count=0;
  77. $prev_next_array=array();
  78. foreach ($rows as $post)
  79. {
  80. $counter++;
  81. $indent=$post['indent_cnt']*'20';
  82. $thread_structure.= "<div style=\"margin-left: ".$indent."px;\">";
  83. if (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]) and !empty($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]) and !empty($whatsnew_post_info[$_GET['forum']][$post['thread_id']]))
  84. {
  85. $post_image=icon('../img/forumpostnew.gif');
  86. }
  87. else
  88. {
  89. $post_image=icon('../img/forumpost.gif');
  90. }
  91. $thread_structure.= $post_image;
  92. if ($_GET['post']==$post['post_id'] OR ($counter==1 AND !isset($_GET['post'])))
  93. {
  94. $thread_structure.='<strong>'.prepare4display($post['post_title']).'</strong></div>';
  95. $prev_next_array[]=$post['post_id'];
  96. }
  97. else
  98. {
  99. if ($post['visible']=='0')
  100. {
  101. $class=' class="invisible"';
  102. }
  103. else
  104. {
  105. $class='';
  106. }
  107. $count_loop=($count==0)?'&id=1' : '';
  108. $thread_structure.= "<a href=\"viewthread.php?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;post=".$post['post_id']."&amp;origin=$origin$count_loop\" $class>".prepare4display($post['post_title'])."</a></div>\n";
  109. $prev_next_array[]=$post['post_id'];
  110. }
  111. $count++;
  112. }
  113. /*--------------------------------------------------
  114. NAVIGATION CONTROLS
  115. ----------------------------------------------------
  116. */
  117. $current_id=array_search($display_post_id,$prev_next_array);
  118. $max=count($prev_next_array);
  119. $next_id=$current_id+1;
  120. $prev_id=$current_id-1;
  121. $firs_message=get_lang('FirstMessage');
  122. $prev_message=get_lang('PrevMessage');
  123. $next_message=get_lang('NextMessage');
  124. $prev_message=get_lang('PrevMessage');
  125. $prev_img = '<img src="'.api_get_path(WEB_CODE_PATH).'img/prev.png" style="vertical-align: middle;"/>';
  126. $next_img = '<img src="'.api_get_path(WEB_CODE_PATH).'img/next.png" style="vertical-align: middle;"/>';
  127. $first_page_text = '<img src="'.api_get_path(WEB_CODE_PATH).'img/first.png" style="vertical-align: middle;"/>';
  128. $last_page_text = '<img src="'.api_get_path(WEB_CODE_PATH).'img/last.png" style="vertical-align: middle;"/>';
  129. $href_prev='"viewthread.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;post='.$prev_next_array[$prev_id].'&amp;origin='. $origin .'&amp;gradebook='. $gradebook .'"';
  130. $href_next='"viewthread.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;post='.$prev_next_array[$next_id].'&amp;origin='. $origin .'&amp;gradebook='. $gradebook .'"';
  131. // the last message
  132. if ($next_id>=$max)
  133. {
  134. $next_id=0;
  135. $class_next=' class="invisible"';
  136. $href_next=1;
  137. }
  138. // the first message
  139. if ($next_id==1) {
  140. $class_prev=' class="invisible"';
  141. $href_prev=1;
  142. }
  143. echo '<center>';
  144. // for the loop
  145. if ($prev_id<0)
  146. {
  147. $prev_id=$max-1;
  148. }
  149. //first message img
  150. echo '<a title="'.get_lang('FirstMessage').'" href="viewthread.php?'.api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;gradebook=".$gradebook."&amp;origin=".$origin."&amp;id=1&amp;post=".$prev_next_array[0]."\" $class>".$first_page_text."</a>";
  151. // prev message link
  152. if ($href_prev==1)
  153. {
  154. echo '<b><span '.$class_prev.' >'.$prev_message.'</b></span>';
  155. }
  156. else
  157. echo '<a href='.$href_prev.' '.$class_prev.' >'.$prev_message.'</a>';
  158. // current counter
  159. echo ' [ '.($current_id+1).' / '.$max.' ] ';
  160. // next message link
  161. if ($href_next==1)
  162. {
  163. echo '<b><span '.$class_next.' >'.$next_message.'</span></b>';
  164. }
  165. else
  166. echo '<a href='.$href_next.' '.$class_next.' >'.$next_message.'</a>';
  167. //last message img
  168. echo '<a title="'.get_lang('LastMessage').'" href="viewthread.php?'.api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;origin=".$origin."&amp;post=".$prev_next_array[$max-1]."\" $class>".$last_page_text."</a>";
  169. echo '</center>';
  170. //--------------------------------------------------------------------------------------------
  171. // the style depends on the status of the message: approved or not
  172. if ($rows[$display_post_id]['visible']=='0')
  173. {
  174. $titleclass='forum_message_post_title_2_be_approved';
  175. $messageclass='forum_message_post_text_2_be_approved';
  176. $leftclass='forum_message_left_2_be_approved';
  177. }
  178. else
  179. {
  180. $titleclass='forum_message_post_title';
  181. $messageclass='forum_message_post_text';
  182. $leftclass='forum_message_left';
  183. }
  184. // --------------------------------------
  185. // Displaying the message
  186. // --------------------------------------
  187. // we mark the image we are displaying as set
  188. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$rows[$display_post_id]['post_id']]);
  189. echo "<table width=\"100%\" class=\"post\" cellspacing=\"5\" border=\"0\">\n";
  190. echo "\t<tr>\n";
  191. echo "\t\t<td rowspan=\"3\" class=\"$leftclass\">";
  192. if ($rows[$display_post_id]['user_id']=='0')
  193. {
  194. $name=prepare4display($rows[$display_post_id]['poster_name']);
  195. }
  196. else
  197. {
  198. $name=$rows[$display_post_id]['firstname'].' '.$rows[$display_post_id]['lastname'];
  199. }
  200. if (api_get_course_setting('allow_user_image_forum')) {echo '<br />'.display_user_image($rows[$display_post_id]['user_id'],$name, $origin).'<br />'; }
  201. echo display_user_link($rows[$display_post_id]['user_id'], $name, $origin).'<br />';
  202. echo $rows[$display_post_id]['post_date'].'<br /><br />';
  203. // get attach id
  204. $attachment_list=get_attachment($display_post_id);
  205. $id_attach = !empty($attachment_list)?$attachment_list['id']:'';
  206. // The user who posted it can edit his thread only if the course admin allowed this in the properties of the forum
  207. // The course admin him/herself can do this off course always
  208. if (($current_forum['allow_edit']==1 AND $rows[$display_post_id]['user_id']==$_user['user_id']) or (api_is_allowed_to_edit(false,true) && !(api_is_course_coach() && $current_forum['session_id']!=$_SESSION['id_session'])))
  209. {
  210. echo "<a href=\"editpost.php?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;origin=".$origin."&amp;post=".$rows[$display_post_id]['post_id']."&id_attach=".$id_attach."\">".icon('../img/edit.gif',get_lang('Edit'))."</a>\n";
  211. }
  212. if (api_is_allowed_to_edit(false,true) && !(api_is_course_coach() && $current_forum['session_id']!=$_SESSION['id_session']))
  213. {
  214. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;action=delete&amp;content=post&amp;id=".$rows[$display_post_id]['post_id']."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang("DeletePost"),ENT_QUOTES,$charset))."')) return false;\">".icon('../img/delete.gif',get_lang('Delete'))."</a>\n";
  215. display_visible_invisible_icon('post', $rows[$display_post_id]['post_id'], $rows[$display_post_id]['visible'],array('forum'=>Security::remove_XSS($_GET['forum']),'thread'=>Security::remove_XSS($_GET['thread']), 'post'=>Security::remove_XSS($_GET['post']) ));
  216. echo "\n";
  217. //verified the post minor
  218. $my_post=get_posts($_GET['thread']);
  219. $id_posts=array();
  220. foreach ($my_post as $post_value) {
  221. $id_posts[]=$post_value['post_id'];
  222. }
  223. sort($id_posts,SORT_NUMERIC);
  224. reset($id_posts);
  225. //the post minor
  226. $post_minor=(int)$id_posts[0];
  227. $post_id = isset($_GET['post'])?(int)$_GET['post']:0;
  228. if (!isset($_GET['id']) && $post_id>$post_minor) {
  229. echo "<a href=\"viewthread.php?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;origin=".$origin."&amp;action=move&amp;post=".$rows[$display_post_id]['post_id']."\">".icon('../img/deplacer_fichier.gif',get_lang('MovePost'))."</a>\n";
  230. }
  231. }
  232. $userinf=api_get_user_info($rows[$display_post_id]['user_id']);
  233. $user_status=api_get_status_of_user_in_course($rows[$display_post_id]['user_id'],api_get_course_id());
  234. if (api_is_allowed_to_edit()) {
  235. if($post_id>$post_minor )
  236. {
  237. if($user_status!=1)
  238. {
  239. $current_qualify_thread=show_qualify('1',$_GET['cidReq'],$_GET['forum'],$rows[$display_post_id]['user_id'],$_GET['thread']);
  240. echo "<a href=\"forumqualify.php?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;action=list&amp;post=".$rows[$display_post_id]['post_id']."&amp;user=".$rows[$display_post_id]['user_id']."&user_id=".$rows[$display_post_id]['user_id']."&origin=".$origin."&idtextqualify=".$current_qualify_thread."\" >".icon('../img/new_test_small.gif',get_lang('Qualify'))."</a>\n";
  241. }
  242. }
  243. }
  244. echo '<br /><br />';
  245. //if (($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0) OR api_is_allowed_to_edit())
  246. if ($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0 OR api_is_allowed_to_edit(false,true))
  247. {
  248. if ($_user['user_id'] OR ($current_forum['allow_anonymous']==1 AND !$_user['user_id']))
  249. {
  250. echo '<a href="reply.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;post='.$rows[$display_post_id]['post_id'].'&amp;action=replymessage&amp;origin='. $origin .'">'.get_lang('ReplyToMessage').'</a><br />';
  251. echo '<a href="reply.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;post='.$rows[$display_post_id]['post_id'].'&amp;action=quote&amp;origin='. $origin .'">'.get_lang('QuoteMessage').'</a><br /><br />';
  252. }
  253. }
  254. else
  255. {
  256. if ($current_forum_category['locked']==1)
  257. {
  258. echo get_lang('ForumcategoryLocked').'<br />';
  259. }
  260. if ($current_forum['locked']==1)
  261. {
  262. echo get_lang('ForumLocked').'<br />';
  263. }
  264. if ($current_thread['locked']==1)
  265. {
  266. echo get_lang('ThreadLocked').'<br />';
  267. }
  268. }
  269. echo "</td>\n";
  270. // note: this can be removed here because it will be displayed in the tree
  271. if (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$rows[$display_post_id]['post_id']]) and !empty($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$rows[$display_post_id]['post_id']]) and !empty($whatsnew_post_info[$_GET['forum']][$rows[$display_post_id]['thread_id']]))
  272. {
  273. $post_image=icon('../img/forumpostnew.gif');
  274. }
  275. else
  276. {
  277. $post_image=icon('../img/forumpost.gif');
  278. }
  279. if ($rows[$display_post_id]['post_notification']=='1' AND $rows[$display_post_id]['poster_id']==$_user['user_id'])
  280. {
  281. $post_image.=icon('../img/forumnotification.gif',get_lang('YouWillBeNotified'));
  282. }
  283. // The post title
  284. echo "\t\t<td class=\"$titleclass\">".prepare4display($rows[$display_post_id]['post_title'])."</td>\n";
  285. echo "\t</tr>\n";
  286. // The post message
  287. echo "\t<tr>\n";
  288. echo "\t\t<td class=\"$messageclass\">".prepare4display($rows[$display_post_id]['post_text'])."</td>\n";
  289. echo "\t</tr>\n";
  290. // The check if there is an attachment
  291. $attachment_list=get_attachment($display_post_id);
  292. if (!empty($attachment_list))
  293. {
  294. echo '<tr><td height="50%">';
  295. $realname=$attachment_list['path'];
  296. $user_filename=$attachment_list['filename'];
  297. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  298. echo '<a href="download.php?file=';
  299. echo $realname;
  300. echo ' "> '.$user_filename.' </a>';
  301. echo '<span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
  302. if (($current_forum['allow_edit']==1 AND $rows[$display_post_id]['user_id']==$_user['user_id']) or (api_is_allowed_to_edit(false,true) && !(api_is_course_coach() && $current_forum['session_id']!=$_SESSION['id_session']))) {
  303. echo '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;origin='.Security::remove_XSS($_GET['origin']).'&amp;action=delete_attach&amp;id_attach='.$attachment_list['id'].'&amp;forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif',get_lang('Delete')).'</a><br />';
  304. }
  305. echo '</td></tr>';
  306. }
  307. // The post has been displayed => it can be removed from the what's new array
  308. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  309. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  310. echo "</table>";
  311. // --------------------------------------
  312. // Displaying the thread (structure)
  313. // --------------------------------------
  314. echo $thread_structure;
  315. /**
  316. * This function builds an array of all the posts in a given thread where the key of the array is the post_id
  317. * It also adds an element children to the array which itself is an array that contains all the id's of the first-level children
  318. * @return an array containing all the information on the posts of a thread
  319. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  320. */
  321. function calculate_children($rows)
  322. {
  323. foreach($rows as $row)
  324. {
  325. $rows_with_children[$row["post_id"]]=$row;
  326. $rows_with_children[$row["post_parent_id"]]["children"][]=$row["post_id"];
  327. }
  328. $rows=$rows_with_children;
  329. $sorted_rows=array(0=>array());
  330. _phorum_recursive_sort($rows, $sorted_rows);
  331. unset($sorted_rows[0]);
  332. return $sorted_rows;
  333. }
  334. function _phorum_recursive_sort($rows, &$threads, $seed=0, $indent=0)
  335. {
  336. if($seed>0)
  337. {
  338. $threads[$rows[$seed]["post_id"]]=$rows[$seed];
  339. $threads[$rows[$seed]["post_id"]]["indent_cnt"]=$indent;
  340. $indent++;
  341. }
  342. if(isset($rows[$seed]["children"]))
  343. {
  344. foreach($rows[$seed]["children"] as $child)
  345. {
  346. _phorum_recursive_sort($rows, $threads, $child, $indent);
  347. }
  348. }
  349. }