blog.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * BLOG HOMEPAGE
  5. * This file takes care of all blog navigation and displaying.
  6. *
  7. * @package chamilo.blogs
  8. */
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $blog_id = isset($_GET['blog_id']) ? $_GET['blog_id'] : 0;
  11. if (empty($blog_id)) {
  12. api_not_allowed(true);
  13. }
  14. $this_section = SECTION_COURSES;
  15. $current_course_tool = TOOL_BLOGS;
  16. /* ACCESS RIGHTS */
  17. // notice for unauthorized people.
  18. api_protect_course_script(true);
  19. $lib_path = api_get_path(LIBRARY_PATH);
  20. $blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  21. $nameTools = get_lang('Blogs');
  22. $DaysShort = api_get_week_days_short();
  23. $DaysLong = api_get_week_days_long();
  24. $MonthsLong = api_get_months_long();
  25. $action = isset($_GET['action']) ? $_GET['action'] : null;
  26. /*
  27. PROCESSING
  28. */
  29. $safe_post_file_comment = isset($_POST['post_file_comment']) ? Security::remove_XSS($_POST['post_file_comment']) : null;
  30. $safe_comment_text = isset($_POST['comment_text']) ? Security::remove_XSS($_POST['comment_text']) : null;
  31. $safe_comment_title = isset($_POST['comment_title']) ? Security::remove_XSS($_POST['comment_title']) : null;
  32. $safe_task_name = isset($_POST['task_name']) ? Security::remove_XSS($_POST['task_name']) : null;
  33. $safe_task_description = isset($_POST['task_description']) ? Security::remove_XSS($_POST['task_description']) : null;
  34. if (!empty($_POST['edit_post_submit'])) {
  35. Blog::editPost(
  36. $_POST['post_id'],
  37. $_POST['title'],
  38. $_POST['full_text'],
  39. $blog_id
  40. );
  41. Display::addFlash(
  42. Display::return_message(get_lang('BlogEdited'), 'success')
  43. );
  44. }
  45. if (!empty($_POST['new_task_submit'])) {
  46. Blog::addTask(
  47. $blog_id,
  48. $safe_task_name,
  49. $safe_task_description,
  50. (isset($_POST['chkArticleDelete']) ? $_POST['chkArticleDelete'] : null),
  51. (isset($_POST['chkArticleEdit']) ? $_POST['chkArticleEdit'] : null),
  52. (isset($_POST['chkCommentsDelete']) ? $_POST['chkCommentsDelete'] : null),
  53. (isset($_POST['task_color']) ? $_POST['task_color'] : null)
  54. );
  55. Display::addFlash(
  56. Display::return_message(get_lang('TaskCreated'), 'success')
  57. );
  58. }
  59. if (isset($_POST['edit_task_submit'])) {
  60. Blog::editTask(
  61. $_POST['blog_id'],
  62. $_POST['task_id'],
  63. $safe_task_name,
  64. $safe_task_description,
  65. $_POST['chkArticleDelete'],
  66. $_POST['chkArticleEdit'],
  67. $_POST['chkCommentsDelete'],
  68. $_POST['task_color']
  69. );
  70. Display::addFlash(
  71. Display::return_message(get_lang('TaskEdited'), 'success')
  72. );
  73. }
  74. if (!empty($_POST['assign_task_submit'])) {
  75. Blog::assignTask(
  76. $blog_id,
  77. $_POST['task_user_id'],
  78. $_POST['task_task_id'],
  79. $_POST['task_day']
  80. );
  81. Display::addFlash(
  82. Display::return_message(get_lang('TaskAssigned'), 'success')
  83. );
  84. }
  85. if (isset($_POST['assign_task_edit_submit'])) {
  86. Blog::updateAssignedTask(
  87. $blog_id,
  88. $_POST['task_user_id'],
  89. $_POST['task_task_id'],
  90. $_POST['task_day'],
  91. $_POST['old_user_id'],
  92. $_POST['old_task_id'],
  93. $_POST['old_target_date']
  94. );
  95. Display::addFlash(
  96. Display::return_message(get_lang('AssignedTaskEdited'), 'success')
  97. );
  98. }
  99. if (!empty($_POST['register'])) {
  100. if (isset($_POST['user']) && is_array($_POST['user'])) {
  101. foreach ($_POST['user'] as $index => $user_id) {
  102. Blog::subscribeUser((int) $_GET['blog_id'], $user_id);
  103. }
  104. }
  105. }
  106. if (!empty($_POST['unregister'])) {
  107. if (isset($_POST['user']) && is_array($_POST['user'])) {
  108. foreach ($_POST['user'] as $index => $user_id) {
  109. Blog::unsubscribeUser($_GET['blog_id'], $user_id);
  110. }
  111. }
  112. }
  113. if (!empty($_GET['register'])) {
  114. Blog::subscribeUser((int) $_GET['blog_id'], (int) $_GET['user_id']);
  115. Display::addFlash(
  116. Display::return_message(get_lang('UserRegistered'), 'success')
  117. );
  118. $flag = 1;
  119. }
  120. if (!empty($_GET['unregister'])) {
  121. Blog::unsubscribeUser($_GET['blog_id'], $_GET['user_id']);
  122. }
  123. if (isset($_GET['action']) && $_GET['action'] == 'manage_tasks') {
  124. if (isset($_GET['do']) && $_GET['do'] == 'delete') {
  125. Blog::deleteTask($blog_id, (int) $_GET['task_id']);
  126. Display::addFlash(
  127. Display::return_message(get_lang('TaskDeleted'), 'success')
  128. );
  129. }
  130. if (isset($_GET['do']) && $_GET['do'] == 'delete_assignment') {
  131. Blog::deleteAssignedTask($blog_id, intval($_GET['task_id']), intval($_GET['user_id']));
  132. Display::addFlash(
  133. Display::return_message(get_lang('TaskAssignmentDeleted'), 'success')
  134. );
  135. }
  136. }
  137. if (isset($_GET['action']) && $_GET['action'] == 'view_post') {
  138. $task_id = (isset($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  139. if (isset($_GET['do']) && $_GET['do'] == 'delete_comment') {
  140. if (api_is_allowed('BLOG_'.$blog_id, 'article_comments_delete', $task_id)) {
  141. Blog::deleteComment($blog_id, (int) $_GET['post_id'], (int) $_GET['comment_id']);
  142. Display::addFlash(
  143. Display::return_message(get_lang('CommentDeleted'), 'success')
  144. );
  145. } else {
  146. Display::addFlash(
  147. Display::return_message(get_lang('ActionNotAllowed'), 'error')
  148. );
  149. }
  150. }
  151. if (isset($_GET['do']) && $_GET['do'] == 'delete_article') {
  152. if (api_is_allowed('BLOG_'.$blog_id, 'article_delete', $task_id)) {
  153. Blog::deletePost($blog_id, (int) $_GET['article_id']);
  154. $action = ''; // Article is gone, go to blog home
  155. Display::addFlash(
  156. Display::return_message(get_lang('BlogDeleted'), 'success')
  157. );
  158. } else {
  159. Display::addFlash(
  160. Display::return_message(get_lang('ActionNotAllowed'), 'error')
  161. );
  162. }
  163. }
  164. if (isset($_GET['do']) && $_GET['do'] == 'rate') {
  165. if (isset($_GET['type']) && $_GET['type'] == 'post') {
  166. if (api_is_allowed('BLOG_'.$blog_id, 'article_rate')) {
  167. Blog::addRating('post', $blog_id, (int) $_GET['post_id'], (int) $_GET['rating']);
  168. Display::addFlash(
  169. Display::return_message(get_lang('RatingAdded'), 'success')
  170. );
  171. }
  172. }
  173. if (isset($_GET['type']) && $_GET['type'] == 'comment') {
  174. if (api_is_allowed('BLOG_'.$blog_id, 'article_comments_add')) {
  175. Blog::addRating('comment', $blog_id, (int) $_GET['comment_id'], (int) $_GET['rating']);
  176. Display::addFlash(
  177. Display::return_message(get_lang('RatingAdded'), 'success')
  178. );
  179. }
  180. }
  181. }
  182. }
  183. /*
  184. DISPLAY
  185. */
  186. // Set breadcrumb
  187. switch ($action) {
  188. case 'new_post':
  189. $nameTools = get_lang('NewPost');
  190. break;
  191. case 'view_post':
  192. $nameTools = '';
  193. break;
  194. case 'manage_tasks':
  195. $nameTools = get_lang('TaskManager');
  196. break;
  197. case 'manage_members':
  198. $nameTools = get_lang('MemberManager');
  199. break;
  200. case 'manage_rights':
  201. $nameTools = get_lang('RightsManager');
  202. break;
  203. case 'view_search_result':
  204. $nameTools = get_lang('SearchResults');
  205. break;
  206. case 'execute_task':
  207. $nameTools = get_lang('ExecuteThisTask');
  208. break;
  209. default:
  210. $nameTools = Blog::getBlogTitle($blog_id);
  211. }
  212. $interbreadcrumb[] = [
  213. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  214. 'name' => Blog::getBlogTitle($blog_id),
  215. ];
  216. $actionsLeft = Display::url(
  217. Display::return_icon('blog.png', get_lang('Home'), '', ICON_SIZE_MEDIUM),
  218. api_get_self().'?blog_id='.$blog_id.'&'.api_get_cidreq()
  219. );
  220. if (api_is_allowed('BLOG_'.$blog_id, 'article_add')) {
  221. $actionsLeft .= Display::url(
  222. Display::return_icon('new_article.png', get_lang('NewPost'), '', ICON_SIZE_MEDIUM),
  223. api_get_self().'?action=new_post&blog_id='.$blog_id.'&'.api_get_cidreq()
  224. );
  225. }
  226. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  227. $actionsLeft .= Display::url(
  228. Display::return_icon('blog_tasks.png', get_lang('TaskManager'), '', ICON_SIZE_MEDIUM),
  229. api_get_self().'?action=manage_tasks&blog_id='.$blog_id.'&'.api_get_cidreq()
  230. );
  231. }
  232. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  233. $actionsLeft .= Display::url(
  234. Display::return_icon('blog_admin_users.png', get_lang('MemberManager'), '', ICON_SIZE_MEDIUM),
  235. api_get_self().'?action=manage_members&blog_id='.$blog_id.'&'.api_get_cidreq()
  236. );
  237. }
  238. $titleBlog = Blog::getBlogTitle($blog_id);
  239. $descriptionBlog = Blog::getBlogSubtitle($blog_id);
  240. $idBlog = $blog_id;
  241. $searchBlog = isset($_GET['q']) ? Security::remove_XSS($_GET['q']) : '';
  242. //calendar blog
  243. $month = isset($_GET['month']) ? (int) $_GET['month'] : (int) date('m');
  244. $year = isset($_GET['year']) ? (int) $_GET['year'] : date('Y');
  245. $calendarBlog = Blog::displayMiniMonthCalendar($month, $year, $blog_id);
  246. //task blogs
  247. $taskBlog = Blog::getPersonalTasksList();
  248. if (isset($flag) && $flag == '1') {
  249. $action = "manage_tasks";
  250. Blog::displayTaskAssignmentForm($blog_id);
  251. }
  252. $user_task = false;
  253. $course_id = api_get_course_int_id();
  254. if (isset($_GET['task_id']) && is_numeric($_GET['task_id'])) {
  255. $task_id = (int) $_GET['task_id'];
  256. } else {
  257. $task_id = 0;
  258. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  259. $sql = "SELECT COUNT(*) as number
  260. FROM ".$tbl_blogs_tasks_rel_user."
  261. WHERE
  262. c_id = $course_id AND
  263. blog_id = ".$blog_id." AND
  264. user_id = ".api_get_user_id()." AND
  265. task_id = ".$task_id;
  266. $result = Database::query($sql);
  267. $row = Database::fetch_array($result);
  268. if ($row['number'] == 1) {
  269. $user_task = true;
  270. }
  271. }
  272. $tpl = new Template($nameTools);
  273. $tpl->setHelp('Blogs');
  274. $tpl->assign('title', $titleBlog);
  275. $tpl->assign('description', $descriptionBlog);
  276. $tpl->assign('id_blog', $idBlog);
  277. $tpl->assign('calendar', $calendarBlog);
  278. $tpl->assign('search', $searchBlog);
  279. $tpl->assign('task', $taskBlog);
  280. $blogLayout = null;
  281. switch ($action) {
  282. case 'new_post':
  283. $formAdd = '';
  284. if (api_is_allowed('BLOG_'.$blog_id, 'article_add', $user_task ? $task_id : 0)) {
  285. $formAdd = Blog::displayPostCreateForm($blog_id);
  286. $tpl->assign('content', $formAdd);
  287. $blogLayout = $tpl->get_template('blog/layout.tpl');
  288. } else {
  289. api_not_allowed();
  290. }
  291. break;
  292. case 'view_post':
  293. $postArticle = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  294. $tpl->assign('post', $postArticle);
  295. $blogLayout = $tpl->get_template('blog/post.tpl');
  296. break;
  297. case 'edit_post':
  298. $task_id = (isset($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  299. if (api_is_allowed('BLOG_'.$blog_id, 'article_edit', $task_id)) {
  300. // we show the form if
  301. // 1. no post data
  302. // 2. there is post data and the required field is empty
  303. if (!$_POST || (!empty($_POST) && empty($_POST['post_title']))) {
  304. // if there is post data there is certainly an error in the form
  305. $formEdit = Blog::displayPostEditForm($blog_id, intval($_GET['post_id']));
  306. $tpl->assign('content', $formEdit);
  307. $blogLayout = $tpl->get_template('blog/layout.tpl');
  308. if ($_POST) {
  309. $post = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  310. $tpl->assign('post', $post);
  311. $blogLayout = $tpl->get_template('blog/post.tpl');
  312. }
  313. }
  314. } else {
  315. api_not_allowed();
  316. }
  317. break;
  318. case 'manage_members':
  319. $manage = null;
  320. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  321. $manage .= Blog::displayUserSubscriptionForm($blog_id);
  322. $manage .= Blog::displayUserUnsubscriptionForm($blog_id);
  323. } else {
  324. api_not_allowed();
  325. }
  326. $tpl->assign('content', $manage);
  327. $blogLayout = $tpl->get_template('blog/layout.tpl');
  328. break;
  329. case 'manage_rights':
  330. $manage = Blog::displayUserRightsForm($blog_id);
  331. $tpl->assign('content', $manage);
  332. $blogLayout = $tpl->get_template('blog/layout.tpl');
  333. break;
  334. case 'manage_tasks':
  335. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  336. $task = null;
  337. if (isset($_GET['do']) && $_GET['do'] == 'add') {
  338. $task .= Blog::displayTaskCreateForm($blog_id);
  339. }
  340. if (isset($_GET['do']) && $_GET['do'] == 'assign') {
  341. $task .= Blog::displayTaskAssignmentForm($blog_id);
  342. }
  343. if (isset($_GET['do']) && $_GET['do'] == 'edit') {
  344. $task .= Blog::displayTaskEditForm(
  345. $blog_id,
  346. intval($_GET['task_id'])
  347. );
  348. }
  349. if (isset($_GET['do']) && $_GET['do'] == 'edit_assignment') {
  350. $task .= Blog::displayAssignedTaskEditForm(
  351. $blog_id,
  352. intval($_GET['task_id']),
  353. intval($_GET['user_id'])
  354. );
  355. }
  356. $task .= Blog::displayTasksList($blog_id);
  357. $task .= Blog::displayAssignedTasksList($blog_id);
  358. $tpl->assign('content', $task);
  359. $blogLayout = $tpl->get_template('blog/layout.tpl');
  360. } else {
  361. api_not_allowed();
  362. }
  363. break;
  364. case 'execute_task':
  365. if (isset($_GET['post_id'])) {
  366. $post = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  367. $tpl->assign('post', $post);
  368. $blogLayout = $tpl->get_template('blog/post.tpl');
  369. } else {
  370. $taskPost = Blog::displayPostSelectionForTask($blog_id, intval($_GET['task_id']));
  371. $tpl->assign('content', $taskPost);
  372. $blogLayout = $tpl->get_template('blog/layout.tpl');
  373. }
  374. break;
  375. case 'view_search_result':
  376. $listArticles = Blog::getSearchResults($blog_id, Database::escape_string($_GET['q']));
  377. $titleSearch = get_lang('SearchResults');
  378. $tpl->assign('search', $titleSearch);
  379. $tpl->assign('articles', $listArticles);
  380. $blogLayout = $tpl->get_template('blog/blog.tpl');
  381. break;
  382. case '':
  383. default:
  384. if (isset($_GET['filter']) && !empty($_GET['filter'])) {
  385. $listArticles = Blog::getDailyResults($blog_id, Database::escape_string($_GET['filter']));
  386. $dateSearch = api_format_date($_GET['filter'], DATE_FORMAT_LONG);
  387. $titleSearch = get_lang('PostsOf').' '.$dateSearch;
  388. $tpl->assign('search', $titleSearch);
  389. $tpl->assign('articles', $listArticles);
  390. $blogLayout = $tpl->get_template('blog/blog.tpl');
  391. } else {
  392. $listArticles = Blog::getPosts($blog_id);
  393. $tpl->assign('articles', $listArticles);
  394. $blogLayout = $tpl->get_template('blog/blog.tpl');
  395. }
  396. break;
  397. }
  398. $content = $tpl->fetch($blogLayout);
  399. if ($actionsLeft) {
  400. $tpl->assign(
  401. 'actions',
  402. Display::return_introduction_section(TOOL_BLOGS)
  403. .Display::toolbarAction('toolbar', [$actionsLeft])
  404. );
  405. }
  406. $tpl->assign('content', $content);
  407. $tpl->display_one_col_template();