blog.php 15 KB

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