viewthread_threaded.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2006-2008 Dokeos S.A.
  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, 44 rue des palais, 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. // --------------------------------------
  62. // Displaying the thread (structure)
  63. // --------------------------------------
  64. $thread_structure="<div class=\"structure\">".get_lang('Structure')."</div>";
  65. $counter=0;
  66. $prev_next_array=array();
  67. foreach ($rows as $post)
  68. {
  69. $counter++;
  70. $indent=$post['indent_cnt']*'20';
  71. $thread_structure.= "<div style=\"margin-left: ".$indent."px;\">";
  72. 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']]))
  73. {
  74. $post_image=icon('../img/forumpostnew.gif');
  75. }
  76. else
  77. {
  78. $post_image=icon('../img/forumpost.gif');
  79. }
  80. $thread_structure.= $post_image;
  81. if ($_GET['post']==$post['post_id'] OR ($counter==1 AND !isset($_GET['post'])))
  82. {
  83. $thread_structure.='<strong>'.prepare4display($post['post_title']).'</strong></div>';
  84. $prev_next_array[]=$post['post_id'];
  85. }
  86. else
  87. {
  88. if ($post['visible']=='0')
  89. {
  90. $class=' class="invisible"';
  91. }
  92. else
  93. {
  94. $class='';
  95. }
  96. $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']."\" $class>".prepare4display($post['post_title'])."</a></div>\n";
  97. $prev_next_array[]=$post['post_id'];
  98. }
  99. }
  100. /*--------------------------------------------------
  101. NAVIGATION CONTROLS
  102. ----------------------------------------------------
  103. */
  104. $current_id=array_search($display_post_id,$prev_next_array);
  105. $max=count($prev_next_array);
  106. $next_id=$current_id+1;
  107. $prev_id=$current_id-1;
  108. $firs_message=get_lang('FirstMessage');
  109. $prev_message=get_lang('PrevMessage');
  110. $next_message=get_lang('NextMessage');
  111. $prev_message=get_lang('PrevMessage');
  112. $prev_img = '<img src="'.api_get_path(WEB_CODE_PATH).'img/prev.png" style="vertical-align: middle;"/>';
  113. $next_img = '<img src="'.api_get_path(WEB_CODE_PATH).'img/next.png" style="vertical-align: middle;"/>';
  114. $first_page_text = '<img src="'.api_get_path(WEB_CODE_PATH).'img/first.png" style="vertical-align: middle;"/>';
  115. $last_page_text = '<img src="'.api_get_path(WEB_CODE_PATH).'img/last.png" style="vertical-align: middle;"/>';
  116. $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].'"';
  117. $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].'"';
  118. // the last message
  119. if ($next_id>=$max)
  120. {
  121. $next_id=0;
  122. $class_next=' class="invisible"';
  123. $href_next=1;
  124. }
  125. // the first message
  126. if ($next_id==1) {
  127. $class_prev=' class="invisible"';
  128. $href_prev=1;
  129. }
  130. echo '<center>';
  131. // for the loop
  132. if ($prev_id<0)
  133. {
  134. $prev_id=$max-1;
  135. }
  136. //first message img
  137. 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;post=".$prev_next_array[0]."\" $class>".$first_page_text."</a>";
  138. // prev message link
  139. if ($href_prev==1)
  140. {
  141. echo '<b><span '.$class_prev.' >'.$prev_message.'</b></span>';
  142. }
  143. else
  144. echo '<a href='.$href_prev.' '.$class_prev.' >'.$prev_message.'</a>';
  145. // current counter
  146. echo ' [ '.($current_id+1).' / '.$max.' ] ';
  147. // next message link
  148. if ($href_next==1)
  149. {
  150. echo '<b><span '.$class_next.' >'.$next_message.'</span></b>';
  151. }
  152. else
  153. echo '<a href='.$href_next.' '.$class_next.' >'.$next_message.'</a>';
  154. //last message img
  155. 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;post=".$prev_next_array[$max-1]."\" $class>".$last_page_text."</a>";
  156. echo '</center>';
  157. //--------------------------------------------------------------------------------------------
  158. // the style depends on the status of the message: approved or not
  159. if ($rows[$display_post_id]['visible']=='0')
  160. {
  161. $titleclass='forum_message_post_title_2_be_approved';
  162. $messageclass='forum_message_post_text_2_be_approved';
  163. $leftclass='forum_message_left_2_be_approved';
  164. }
  165. else
  166. {
  167. $titleclass='forum_message_post_title';
  168. $messageclass='forum_message_post_text';
  169. $leftclass='forum_message_left';
  170. }
  171. // --------------------------------------
  172. // Displaying the message
  173. // --------------------------------------
  174. // we mark the image we are displaying as set
  175. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$rows[$display_post_id]['post_id']]);
  176. echo "<table width=\"100%\" class=\"post\" cellspacing=\"5\" border=\"0\">\n";
  177. echo "\t<tr>\n";
  178. echo "\t\t<td rowspan=\"3\" class=\"$leftclass\">";
  179. if ($rows[$display_post_id]['user_id']=='0')
  180. {
  181. $name=prepare4display($rows[$display_post_id]['poster_name']);
  182. }
  183. else
  184. {
  185. $name=$rows[$display_post_id]['firstname'].' '.$rows[$display_post_id]['lastname'];
  186. }
  187. if (api_get_course_setting('allow_user_image_forum')) {echo '<br />'.display_user_image($rows[$display_post_id]['user_id'],$name,false,true).'<br />'; }
  188. echo display_user_link($rows[$display_post_id]['user_id'], $name).'<br />';
  189. echo $rows[$display_post_id]['post_date'].'<br /><br />';
  190. // The user who posted it can edit his thread only if the course admin allowed this in the properties of the forum
  191. // The course admin him/herself can do this off course always
  192. if (($current_forum['allow_edit']==1 AND $rows[$display_post_id]['user_id']==$_user['user_id']) or api_is_allowed_to_edit())
  193. {
  194. echo "<a href=\"editpost.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']."\">".icon('../img/edit.gif',get_lang('Edit'))."</a>\n";
  195. }
  196. if (api_is_allowed_to_edit())
  197. {
  198. 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(htmlentities(get_lang("DeletePost"),ENT_QUOTES,$charset))."')) return false;\">".icon('../img/delete.gif',get_lang('Delete'))."</a>\n";
  199. 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']) ));
  200. echo "\n";
  201. echo "<a href=\"viewthread.php?".api_get_cidreq()."&forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;action=move&amp;post=".$rows[$display_post_id]['post_id']."\">".icon('../img/deplacer_fichier.gif',get_lang('MovePost'))."</a>\n";
  202. }
  203. echo '<br /><br />';
  204. //if (($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0) OR api_is_allowed_to_edit())
  205. if ($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0 OR api_is_allowed_to_edit())
  206. {
  207. if ($_user['user_id'] OR ($current_forum['allow_anonymous']==1 AND !$_user['user_id']))
  208. {
  209. 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">'.get_lang('ReplyToMessage').'</a><br />';
  210. 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">'.get_lang('QuoteMessage').'</a><br /><br />';
  211. }
  212. }
  213. else
  214. {
  215. if ($current_forum_category['locked']==1)
  216. {
  217. echo get_lang('ForumcategoryLocked').'<br />';
  218. }
  219. if ($current_forum['locked']==1)
  220. {
  221. echo get_lang('ForumLocked').'<br />';
  222. }
  223. if ($current_thread['locked']==1)
  224. {
  225. echo get_lang('ThreadLocked').'<br />';
  226. }
  227. }
  228. echo "</td>\n";
  229. // note: this can be removed here because it will be displayed in the tree
  230. 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']]))
  231. {
  232. $post_image=icon('../img/forumpostnew.gif');
  233. }
  234. else
  235. {
  236. $post_image=icon('../img/forumpost.gif');
  237. }
  238. if ($rows[$display_post_id]['post_notification']=='1' AND $rows[$display_post_id]['poster_id']==$_user['user_id'])
  239. {
  240. $post_image.=icon('../img/forumnotification.gif',get_lang('YouWillBeNotified'));
  241. }
  242. // The post title
  243. echo "\t\t<td class=\"$titleclass\">".prepare4display($rows[$display_post_id]['post_title'])."</td>\n";
  244. echo "\t</tr>\n";
  245. // The post message
  246. echo "\t<tr>\n";
  247. echo "\t\t<td class=\"$messageclass\">".prepare4display($rows[$display_post_id]['post_text'])."</td>\n";
  248. echo "\t</tr>\n";
  249. // The post has been displayed => it can be removed from the what's new array
  250. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  251. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  252. echo "</table>";
  253. // --------------------------------------
  254. // Displaying the thread (structure)
  255. // --------------------------------------
  256. echo $thread_structure;
  257. /**
  258. * This function builds an array of all the posts in a given thread where the key of the array is the post_id
  259. * 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
  260. * @return an array containing all the information on the posts of a thread
  261. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  262. */
  263. function calculate_children($rows)
  264. {
  265. foreach($rows as $row)
  266. {
  267. $rows_with_children[$row["post_id"]]=$row;
  268. $rows_with_children[$row["post_parent_id"]]["children"][]=$row["post_id"];
  269. }
  270. $rows=$rows_with_children;
  271. $sorted_rows=array(0=>array());
  272. _phorum_recursive_sort($rows, $sorted_rows);
  273. unset($sorted_rows[0]);
  274. return $sorted_rows;
  275. }
  276. function _phorum_recursive_sort($rows, &$threads, $seed=0, $indent=0)
  277. {
  278. if($seed>0)
  279. {
  280. $threads[$rows[$seed]["post_id"]]=$rows[$seed];
  281. $threads[$rows[$seed]["post_id"]]["indent_cnt"]=$indent;
  282. $indent++;
  283. }
  284. if(isset($rows[$seed]["children"]))
  285. {
  286. foreach($rows[$seed]["children"] as $child)
  287. {
  288. _phorum_recursive_sort($rows, $threads, $child, $indent);
  289. }
  290. }
  291. }
  292. ?>