session_course_user.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. use Chamilo\CoreBundle\Entity\Course;
  7. use Chamilo\CoreBundle\Entity\Session;
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. // setting the section (for the tabs)
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. $tool_name = get_lang('Edit session courses by user');
  14. $id_session = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
  15. SessionManager::protectSession($id_session);
  16. $id_user = intval($_GET['id_user']);
  17. $em = Database::getManager();
  18. /** @var Session $session */
  19. $session = $em->find('ChamiloCoreBundle:Session', $id_session);
  20. $user = api_get_user_entity($id_user);
  21. if (!api_is_platform_admin() && $session->getSessionAdminId() != api_get_user_id()) {
  22. api_not_allowed(true);
  23. }
  24. if (!$session->getCourses()->count()) {
  25. Display::addFlash(Display::return_message(get_lang('No course for this session'), 'warning'));
  26. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user);
  27. exit;
  28. }
  29. $avoidedCourseIds = SessionManager::getAvoidedCoursesInSession($user, $session);
  30. $form = new FormValidator(
  31. 'session_course_user',
  32. 'post',
  33. api_get_self().'?id_user='.$user->getId().'&id_session='.$session->getId()
  34. );
  35. $form->addElement(
  36. 'advmultiselect',
  37. 'courses_to_avoid',
  38. $tool_name,
  39. getSessionCourseList($session)
  40. );
  41. $form->addButtonSave(get_lang('Save'));
  42. if ($form->validate()) {
  43. $values = $form->exportValues();
  44. $values['courses_to_avoid'] = !empty($values['courses_to_avoid']) ? $values['courses_to_avoid'] : [];
  45. if ($session->getCourses()->count() == count($values['courses_to_avoid'])) {
  46. Display::addFlash(Display::return_message(get_lang('Maybe you want to delete the user, instead of unsubscribing him from all courses...?')));
  47. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user);
  48. exit;
  49. }
  50. foreach ($values['courses_to_avoid'] as $courseId) {
  51. /** @var Course $course */
  52. $course = $em->find('ChamiloCoreBundle:Course', $courseId);
  53. if (!$session->getUserInCourse($user, $course)->count()) {
  54. continue;
  55. }
  56. $session->removeUserCourseSubscription($user, $course);
  57. }
  58. $coursesToResubscribe = array_diff($avoidedCourseIds, $values['courses_to_avoid']);
  59. foreach ($coursesToResubscribe as $courseId) {
  60. /** @var Course $course */
  61. $course = $em->find('ChamiloCoreBundle:Course', $courseId);
  62. if ($session->getUserInCourse($user, $course)->count()) {
  63. continue;
  64. }
  65. $session->addUserInCourse(Session::STUDENT, $user, $course);
  66. }
  67. $em->persist($session);
  68. $em->flush();
  69. Display::addFlash(Display::return_message(get_lang('Courses updated')));
  70. header('Location: session_course_user.php?id_session='.$session->getId().'&id_user='.$user->getId());
  71. exit;
  72. }
  73. $form->setDefaults(['courses_to_avoid' => $avoidedCourseIds]);
  74. /* View */
  75. // setting breadcrumbs
  76. $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('Session list')];
  77. $interbreadcrumb[] = [
  78. 'url' => 'resume_session.php?id_session='.$id_session,
  79. 'name' => get_lang('Session overview'),
  80. ];
  81. Display::display_header($tool_name);
  82. echo Display::page_header($session->getName().' - '.UserManager::formatUserFullName($user));
  83. ?>
  84. <div class="row">
  85. <div class="col-sm-8 col-sm-offset-2">
  86. <div class="row">
  87. <div class="col-sm-5">
  88. <label for="courses_to_avoid-f"><?php echo get_lang('Courses in this session'); ?></label>
  89. </div>
  90. <div class="col-sm-5 col-sm-offset-2">
  91. <label for="courses_to_avoid-t"><?php echo get_lang('Unaccessible courses'); ?></label>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <?php
  97. echo $form->returnForm();
  98. Display::display_footer();
  99. /**
  100. * @param Session $session
  101. *
  102. * @return array
  103. */
  104. function getSessionCourseList(Session $session)
  105. {
  106. $return = [];
  107. foreach ($session->getCourses() as $sessionCourse) {
  108. /** @var Course $course */
  109. $course = $sessionCourse->getCourse();
  110. $return[$course->getId()] = $course->getTitle().' ('.$course->getCode().')';
  111. }
  112. return $return;
  113. }