index.php 22 KB

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