blog.php 16 KB

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