blog.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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(stripslashes(api_html_entity_decode($_POST['comment_text'])), COURSEMANAGERLOWSECURITY) : 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['new_post_submit'])) {
  34. Blog:: create_post(
  35. $_POST['title'],
  36. $_POST['full_text'],
  37. $_POST['post_file_comment'],
  38. $blog_id
  39. );
  40. $return_message = array('type' => 'confirmation', 'message' => get_lang('BlogAdded'));
  41. }
  42. if (!empty($_POST['edit_post_submit'])) {
  43. Blog:: edit_post(
  44. $_POST['post_id'],
  45. $_POST['title'],
  46. $_POST['full_text'],
  47. $blog_id
  48. );
  49. $return_message = array('type' => 'confirmation', 'message' => get_lang('BlogEdited'));
  50. }
  51. if (!empty($_POST['new_comment_submit'])) {
  52. Blog:: create_comment(
  53. $_POST['title'],
  54. $_POST['comment'],
  55. $_POST['post_file_comment'],
  56. $blog_id,
  57. $_GET['post_id'],
  58. $_POST['comment_parent_id']
  59. );
  60. $return_message = array('type' => 'confirmation', 'message' => get_lang('CommentAdded'));
  61. }
  62. if (!empty($_POST['new_task_submit'])) {
  63. Blog:: create_task(
  64. $blog_id,
  65. $safe_task_name,
  66. $safe_task_description,
  67. $_POST['chkArticleDelete'],
  68. $_POST['chkArticleEdit'],
  69. $_POST['chkCommentsDelete'],
  70. $_POST['task_color']
  71. );
  72. $return_message = array('type' => 'confirmation', 'message' => get_lang('TaskCreated'));
  73. }
  74. if (isset($_POST['edit_task_submit'])) {
  75. Blog:: edit_task(
  76. $_POST['blog_id'],
  77. $_POST['task_id'],
  78. $safe_task_name,
  79. $safe_task_description,
  80. $_POST['chkArticleDelete'],
  81. $_POST['chkArticleEdit'],
  82. $_POST['chkCommentsDelete'],
  83. $_POST['task_color']
  84. );
  85. $return_message = array(
  86. 'type' => 'confirmation',
  87. 'message' => get_lang('TaskEdited')
  88. );
  89. }
  90. if (!empty($_POST['assign_task_submit'])) {
  91. Blog:: assign_task(
  92. $blog_id,
  93. $_POST['task_user_id'],
  94. $_POST['task_task_id'],
  95. $_POST['task_day']
  96. );
  97. $return_message = array(
  98. 'type' => 'confirmation',
  99. 'message' => get_lang('TaskAssigned')
  100. );
  101. }
  102. if (isset($_POST['assign_task_edit_submit'])) {
  103. Blog:: edit_assigned_task(
  104. $blog_id,
  105. $_POST['task_user_id'],
  106. $_POST['task_task_id'],
  107. $_POST['task_day'],
  108. $_POST['old_user_id'],
  109. $_POST['old_task_id'],
  110. $_POST['old_target_date']
  111. );
  112. $return_message = array(
  113. 'type' => 'confirmation',
  114. 'message' => get_lang('AssignedTaskEdited')
  115. );
  116. }
  117. if (!empty($_POST['new_task_execution_submit'])) {
  118. Blog:: create_comment(
  119. $safe_comment_title,
  120. $safe_comment_text,
  121. $blog_id,
  122. (int)$_GET['post_id'],
  123. $_POST['comment_parent_id'],
  124. $_POST['task_id']
  125. );
  126. $return_message = array(
  127. 'type' => 'confirmation',
  128. 'message' => get_lang('CommentCreated')
  129. );
  130. }
  131. if (!empty($_POST['register'])) {
  132. if (is_array($_POST['user'])) {
  133. foreach ($_POST['user'] as $index => $user_id) {
  134. Blog :: set_user_subscribed((int)$_GET['blog_id'], $user_id);
  135. }
  136. }
  137. }
  138. if (!empty($_POST['unregister'])) {
  139. if (is_array($_POST['user'])) {
  140. foreach ($_POST['user'] as $index => $user_id) {
  141. Blog :: set_user_unsubscribed((int)$_GET['blog_id'], $user_id);
  142. }
  143. }
  144. }
  145. if (!empty($_GET['register'])) {
  146. Blog :: set_user_subscribed((int)$_GET['blog_id'], (int)$_GET['user_id']);
  147. $return_message = array('type' => 'confirmation', 'message' => get_lang('UserRegistered'));
  148. $flag = 1;
  149. }
  150. if (!empty($_GET['unregister'])) {
  151. Blog :: set_user_unsubscribed((int)$_GET['blog_id'], (int)$_GET['user_id']);
  152. }
  153. if (isset($_GET['action']) && $_GET['action'] == 'manage_tasks') {
  154. if (isset($_GET['do']) && $_GET['do'] == 'delete') {
  155. Blog :: delete_task($blog_id, (int)$_GET['task_id']);
  156. $return_message = array('type' => 'confirmation', 'message' => get_lang('TaskDeleted'));
  157. }
  158. if (isset($_GET['do']) && $_GET['do'] == 'delete_assignment') {
  159. Blog :: delete_assigned_task($blog_id, intval($_GET['task_id']), intval($_GET['user_id']));
  160. $return_message = array('type' => 'confirmation', 'message' => get_lang('TaskAssignmentDeleted'));
  161. }
  162. }
  163. if (isset($_GET['action']) && $_GET['action'] == 'view_post') {
  164. $task_id = (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  165. if (isset($_GET['do']) && $_GET['do'] == 'delete_comment') {
  166. if (api_is_allowed('BLOG_'.$blog_id, 'article_comments_delete', $task_id)) {
  167. Blog :: delete_comment($blog_id, (int)$_GET['post_id'],(int)$_GET['comment_id']);
  168. $return_message = array('type' => 'confirmation', 'message' => get_lang('CommentDeleted'));
  169. } else {
  170. $error = true;
  171. $message = get_lang('ActionNotAllowed');
  172. }
  173. }
  174. if (isset($_GET['do']) && $_GET['do'] == 'delete_article') {
  175. if (api_is_allowed('BLOG_'.$blog_id, 'article_delete', $task_id)) {
  176. Blog :: delete_post($blog_id, (int)$_GET['article_id']);
  177. $action = ''; // Article is gone, go to blog home
  178. $return_message = array('type' => 'confirmation', 'message' => get_lang('BlogDeleted'));
  179. } else {
  180. $error = true;
  181. $message = get_lang('ActionNotAllowed');
  182. }
  183. }
  184. if (isset($_GET['do']) && $_GET['do'] == 'rate') {
  185. if (isset($_GET['type']) && $_GET['type'] == 'post') {
  186. if (api_is_allowed('BLOG_'.$blog_id, 'article_rate')) {
  187. Blog :: add_rating('post', $blog_id, (int)$_GET['post_id'], (int)$_GET['rating']);
  188. $return_message = array('type' => 'confirmation', 'message' => get_lang('RatingAdded'));
  189. }
  190. }
  191. if (isset($_GET['type']) && $_GET['type'] == 'comment') {
  192. if (api_is_allowed('BLOG_'.$blog_id, 'article_comments_add')) {
  193. Blog :: add_rating('comment', $blog_id, (int)$_GET['comment_id'], (int)$_GET['rating']);
  194. $return_message = array('type' => 'confirmation', 'message' => get_lang('RatingAdded'));
  195. }
  196. }
  197. }
  198. }
  199. /*
  200. DISPLAY
  201. */
  202. // Set breadcrumb
  203. switch ($action) {
  204. case 'new_post' :
  205. $nameTools = get_lang('NewPost');
  206. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", "name" => Blog :: get_blog_title($blog_id));
  207. Display :: display_header($nameTools, 'Blogs');
  208. break;
  209. case 'manage_tasks' :
  210. $nameTools = get_lang('TaskManager');
  211. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", "name" => Blog :: get_blog_title($blog_id));
  212. Display :: display_header($nameTools, 'Blogs');
  213. break;
  214. case 'manage_members' :
  215. $nameTools = get_lang('MemberManager');
  216. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", "name" => Blog :: get_blog_title($blog_id));
  217. Display :: display_header($nameTools, 'Blogs');
  218. break;
  219. case 'manage_rights' :
  220. $nameTools = get_lang('RightsManager');
  221. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", 'name' => Blog :: get_blog_title($blog_id));
  222. Display :: display_header($nameTools, 'Blogs');
  223. break;
  224. case 'view_search_result' :
  225. $nameTools = get_lang('SearchResults');
  226. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", 'name' => Blog :: get_blog_title($blog_id));
  227. Display :: display_header($nameTools, 'Blogs');
  228. break;
  229. case 'execute_task' :
  230. $nameTools = get_lang('ExecuteThisTask');
  231. $interbreadcrumb[] = array ('url' => "blog.php?blog_id=$blog_id", 'name' => Blog :: get_blog_title($blog_id));
  232. Display :: display_header($nameTools, 'Blogs');
  233. break;
  234. default :
  235. $nameTools = Blog :: get_blog_title($blog_id);
  236. Display :: display_header($nameTools, 'Blogs');
  237. }
  238. // feedback messages
  239. if (!empty($return_message)) {
  240. if ($return_message['type'] == 'confirmation') {
  241. Display::display_confirmation_message($return_message['message']);
  242. }
  243. if ($return_message['type'] == 'error') {
  244. Display::display_error_message($return_message['message']);
  245. }
  246. }
  247. // actions
  248. echo '<div class=actions>';
  249. ?>
  250. <a href="<?php echo api_get_self(); ?>?blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('Home') ?>"><?php echo Display::return_icon('blog.png', get_lang('Home'),'',ICON_SIZE_MEDIUM); ?></a>
  251. <?php if(api_is_allowed('BLOG_'.$blog_id, 'article_add')) { ?><a href="<?php echo api_get_self(); ?>?action=new_post&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('NewPost') ?>"><?php echo Display::return_icon('new_article.png', get_lang('NewPost'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  252. <?php if(api_is_allowed('BLOG_'.$blog_id, 'task_management')) { ?><a href="<?php echo api_get_self(); ?>?action=manage_tasks&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('ManageTasks') ?>"><?php echo Display::return_icon('blog_tasks.png', get_lang('TaskManager'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  253. <?php if(api_is_allowed('BLOG_'.$blog_id, 'member_management')) { ?><a href="<?php echo api_get_self(); ?>?action=manage_members&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('ManageMembers') ?>"><?php echo Display::return_icon('blog_admin_users.png', get_lang('MemberManager'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  254. <?php
  255. echo '</div>';
  256. // Tool introduction
  257. Display::display_introduction_section(TOOL_BLOGS);
  258. ?>
  259. <div class="sectiontitle"><?php echo Blog::get_blog_title($blog_id); ?></div>
  260. <div class="sectioncomment"><?php echo Blog::get_blog_subtitle($blog_id); ?></div>
  261. <div class="row">
  262. <div class="col-md-3">
  263. <?php
  264. $month = isset($_GET['month']) ? (int)$_GET['month'] : (int) date('m');
  265. $year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
  266. Blog::display_minimonthcalendar($month, $year, $blog_id);
  267. ?>
  268. <br />
  269. <br />
  270. <table width="100%">
  271. <tr>
  272. <td class="sectiontitle"><?php echo get_lang('Search') ?></td>
  273. </tr>
  274. <tr>
  275. <td class="blog_menu">
  276. <form action="blog.php" method="get" enctype="multipart/form-data">
  277. <input type="hidden" name="blog_id" value="<?php echo $blog_id ?>" />
  278. <input type="hidden" name="action" value="view_search_result" />
  279. <input type="text" size="20" name="q" value="<?php echo isset($_GET['q']) ? Security::remove_XSS($_GET['q']) : ''; ?>" /><button class="btn btn-success" type="submit"><i class="fa fa-search"></i> <?php echo get_lang('Search'); ?></button>
  280. </form>
  281. </td>
  282. </tr>
  283. </table>
  284. <br />
  285. <table width="100%">
  286. <tr>
  287. <td class="sectiontitle"><?php echo get_lang('MyTasks') ?></td>
  288. </tr>
  289. <tr>
  290. <td class="blog_menu">
  291. <?php Blog::get_personal_task_list(); ?>
  292. </td>
  293. </tr>
  294. </table>
  295. </td>
  296. </div>
  297. <div class="col-md-9">
  298. <?php
  299. if (isset($error)) {
  300. Display :: display_error_message($message);
  301. }
  302. if (isset($flag) && $flag == '1') {
  303. $action = "manage_tasks";
  304. Blog :: display_assign_task_form($blog_id);
  305. }
  306. $user_task = false;
  307. $course_id = api_get_course_int_id();
  308. if (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) {
  309. $task_id = (int)$_GET['task_id'];
  310. } else {
  311. $task_id = 0;
  312. $tbl_blogs_tasks_rel_user = Database :: get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  313. $sql = "SELECT COUNT(*) as number
  314. FROM ".$tbl_blogs_tasks_rel_user."
  315. WHERE
  316. c_id = $course_id AND
  317. blog_id = ".$blog_id." AND
  318. user_id = ".api_get_user_id()." AND
  319. task_id = ".$task_id;
  320. $result = Database::query($sql);
  321. $row = Database::fetch_array($result);
  322. if ($row['number'] == 1)
  323. $user_task = true;
  324. }
  325. switch ($action) {
  326. case 'new_post':
  327. if (api_is_allowed('BLOG_'.$blog_id, 'article_add', $user_task ? $task_id : 0)) {
  328. // we show the form if
  329. // 1. no post data
  330. // 2. there is post data and the required field is empty
  331. if (!$_POST OR (!empty($_POST) AND empty($_POST['title']))) {
  332. // if there is post data there is certainly an error in the form
  333. if ($_POST) {
  334. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  335. }
  336. Blog :: display_form_new_post($blog_id);
  337. } else {
  338. if (isset($_GET['filter']) && !empty($_GET['filter'])) {
  339. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  340. } else {
  341. Blog :: display_blog_posts($blog_id);
  342. }
  343. }
  344. } else {
  345. api_not_allowed();
  346. }
  347. break;
  348. case 'view_post' :
  349. Blog :: display_post($blog_id, intval($_GET['post_id']));
  350. break;
  351. case 'edit_post' :
  352. $task_id = (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  353. if (api_is_allowed('BLOG_'.$blog_id, 'article_edit', $task_id)) {
  354. // we show the form if
  355. // 1. no post data
  356. // 2. there is post data and the required field is empty
  357. if (!$_POST OR (!empty($_POST) AND empty($_POST['post_title']))) {
  358. // if there is post data there is certainly an error in the form
  359. if ($_POST) {
  360. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  361. }
  362. Blog :: display_form_edit_post($blog_id, intval($_GET['post_id']));
  363. } else {
  364. if (isset ($_GET['filter']) && !empty ($_GET['filter'])) {
  365. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  366. } else {
  367. Blog :: display_blog_posts($blog_id);
  368. }
  369. }
  370. } else {
  371. api_not_allowed();
  372. }
  373. break;
  374. case 'manage_members' :
  375. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  376. Blog :: display_form_user_subscribe($blog_id);
  377. echo '<br /><br />';
  378. Blog :: display_form_user_unsubscribe($blog_id);
  379. } else {
  380. api_not_allowed();
  381. }
  382. break;
  383. case 'manage_rights' :
  384. Blog :: display_form_user_rights($blog_id);
  385. break;
  386. case 'manage_tasks' :
  387. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  388. if (isset($_GET['do']) && $_GET['do'] == 'add') {
  389. Blog:: display_new_task_form($blog_id);
  390. }
  391. if (isset($_GET['do']) && $_GET['do'] == 'assign') {
  392. Blog:: display_assign_task_form($blog_id);
  393. }
  394. if (isset($_GET['do']) && $_GET['do'] == 'edit') {
  395. Blog:: display_edit_task_form(
  396. $blog_id,
  397. intval($_GET['task_id'])
  398. );
  399. }
  400. if (isset($_GET['do']) && $_GET['do'] == 'edit_assignment') {
  401. Blog :: display_edit_assigned_task_form($blog_id, intval($_GET['task_id']), intval($_GET['user_id']));
  402. }
  403. Blog :: display_task_list($blog_id);
  404. echo '<br /><br />';
  405. Blog :: display_assigned_task_list($blog_id);
  406. echo '<br /><br />';
  407. }
  408. else
  409. api_not_allowed();
  410. break;
  411. case 'execute_task' :
  412. if (isset ($_GET['post_id']))
  413. Blog :: display_post($blog_id, intval($_GET['post_id']));
  414. else
  415. Blog :: display_select_task_post($blog_id, intval($_GET['task_id']));
  416. break;
  417. case 'view_search_result' :
  418. Blog :: display_search_results($blog_id, Database::escape_string($_GET['q']));
  419. break;
  420. case '' :
  421. default :
  422. if (isset ($_GET['filter']) && !empty ($_GET['filter'])) {
  423. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  424. } else {
  425. Blog :: display_blog_posts($blog_id);
  426. }
  427. }
  428. ?>
  429. </div>
  430. </div>
  431. <?php
  432. // Display the footer
  433. Display::display_footer();