index.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. * @copyright Ghent University
  19. * @copyright Patrick Cool
  20. *
  21. * @package chamilo.forum
  22. */
  23. /**
  24. * Code
  25. */
  26. // Name of the language file that needs to be included.
  27. $language_file = 'forum';
  28. // Including the global initialization file.
  29. require_once '../inc/global.inc.php';
  30. $htmlHeadXtra[] = '<script type="text/javascript" language="javascript">
  31. $(document).ready(function(){ $(\'.hide-me\').slideUp() });
  32. function hidecontent(content){ $(content).slideToggle(\'normal\'); }
  33. </script>';
  34. $htmlHeadXtra[] = '<script type="text/javascript" language="javascript">
  35. function advanced_parameters() {
  36. if(document.getElementById(\'options\').style.display == \'none\') {
  37. document.getElementById(\'options\').style.display = \'block\';
  38. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif',get_lang('Hide'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  39. } else {
  40. document.getElementById(\'options\').style.display = \'none\';
  41. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  42. }
  43. }
  44. </script>';
  45. // The section (tabs).
  46. $this_section = SECTION_COURSES;
  47. // Notification for unauthorized people.
  48. api_protect_course_script(true);
  49. // Including additional library scripts.
  50. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  51. $nameTools = get_lang('Forums');
  52. // Including necessary files.
  53. require_once 'forumconfig.inc.php';
  54. require_once 'forumfunction.inc.php';
  55. /* MAIN DISPLAY SECTION */
  56. /* Header */
  57. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  58. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  59. $gradebook = $_SESSION['gradebook'];
  60. } elseif (empty($_GET['gradebook'])) {
  61. unset($_SESSION['gradebook']);
  62. $gradebook = '';
  63. }
  64. if (!empty($gradebook) && $gradebook == 'view') {
  65. $interbreadcrumb[] = array (
  66. 'url' => '../gradebook/' . $_SESSION['gradebook_dest'],
  67. 'name' => get_lang('ToolGradebook')
  68. );
  69. }
  70. $search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : '';
  71. if (isset($_GET['action']) && $_GET['action'] == 'add') {
  72. switch ($_GET['content']) {
  73. case 'forum':
  74. $interbreadcrumb[] = array('url' => 'index.php?gradebook='.$gradebook.'&amp;search='.$search_forum, 'name' => get_lang('ForumCategories'));
  75. $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('AddForum'));
  76. break;
  77. case 'forumcategory':
  78. $interbreadcrumb[] = array('url' =>'index.php?gradebook='.$gradebook.'&amp;search='.$search_forum, 'name' => get_lang('ForumCategories'));
  79. $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('AddForumCategory'));
  80. break;
  81. default:
  82. break;
  83. }
  84. } else {
  85. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('ForumCategories'));
  86. }
  87. Display::display_header('');
  88. // Tool introduction
  89. Display::display_introduction_section(TOOL_FORUM);
  90. $form_count = 0;
  91. /* ACTIONS */
  92. $get_actions = isset($_GET['action']) ? $_GET['action'] : '';
  93. if (api_is_allowed_to_edit(false, true)) {
  94. //if is called from a learning path lp_id
  95. $lp_id = isset($_REQUEST['lp_id']) ? Security::remove_XSS($_REQUEST['lp_id']): null;
  96. handle_forum_and_forumcategories($lp_id);
  97. }
  98. // Notification
  99. if (isset($_GET['action']) && $_GET['action'] == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
  100. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  101. api_not_allowed();
  102. }
  103. $return_message = set_notification($_GET['content'], $_GET['id']);
  104. Display :: display_confirmation_message($return_message, false);
  105. }
  106. get_whats_new();
  107. $whatsnew_post_info = array();
  108. $whatsnew_post_info = $_SESSION['whatsnew_post_info'];
  109. /* TRACKING */
  110. event_access_tool(TOOL_FORUM);
  111. /*
  112. RETRIEVING ALL THE FORUM CATEGORIES AND FORUMS
  113. note: we do this here just after het handling of the actions to be sure that we already incorporate the
  114. latest changes
  115. */
  116. // Step 1: We store all the forum categories in an array $forum_categories.
  117. $forum_categories = array();
  118. $forum_categories_list = get_forum_categories();
  119. // Step 2: We find all the forums (only the visible ones if it is a student).
  120. $forum_list = array();
  121. $forum_list = get_forums();
  122. $user_id = api_get_user_id();
  123. /* RETRIEVING ALL GROUPS AND THOSE OF THE USER */
  124. // The groups of the user.
  125. $groups_of_user = array();
  126. $groups_of_user = GroupManager::get_group_ids($_course['real_id'], $user_id);
  127. // All groups in the course (and sorting them as the id of the group = the key of the array).
  128. if (!api_is_anonymous()) {
  129. $all_groups = GroupManager::get_group_list();
  130. if (is_array($all_groups)) {
  131. foreach ($all_groups as $group) {
  132. $all_groups[$group['id']] = $group;
  133. }
  134. }
  135. }
  136. /* CLEAN GROUP ID FOR AJAXFILEMANAGER */
  137. if (isset($_SESSION['_gid'])) {
  138. unset($_SESSION['_gid']);
  139. }
  140. /* ACTION LINKS */
  141. $session_id = isset($_SESSION['id_session']) ? $_SESSION['id_session'] : false;
  142. echo '<div class="actions">';
  143. //if is called from learning path
  144. if (!empty($_GET['lp_id']) || !empty($_POST['lp_id'])){
  145. echo "<a href=\"../newscorm/lp_controller.php?".api_get_cidreq()."&gradebook=&action=add_item&type=step&lp_id=".$lp_id."#resource_tab-5\">".Display::return_icon('back.png', get_lang("BackTo").' '.get_lang("LearningPaths"),'',ICON_SIZE_MEDIUM)."</a>";
  146. }
  147. if (!empty($forum_list)) {
  148. echo search_link();
  149. }
  150. if (api_is_allowed_to_edit(false, true)) {
  151. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=add&amp;content=forumcategory&amp;lp_id='.$lp_id.'"> '.Display::return_icon('new_folder.png', get_lang('AddForumCategory'),'',ICON_SIZE_MEDIUM).'</a>';
  152. if (is_array($forum_categories_list) and !empty($forum_categories_list)) {
  153. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=add&amp;content=forum&amp;lp_id='.$lp_id.'"> '.Display::return_icon('new_forum.png', get_lang('AddForum'),'',ICON_SIZE_MEDIUM).'</a>';
  154. }
  155. }
  156. echo '</div>';
  157. /* Display Forum Categories and the Forums in it */
  158. // Step 3: We display the forum_categories first.
  159. if (is_array($forum_categories_list)) {
  160. foreach ($forum_categories_list as $forum_category_key => $forum_category) {
  161. // The forums in this category.
  162. $forums_in_category = get_forums_in_category($forum_category['cat_id']);
  163. echo '<table class="forum_table">';
  164. // Validacion when belongs to a session.
  165. $session_img = api_get_session_image($forum_category['session_id'], $_user['status']);
  166. if ((!isset($_SESSION['id_session']) || $_SESSION['id_session'] == 0) && !empty($forum_category['session_name'])) {
  167. $session_displayed = ' ('.Security::remove_XSS($forum_category['session_name']).')';
  168. } else {
  169. $session_displayed = '';
  170. }
  171. echo '<thead>';
  172. echo '<tr><th class="forum_head" colspan="5">';
  173. echo '<a href="viewforumcategory.php?'.api_get_cidreq().'&amp;forumcategory='.strval(intval($forum_category['cat_id'])).'" '.class_visible_invisible(strval(intval($forum_category['visibility']))).'>'.prepare4display($forum_category['cat_title']).$session_displayed.'</a>'. $session_img .'<br />';
  174. if ($forum_category['cat_comment'] != '' && trim($forum_category['cat_comment']) != '&nbsp;') {
  175. echo '<span class="forum_description">'.prepare4display($forum_category['cat_comment']).'</span>';
  176. }
  177. echo '</th>';
  178. echo '<th style="vertical-align: top;" align="center" >';
  179. if (api_is_allowed_to_edit(false, true) && !($forum_category['session_id'] == 0 && intval($session_id) != 0)) {
  180. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=edit&amp;content=forumcategory&amp;id='.intval($forum_category['cat_id']).'">'.Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  181. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=delete&amp;content=forumcategory&amp;id='.intval($forum_category['cat_id'])."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('DeleteForumCategory'), ENT_QUOTES))."')) return false;\">".Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  182. display_visible_invisible_icon('forumcategory', strval(intval($forum_category['cat_id'])), strval(intval($forum_category['visibility'])));
  183. display_lock_unlock_icon('forumcategory', strval(intval($forum_category['cat_id'])), strval(intval($forum_category['locked'])));
  184. display_up_down_icon('forumcategory', strval(intval($forum_category['cat_id'])), $forum_categories_list);
  185. }
  186. echo '</th>';
  187. echo '</tr>';
  188. echo '</thead>';
  189. if (!empty($forums_in_category)) {
  190. // Step 4: The interim headers (for the forum).
  191. echo '<tr class="forum_header">';
  192. echo '<td></td>';
  193. echo '<td>'.get_lang('Forum').'</td>';
  194. echo '<td>'.get_lang('Topics').'</td>';
  195. echo '<td>'.get_lang('Posts').'</td>';
  196. echo '<td>'.get_lang('LastPosts').'</td>';
  197. echo '<td>'.get_lang('Actions').'</td>';
  198. echo '</tr>';
  199. // Step 5: We display all the forums in this category.
  200. $forum_count = 0;
  201. foreach ($forum_list as $key => $forum) {
  202. // Here we clean the whatnew_post_info array a little bit because to display the icon we
  203. // test if $whatsnew_post_info[$forum['forum_id']] is empty or not.
  204. if (!empty($whatsnew_post_info)) {
  205. if (is_array(isset($whatsnew_post_info[$forum['forum_id']]) ? $whatsnew_post_info[$forum['forum_id']] : null)) {
  206. foreach ($whatsnew_post_info[$forum['forum_id']] as $key_thread_id => $new_post_array) {
  207. if (empty($whatsnew_post_info[$forum['forum_id']][$key_thread_id])) {
  208. unset($whatsnew_post_info[$forum['forum_id']][$key_thread_id]);
  209. unset($_SESSION['whatsnew_post_info'][$forum['forum_id']][$key_thread_id]);
  210. }
  211. }
  212. }
  213. }
  214. // Note: This can be speeded up if we transform the $forum_list to an array that uses the forum_category as the key.
  215. if ($forum['forum_category'] == $forum_category['cat_id']) {
  216. // The forum has to be showed if
  217. // 1.v it is a not a group forum (teacher and student)
  218. // 2.v it is a group forum and it is public (teacher and student)
  219. // 3. it is a group forum and it is private (always for teachers only if the user is member of the forum
  220. // if the forum is private and it is a group forum and the user is not a member of the group forum then it cannot be displayed
  221. //if (!($forum['forum_group_public_private']=='private' AND !is_null($forum['forum_of_group']) AND !in_array($forum['forum_of_group'], $groups_of_user)))
  222. //{
  223. $show_forum = false;
  224. // SHOULD WE SHOW THIS PARTICULAR FORUM
  225. // you are teacher => show forum
  226. if (api_is_allowed_to_edit(false, true)) {
  227. //echo 'teacher';
  228. $show_forum = true;
  229. } else {
  230. // you are not a teacher
  231. //echo 'student';
  232. // it is not a group forum => show forum (invisible forums are already left out see get_forums function)
  233. if ($forum['forum_of_group'] == '0') {
  234. //echo '-gewoon forum';
  235. $show_forum = true;
  236. } else {
  237. $show_forum = GroupManager::user_has_access($user_id, $forum['forum_of_group'], GROUP_TOOL_FORUM);
  238. //var_dump($forum['forum_id'].' - '.$show_forum);
  239. /*
  240. // it is a group forum
  241. //echo '-groepsforum';
  242. // it is a group forum but it is public => show
  243. if ($forum['forum_group_public_private'] == 'public') {
  244. $show_forum = true;
  245. //echo '-publiek';
  246. } elseif ($forum['forum_group_public_private'] == 'private') {
  247. // it is a group forum and it is private
  248. //echo '-prive';
  249. // it is a group forum and it is private but the user is member of the group
  250. if (in_array($forum['forum_of_group'], $groups_of_user)) {
  251. //echo '-is lid';
  252. $show_forum = true;
  253. } else {
  254. //echo '-is GEEN lid';
  255. $show_forum = false;
  256. }
  257. } else {
  258. $show_forum = false;
  259. }*/
  260. }
  261. }
  262. if ($show_forum) {
  263. $form_count++;
  264. $mywhatsnew_post_info = isset($whatsnew_post_info[$forum['forum_id']]) ? $whatsnew_post_info[$forum['forum_id']] : null;
  265. $forum_image = '';
  266. echo '<td width="20">';
  267. // Showing the image
  268. if (!empty($forum['forum_image'])) {
  269. $image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$forum['forum_image'];
  270. $image_size = api_getimagesize($image_path);
  271. $img_attributes = '';
  272. if (!empty($image_size)) {
  273. if ($image_size['width'] > 100 || $image_size['height'] > 100) {
  274. //limit display width and height to 100px
  275. $img_attributes = 'width="100" height="100"';
  276. }
  277. $forum_image = "<img src=\"$image_path\" $img_attributes>";
  278. } else {
  279. $forum_image = '';
  280. }
  281. echo $forum_image;
  282. } else {
  283. if ($forum['forum_of_group'] !== '0') {
  284. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  285. echo Display::return_icon('forumgroupnew.gif');
  286. } else {
  287. echo Display::return_icon('forumgroup.gif', get_lang('GroupForum'));
  288. }
  289. } else {
  290. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  291. echo Display::return_icon('forum.gif', get_lang('Forum'));
  292. } else {
  293. echo Display::return_icon('forum.gif');
  294. }
  295. }
  296. }
  297. echo '</td>';
  298. // Validacion when belongs to a session
  299. $session_img = api_get_session_image($forum['session_id'], $_user['status']);
  300. if ($forum['forum_of_group'] != '0') {
  301. $my_all_groups_forum_name = isset($all_groups[$forum['forum_of_group']]['name']) ? $all_groups[$forum['forum_of_group']]['name'] : null;
  302. $my_all_groups_forum_id = isset($all_groups[$forum['forum_of_group']]['id']) ? $all_groups[$forum['forum_of_group']]['id'] : null;
  303. $group_title = api_substr($my_all_groups_forum_name, 0, 30);
  304. $forum_title_group_addition = ' (<a href="../group/group_space.php?'.api_get_cidreq().'&amp;gidReq='.$forum['forum_of_group'].'" class="forum_group_link">'.get_lang('GoTo').' '.$group_title.'</a>)' . $session_img;
  305. } else {
  306. $forum_title_group_addition = '';
  307. }
  308. if ((!isset($_SESSION['id_session']) || $_SESSION['id_session'] == 0) && !empty($forum['session_name'])) {
  309. $session_displayed = ' ('.$forum['session_name'].')';
  310. } else {
  311. $session_displayed = '';
  312. }
  313. $forum['forum_of_group'] == 0 ? $groupid = '' : $groupid = $forum['forum_of_group'];
  314. echo '<td><a href="viewforum.php?'.api_get_cidreq().'&amp;gidReq='.intval($groupid).'&amp;forum='.intval($forum['forum_id']).'" '.class_visible_invisible(strval(intval($forum['visibility']))).'>';
  315. //Forum title
  316. echo prepare4display($forum['forum_title']).$session_displayed.'</a>'.$forum_title_group_addition.'<br />';
  317. echo '<span class="forum_description">'.prepare4display($forum['forum_comment']).'</span>';
  318. echo '</td>';
  319. //$number_forum_topics_and_posts = get_post_topics_of_forum($forum['forum_id']); // deprecated
  320. // The number of topics and posts.
  321. $number_threads = isset($forum['number_of_threads']) ? $forum['number_of_threads'] : null;
  322. $number_posts = isset($forum['number_of_posts']) ? $forum['number_of_posts'] : null;
  323. echo '<td>'.$number_threads.'</td>';
  324. echo '<td>'.$number_posts.'</td>';
  325. // The last post in the forum.
  326. if ($forum['last_poster_name'] != '') {
  327. $name = $forum['last_poster_name'];
  328. $poster_id = 0;
  329. $username = "";
  330. } else {
  331. $name = api_get_person_name($forum['last_poster_firstname'], $forum['last_poster_lastname']);
  332. $poster_id = $forum['last_poster_id'];
  333. $userinfo = api_get_user_info($poster_id);
  334. $username = sprintf(get_lang('LoginX'), $userinfo['username']);
  335. }
  336. echo '<td nowrap="nowrap">';
  337. if (!empty($forum['last_post_id'])) {
  338. echo api_convert_and_format_date($forum['last_post_date']).'<br /> '.get_lang('By').' '.display_user_link($poster_id, $name, '', $username);
  339. }
  340. echo '</td>';
  341. echo '<td nowrap="nowrap" align="center">';
  342. if (api_is_allowed_to_edit(false, true) && !($forum['session_id'] == 0 && intval($session_id) != 0)) {
  343. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=edit&amp;content=forum&amp;id='.$forum['forum_id'].'">'.Display::return_icon('edit.png',get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  344. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=delete&amp;content=forum&amp;id='.$forum['forum_id']."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('DeleteForum'), ENT_QUOTES))."')) return false;\">".Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  345. display_visible_invisible_icon('forum', $forum['forum_id'], $forum['visibility']);
  346. display_lock_unlock_icon('forum', $forum['forum_id'], $forum['locked']);
  347. display_up_down_icon('forum', $forum['forum_id'], $forums_in_category);
  348. }
  349. $iconnotify = 'send_mail.gif';
  350. $session_forum_notification = isset($_SESSION['forum_notification']['forum']) ? $_SESSION['forum_notification']['forum'] : false;
  351. if (is_array($session_forum_notification)) {
  352. if (in_array($forum['forum_id'], $session_forum_notification)) {
  353. $iconnotify = 'send_mail_checked.gif';
  354. }
  355. }
  356. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true) ) {
  357. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook='.$gradebook.'&amp;action=notify&amp;content=forum&amp;id='.$forum['forum_id'].'">'.Display::return_icon($iconnotify, get_lang('NotifyMe')).'</a>';
  358. }
  359. echo '</td></tr>';
  360. }
  361. }
  362. }
  363. } else {
  364. echo '<tr><td>'.get_lang('NoForumInThisCategory').'</td>'.(api_is_allowed_to_edit(false, true) ? '<td colspan="6"></td>' : '<td colspan="6"></td>').'</tr>';
  365. }
  366. echo '</table>';
  367. }
  368. }
  369. Display :: display_footer();