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