index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @license see /license.txt
  4. */
  5. use Chamilo\CoreBundle\Entity\Course;
  6. use Chamilo\CoreBundle\Entity\Portfolio;
  7. use Chamilo\CoreBundle\Entity\PortfolioCategory;
  8. use Chamilo\CoreBundle\Entity\Session;
  9. use Chamilo\UserBundle\Entity\User;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. api_block_anonymous_users();
  12. if (false === api_get_configuration_value('allow_portfolio_tool')) {
  13. api_not_allowed(true);
  14. }
  15. $em = Database::getManager();
  16. $currentUserId = api_get_user_id();
  17. $userId = isset($_GET['user']) ? (int) $_GET['user'] : $currentUserId;
  18. /** @var User $user */
  19. $user = api_get_user_entity($userId);
  20. /** @var Course $course */
  21. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  22. /** @var Session $session */
  23. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  24. $action = isset($_GET['action']) ? $_GET['action'] : 'list';
  25. $cidreq = api_get_cidreq();
  26. $baseUrl = api_get_self().'?'.($cidreq ? $cidreq.'&' : '');
  27. $allowEdit = $currentUserId == $user->getId();
  28. if (isset($_GET['preview'])) {
  29. $allowEdit = false;
  30. }
  31. $toolName = get_lang('Portfolio');
  32. $actions = [];
  33. $content = '';
  34. /**
  35. * Check if the portfolio item or category is valid for the current user.
  36. *
  37. * @param $item
  38. *
  39. * @return bool
  40. */
  41. $isValid = function ($item) use ($user, $course, $session) {
  42. if (!$item) {
  43. return false;
  44. }
  45. if (get_class($item) == Portfolio::class) {
  46. if ($session && $item->getSession()->getId() != $session->getId()) {
  47. return false;
  48. }
  49. if ($course && $item->getCourse()->getId() != $course->getId()) {
  50. return false;
  51. }
  52. }
  53. if ($item->getUser()->getId() != $user->getId()) {
  54. return false;
  55. }
  56. return true;
  57. };
  58. switch ($action) {
  59. case 'add_category':
  60. require 'add_category.php';
  61. break;
  62. case 'edit_category':
  63. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  64. if (!$id) {
  65. break;
  66. }
  67. /** @var PortfolioCategory $category */
  68. $category = $em->find('ChamiloCoreBundle:PortfolioCategory', $id);
  69. if (!$isValid($category)) {
  70. api_not_allowed(true);
  71. }
  72. require 'edit_category.php';
  73. break;
  74. case 'hide_category':
  75. case 'show_category':
  76. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  77. if (!$id) {
  78. break;
  79. }
  80. /** @var PortfolioCategory $category */
  81. $category = $em->find('ChamiloCoreBundle:PortfolioCategory', $id);
  82. if (!$isValid($category)) {
  83. api_not_allowed(true);
  84. }
  85. $category->setIsVisible(!$category->isVisible());
  86. $em->persist($category);
  87. $em->flush();
  88. Display::addFlash(
  89. Display::return_message(get_lang('VisibilityChanged'), 'success')
  90. );
  91. header("Location: $baseUrl");
  92. exit;
  93. case 'delete_category':
  94. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  95. if (!$id) {
  96. break;
  97. }
  98. /** @var PortfolioCategory $category */
  99. $category = $em->find('ChamiloCoreBundle:PortfolioCategory', $id);
  100. if (!$isValid($category)) {
  101. api_not_allowed(true);
  102. }
  103. $em->remove($category);
  104. $em->flush();
  105. Display::addFlash(
  106. Display::return_message(get_lang('PortfolioItemDeleted'), 'success')
  107. );
  108. header("Location: $baseUrl");
  109. exit;
  110. case 'add_item':
  111. require 'add_item.php';
  112. break;
  113. case 'edit_item':
  114. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  115. if (!$id) {
  116. break;
  117. }
  118. /** @var CPortfolio $item */
  119. $item = $em->find('ChamiloCoreBundle:Portfolio', $id);
  120. if (!$isValid($item)) {
  121. api_not_allowed(true);
  122. }
  123. require 'edit_item.php';
  124. break;
  125. case 'hide_item':
  126. case 'show_item':
  127. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  128. if (!$id) {
  129. break;
  130. }
  131. /** @var Portfolio $item */
  132. $item = $em->find('ChamiloCoreBundle:Portfolio', $id);
  133. if (!$isValid($item)) {
  134. api_not_allowed(true);
  135. }
  136. $item->setIsVisible(!$item->isVisible());
  137. $em->persist($item);
  138. $em->flush();
  139. Display::addFlash(
  140. Display::return_message(get_lang('VisibilityChanged'), 'success')
  141. );
  142. header("Location: $baseUrl");
  143. exit;
  144. case 'delete_item':
  145. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  146. if (!$id) {
  147. break;
  148. }
  149. /** @var Portfolio $item */
  150. $item = $em->find('ChamiloCoreBundle:Portfolio', $id);
  151. if (!$isValid($item)) {
  152. api_not_allowed(true);
  153. }
  154. $em->remove($item);
  155. $em->flush();
  156. Display::addFlash(
  157. Display::return_message(get_lang('PortfolioItemDeleted'), 'success')
  158. );
  159. header("Location: $baseUrl");
  160. exit;
  161. case 'list':
  162. default:
  163. require 'list.php';
  164. }
  165. /*
  166. * View
  167. */
  168. $this_section = $course ? SECTION_COURSES : SECTION_SOCIAL;
  169. $actions = implode(PHP_EOL, $actions);
  170. Display::display_header($toolName);
  171. Display::display_introduction_section(TOOL_PORTFOLIO);
  172. echo $actions ? Display::toolbarAction('portfolio-toolbar', [$actions]) : '';
  173. echo Display::page_header($toolName);
  174. echo $content;
  175. Display::display_footer();