index.php 20 KB

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