viewforumcategory.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CForumPost;
  4. use ChamiloSession as Session;
  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. Session::erase('_gid');
  27. // Notification for unauthorized people.
  28. api_protect_course_script(true);
  29. $htmlHeadXtra[] = '<script>
  30. $(function() {
  31. $(\'.hide-me\').slideUp()
  32. });
  33. function hidecontent(content){
  34. $(content).slideToggle(\'normal\');
  35. }
  36. </script>';
  37. // The section (tabs)
  38. $this_section = SECTION_COURSES;
  39. // Including additional library scripts.
  40. $nameTools = get_lang('ToolForum');
  41. $_user = api_get_user_info();
  42. $_course = api_get_course_info();
  43. $action = isset($_GET['action']) ? $_GET['action'] : '';
  44. $hideNotifications = api_get_course_setting('hide_forum_notifications');
  45. $hideNotifications = $hideNotifications == 1;
  46. require_once 'forumfunction.inc.php';
  47. // Are we in a lp ?
  48. $origin = api_get_origin();
  49. if (api_is_in_gradebook()) {
  50. $interbreadcrumb[] = [
  51. 'url' => Category::getUrl(),
  52. 'name' => get_lang('ToolGradebook'),
  53. ];
  54. }
  55. $sessionId = api_get_session_id();
  56. $current_forum_category = get_forum_categories($_GET['forumcategory']);
  57. $interbreadcrumb[] = [
  58. 'url' => 'index.php?'.api_get_cidreq().'&search='.Security::remove_XSS(urlencode(isset($_GET['search']) ? $_GET['search'] : '')),
  59. 'name' => get_lang('Forum'),
  60. ];
  61. if (!empty($action) && !empty($_GET['content'])) {
  62. if ($action == 'add' && $_GET['content'] == 'forum') {
  63. $interbreadcrumb[] = [
  64. 'url' => 'viewforumcategory.php?'.api_get_cidreq().'&forumcategory='.$current_forum_category['cat_id'],
  65. 'name' => $current_forum_category['cat_title'],
  66. ];
  67. $interbreadcrumb[] = [
  68. 'url' => '#',
  69. 'name' => get_lang('AddForum'),
  70. ];
  71. }
  72. } else {
  73. $interbreadcrumb[] = [
  74. 'url' => '#',
  75. 'name' => $current_forum_category['cat_title'],
  76. ];
  77. }
  78. if ($origin == 'learnpath') {
  79. Display::display_reduced_header();
  80. } else {
  81. Display::display_header(null);
  82. }
  83. /* ACTIONS */
  84. $whatsnew_post_info = isset($_SESSION['whatsnew_post_info']) ? $_SESSION['whatsnew_post_info'] : null;
  85. /* Is the user allowed here? */
  86. // if the user is not a course administrator and the forum is hidden
  87. // then the user is not allowed here.
  88. if (!api_is_allowed_to_edit(false, true) &&
  89. ($current_forum_category && $current_forum_category['visibility'] == 0)
  90. ) {
  91. api_not_allowed();
  92. }
  93. /* Action Links */
  94. $html = '<div class="actions">';
  95. $html .= '<a href="index.php?'.api_get_cidreq().'">'.
  96. Display::return_icon('back.png', get_lang('BackToForumOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  97. if (api_is_allowed_to_edit(false, true)) {
  98. $html .= '<a href="'.api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&forumcategory='
  99. .$current_forum_category['cat_id'].'&action=add&content=forum"> '
  100. .Display::return_icon('new_forum.png', get_lang('AddForum'), '', ICON_SIZE_MEDIUM).'</a>';
  101. }
  102. $html .= search_link();
  103. $html .= '</div>';
  104. echo $html;
  105. $logInfo = [
  106. 'tool' => TOOL_FORUM,
  107. 'tool_id' => 0,
  108. 'tool_id_detail' => 0,
  109. 'action' => $action,
  110. 'info' => isset($_GET['content']) ? $_GET['content'] : '',
  111. ];
  112. Event::registerLog($logInfo);
  113. if (api_is_allowed_to_edit(false, true)) {
  114. handle_forum_and_forumcategories();
  115. }
  116. // Notification
  117. if ($action == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
  118. $return_message = set_notification($_GET['content'], $_GET['id']);
  119. echo Display::return_message($return_message, 'confirm', false);
  120. }
  121. if ($action != 'add') {
  122. /*
  123. RETRIEVING ALL THE FORUM CATEGORIES AND FORUMS
  124. Note: We do this here just after het handling of the actions to be sure that we already incorporate the
  125. latest changes.
  126. */
  127. // Step 1: We store all the forum categories in an array $forum_categories.
  128. $forum_categories = [];
  129. $forum_category = get_forum_categories($_GET['forumcategory']);
  130. // Step 2: We find all the forums.
  131. $forum_list = get_forums();
  132. /* RETRIEVING ALL GROUPS AND THOSE OF THE USER */
  133. // The groups of the user.
  134. $groups_of_user = GroupManager::get_group_ids($_course['real_id'], $_user['user_id']);
  135. // All groups in the course (and sorting them as the id of the group = the key of the array.
  136. $all_groups = GroupManager::get_group_list();
  137. if (is_array($all_groups)) {
  138. foreach ($all_groups as $group) {
  139. $all_groups[$group['id']] = $group;
  140. }
  141. }
  142. /* Display Forum Categories and the Forums in it */
  143. $html = '';
  144. $html .= '<div class="category-forum">';
  145. $session_displayed = '';
  146. if ((!isset($sessionId) || $sessionId == 0) &&
  147. !empty($forum_category['session_name'])
  148. ) {
  149. $session_displayed = ' ('.Security::remove_XSS($forum_category['session_name']).')';
  150. }
  151. $forum_categories_list = [];
  152. $forumId = $forum_category['cat_id'];
  153. $forumTitle = $forum_category['cat_title'];
  154. $linkForumCategory = 'viewforumcategory.php?'.api_get_cidreq().'&forumcategory='.strval(intval($forumId));
  155. $descriptionCategory = $forum_category['cat_comment'];
  156. $icoCategory = Display::return_icon(
  157. 'forum_blue.png',
  158. get_lang($forum_category['cat_title']),
  159. ['class' => ''],
  160. ICON_SIZE_MEDIUM
  161. );
  162. if (api_is_allowed_to_edit(false, true) &&
  163. !($forum_category['session_id'] == 0 && $sessionId != 0)
  164. ) {
  165. $iconsEdit = '<a href="'.api_get_self().'?'.api_get_cidreq().'&forumcategory='
  166. .Security::remove_XSS($_GET['forumcategory']).'&action=edit&content=forumcategory&id='
  167. .''.$forumId.'">'
  168. .Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
  169. $iconsEdit .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&forumcategory='
  170. .Security::remove_XSS($_GET['forumcategory'])
  171. .'&action=delete&content=forumcategory&id='.$forumId
  172. ."\" onclick=\"javascript:if(!confirm('"
  173. .addslashes(api_htmlentities(get_lang('DeleteForumCategory'), ENT_QUOTES))
  174. ."')) return false;\">".Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL)
  175. .'</a>';
  176. $iconsEdit .= return_visible_invisible_icon(
  177. 'forumcategory',
  178. $forum_category['cat_id'],
  179. $forum_category['visibility'],
  180. ['forumcategory' => $_GET['forumcategory']]
  181. );
  182. $iconsEdit .= return_lock_unlock_icon(
  183. 'forumcategory',
  184. $forum_category['cat_id'],
  185. $forum_category['locked'],
  186. ['forumcategory' => $_GET['forumcategory']]
  187. );
  188. $iconsEdit .= return_up_down_icon(
  189. 'forumcategory',
  190. $forum_category['cat_id'],
  191. $forum_categories_list
  192. );
  193. $html .= Display::tag(
  194. 'div',
  195. $iconsEdit,
  196. ['class' => 'pull-right']
  197. );
  198. }
  199. $session_img = api_get_session_image($forum_category['session_id'], $_user['status']);
  200. $html .= Display::tag(
  201. 'h3',
  202. $icoCategory.
  203. Display::tag(
  204. 'a',
  205. $forumTitle,
  206. [
  207. 'href' => $linkForumCategory,
  208. 'class' => empty($forum_category['visibility']) ? 'text-muted' : null,
  209. ]
  210. ).$session_displayed.$session_img,
  211. null
  212. );
  213. if ($descriptionCategory != '' && trim($descriptionCategory) != '&nbsp;') {
  214. $html .= '<div class="forum-description">'.$descriptionCategory.'</div>';
  215. }
  216. $html .= '</div>';
  217. echo $html;
  218. echo '<div class="forum_display">';
  219. // The forums in this category.
  220. $forums_in_category = get_forums_in_category($forum_category['cat_id']);
  221. // Step 4: We display all the forums in this category.
  222. $forum_count = 0;
  223. foreach ($forum_list as $forum) {
  224. if (!empty($forum['forum_category'])) {
  225. if ($forum['forum_category'] == $forum_category['cat_id']) {
  226. // The forum has to be showed if
  227. // 1.v it is a not a group forum (teacher and student)
  228. // 2.v it is a group forum and it is public (teacher and student)
  229. // 3. it is a group forum and it is private (always for teachers only if the user is member of the forum
  230. // if the forum is private and it is a group forum and the user
  231. // is not a member of the group forum then it cannot be displayed
  232. $show_forum = false;
  233. // SHOULD WE SHOW THIS PARTICULAR FORUM
  234. // you are teacher => show forum
  235. if (api_is_allowed_to_edit(false, true)) {
  236. //echo 'teacher';
  237. $show_forum = true;
  238. } else {
  239. // you are not a teacher
  240. //echo 'student';
  241. // it is not a group forum => show forum
  242. // (invisible forums are already left out see get_forums function)
  243. if ($forum['forum_of_group'] == '0') {
  244. //echo '-gewoon forum';
  245. $show_forum = true;
  246. } else {
  247. // it is a group forum
  248. //echo '-groepsforum';
  249. // it is a group forum but it is public => show
  250. if ($forum['forum_group_public_private'] == 'public') {
  251. $show_forum = true;
  252. //echo '-publiek';
  253. } else {
  254. // it is a group forum and it is private
  255. //echo '-prive';
  256. // it is a group forum and it is private but the user is member of the group
  257. if (in_array($forum['forum_of_group'], $groups_of_user)) {
  258. //echo '-is lid';
  259. $show_forum = true;
  260. } else {
  261. //echo '-is GEEN lid';
  262. $show_forum = false;
  263. }
  264. }
  265. }
  266. }
  267. $form_count = isset($form_count) ? $form_count : 0;
  268. if ($show_forum === true) {
  269. $form_count++;
  270. $html = '<div class="panel panel-default forum">';
  271. $html .= '<div class="panel-body">';
  272. $my_whatsnew_post_info = isset($whatsnew_post_info[$forum['forum_id']]) ? $whatsnew_post_info[$forum['forum_id']] : null;
  273. if ($forum['forum_of_group'] == '0') {
  274. $forum_image = Display::return_icon(
  275. 'forum_group.png',
  276. get_lang('GroupForum'),
  277. null,
  278. ICON_SIZE_LARGE
  279. );
  280. } else {
  281. $forum_image = Display::return_icon(
  282. 'forum.png',
  283. get_lang('Forum'),
  284. null,
  285. ICON_SIZE_LARGE
  286. );
  287. }
  288. if ($forum['forum_of_group'] != '0') {
  289. $my_all_groups_forum_name = isset($all_groups[$forum['forum_of_group']]['name'])
  290. ? $all_groups[$forum['forum_of_group']]['name']
  291. : null;
  292. $my_all_groups_forum_id = isset($all_groups[$forum['forum_of_group']]['id'])
  293. ? $all_groups[$forum['forum_of_group']]['id']
  294. : null;
  295. $group_title = api_substr($my_all_groups_forum_name, 0, 30);
  296. $forum_title_group_addition = ' (<a href="../group/group_space.php?'.api_get_cidreq()
  297. .'&gidReq='.$my_all_groups_forum_id.'" class="forum_group_link">'
  298. .get_lang('GoTo').' '.$group_title.'</a>)';
  299. } else {
  300. $forum_title_group_addition = '';
  301. }
  302. if (!empty($sessionId) && !empty($forum['session_name'])) {
  303. $session_displayed = ' ('.$forum['session_name'].')';
  304. } else {
  305. $session_displayed = '';
  306. }
  307. // the number of topics and posts
  308. $my_number_threads = isset($forum['number_of_threads']) ? $forum['number_of_threads'] : 0;
  309. $my_number_posts = isset($forum['number_of_posts']) ? $forum['number_of_posts'] : 0;
  310. $html .= '<div class="row">';
  311. $html .= '<div class="col-md-6">';
  312. $html .= '<div class="col-md-3">';
  313. $html .= '<div class="number-post">'.$forum_image.
  314. '<p>'.$my_number_threads.' '.get_lang('ForumThreads').'</p></div>';
  315. $html .= '</div>';
  316. $html .= '<div class="col-md-9">';
  317. $iconForum = Display::return_icon(
  318. 'forum_yellow.png',
  319. get_lang($forum_category['cat_title']),
  320. null,
  321. ICON_SIZE_MEDIUM
  322. );
  323. $linkForum = Display::tag(
  324. 'a',
  325. $forum['forum_title'].$session_displayed,
  326. [
  327. 'href' => 'viewforum.php?'.api_get_cidreq(true, false)."&gidReq={$forum['forum_of_group']}&forum={$forum['forum_id']}&search=".Security::remove_XSS(urlencode(isset($_GET['search']) ? $_GET['search'] : '')),
  328. 'class' => empty($forum['visibility']) ? 'text-muted' : null,
  329. ]
  330. );
  331. $html .= Display::tag(
  332. 'h3',
  333. $linkForum.' '.$forum_title_group_addition,
  334. [
  335. 'class' => 'title',
  336. ]
  337. );
  338. $html .= Display::tag(
  339. 'p',
  340. strip_tags($forum['forum_comment']),
  341. [
  342. 'class' => 'description',
  343. ]
  344. );
  345. if ($forum['moderated'] == 1 && api_is_allowed_to_edit(false, true)) {
  346. $waitingCount = getCountPostsWithStatus(
  347. CForumPost::STATUS_WAITING_MODERATION,
  348. $forum
  349. );
  350. if (!empty($waitingCount)) {
  351. $html .= Display::label(
  352. get_lang('PostsPendingModeration').': '.$waitingCount,
  353. 'warning'
  354. );
  355. }
  356. }
  357. $html .= '</div>';
  358. $html .= '</div>';
  359. $html .= '<div class="col-md-6">';
  360. $iconEmpty = '';
  361. // The number of topics and posts.
  362. if ($forum['forum_of_group'] !== '0') {
  363. $newPost = $iconEmpty;
  364. if (is_array($my_whatsnew_post_info) && !empty($my_whatsnew_post_info)) {
  365. $newPost = ' '.Display::return_icon('alert.png', get_lang('Forum'), null, ICON_SIZE_SMALL);
  366. }
  367. } else {
  368. $newPost = $iconEmpty;
  369. if (is_array($my_whatsnew_post_info) && !empty($my_whatsnew_post_info)) {
  370. $newPost = ' '.Display::return_icon('alert.png', get_lang('Forum'), null, ICON_SIZE_SMALL);
  371. }
  372. }
  373. $html .= '<div class="row">';
  374. $html .= '<div class="col-md-2">';
  375. $html .= $newPost.'</div>';
  376. $poster_id = 0;
  377. $name = '';
  378. // the last post in the forum
  379. if (isset($forum['last_poster_name']) && $forum['last_poster_name'] != '') {
  380. $name = $forum['last_poster_name'];
  381. } else {
  382. if (isset($forum['last_poster_lastname'])) {
  383. $name = api_get_person_name($forum['last_poster_firstname'], $forum['last_poster_lastname']);
  384. $poster_id = $forum['last_poster_id'];
  385. }
  386. }
  387. $html .= '<div class="col-md-6">';
  388. if (!empty($forum['last_post_id'])) {
  389. $html .= Display::return_icon('post-item.png', null, null, ICON_SIZE_TINY).' ';
  390. $html .= Display::dateToStringAgoAndLongDate($forum['last_post_date'])
  391. .' '.get_lang('By').' '
  392. .display_user_link($poster_id, $name);
  393. }
  394. $html .= '</div>';
  395. $html .= '<div class="col-md-4">';
  396. $url = api_get_path(WEB_CODE_PATH).'forum/index.php';
  397. $forumCategoryId = (int) $_GET['forumcategory'];
  398. if (api_is_allowed_to_edit(false, true) &&
  399. !($forum['session_id'] == 0 && $sessionId != 0)
  400. ) {
  401. $html .= '<a href="'.$url.'?'.api_get_cidreq().'&forumcategory='.$forumCategoryId.'&action=edit&content=forum&id='.$forum['forum_id'].'">'
  402. .Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
  403. $html .= '<a href="'.$url.'?'.api_get_cidreq().'&forumcategory='.$forumCategoryId.'&action=delete&content=forum&id='.$forum['forum_id']
  404. ."\" onclick=\"javascript:if(!confirm('"
  405. .addslashes(api_htmlentities(get_lang('DeleteForum'), ENT_QUOTES))
  406. ."')) return false;\">"
  407. .Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL)
  408. .'</a>';
  409. $html .= return_visible_invisible_icon(
  410. 'forum',
  411. $forum['forum_id'],
  412. $forum['visibility'],
  413. ['forumcategory' => $_GET['forumcategory']]
  414. );
  415. $html .= return_lock_unlock_icon(
  416. 'forum',
  417. $forum['forum_id'],
  418. $forum['locked'],
  419. ['forumcategory' => $_GET['forumcategory']]
  420. );
  421. $html .= return_up_down_icon(
  422. 'forum',
  423. $forum['forum_id'],
  424. $forums_in_category
  425. );
  426. }
  427. $iconnotify = 'notification_mail_na.png';
  428. if (is_array(isset($_SESSION['forum_notification']['forum']) ? $_SESSION['forum_notification']['forum'] : null)) {
  429. if (in_array($forum['forum_id'], $_SESSION['forum_notification']['forum'])) {
  430. $iconnotify = 'notification_mail.png';
  431. }
  432. }
  433. if (!api_is_anonymous() && $hideNotifications == false) {
  434. $html .= '<a href="'.$url.'?'.api_get_cidreq().'&forumcategory='.$forumCategoryId.'&action=notify&content=forum&id='.$forum['forum_id'].'">'.
  435. Display::return_icon($iconnotify, get_lang('NotifyMe')).
  436. '</a>';
  437. }
  438. $html .= '</div>';
  439. $html .= '</div>';
  440. $html .= '</div>';
  441. $html .= '</div>';
  442. $html .= '</div></div>';
  443. }
  444. echo $html;
  445. }
  446. }
  447. }
  448. if (count($forum_list) == 0) {
  449. echo '<div class="alert alert-warning">'.get_lang('NoForumInThisCategory').'</div>';
  450. }
  451. echo '</div>';
  452. }
  453. /* FOOTER */
  454. if ($origin != 'learnpath') {
  455. Display::display_footer();
  456. }