index.php 5.5 KB

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