index.php 21 KB

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