viewthread_threaded.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. // text
  122. $first_message=get_lang('FirstMessage');
  123. $last_message=get_lang('LastMessage');
  124. $next_message=get_lang('NextMessage');
  125. $prev_message=get_lang('PrevMessage');
  126. // images
  127. $first_img = Display::return_icon('first.png',get_lang(''),array('style'=>'vertical-align: middle;'));
  128. $last_img = Display::return_icon('last.png',get_lang(''),array('style'=>'vertical-align: middle;'));
  129. $prev_img = Display::return_icon('prev.png',get_lang(''),array('style'=>'vertical-align: middle;'));
  130. $next_img = Display::return_icon('next.png',get_lang(''),array('style'=>'vertical-align: middle;'));
  131. // links
  132. $first_href = 'viewthread.php?'.api_get_cidreq().'&amp;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];
  133. $last_href = 'viewthread.php?'.api_get_cidreq()."&amp;forum=".Security::remove_XSS($_GET['forum'])."&amp;thread=".Security::remove_XSS($_GET['thread'])."&amp;gradebook='.$gradebook.'&amp;origin=".$origin."&amp;post=".$prev_next_array[$max-1];
  134. $prev_href = 'viewthread.php?'.api_get_cidreq().'&amp;forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;gradebook='.$gradebook.'&amp;origin='.$origin.'&amp;post='.$prev_next_array[$prev_id];
  135. $next_href = 'viewthread.php?'.api_get_cidreq().'&amp;forum='.Security::remove_XSS($_GET['forum']).'&amp;thread='.Security::remove_XSS($_GET['thread']).'&amp;gradebook='.$gradebook.'&amp;origin='.$origin.'&amp;post='.$prev_next_array[$next_id];
  136. echo '<center>';
  137. //go to: first and previous
  138. if ((int)$current_id > 0)
  139. {
  140. echo '<a href="'.$first_href.'" '.$class.' title='.$first_message.'>'.$first_img.' '.$first_message.'</a>';
  141. echo '<a href="'.$prev_href.'" '.$class_prev.' title='.$prev_message.'>'.$prev_img.' '.$prev_message.'</a>';
  142. }
  143. else
  144. {
  145. echo '<b><span class="invisible">'.$first_img.' '.$first_message.'</b></span>';
  146. echo '<b><span class="invisible">'.$prev_img.' '.$prev_message.'</b></span>';
  147. }
  148. // current counter
  149. echo ' [ '.($current_id+1).' / '.$max.' ] ';
  150. // go to: next and last
  151. if (($current_id+1) < $max)
  152. {
  153. echo '<a href="'.$next_href.'" '.$class_next.' title='.$next_message.'>'.$next_message.' '.$next_img.'</a>';
  154. echo '<a href="'.$last_href.'" '.$class.' title='.$last_message.'>'.$last_message.' '.$last_img.'</a>';
  155. }
  156. else
  157. {
  158. echo '<b><span class="invisible">'.$next_message.' '.$next_img.'</b></span>';
  159. echo '<b><span class="invisible">'.$last_message.' '.$last_img.'</b></span>';
  160. }
  161. echo '</center>';
  162. //--------------------------------------------------------------------------------------------
  163. // the style depends on the status of the message: approved or not
  164. if ($rows[$display_post_id]['visible']=='0')
  165. {
  166. $titleclass='forum_message_post_title_2_be_approved';
  167. $messageclass='forum_message_post_text_2_be_approved';
  168. $leftclass='forum_message_left_2_be_approved';
  169. }
  170. else
  171. {
  172. $titleclass='forum_message_post_title';
  173. $messageclass='forum_message_post_text';
  174. $leftclass='forum_message_left';
  175. }
  176. // --------------------------------------
  177. // Displaying the message
  178. // --------------------------------------
  179. // we mark the image we are displaying as set
  180. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$rows[$display_post_id]['post_id']]);
  181. echo "<table width=\"100%\" class=\"post\" cellspacing=\"5\" border=\"0\">\n";
  182. echo "\t<tr>\n";
  183. echo "\t\t<td rowspan=\"3\" class=\"$leftclass\">";
  184. if ($rows[$display_post_id]['user_id']=='0')
  185. {
  186. $name=prepare4display($rows[$display_post_id]['poster_name']);
  187. }
  188. else
  189. {
  190. $name=api_get_person_name($rows[$display_post_id]['firstname'], $rows[$display_post_id]['lastname']);
  191. }
  192. if (api_get_course_setting('allow_user_image_forum')) {echo '<br />'.display_user_image($rows[$display_post_id]['user_id'],$name, $origin).'<br />'; }
  193. echo display_user_link($rows[$display_post_id]['user_id'], $name, $origin).'<br />';
  194. echo $rows[$display_post_id]['post_date'].'<br /><br />';
  195. // get attach id
  196. $attachment_list=get_attachment($display_post_id);
  197. $id_attach = !empty($attachment_list)?$attachment_list['id']:'';
  198. // The user who posted it can edit his thread only if the course admin allowed this in the properties of the forum
  199. // The course admin him/herself can do this off course always
  200. 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'])))
  201. {
  202. 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";
  203. }
  204. if (api_is_allowed_to_edit(false,true) && !(api_is_course_coach() && $current_forum['session_id']!=$_SESSION['id_session']))
  205. {
  206. 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";
  207. 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']) ));
  208. echo "\n";
  209. //verified the post minor
  210. $my_post=get_posts($_GET['thread']);
  211. $id_posts=array();
  212. foreach ($my_post as $post_value) {
  213. $id_posts[]=$post_value['post_id'];
  214. }
  215. sort($id_posts,SORT_NUMERIC);
  216. reset($id_posts);
  217. //the post minor
  218. $post_minor=(int)$id_posts[0];
  219. $post_id = isset($_GET['post'])?(int)$_GET['post']:0;
  220. if (!isset($_GET['id']) && $post_id>$post_minor) {
  221. 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";
  222. }
  223. }
  224. $userinf=api_get_user_info($rows[$display_post_id]['user_id']);
  225. $user_status=api_get_status_of_user_in_course($rows[$display_post_id]['user_id'],api_get_course_id());
  226. if (api_is_allowed_to_edit()) {
  227. if($post_id>$post_minor )
  228. {
  229. if($user_status!=1)
  230. {
  231. $current_qualify_thread=show_qualify('1',$_GET['cidReq'],$_GET['forum'],$rows[$display_post_id]['user_id'],$_GET['thread']);
  232. 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";
  233. }
  234. }
  235. }
  236. //echo '<br /><br />';
  237. //if (($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0) OR api_is_allowed_to_edit())
  238. if ($current_forum_category['locked']==0 AND $current_forum['locked']==0 AND $current_thread['locked']==0 OR api_is_allowed_to_edit(false,true))
  239. {
  240. if ($_user['user_id'] OR ($current_forum['allow_anonymous']==1 AND !$_user['user_id']))
  241. {
  242. if (!api_is_anonymous()) {
  243. 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 .'">'.Display :: return_icon('message_reply_forum.png', get_lang('ReplyToMessage'))."</a>\n";
  244. 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 .'">'.Display :: return_icon('lp_thread_forum.gif', get_lang('QuoteMessage'))."</a>\n";
  245. }
  246. }
  247. }
  248. else
  249. {
  250. if ($current_forum_category['locked']==1)
  251. {
  252. echo get_lang('ForumcategoryLocked').'<br />';
  253. }
  254. if ($current_forum['locked']==1)
  255. {
  256. echo get_lang('ForumLocked').'<br />';
  257. }
  258. if ($current_thread['locked']==1)
  259. {
  260. echo get_lang('ThreadLocked').'<br />';
  261. }
  262. }
  263. echo "</td>\n";
  264. // note: this can be removed here because it will be displayed in the tree
  265. 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']]))
  266. {
  267. $post_image=icon('../img/forumpostnew.gif');
  268. }
  269. else
  270. {
  271. $post_image=icon('../img/forumpost.gif');
  272. }
  273. if ($rows[$display_post_id]['post_notification']=='1' AND $rows[$display_post_id]['poster_id']==$_user['user_id'])
  274. {
  275. $post_image.=icon('../img/forumnotification.gif',get_lang('YouWillBeNotified'));
  276. }
  277. // The post title
  278. echo "\t\t<td class=\"$titleclass\">".prepare4display($rows[$display_post_id]['post_title'])."</td>\n";
  279. echo "\t</tr>\n";
  280. // The post message
  281. echo "\t<tr>\n";
  282. echo "\t\t<td class=\"$messageclass\">".prepare4display($rows[$display_post_id]['post_text'])."</td>\n";
  283. echo "\t</tr>\n";
  284. // The check if there is an attachment
  285. $attachment_list=get_attachment($display_post_id);
  286. if (!empty($attachment_list))
  287. {
  288. echo '<tr><td height="50%">';
  289. $realname=$attachment_list['path'];
  290. $user_filename=$attachment_list['filename'];
  291. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  292. echo '<a href="download.php?file=';
  293. echo $realname;
  294. echo ' "> '.$user_filename.' </a>';
  295. echo '<span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
  296. 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']))) {
  297. 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(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif',get_lang('Delete')).'</a><br />';
  298. }
  299. echo '</td></tr>';
  300. }
  301. // The post has been displayed => it can be removed from the what's new array
  302. unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  303. unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$row['post_id']]);
  304. echo "</table>";
  305. // --------------------------------------
  306. // Displaying the thread (structure)
  307. // --------------------------------------
  308. echo $thread_structure;
  309. /**
  310. * This function builds an array of all the posts in a given thread where the key of the array is the post_id
  311. * 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
  312. * @return an array containing all the information on the posts of a thread
  313. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  314. */
  315. function calculate_children($rows)
  316. {
  317. foreach($rows as $row)
  318. {
  319. $rows_with_children[$row["post_id"]]=$row;
  320. $rows_with_children[$row["post_parent_id"]]["children"][]=$row["post_id"];
  321. }
  322. $rows=$rows_with_children;
  323. $sorted_rows=array(0=>array());
  324. _phorum_recursive_sort($rows, $sorted_rows);
  325. unset($sorted_rows[0]);
  326. return $sorted_rows;
  327. }
  328. function _phorum_recursive_sort($rows, &$threads, $seed=0, $indent=0)
  329. {
  330. if($seed>0)
  331. {
  332. $threads[$rows[$seed]["post_id"]]=$rows[$seed];
  333. $threads[$rows[$seed]["post_id"]]["indent_cnt"]=$indent;
  334. $indent++;
  335. }
  336. if(isset($rows[$seed]["children"]))
  337. {
  338. foreach($rows[$seed]["children"] as $child)
  339. {
  340. _phorum_recursive_sort($rows, $threads, $child, $indent);
  341. }
  342. }
  343. }