index.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CForumPost;
  5. /**
  6. * These files are a complete rework of the forum. The database structure is
  7. * based on phpBB but all the code is rewritten. A lot of new functionalities
  8. * are added:
  9. * - forum categories and forums can be sorted up or down, locked or made invisible
  10. * - consistent and integrated forum administration
  11. * - forum options: are students allowed to edit their post?
  12. * moderation of posts (approval)
  13. * reply only forums (students cannot create new threads)
  14. * multiple forums per group
  15. * - sticky messages
  16. * - new view option: nested view
  17. * - quoting a message
  18. *
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  20. * @copyright Ghent University
  21. * @copyright Patrick Cool
  22. *
  23. * @package chamilo.forum
  24. */
  25. require_once __DIR__.'/../inc/global.inc.php';
  26. $current_course_tool = TOOL_FORUM;
  27. $htmlHeadXtra[] = '<script>
  28. $(document).ready(function() {
  29. $(\'.hide-me\').slideUp();
  30. });
  31. function hidecontent(content){
  32. $(content).slideToggle(\'normal\');
  33. }
  34. </script>';
  35. // The section (tabs).
  36. $this_section = SECTION_COURSES;
  37. // Notification for unauthorized people.
  38. api_protect_course_script(true);
  39. $nameTools = get_lang('Forums');
  40. $_course = api_get_course_info();
  41. $sessionId = api_get_session_id();
  42. $_user = api_get_user_info();
  43. // Including necessary files.
  44. require_once 'forumconfig.inc.php';
  45. require_once 'forumfunction.inc.php';
  46. if (api_is_in_gradebook()) {
  47. $interbreadcrumb[] = array(
  48. 'url' => Category::getUrl(),
  49. 'name' => get_lang('ToolGradebook'),
  50. );
  51. }
  52. $search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : '';
  53. /* ACTIONS */
  54. $actions = isset($_GET['action']) ? $_GET['action'] : '';
  55. if ($actions === 'add') {
  56. switch ($_GET['content']) {
  57. case 'forum':
  58. $interbreadcrumb[] = array(
  59. 'url' => 'index.php?search='.$search_forum.'&'.api_get_cidreq(),
  60. 'name' => get_lang('Forum'),
  61. );
  62. $interbreadcrumb[] = array(
  63. 'url' => '#',
  64. 'name' => get_lang('AddForum'),
  65. );
  66. break;
  67. case 'forumcategory':
  68. $interbreadcrumb[] = array(
  69. 'url' => 'index.php?search='.$search_forum.'&'.api_get_cidreq(),
  70. 'name' => get_lang('Forum'),
  71. );
  72. $interbreadcrumb[] = array(
  73. 'url' => '#',
  74. 'name' => get_lang('AddForumCategory'),
  75. );
  76. break;
  77. default:
  78. break;
  79. }
  80. } else {
  81. $interbreadcrumb[] = array(
  82. 'url' => '#',
  83. 'name' => get_lang('ForumCategories'),
  84. );
  85. }
  86. // Tool introduction
  87. $introduction = Display::return_introduction_section(TOOL_FORUM);
  88. $form_count = 0;
  89. $formContent = '';
  90. if (api_is_allowed_to_edit(false, true)) {
  91. //if is called from a learning path lp_id
  92. $lp_id = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : null;
  93. $formContent = handle_forum_and_forumcategories($lp_id);
  94. }
  95. // Notification
  96. if ($actions == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
  97. if (api_get_session_id() != 0 &&
  98. api_is_allowed_to_session_edit(false, true) == false
  99. ) {
  100. api_not_allowed();
  101. }
  102. $return_message = set_notification($_GET['content'], $_GET['id']);
  103. Display::addFlash(Display::return_message($return_message, 'confirm', false));
  104. }
  105. get_whats_new();
  106. $whatsnew_post_info = Session::read('whatsnew_post_info');
  107. /* TRACKING */
  108. Event::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
  112. sure that we already incorporate the latest changes
  113. */
  114. // Step 1: We store all the forum categories in an array $forum_categories.
  115. $forumCategories = get_forum_categories();
  116. // Step 2: We find all the forums (only the visible ones if it is a student).
  117. // display group forum in general forum tool depending to configuration option
  118. $setting = api_get_setting('display_groups_forum_in_general_tool');
  119. $allCourseForums = get_forums('', '', $setting === 'true');
  120. $user_id = api_get_user_id();
  121. /* RETRIEVING ALL GROUPS AND THOSE OF THE USER */
  122. // The groups of the user.
  123. $groups_of_user = array();
  124. $groups_of_user = GroupManager::get_group_ids($_course['real_id'], $user_id);
  125. // All groups in the course (and sorting them as the
  126. // id of the group = the key of the array).
  127. if (!api_is_anonymous()) {
  128. $all_groups = GroupManager::get_group_list();
  129. if (is_array($all_groups)) {
  130. foreach ($all_groups as $group) {
  131. $all_groups[$group['id']] = $group;
  132. }
  133. }
  134. }
  135. /* ACTION LINKS */
  136. $actionLeft = null;
  137. //if is called from learning path
  138. if (!empty($_GET['lp_id']) || !empty($_POST['lp_id'])) {
  139. $url = "../lp/lp_controller.php?".api_get_cidreq()
  140. ."&gradebook=&action=add_item&type=step&lp_id='.$lp_id.'#resource_tab-5";
  141. $actionLeft .= Display::url(
  142. Display::return_icon(
  143. 'back.png',
  144. get_lang("BackTo").' '.get_lang("LearningPaths"),
  145. null,
  146. ICON_SIZE_MEDIUM
  147. ),
  148. $url
  149. );
  150. }
  151. if (!empty($allCourseForums)) {
  152. $actionLeft .= search_link();
  153. }
  154. if (api_is_allowed_to_edit(false, true)) {
  155. $actionLeft .= Display::url(
  156. Display::return_icon(
  157. 'new_folder.png',
  158. get_lang('AddForumCategory'),
  159. null,
  160. ICON_SIZE_MEDIUM
  161. ),
  162. api_get_self().'?'.api_get_cidreq().'&action=add&content=forumcategory&lp_id='.$lp_id
  163. );
  164. if (is_array($forumCategories) && !empty($forumCategories)) {
  165. $actionLeft .= Display::url(
  166. Display::return_icon(
  167. 'new_forum.png',
  168. get_lang('AddForum'),
  169. null,
  170. ICON_SIZE_MEDIUM
  171. ),
  172. api_get_self().'?'.api_get_cidreq().'&action=add&content=forum&lp_id='.$lp_id
  173. );
  174. }
  175. }
  176. $actions = Display::toolbarAction('toolbar-forum', array($actionLeft));
  177. // Fixes error if there forums with no category.
  178. $forumsInNoCategory = get_forums_in_category(0);
  179. if (!empty($forumsInNoCategory)) {
  180. $forumCategories = array_merge(
  181. $forumCategories,
  182. array(
  183. array(
  184. 'cat_id' => 0,
  185. 'session_id' => 0,
  186. 'visibility' => 1,
  187. 'cat_comment' => null
  188. )
  189. )
  190. );
  191. }
  192. /* Display Forum Categories and the Forums in it */
  193. // Step 3: We display the forum_categories first.
  194. $listForumCategory = array();
  195. $forumCategoryInfo = array();
  196. if (is_array($forumCategories)) {
  197. foreach ($forumCategories as $forumCategory) {
  198. $forumCategoryInfo['id'] = $forumCategory['cat_id'];
  199. if (empty($forumCategory['cat_title'])) {
  200. $forumCategoryInfo['title'] = get_lang('WithoutCategory');
  201. } else {
  202. $forumCategoryInfo['title'] = $forumCategory['cat_title'];
  203. }
  204. $forumCategoryInfo['icon_session'] = api_get_session_image(
  205. $forumCategory['session_id'],
  206. $_user['status']
  207. );
  208. // Validation when belongs to a session
  209. $forumCategoryInfo['description'] = $forumCategory['cat_comment'];
  210. if (empty($sessionId) && !empty($forumCategory['session_name'])) {
  211. $forumCategory['session_display'] = ' ('.Security::remove_XSS($forumCategory['session_name']).')';
  212. } else {
  213. $forumCategory['session_display'] = null;
  214. }
  215. $tools = null;
  216. $idCategory = $forumCategory['cat_id'];
  217. $forumCategoryInfo['url'] = 'viewforumcategory.php?'.api_get_cidreq().'&forumcategory='.intval($idCategory);
  218. if (!empty($idCategory)) {
  219. if (api_is_allowed_to_edit(false, true) &&
  220. !($forumCategory['session_id'] == 0 && intval($sessionId) != 0)
  221. ) {
  222. $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq()
  223. .'&action=edit&content=forumcategory&id='.intval($idCategory)
  224. .'">'.Display::return_icon(
  225. 'edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL
  226. )
  227. .'</a>';
  228. $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq()
  229. .'&action=delete&content=forumcategory&id='.intval($idCategory)
  230. ."\" onclick=\"javascript:if(!confirm('"
  231. .addslashes(api_htmlentities(
  232. get_lang('DeleteForumCategory'), ENT_QUOTES
  233. ))
  234. ."')) return false;\">"
  235. .Display::return_icon(
  236. 'delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL
  237. )
  238. .'</a>';
  239. $tools .= return_visible_invisible_icon(
  240. 'forumcategory',
  241. $idCategory,
  242. $forumCategory['visibility']
  243. );
  244. $tools .= return_lock_unlock_icon(
  245. 'forumcategory',
  246. $idCategory,
  247. $forumCategory['locked']
  248. );
  249. $tools .= return_up_down_icon(
  250. 'forumcategory',
  251. $idCategory,
  252. $forumCategories
  253. );
  254. }
  255. }
  256. $forumCategoryInfo['tools'] = $tools;
  257. $forumCategoryInfo['forums'] = [];
  258. // The forums in this category.
  259. $forumInfo = array();
  260. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  261. if (!empty($forumsInCategory)) {
  262. $forumsDetailsList = null;
  263. // We display all the forums in this category.
  264. foreach ($allCourseForums as $forum) {
  265. // Here we clean the whatnew_post_info array a little bit because to display the icon we
  266. // test if $whatsnew_post_info[$forum['forum_id']] is empty or not.
  267. if (isset($forum['forum_id'])) {
  268. if (!empty($whatsnew_post_info)) {
  269. if (isset($whatsnew_post_info[$forum['forum_id']]) &&
  270. is_array($whatsnew_post_info[$forum['forum_id']])
  271. ) {
  272. foreach ($whatsnew_post_info[$forum['forum_id']] as $key_thread_id => $new_post_array) {
  273. if (empty($whatsnew_post_info[$forum['forum_id']][$key_thread_id])) {
  274. unset($whatsnew_post_info[$forum['forum_id']][$key_thread_id]);
  275. unset($_SESSION['whatsnew_post_info'][$forum['forum_id']][$key_thread_id]);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // Note: This can be speed up if we transform the $allCourseForums
  282. // to an array that uses the forum_category as the key.
  283. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  284. $show_forum = false;
  285. // SHOULD WE SHOW THIS PARTICULAR FORUM
  286. // you are teacher => show forum
  287. if (api_is_allowed_to_edit(false, true)) {
  288. $show_forum = true;
  289. } else {
  290. // you are not a teacher
  291. // it is not a group forum => show forum
  292. // (invisible forums are already left out see get_forums function)
  293. if ($forum['forum_of_group'] == '0') {
  294. $show_forum = true;
  295. } else {
  296. $show_forum = GroupManager::user_has_access(
  297. $user_id,
  298. $forum['forum_of_group'],
  299. GroupManager::GROUP_TOOL_FORUM
  300. );
  301. }
  302. }
  303. if ($show_forum) {
  304. $form_count++;
  305. $mywhatsnew_post_info = isset($whatsnew_post_info[$forum['forum_id']])
  306. ? $whatsnew_post_info[$forum['forum_id']]
  307. : null;
  308. $forumInfo['id'] = $forum['forum_id'];
  309. $forumInfo['forum_of_group'] = $forum['forum_of_group'];
  310. $forumInfo['title'] = $forum['forum_title'];
  311. $forumInfo['forum_image'] = null;
  312. // Showing the image
  313. if (!empty($forum['forum_image'])) {
  314. $image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'
  315. .$forum['forum_image'];
  316. $image_size = api_getimagesize($image_path);
  317. $img_attributes = '';
  318. if (!empty($image_size)) {
  319. $forumInfo['forum_image'] = $image_path;
  320. }
  321. }
  322. // Validation when belongs to a session
  323. $forumInfo['icon_session'] = api_get_session_image(
  324. $forum['session_id'],
  325. $_user['status']
  326. );
  327. if ($forum['forum_of_group'] != '0') {
  328. $my_all_groups_forum_name = isset($all_groups[$forum['forum_of_group']]['name'])
  329. ? $all_groups[$forum['forum_of_group']]['name']
  330. : null;
  331. $my_all_groups_forum_id = isset($all_groups[$forum['forum_of_group']]['id'])
  332. ? $all_groups[$forum['forum_of_group']]['id']
  333. : null;
  334. $group_title = api_substr($my_all_groups_forum_name, 0, 30);
  335. $forumInfo['forum_group_title'] = $group_title;
  336. }
  337. $forum['forum_of_group'] == 0 ? $groupid = '' : $groupid = $forum['forum_of_group'];
  338. $forumInfo['visibility'] = $forum['visibility'];
  339. $forumInfo['number_threads'] = isset($forum['number_of_threads'])
  340. ? (int) $forum['number_of_threads']
  341. : 0;
  342. //$number_posts = isset($forum['number_of_posts']) ? $forum['number_of_posts'] : 0;
  343. $linkForum = api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq()
  344. .'&gidReq='.$groupid.'&forum='.$forum['forum_id'];
  345. $forumInfo['url'] = $linkForum;
  346. if (!empty($forum['start_time']) && !empty($forum['end_time'])) {
  347. $res = api_is_date_in_date_range($forum['start_time'], $forum['end_time']);
  348. if (!$res) {
  349. $linkForum = $forum['forum_title'];
  350. }
  351. }
  352. $forumInfo['description'] = Security::remove_XSS($forum['forum_comment']);
  353. if ($forum['moderated'] == 1 && api_is_allowed_to_edit(false, true)) {
  354. $waitingCount = getCountPostsWithStatus(
  355. CForumPost::STATUS_WAITING_MODERATION,
  356. $forum
  357. );
  358. if (!empty($waitingCount)) {
  359. $forumInfo['moderation'] = $waitingCount;
  360. }
  361. }
  362. $toolActions = null;
  363. $forumInfo['alert'] = null;
  364. // The number of topics and posts.
  365. if ($forum['forum_of_group'] !== '0') {
  366. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  367. $forumInfo['alert'] = ' '.
  368. Display::return_icon(
  369. 'alert.png',
  370. get_lang('Forum'),
  371. null,
  372. ICON_SIZE_SMALL
  373. );
  374. }
  375. } else {
  376. if (is_array($mywhatsnew_post_info) && !empty($mywhatsnew_post_info)) {
  377. $forumInfo['alert'] = ' '.Display::return_icon(
  378. 'alert.png',
  379. get_lang('Forum'),
  380. null,
  381. ICON_SIZE_SMALL
  382. );
  383. }
  384. }
  385. $poster_id = null;
  386. // The last post in the forum.
  387. if (isset($forum['last_poster_name']) && $forum['last_poster_name'] != '') {
  388. $name = $forum['last_poster_name'];
  389. $poster_id = 0;
  390. $username = "";
  391. } else {
  392. if (isset($forum['last_poster_firstname'])) {
  393. $name = api_get_person_name(
  394. $forum['last_poster_firstname'],
  395. $forum['last_poster_lastname']
  396. );
  397. $poster_id = $forum['last_poster_id'];
  398. $userinfo = api_get_user_info($poster_id);
  399. $username = sprintf(
  400. get_lang('LoginX'),
  401. $userinfo['username']
  402. );
  403. }
  404. }
  405. $forumInfo['last_poster_id'] = $poster_id;
  406. if (!empty($forum['last_poster_id'])) {
  407. $forumInfo['last_poster_date'] = api_convert_and_format_date($forum['last_post_date']);
  408. $forumInfo['last_poster_user'] = display_user_link($poster_id, $name, null, $username);
  409. }
  410. if (api_is_allowed_to_edit(false, true)
  411. && !($forum['session_id'] == 0 && intval($sessionId) != 0)
  412. ) {
  413. $toolActions .= '<a href="'.api_get_self().'?'.api_get_cidreq()
  414. .'&action=edit&content=forum&id='.$forum['forum_id'].'">'
  415. .Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)
  416. .'</a>';
  417. $toolActions .= '<a href="'.api_get_self().'?'.api_get_cidreq()
  418. .'&action=delete&content=forum&id='.$forum['forum_id']
  419. ."\" onclick=\"javascript:if(!confirm('".addslashes(
  420. api_htmlentities(get_lang('DeleteForum'), ENT_QUOTES)
  421. )
  422. ."')) return false;\">"
  423. .Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)
  424. .'</a>';
  425. $toolActions .= return_visible_invisible_icon(
  426. 'forum',
  427. $forum['forum_id'],
  428. $forum['visibility']
  429. );
  430. $toolActions .= return_lock_unlock_icon(
  431. 'forum',
  432. $forum['forum_id'],
  433. $forum['locked']
  434. );
  435. $toolActions .= return_up_down_icon(
  436. 'forum',
  437. $forum['forum_id'],
  438. $forumsInCategory
  439. );
  440. }
  441. $iconnotify = 'notification_mail_na.png';
  442. $session_forum_notification = isset($_SESSION['forum_notification']['forum'])
  443. ? $_SESSION['forum_notification']['forum']
  444. : false;
  445. if (is_array($session_forum_notification)) {
  446. if (in_array($forum['forum_id'], $session_forum_notification)) {
  447. $iconnotify = 'notification_mail.png';
  448. }
  449. }
  450. if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
  451. $toolActions .= '<a href="'.api_get_self().'?'.api_get_cidreq()
  452. .'&action=notify&content=forum&id='.$forum['forum_id'].'">'
  453. .Display::return_icon($iconnotify, get_lang('NotifyMe'), null, ICON_SIZE_SMALL)
  454. .'</a>';
  455. }
  456. $forumInfo['tools'] = $toolActions;
  457. $forumsDetailsList[] = $forumInfo;
  458. }
  459. }
  460. }
  461. $forumCategoryInfo['forums'] = $forumsDetailsList;
  462. }
  463. $listForumCategory[] = $forumCategoryInfo;
  464. }
  465. }
  466. $tpl = new Template($nameTools);
  467. $tpl->assign('introduction_section', $introduction);
  468. $tpl->assign('actions', $actions);
  469. $tpl->assign('data', $listForumCategory);
  470. $tpl->assign('form_content', $formContent);
  471. $layout = $tpl->get_template('forum/list.tpl');
  472. $tpl->display($layout);