index.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * These files are a complete rework of the forum. The database structure is
  6. * based on phpBB but all the code is rewritten. A lot of new functionalities
  7. * are added:
  8. * - forum categories and forums can be sorted up or down, locked or made invisible
  9. * - consistent and integrated forum administration
  10. * - forum options: are students allowed to edit their post?
  11. * moderation of posts (approval)
  12. * reply only forums (students cannot create new threads)
  13. * multiple forums per group
  14. * - sticky messages
  15. * - new view option: nested view
  16. * - quoting a message
  17. *
  18. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  19. * @copyright Ghent University
  20. * @copyright Patrick Cool
  21. *
  22. * @package chamilo.forum
  23. */
  24. $current_course_tool = TOOL_FORUM;
  25. $htmlHeadXtra[] = '<script>
  26. $(document).ready(function() {
  27. $(\'.hide-me\').slideUp();
  28. });
  29. function hidecontent(content){
  30. $(content).slideToggle(\'normal\');
  31. }
  32. </script>';
  33. // The section (tabs).
  34. $this_section = SECTION_COURSES;
  35. // Notification for unauthorized people.
  36. api_protect_course_script(true);
  37. $nameTools = get_lang('Forums');
  38. $_course = api_get_course_info();
  39. $sessionId = api_get_session_id();
  40. $_user = api_get_user_info();
  41. // Including necessary files.
  42. require_once 'forumconfig.inc.php';
  43. require_once 'forumfunction.inc.php';
  44. if (api_is_in_gradebook()) {
  45. $interbreadcrumb[]= array(
  46. 'url' => api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq(),
  47. 'name' => get_lang('ToolGradebook')
  48. );
  49. }
  50. $search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : '';
  51. /* ACTIONS */
  52. $actions = isset($_GET['action']) ? $_GET['action'] : '';
  53. if ($actions === 'add') {
  54. switch ($_GET['content']) {
  55. case 'forum':
  56. $interbreadcrumb[] = array(
  57. 'url' => 'index.php?search=' . $search_forum . '&' . api_get_cidreq(),
  58. 'name' => get_lang('Forum'),
  59. );
  60. $interbreadcrumb[] = array(
  61. 'url' => '#',
  62. 'name' => get_lang('AddForum'),
  63. );
  64. break;
  65. case 'forumcategory':
  66. $interbreadcrumb[] = array(
  67. 'url' => 'index.php?search=' . $search_forum . '&' . api_get_cidreq(),
  68. 'name' => get_lang('Forum'),
  69. );
  70. $interbreadcrumb[] = array(
  71. 'url' => '#',
  72. 'name' => get_lang('AddForumCategory'),
  73. );
  74. break;
  75. default:
  76. break;
  77. }
  78. } else {
  79. $interbreadcrumb[] = array(
  80. 'url' => '#',
  81. 'name' => get_lang('ForumCategories'),
  82. );
  83. }
  84. Display::display_header('');
  85. // Tool introduction
  86. Display::display_introduction_section(TOOL_FORUM);
  87. $form_count = 0;
  88. if (api_is_allowed_to_edit(false, true)) {
  89. //if is called from a learning path lp_id
  90. $lp_id = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : null;
  91. handle_forum_and_forumcategories($lp_id);
  92. }
  93. // Notification
  94. if ($actions == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
  95. if (
  96. api_get_session_id() != 0 &&
  97. api_is_allowed_to_session_edit(false, true) == false
  98. ) {
  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 = Session::read('whatsnew_post_info');
  106. /* TRACKING */
  107. Event::event_access_tool(TOOL_FORUM);
  108. /*
  109. RETRIEVING ALL THE FORUM CATEGORIES AND FORUMS
  110. note: we do this here just after het handling of the actions to be
  111. sure that we already incorporate the latest changes
  112. */
  113. // Step 1: We store all the forum categories in an array $forum_categories.
  114. $forumCategories = get_forum_categories();
  115. // Step 2: We find all the forums (only the visible ones if it is a student).
  116. // display group forum in general forum tool depending to configuration option
  117. $setting = api_get_setting('forum.display_groups_forum_in_general_tool');
  118. $forum_list = get_forums('', '', $setting === 'true');
  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
  125. // id of the group = the key of the array).
  126. if (!api_is_anonymous()) {
  127. $all_groups = GroupManager::get_group_list();
  128. if (is_array($all_groups)) {
  129. foreach ($all_groups as $group) {
  130. $all_groups[$group['id']] = $group;
  131. }
  132. }
  133. }
  134. /* ACTION LINKS */
  135. echo '<div class="actions">';
  136. //if is called from learning path
  137. if (!empty($_GET['lp_id']) || !empty($_POST['lp_id'])) {
  138. echo "<a href=\"../lp/lp_controller.php?"
  139. . api_get_cidreq()
  140. . "&gradebook=&action=add_item&type=step&lp_id=$lp_id#resource_tab-5\">"
  141. . Display::return_icon(
  142. 'back.png',
  143. get_lang("BackTo") . ' ' . get_lang("LearningPaths"),
  144. '',
  145. ICON_SIZE_MEDIUM
  146. )
  147. . "</a>";
  148. }
  149. if (!empty($forum_list)) {
  150. echo search_link();
  151. }
  152. if (api_is_allowed_to_edit(false, true)) {
  153. echo '<a href="'
  154. . api_get_self() . '?' . api_get_cidreq()
  155. . '&action=add&content=forumcategory&lp_id=' . $lp_id . '"> ' .
  156. Display::return_icon(
  157. 'new_folder.png', get_lang('AddForumCategory'), '', ICON_SIZE_MEDIUM
  158. )
  159. . '</a>';
  160. if (is_array($forumCategories) && !empty($forumCategories)) {
  161. echo '<a href="'.api_get_self().'?'.api_get_cidreq(
  162. ).'&action=add&content=forum&lp_id='.$lp_id.'"> '.
  163. Display::return_icon(
  164. 'new_forum.png',
  165. get_lang('AddForum'),
  166. '',
  167. ICON_SIZE_MEDIUM
  168. ).'</a>';
  169. }
  170. }
  171. echo '</div>';
  172. // Fixes error if there forums with no category.
  173. $forumsInNoCategory = get_forums_in_category(0);
  174. if (!empty($forumsInNoCategory)) {
  175. $forumCategories = array_merge(
  176. $forumCategories,
  177. array(
  178. array(
  179. 'cat_id' => 0,
  180. 'session_id' => 0,
  181. 'visibility' => 1,
  182. 'cat_comment' => null
  183. )
  184. )
  185. );
  186. }
  187. /* Display Forum Categories and the Forums in it */
  188. // Step 3: We display the forum_categories first.
  189. if (is_array($forumCategories)) {
  190. foreach ($forumCategories as $forumCategory) {
  191. // The forums in this category.
  192. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  193. // Validation when belongs to a session.
  194. $session_img = api_get_session_image(
  195. $forumCategory['session_id'],
  196. $_user['status']
  197. );
  198. if (empty($sessionId) && !empty($forumCategory['session_name'])) {
  199. $session_displayed = ' (' . Security::remove_XSS($forumCategory['session_name']) . ')';
  200. } else {
  201. $session_displayed = '';
  202. }
  203. if (empty($forumCategory['cat_title'])) {
  204. $forumCategory['cat_title'] = get_lang('WithoutCategory');
  205. }
  206. $html = '';
  207. $iconsEdit = '';
  208. $idCategory = $forumCategory['cat_id'];
  209. $urlCategory = 'viewforumcategory.php?' . api_get_cidreq() . '&forumcategory=' . intval($idCategory);
  210. $titleCategory = Display::tag(
  211. 'a',
  212. $forumCategory['cat_title'],
  213. array(
  214. 'href' => $urlCategory,
  215. 'class' => empty($forumCategory['visibility']) ? 'text-muted' : null
  216. )
  217. );
  218. $descriptionCategory = $forumCategory['cat_comment'];
  219. if (!empty($idCategory)) {
  220. if (
  221. api_is_allowed_to_edit(false, true) &&
  222. !($forumCategory['session_id'] == 0 &&
  223. intval($sessionId) != 0)
  224. ) {
  225. $iconsEdit .= '<a href="' . api_get_self() . '?' . api_get_cidreq()
  226. . '&action=edit&content=forumcategory&id=' . intval($idCategory)
  227. . '">' . Display::return_icon(
  228. 'edit.png',
  229. get_lang('Edit'),
  230. array(),
  231. ICON_SIZE_SMALL
  232. )
  233. . '</a>';
  234. $iconsEdit .='<a href="' . api_get_self() . '?' . api_get_cidreq()
  235. . '&action=delete&content=forumcategory&id=' . intval($idCategory)
  236. . "\" onclick=\"javascript:if(!confirm('"
  237. . addslashes(api_htmlentities(
  238. get_lang('DeleteForumCategory'),
  239. ENT_QUOTES
  240. ))
  241. . "')) return false;\">"
  242. . Display::return_icon(
  243. 'delete.png',
  244. get_lang('Delete'),
  245. array(),
  246. ICON_SIZE_SMALL
  247. )
  248. . '</a>';
  249. $iconsEdit .= return_visible_invisible_icon(
  250. 'forumcategory',
  251. strval(intval($idCategory)),
  252. strval(intval($forumCategory['visibility']))
  253. );
  254. $iconsEdit .= return_lock_unlock_icon(
  255. 'forumcategory',
  256. strval(intval($idCategory)),
  257. strval(intval($forumCategory['locked']))
  258. );
  259. $iconsEdit .= return_up_down_icon(
  260. 'forumcategory',
  261. strval(intval($idCategory)),
  262. $forumCategories
  263. );
  264. }
  265. }
  266. $html .= '<div class="category-forum">';
  267. $html .= Display::tag(
  268. 'div',
  269. $iconsEdit,
  270. array(
  271. 'class' => 'pull-right'
  272. )
  273. );
  274. $icoCategory = Display::return_icon(
  275. 'forum_blue.png',
  276. get_lang(
  277. $forumCategory['cat_title']
  278. ),
  279. array(),
  280. ICON_SIZE_MEDIUM
  281. );
  282. $html .= '<h3>' . $icoCategory . $titleCategory . '</h3>';
  283. if ($descriptionCategory != '' && trim($descriptionCategory) != '&nbsp;') {
  284. $html .= '<div class="forum-description">'.$descriptionCategory.'</div>';
  285. }
  286. $html .= '</div>';
  287. echo $html;
  288. echo '<div class="forum_display">';
  289. if (!empty($forumsInCategory)) {
  290. // We display all the forums in this category.
  291. foreach ($forum_list as $forum) {
  292. // Here we clean the whatnew_post_info array a little bit because to display the icon we
  293. // test if $whatsnew_post_info[$forum['forum_id']] is empty or not.
  294. if (isset($forum['forum_id'])) {
  295. if (!empty($whatsnew_post_info)) {
  296. if (
  297. isset($whatsnew_post_info[$forum['forum_id']]) &&
  298. is_array($whatsnew_post_info[$forum['forum_id']])
  299. ) {
  300. foreach ($whatsnew_post_info[$forum['forum_id']] as $key_thread_id => $new_post_array) {
  301. if (empty($whatsnew_post_info[$forum['forum_id']][$key_thread_id])) {
  302. unset($whatsnew_post_info[$forum['forum_id']][$key_thread_id]);
  303. unset($_SESSION['whatsnew_post_info'][$forum['forum_id']][$key_thread_id]);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. // Note: This can be speed up if we transform the $forum_list
  310. // to an array that uses the forum_category as the key.
  311. if (
  312. isset($forum['forum_category']) &&
  313. $forum['forum_category'] == $forumCategory['cat_id']
  314. ) {
  315. $show_forum = false;
  316. // SHOULD WE SHOW THIS PARTICULAR FORUM
  317. // you are teacher => show forum
  318. if (api_is_allowed_to_edit(false, true)) {
  319. $show_forum = true;
  320. } else {
  321. // you are not a teacher
  322. // it is not a group forum => show forum
  323. // (invisible forums are already left out see get_forums function)
  324. if ($forum['forum_of_group'] == '0') {
  325. $show_forum = true;
  326. } else {
  327. $show_forum = GroupManager::user_has_access(
  328. $user_id,
  329. $forum['forum_of_group'],
  330. GroupManager::GROUP_TOOL_FORUM
  331. );
  332. }
  333. }
  334. if ($show_forum) {
  335. $form_count++;
  336. $mywhatsnew_post_info = isset($whatsnew_post_info[$forum['forum_id']]) ?
  337. $whatsnew_post_info[$forum['forum_id']] : null;
  338. $html = '<div class="panel panel-default forum">';
  339. $html .= '<div class="panel-body">';
  340. $forum_image = '';
  341. $imgForum = '';
  342. // Showing the image
  343. if (!empty($forum['forum_image'])) {
  344. $image_path = api_get_path(WEB_COURSE_PATH). api_get_course_path(). '/upload/forum/images/'. $forum['forum_image'];
  345. $image_size = api_getimagesize($image_path);
  346. $img_attributes = '';
  347. if (!empty($image_size)) {
  348. //limit display width and height to 100px
  349. $img_attributes = ' style="width:80px" height="80px"';
  350. $imgForum = "<img src=\"$image_path\" $img_attributes>";
  351. } else {
  352. $imgForum = '';
  353. }
  354. $forum_image = $imgForum;
  355. } else {
  356. if ($forum['forum_of_group'] == '0') {
  357. $forum_image = Display::return_icon(
  358. 'forum_group.png',
  359. get_lang('GroupForum'),
  360. null,
  361. ICON_SIZE_LARGE
  362. );
  363. } else {
  364. $forum_image = Display::return_icon(
  365. 'forum.png',
  366. get_lang('Forum'),
  367. null,
  368. ICON_SIZE_LARGE
  369. );
  370. }
  371. }
  372. // Validation when belongs to a session
  373. $session_img = api_get_session_image(
  374. $forum['session_id'],
  375. $_user['status']
  376. );
  377. if ($forum['forum_of_group'] != '0') {
  378. $my_all_groups_forum_name = isset($all_groups[$forum['forum_of_group']]['name']) ?
  379. $all_groups[$forum['forum_of_group']]['name'] : null;
  380. $my_all_groups_forum_id = isset($all_groups[$forum['forum_of_group']]['id']) ?
  381. $all_groups[$forum['forum_of_group']]['id'] : null;
  382. $group_title = api_substr(
  383. $my_all_groups_forum_name,
  384. 0,
  385. 30
  386. );
  387. $forum_title_group_addition = ' (<a href="../group/group_space.php?'
  388. . api_get_cidreq() . '&gidReq=' . $forum['forum_of_group']
  389. . '" class="forum_group_link">' . get_lang('GoTo') . ' ' . $group_title . '</a>)'
  390. . $session_img;
  391. } else {
  392. $forum_title_group_addition = '';
  393. }
  394. if (empty($sessionId) && !empty($forum['session_name'])) {
  395. $session_displayed = ' (' . $forum['session_name'] . ')';
  396. } else {
  397. $session_displayed = '';
  398. }
  399. $forum['forum_of_group'] == 0 ? $groupid = '' : $groupid = $forum['forum_of_group'];
  400. $number_threads = isset($forum['number_of_threads']) ? (int) $forum['number_of_threads'] : 0;
  401. $number_posts = isset($forum['number_of_posts']) ? $forum['number_of_posts'] : 0;
  402. $html .= '<div class="row">';
  403. $html .= '<div class="col-md-6">';
  404. $html .= '<div class="col-md-3">';
  405. $html .= '<div class="number-post">'.$forum_image.'<p>'.$number_threads.' ' . get_lang('ForumThreads') . '</p>'
  406. . '</div>';
  407. $html .= '</div>';
  408. $html .= '<div class="col-md-9">';
  409. $iconForum = Display::return_icon(
  410. 'forum_yellow.png',
  411. get_lang($forumCategory['cat_title']),
  412. null,
  413. ICON_SIZE_MEDIUM
  414. );
  415. $linkForum = Display::tag(
  416. 'a',
  417. $forum['forum_title'],
  418. [
  419. 'href' => 'viewforum.php?' . api_get_cidreq()
  420. . '&gidReq=' . intval($groupid)
  421. . '&forum=' . intval($forum['forum_id']),
  422. 'class' => empty($forum['visibility']) ? 'text-muted' : null
  423. ]
  424. );
  425. if (!empty($forum['start_time']) && !empty($forum['end_time'])) {
  426. $res = api_is_date_in_date_range($forum['start_time'], $forum['end_time']);
  427. if (!$res) {
  428. $linkForum = $forum['forum_title'];
  429. }
  430. }
  431. $html .= '<h3 class="title">' . $iconForum . $linkForum . '</h3>';
  432. $html .= Display::tag(
  433. 'p',
  434. Security::remove_XSS($forum['forum_comment']),
  435. [
  436. 'class'=>'description'
  437. ]
  438. );
  439. $html .= '</div>';
  440. $html .= '</div>';
  441. $iconEmpty = '';
  442. // The number of topics and posts.
  443. if ($forum['forum_of_group'] !== '0') {
  444. $newPost='';
  445. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  446. $newPost = ' ' . Display::return_icon(
  447. 'alert.png',
  448. get_lang('Forum'),
  449. null,
  450. ICON_SIZE_SMALL
  451. );
  452. } else {
  453. $newPost = $iconEmpty;
  454. }
  455. } else {
  456. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  457. $newPost = ' ' . Display::return_icon(
  458. 'alert.png',
  459. get_lang('Forum'),
  460. null,
  461. ICON_SIZE_SMALL
  462. );
  463. } else {
  464. $newPost = $iconEmpty;
  465. }
  466. }
  467. $html .= '<div class="col-md-6">';
  468. $html .= '<div class="row">';
  469. $html .= '<div class="col-md-2">';
  470. $html .= $newPost.'</div>';
  471. $html .= '<div class="col-md-6">';
  472. // The last post in the forum.
  473. if (isset($forum['last_poster_name']) && $forum['last_poster_name'] != '') {
  474. $name = $forum['last_poster_name'];
  475. $poster_id = 0;
  476. $username = "";
  477. } else {
  478. if (isset($forum['last_poster_firstname'])) {
  479. $name = api_get_person_name(
  480. $forum['last_poster_firstname'],
  481. $forum['last_poster_lastname']
  482. );
  483. $poster_id = $forum['last_poster_id'];
  484. $userinfo = api_get_user_info($poster_id);
  485. $username = sprintf(
  486. get_lang('LoginX'),
  487. $userinfo['username']
  488. );
  489. }
  490. }
  491. if (!empty($forum['last_post_id'])) {
  492. $html .= Display::return_icon(
  493. 'post-item.png',
  494. null,
  495. null,
  496. ICON_SIZE_TINY
  497. )
  498. . ' ' .
  499. api_convert_and_format_date($forum['last_post_date'])
  500. . '<br /> ' . get_lang('By') . ' ' .
  501. display_user_link(
  502. $poster_id,
  503. $name,
  504. '',
  505. $username
  506. );
  507. }
  508. $html .= '</div>';
  509. $html .= '<div class="col-md-4">';
  510. if (api_is_allowed_to_edit(false, true) &&
  511. !($forum['session_id'] == 0 && intval($sessionId) != 0)
  512. ) {
  513. $html .= '<a href="'.api_get_self() . '?' . api_get_cidreq()
  514. . '&action=edit&content=forum&id=' . $forum['forum_id'] . '">'
  515. . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)
  516. . '</a>';
  517. $html .= '<a href="' . api_get_self() . '?' . api_get_cidreq()
  518. . '&action=delete&content=forum&id=' . $forum['forum_id']
  519. . "\" onclick=\"javascript:if(!confirm('" . addslashes(
  520. api_htmlentities(get_lang('DeleteForum'), ENT_QUOTES)
  521. )
  522. . "')) return false;\">"
  523. . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  524. . '</a>';
  525. $html .= return_visible_invisible_icon(
  526. 'forum',
  527. $forum['forum_id'],
  528. $forum['visibility']
  529. );
  530. $html .= return_lock_unlock_icon(
  531. 'forum',
  532. $forum['forum_id'],
  533. $forum['locked']
  534. );
  535. $html .= return_up_down_icon(
  536. 'forum',
  537. $forum['forum_id'],
  538. $forumsInCategory
  539. );
  540. }
  541. $iconnotify = 'notification_mail_na.png';
  542. $session_forum_notification = isset($_SESSION['forum_notification']['forum']) ?
  543. $_SESSION['forum_notification']['forum'] : false;
  544. if (is_array($session_forum_notification)) {
  545. if (in_array(
  546. $forum['forum_id'],
  547. $session_forum_notification
  548. )) {
  549. $iconnotify = 'notification_mail.png';
  550. }
  551. }
  552. if (
  553. !api_is_anonymous() &&
  554. api_is_allowed_to_session_edit(false, true)
  555. ) {
  556. $html .= '<a href="' . api_get_self() . '?' . api_get_cidreq()
  557. . '&action=notify&content=forum&id=' . $forum['forum_id'] . '">'
  558. . Display::return_icon($iconnotify, get_lang('NotifyMe'), null, ICON_SIZE_SMALL)
  559. . '</a>';
  560. }
  561. $html .= '</div>';
  562. $html .= '</div>';
  563. $html .= '</div>';
  564. $html .= '</div>';
  565. $html .= '</div>';
  566. $html .= '</div>';
  567. }
  568. echo $html;
  569. }
  570. }
  571. } else {
  572. echo '<div class="alert alert-warning">' . get_lang('NoForumInThisCategory') . '</div>'
  573. . (api_is_allowed_to_edit(false, true) ? '<div>' : '</div>')
  574. . '</div>';
  575. }
  576. echo '</div>';
  577. }
  578. }
  579. Display:: display_footer();