blog.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 '../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. $interbreadcrumb[] = array(
  190. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  191. "name" => Blog::getBlogTitle($blog_id),
  192. );
  193. break;
  194. case 'view_post' :
  195. $nameTools = '';
  196. $interbreadcrumb[] = array(
  197. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  198. "name" => Blog::getBlogTitle($blog_id),
  199. );
  200. break;
  201. case 'manage_tasks' :
  202. $nameTools = get_lang('TaskManager');
  203. $interbreadcrumb[] = array(
  204. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  205. "name" => Blog::getBlogTitle($blog_id),
  206. );
  207. break;
  208. case 'manage_members' :
  209. $nameTools = get_lang('MemberManager');
  210. $interbreadcrumb[] = array(
  211. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  212. "name" => Blog::getBlogTitle($blog_id),
  213. );
  214. break;
  215. case 'manage_rights' :
  216. $nameTools = get_lang('RightsManager');
  217. $interbreadcrumb[] = array(
  218. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  219. 'name' => Blog::getBlogTitle($blog_id),
  220. );
  221. break;
  222. case 'view_search_result' :
  223. $nameTools = get_lang('SearchResults');
  224. $interbreadcrumb[] = array(
  225. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  226. 'name' => Blog::getBlogTitle($blog_id),
  227. );
  228. break;
  229. case 'execute_task' :
  230. $nameTools = get_lang('ExecuteThisTask');
  231. $interbreadcrumb[] = array(
  232. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  233. 'name' => Blog::getBlogTitle($blog_id),
  234. );
  235. break;
  236. default :
  237. $nameTools = Blog::getBlogTitle($blog_id);
  238. }
  239. $actionsLeft = [];
  240. $actionsLeft[] = Display::url(
  241. Display::return_icon('blog.png', get_lang('Home'), '', ICON_SIZE_MEDIUM),
  242. api_get_self().'?blog_id='.$blog_id.'&'.api_get_cidreq()
  243. );
  244. if (api_is_allowed('BLOG_'.$blog_id, 'article_add')) {
  245. $actionsLeft[] = Display::url(
  246. Display::return_icon('new_article.png', get_lang('NewPost'), '', ICON_SIZE_MEDIUM),
  247. api_get_self().'?action=new_post&amp;blog_id='.$blog_id
  248. );
  249. }
  250. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  251. $actionsLeft[] = Display::url(
  252. Display::return_icon('blog_tasks.png', get_lang('TaskManager'), '', ICON_SIZE_MEDIUM),
  253. api_get_self().'?action=manage_tasks&amp;blog_id='.$blog_id
  254. );
  255. }
  256. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  257. $actionsLeft[] = Display::url(
  258. Display::return_icon('blog_admin_users.png', get_lang('MemberManager'), '', ICON_SIZE_MEDIUM),
  259. api_get_self().'?action=manage_members&amp;blog_id='.$blog_id
  260. );
  261. }
  262. $titleBlog = Blog::getBlogTitle($blog_id);
  263. $descriptionBlog = Blog::getBlogSubtitle($blog_id);
  264. $idBlog = $blog_id;
  265. $searchBlog = isset($_GET['q']) ? Security::remove_XSS($_GET['q']) : '';
  266. //calendar blog
  267. $month = isset($_GET['month']) ? (int) $_GET['month'] : (int) date('m');
  268. $year = isset($_GET['year']) ? (int) $_GET['year'] : date('Y');
  269. $calendarBlog = Blog::displayMiniMonthCalendar($month, $year, $blog_id);
  270. //task blogs
  271. $taskBlog = Blog::getPersonalTasksList();
  272. if (isset($flag) && $flag == '1') {
  273. $action = "manage_tasks";
  274. Blog::displayTaskAssignmentForm($blog_id);
  275. }
  276. $user_task = false;
  277. $course_id = api_get_course_int_id();
  278. if (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) {
  279. $task_id = (int) $_GET['task_id'];
  280. } else {
  281. $task_id = 0;
  282. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  283. $sql = "SELECT COUNT(*) as number
  284. FROM ".$tbl_blogs_tasks_rel_user."
  285. WHERE
  286. c_id = $course_id AND
  287. blog_id = ".$blog_id." AND
  288. user_id = ".api_get_user_id()." AND
  289. task_id = ".$task_id;
  290. $result = Database::query($sql);
  291. $row = Database::fetch_array($result);
  292. if ($row['number'] == 1) {
  293. $user_task = true;
  294. }
  295. }
  296. $tpl = new Template($nameTools);
  297. $tpl->setHelp('Blogs');
  298. $tpl->assign('title', $titleBlog);
  299. $tpl->assign('description', $descriptionBlog);
  300. $tpl->assign('id_blog', $idBlog);
  301. $tpl->assign('calendar', $calendarBlog);
  302. $tpl->assign('search', $searchBlog);
  303. $tpl->assign('task', $taskBlog);
  304. $blogLayout = null;
  305. switch ($action) {
  306. case 'new_post':
  307. if (api_is_allowed('BLOG_'.$blog_id, 'article_add', $user_task ? $task_id : 0)) {
  308. $formAdd = Blog::displayPostCreateForm($blog_id);
  309. $tpl->assign('content', $formAdd);
  310. $blogLayout = $tpl->get_template('blog/layout.tpl');
  311. } else {
  312. api_not_allowed();
  313. }
  314. break;
  315. case 'view_post' :
  316. $postArticle = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  317. $tpl->assign('post', $postArticle);
  318. $blogLayout = $tpl->get_template('blog/post.tpl');
  319. break;
  320. case 'edit_post' :
  321. $task_id = (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  322. if (api_is_allowed('BLOG_'.$blog_id, 'article_edit', $task_id)) {
  323. // we show the form if
  324. // 1. no post data
  325. // 2. there is post data and the required field is empty
  326. if (!$_POST OR (!empty($_POST) AND empty($_POST['post_title']))) {
  327. // if there is post data there is certainly an error in the form
  328. $formEdit = Blog::displayPostEditForm($blog_id, intval($_GET['post_id']));
  329. $tpl->assign('content', $formEdit);
  330. $blogLayout = $tpl->get_template('blog/layout.tpl');
  331. if ($_POST) {
  332. $post = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  333. $tpl->assign('post', $post);
  334. $blogLayout = $tpl->get_template('blog/post.tpl');
  335. }
  336. }
  337. } else {
  338. api_not_allowed();
  339. }
  340. break;
  341. case 'manage_members' :
  342. $manage = null;
  343. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  344. $manage .= Blog::displayUserSubscriptionForm($blog_id);
  345. $manage .= Blog::displayUserUnsubscriptionForm($blog_id);
  346. } else {
  347. api_not_allowed();
  348. }
  349. $tpl->assign('content', $manage);
  350. $blogLayout = $tpl->get_template('blog/layout.tpl');
  351. break;
  352. case 'manage_rights' :
  353. $manage = Blog::displayUserRightsForm($blog_id);
  354. $tpl->assign('content', $manage);
  355. $blogLayout = $tpl->get_template('blog/layout.tpl');
  356. break;
  357. case 'manage_tasks' :
  358. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  359. $task = null;
  360. if (isset($_GET['do']) && $_GET['do'] == 'add') {
  361. $task .= Blog::displayTaskCreateForm($blog_id);
  362. }
  363. if (isset($_GET['do']) && $_GET['do'] == 'assign') {
  364. $task .= Blog::displayTaskAssignmentForm($blog_id);
  365. }
  366. if (isset($_GET['do']) && $_GET['do'] == 'edit') {
  367. $task .= Blog::displayTaskEditForm(
  368. $blog_id,
  369. intval($_GET['task_id'])
  370. );
  371. }
  372. if (isset($_GET['do']) && $_GET['do'] == 'edit_assignment') {
  373. $taks .= Blog::displayAssignedTaskEditForm(
  374. $blog_id,
  375. intval($_GET['task_id']),
  376. intval($_GET['user_id'])
  377. );
  378. }
  379. $task .= Blog::displayTasksList($blog_id);
  380. $task .= Blog::displayAssignedTasksList($blog_id);
  381. $tpl->assign('content', $task);
  382. $blogLayout = $tpl->get_template('blog/layout.tpl');
  383. } else {
  384. api_not_allowed();
  385. }
  386. break;
  387. case 'execute_task' :
  388. if (isset ($_GET['post_id'])) {
  389. $post = Blog::getSinglePost($blog_id, intval($_GET['post_id']));
  390. $tpl->assign('post', $post);
  391. $blogLayout = $tpl->get_template('blog/post.tpl');
  392. } else {
  393. $taskPost = Blog::displayPostSelectionForTask($blog_id, intval($_GET['task_id']));
  394. $tpl->assign('content', $taskPost);
  395. $blogLayout = $tpl->get_template('blog/layout.tpl');
  396. }
  397. break;
  398. case 'view_search_result' :
  399. $listArticles = Blog::getSearchResults($blog_id, Database::escape_string($_GET['q']));
  400. $titleSearch = get_lang('SearchResults');
  401. $tpl->assign('search', $titleSearch);
  402. $tpl->assign('articles', $listArticles);
  403. $blogLayout = $tpl->get_template('blog/blog.tpl');
  404. break;
  405. case '':
  406. default:
  407. if (isset ($_GET['filter']) && !empty ($_GET['filter'])) {
  408. $listArticles = Blog::getDailyResults($blog_id, Database::escape_string($_GET['filter']));
  409. $dateSearch = api_format_date($_GET['filter'], DATE_FORMAT_LONG);
  410. $titleSearch = get_lang('PostsOf').' '.$dateSearch;
  411. $tpl->assign('search', $titleSearch);
  412. $tpl->assign('articles', $listArticles);
  413. $blogLayout = $tpl->get_template('blog/blog.tpl');
  414. } else {
  415. $listArticles = Blog::getPosts($blog_id);
  416. $tpl->assign('articles', $listArticles);
  417. $blogLayout = $tpl->get_template('blog/blog.tpl');
  418. }
  419. break;
  420. }
  421. $content = Display::return_introduction_section(TOOL_BLOGS);
  422. $content .= $tpl->fetch($blogLayout);
  423. $tpl->assign('actions', implode(PHP_EOL, $actionsLeft));
  424. $tpl->assign('content', $content);
  425. $tpl->display_one_col_template();