index.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. $current_course_tool = TOOL_FORUM;
  31. $htmlHeadXtra[] = '<script type="text/javascript">
  32. $(document).ready(function(){ $(\'.hide-me\').slideUp() });
  33. function hidecontent(content){ $(content).slideToggle(\'normal\'); }
  34. </script>';
  35. $htmlHeadXtra[] = '<script type="text/javascript">
  36. function advanced_parameters() {
  37. if(document.getElementById(\'options\').style.display == \'none\') {
  38. document.getElementById(\'options\').style.display = \'block\';
  39. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif',get_lang('Hide'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  40. } else {
  41. document.getElementById(\'options\').style.display = \'none\';
  42. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  43. }
  44. }
  45. </script>';
  46. // The section (tabs).
  47. $this_section = SECTION_COURSES;
  48. // Notification for unauthorized people.
  49. api_protect_course_script(true);
  50. // Including additional library scripts.
  51. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  52. $nameTools = get_lang('Forums');
  53. // Including necessary files.
  54. require_once 'forumconfig.inc.php';
  55. require_once 'forumfunction.inc.php';
  56. /* MAIN DISPLAY SECTION */
  57. /* Header */
  58. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  59. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  60. $gradebook = $_SESSION['gradebook'];
  61. } elseif (empty($_GET['gradebook'])) {
  62. unset($_SESSION['gradebook']);
  63. $gradebook = '';
  64. }
  65. if (!empty($gradebook) && $gradebook == 'view') {
  66. $interbreadcrumb[] = array (
  67. 'url' => '../gradebook/' . $_SESSION['gradebook_dest'],
  68. 'name' => get_lang('ToolGradebook')
  69. );
  70. }
  71. $search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : '';
  72. /* ACTIONS */
  73. $actions = isset($_GET['action']) ? $_GET['action'] : '';
  74. if ($actions == 'add') {
  75. switch ($_GET['content']) {
  76. case 'forum':
  77. $interbreadcrumb[] = array('url' => 'index.php?gradebook='.$gradebook.'&amp;search='.$search_forum, 'name' => get_lang('ForumCategories'));
  78. $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('AddForum'));
  79. break;
  80. case 'forumcategory':
  81. $interbreadcrumb[] = array('url' =>'index.php?gradebook='.$gradebook.'&amp;search='.$search_forum, 'name' => get_lang('ForumCategories'));
  82. $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('AddForumCategory'));
  83. break;
  84. default:
  85. break;
  86. }
  87. } else {
  88. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('ForumCategories'));
  89. }
  90. Display::display_header('');
  91. // Tool introduction
  92. Display::display_introduction_section(TOOL_FORUM);
  93. $form_count = 0;
  94. if (api_is_allowed_to_edit(false, true)) {
  95. //if is called from a learning path lp_id
  96. $lp_id = isset($_REQUEST['lp_id']) ? Security::remove_XSS($_REQUEST['lp_id']): null;
  97. handle_forum_and_forumcategories($lp_id);
  98. }
  99. // Notification
  100. if ($actions == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
  101. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  102. api_not_allowed();
  103. }
  104. $return_message = set_notification($_GET['content'], $_GET['id']);
  105. Display :: display_confirmation_message($return_message, false);
  106. }
  107. get_whats_new();
  108. $whatsnew_post_info = array();
  109. $whatsnew_post_info = $_SESSION['whatsnew_post_info'];
  110. /* TRACKING */
  111. event_access_tool(TOOL_FORUM);
  112. /*
  113. RETRIEVING ALL THE FORUM CATEGORIES AND FORUMS
  114. note: we do this here just after het handling of the actions to be sure that we already incorporate the
  115. latest changes
  116. */
  117. // Step 1: We store all the forum categories in an array $forum_categories.
  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 = api_get_session_id();
  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) {
  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. foreach ($forum_list as $forum) {
  201. // Here we clean the whatnew_post_info array a little bit because to display the icon we
  202. // test if $whatsnew_post_info[$forum['forum_id']] is empty or not.
  203. if (!empty($whatsnew_post_info)) {
  204. if (is_array(isset($whatsnew_post_info[$forum['forum_id']]) ? $whatsnew_post_info[$forum['forum_id']] : null)) {
  205. foreach ($whatsnew_post_info[$forum['forum_id']] as $key_thread_id => $new_post_array) {
  206. if (empty($whatsnew_post_info[$forum['forum_id']][$key_thread_id])) {
  207. unset($whatsnew_post_info[$forum['forum_id']][$key_thread_id]);
  208. unset($_SESSION['whatsnew_post_info'][$forum['forum_id']][$key_thread_id]);
  209. }
  210. }
  211. }
  212. }
  213. // Note: This can be speeded up if we transform the $forum_list to an array that uses the forum_category as the key.
  214. if ($forum['forum_category'] == $forum_category['cat_id']) {
  215. // The forum has to be showed if
  216. // 1.v it is a not a group forum (teacher and student)
  217. // 2.v it is a group forum and it is public (teacher and student)
  218. // 3. it is a group forum and it is private (always for teachers only if the user is member of the forum
  219. // 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
  220. //if (!($forum['forum_group_public_private']=='private' AND !is_null($forum['forum_of_group']) AND !in_array($forum['forum_of_group'], $groups_of_user)))
  221. //{
  222. $show_forum = false;
  223. // SHOULD WE SHOW THIS PARTICULAR FORUM
  224. // you are teacher => show forum
  225. if (api_is_allowed_to_edit(false, true)) {
  226. //echo 'teacher';
  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'], 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. echo '<td width="20">';
  242. // Showing the image
  243. if (!empty($forum['forum_image'])) {
  244. $image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$forum['forum_image'];
  245. $image_size = api_getimagesize($image_path);
  246. $img_attributes = '';
  247. if (!empty($image_size)) {
  248. if ($image_size['width'] > 100 || $image_size['height'] > 100) {
  249. //limit display width and height to 100px
  250. $img_attributes = 'width="100" height="100"';
  251. }
  252. $forum_image = "<img src=\"$image_path\" $img_attributes>";
  253. } else {
  254. $forum_image = '';
  255. }
  256. echo $forum_image;
  257. } else {
  258. if ($forum['forum_of_group'] !== '0') {
  259. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  260. echo Display::return_icon('forumgroupnew.gif');
  261. } else {
  262. echo Display::return_icon('forumgroup.gif', get_lang('GroupForum'));
  263. }
  264. } else {
  265. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  266. echo Display::return_icon('forum.gif', get_lang('Forum'));
  267. } else {
  268. echo Display::return_icon('forum.gif');
  269. }
  270. }
  271. }
  272. echo '</td>';
  273. // Validacion when belongs to a session
  274. $session_img = api_get_session_image($forum['session_id'], $_user['status']);
  275. if ($forum['forum_of_group'] != '0') {
  276. $my_all_groups_forum_name = isset($all_groups[$forum['forum_of_group']]['name']) ? $all_groups[$forum['forum_of_group']]['name'] : null;
  277. $my_all_groups_forum_id = isset($all_groups[$forum['forum_of_group']]['id']) ? $all_groups[$forum['forum_of_group']]['id'] : null;
  278. $group_title = api_substr($my_all_groups_forum_name, 0, 30);
  279. $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;
  280. } else {
  281. $forum_title_group_addition = '';
  282. }
  283. if ((!isset($_SESSION['id_session']) || $_SESSION['id_session'] == 0) && !empty($forum['session_name'])) {
  284. $session_displayed = ' ('.$forum['session_name'].')';
  285. } else {
  286. $session_displayed = '';
  287. }
  288. $forum['forum_of_group'] == 0 ? $groupid = '' : $groupid = $forum['forum_of_group'];
  289. 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']))).'>';
  290. //Forum title
  291. echo prepare4display($forum['forum_title']).$session_displayed.'</a>'.$forum_title_group_addition.'<br />';
  292. echo '<span class="forum_description">'.prepare4display($forum['forum_comment']).'</span>';
  293. echo '</td>';
  294. //$number_forum_topics_and_posts = get_post_topics_of_forum($forum['forum_id']); // deprecated
  295. // The number of topics and posts.
  296. $number_threads = isset($forum['number_of_threads']) ? $forum['number_of_threads'] : null;
  297. $number_posts = isset($forum['number_of_posts']) ? $forum['number_of_posts'] : null;
  298. echo '<td>'.$number_threads.'</td>';
  299. echo '<td>'.$number_posts.'</td>';
  300. // The last post in the forum.
  301. if ($forum['last_poster_name'] != '') {
  302. $name = $forum['last_poster_name'];
  303. $poster_id = 0;
  304. $username = "";
  305. } else {
  306. $name = api_get_person_name($forum['last_poster_firstname'], $forum['last_poster_lastname']);
  307. $poster_id = $forum['last_poster_id'];
  308. $userinfo = api_get_user_info($poster_id);
  309. $username = sprintf(get_lang('LoginX'), $userinfo['username']);
  310. }
  311. echo '<td nowrap="nowrap">';
  312. if (!empty($forum['last_post_id'])) {
  313. echo api_convert_and_format_date($forum['last_post_date']).'<br /> '.get_lang('By').' '.display_user_link($poster_id, $name, '', $username);
  314. }
  315. echo '</td>';
  316. echo '<td class="td_actions">';
  317. if (api_is_allowed_to_edit(false, true) && !($forum['session_id'] == 0 && intval($session_id) != 0)) {
  318. 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>';
  319. 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>';
  320. display_visible_invisible_icon('forum', $forum['forum_id'], $forum['visibility']);
  321. display_lock_unlock_icon('forum', $forum['forum_id'], $forum['locked']);
  322. display_up_down_icon('forum', $forum['forum_id'], $forums_in_category);
  323. }
  324. $iconnotify = 'send_mail.gif';
  325. $session_forum_notification = isset($_SESSION['forum_notification']['forum']) ? $_SESSION['forum_notification']['forum'] : false;
  326. if (is_array($session_forum_notification)) {
  327. if (in_array($forum['forum_id'], $session_forum_notification)) {
  328. $iconnotify = 'send_mail_checked.gif';
  329. }
  330. }
  331. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true) ) {
  332. 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>';
  333. }
  334. echo '</td></tr>';
  335. }
  336. }
  337. }
  338. } else {
  339. echo '<tr><td>'.get_lang('NoForumInThisCategory').'</td>'.(api_is_allowed_to_edit(false, true) ? '<td colspan="6"></td>' : '<td colspan="6"></td>').'</tr>';
  340. }
  341. echo '</table>';
  342. }
  343. }
  344. Display :: display_footer();