blog.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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['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. (isset($_POST['chkArticleDelete']) ? $_POST['chkArticleDelete'] : null),
  68. (isset($_POST['chkArticleEdit']) ? $_POST['chkArticleEdit'] : null),
  69. (isset($_POST['chkCommentsDelete']) ? $_POST['chkCommentsDelete'] : null),
  70. (isset($_POST['task_color']) ? $_POST['task_color'] : null)
  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(
  207. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  208. "name" => Blog:: get_blog_title($blog_id),
  209. );
  210. Display :: display_header($nameTools, 'Blogs');
  211. break;
  212. case 'manage_tasks' :
  213. $nameTools = get_lang('TaskManager');
  214. $interbreadcrumb[] = array(
  215. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  216. "name" => Blog:: get_blog_title($blog_id),
  217. );
  218. Display :: display_header($nameTools, 'Blogs');
  219. break;
  220. case 'manage_members' :
  221. $nameTools = get_lang('MemberManager');
  222. $interbreadcrumb[] = array(
  223. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  224. "name" => Blog:: get_blog_title($blog_id),
  225. );
  226. Display :: display_header($nameTools, 'Blogs');
  227. break;
  228. case 'manage_rights' :
  229. $nameTools = get_lang('RightsManager');
  230. $interbreadcrumb[] = array(
  231. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  232. 'name' => Blog:: get_blog_title($blog_id),
  233. );
  234. Display :: display_header($nameTools, 'Blogs');
  235. break;
  236. case 'view_search_result' :
  237. $nameTools = get_lang('SearchResults');
  238. $interbreadcrumb[] = array(
  239. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  240. 'name' => Blog:: get_blog_title($blog_id),
  241. );
  242. Display :: display_header($nameTools, 'Blogs');
  243. break;
  244. case 'execute_task' :
  245. $nameTools = get_lang('ExecuteThisTask');
  246. $interbreadcrumb[] = array(
  247. 'url' => "blog.php?blog_id=$blog_id&".api_get_cidreq(),
  248. 'name' => Blog:: get_blog_title($blog_id),
  249. );
  250. Display :: display_header($nameTools, 'Blogs');
  251. break;
  252. default :
  253. $nameTools = Blog :: get_blog_title($blog_id);
  254. Display :: display_header($nameTools, 'Blogs');
  255. }
  256. // feedback messages
  257. if (!empty($return_message)) {
  258. if ($return_message['type'] == 'confirmation') {
  259. Display::display_confirmation_message($return_message['message']);
  260. }
  261. if ($return_message['type'] == 'error') {
  262. Display::display_error_message($return_message['message']);
  263. }
  264. }
  265. // actions
  266. echo '<div class=actions>';
  267. ?>
  268. <a href="<?php echo api_get_self(); ?>?blog_id=<?php echo $blog_id ?>&<?php echo api_get_cidreq(); ?>" title="<?php echo get_lang('Home') ?>">
  269. <?php echo Display::return_icon('blog.png', get_lang('Home'),'',ICON_SIZE_MEDIUM); ?></a>
  270. <?php if(api_is_allowed('BLOG_'.$blog_id, 'article_add')) { ?>
  271. <a href="<?php echo api_get_self(); ?>?action=new_post&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('NewPost') ?>">
  272. <?php echo Display::return_icon('new_article.png', get_lang('NewPost'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  273. <?php if(api_is_allowed('BLOG_'.$blog_id, 'task_management')) { ?>
  274. <a href="<?php echo api_get_self(); ?>?action=manage_tasks&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('ManageTasks') ?>">
  275. <?php echo Display::return_icon('blog_tasks.png', get_lang('TaskManager'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  276. <?php if(api_is_allowed('BLOG_'.$blog_id, 'member_management')) { ?>
  277. <a href="<?php echo api_get_self(); ?>?action=manage_members&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('ManageMembers') ?>">
  278. <?php echo Display::return_icon('blog_admin_users.png', get_lang('MemberManager'),'',ICON_SIZE_MEDIUM); ?></a><?php } ?>
  279. <?php
  280. echo '</div>';
  281. // Tool introduction
  282. Display::display_introduction_section(TOOL_BLOGS);
  283. ?>
  284. <div class="blog-title"><h1><?php echo Blog::get_blog_title($blog_id); ?></h1></div>
  285. <div class="sectioncomment"><p><?php echo Blog::get_blog_subtitle($blog_id); ?></p></div>
  286. <div class="row">
  287. <div class="col-md-3">
  288. <div class="panel panel-default">
  289. <div class="panel-heading"><?php echo get_lang('Calendar') ?></div>
  290. <div class="panel-body">
  291. <?php
  292. $month = isset($_GET['month']) ? (int)$_GET['month'] : (int) date('m');
  293. $year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
  294. Blog::display_minimonthcalendar($month, $year, $blog_id);
  295. ?>
  296. </div>
  297. </div>
  298. <div class="panel panel-default">
  299. <div class="panel-heading"><?php echo get_lang('Search') ?></div>
  300. <div class="panel-body">
  301. <form action="blog.php" method="get" enctype="multipart/form-data">
  302. <div class="form-group">
  303. <input type="hidden" name="blog_id" value="<?php echo $blog_id ?>" />
  304. <input type="hidden" name="action" value="view_search_result" />
  305. <input type="text" class="form-control" size="20" name="q" value="<?php echo isset($_GET['q']) ? Security::remove_XSS($_GET['q']) : ''; ?>" />
  306. </div>
  307. <button class="btn btn-default btn-block" type="submit">
  308. <em class="fa fa-search"></em> <?php echo get_lang('Search'); ?>
  309. </button>
  310. </form>
  311. </div>
  312. </div>
  313. <div class="panel panel-default">
  314. <div class="panel-heading"><?php echo get_lang('MyTasks') ?></div>
  315. <div class="panel-body">
  316. <?php Blog::get_personal_task_list(); ?>
  317. </div>
  318. </div>
  319. </div>
  320. <div class="col-md-9">
  321. <?php
  322. if (isset($error)) {
  323. Display :: display_error_message($message);
  324. }
  325. if (isset($flag) && $flag == '1') {
  326. $action = "manage_tasks";
  327. Blog :: display_assign_task_form($blog_id);
  328. }
  329. $user_task = false;
  330. $course_id = api_get_course_int_id();
  331. if (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) {
  332. $task_id = (int)$_GET['task_id'];
  333. } else {
  334. $task_id = 0;
  335. $tbl_blogs_tasks_rel_user = Database :: get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  336. $sql = "SELECT COUNT(*) as number
  337. FROM ".$tbl_blogs_tasks_rel_user."
  338. WHERE
  339. c_id = $course_id AND
  340. blog_id = ".$blog_id." AND
  341. user_id = ".api_get_user_id()." AND
  342. task_id = ".$task_id;
  343. $result = Database::query($sql);
  344. $row = Database::fetch_array($result);
  345. if ($row['number'] == 1)
  346. $user_task = true;
  347. }
  348. switch ($action) {
  349. case 'new_post':
  350. if (api_is_allowed('BLOG_'.$blog_id, 'article_add', $user_task ? $task_id : 0)) {
  351. // we show the form if
  352. // 1. no post data
  353. // 2. there is post data and the required field is empty
  354. if (!$_POST OR (!empty($_POST) AND empty($_POST['title']))) {
  355. // if there is post data there is certainly an error in the form
  356. if ($_POST) {
  357. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  358. }
  359. Blog :: display_form_new_post($blog_id);
  360. } else {
  361. if (isset($_GET['filter']) && !empty($_GET['filter'])) {
  362. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  363. } else {
  364. Blog :: display_blog_posts($blog_id);
  365. }
  366. }
  367. } else {
  368. api_not_allowed();
  369. }
  370. break;
  371. case 'view_post' :
  372. Blog :: display_post($blog_id, intval($_GET['post_id']));
  373. break;
  374. case 'edit_post' :
  375. $task_id = (isset ($_GET['task_id']) && is_numeric($_GET['task_id'])) ? $_GET['task_id'] : 0;
  376. if (api_is_allowed('BLOG_'.$blog_id, 'article_edit', $task_id)) {
  377. // we show the form if
  378. // 1. no post data
  379. // 2. there is post data and the required field is empty
  380. if (!$_POST OR (!empty($_POST) AND empty($_POST['post_title']))) {
  381. // if there is post data there is certainly an error in the form
  382. if ($_POST) {
  383. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  384. }
  385. Blog :: display_form_edit_post($blog_id, intval($_GET['post_id']));
  386. } else {
  387. if (isset ($_GET['filter']) && !empty ($_GET['filter'])) {
  388. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  389. } else {
  390. Blog :: display_blog_posts($blog_id);
  391. }
  392. }
  393. } else {
  394. api_not_allowed();
  395. }
  396. break;
  397. case 'manage_members' :
  398. if (api_is_allowed('BLOG_'.$blog_id, 'member_management')) {
  399. Blog :: display_form_user_subscribe($blog_id);
  400. echo '<br /><br />';
  401. Blog :: display_form_user_unsubscribe($blog_id);
  402. } else {
  403. api_not_allowed();
  404. }
  405. break;
  406. case 'manage_rights' :
  407. Blog :: display_form_user_rights($blog_id);
  408. break;
  409. case 'manage_tasks' :
  410. if (api_is_allowed('BLOG_'.$blog_id, 'task_management')) {
  411. if (isset($_GET['do']) && $_GET['do'] == 'add') {
  412. Blog:: display_new_task_form($blog_id);
  413. }
  414. if (isset($_GET['do']) && $_GET['do'] == 'assign') {
  415. Blog:: display_assign_task_form($blog_id);
  416. }
  417. if (isset($_GET['do']) && $_GET['do'] == 'edit') {
  418. Blog:: display_edit_task_form(
  419. $blog_id,
  420. intval($_GET['task_id'])
  421. );
  422. }
  423. if (isset($_GET['do']) && $_GET['do'] == 'edit_assignment') {
  424. Blog :: display_edit_assigned_task_form($blog_id, intval($_GET['task_id']), intval($_GET['user_id']));
  425. }
  426. Blog :: display_task_list($blog_id);
  427. echo '<br /><br />';
  428. Blog :: display_assigned_task_list($blog_id);
  429. echo '<br /><br />';
  430. } else {
  431. api_not_allowed();
  432. }
  433. break;
  434. case 'execute_task' :
  435. if (isset ($_GET['post_id'])) {
  436. Blog :: display_post($blog_id, intval($_GET['post_id']));
  437. } else {
  438. Blog :: display_select_task_post($blog_id, intval($_GET['task_id']));
  439. }
  440. break;
  441. case 'view_search_result' :
  442. Blog :: display_search_results($blog_id, Database::escape_string($_GET['q']));
  443. break;
  444. case '' :
  445. default :
  446. if (isset ($_GET['filter']) && !empty ($_GET['filter'])) {
  447. Blog :: display_day_results($blog_id, Database::escape_string($_GET['filter']));
  448. } else {
  449. Blog :: display_blog_posts($blog_id);
  450. }
  451. break;
  452. }
  453. ?>
  454. </div>
  455. </div>
  456. <?php
  457. // Display the footer
  458. Display::display_footer();