blog.php 16 KB

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