add_user.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. // Including necessary files
  5. require_once 'work.lib.php';
  6. $current_course_tool = TOOL_STUDENTPUBLICATION;
  7. $workId = isset($_GET['id']) ? (int) $_GET['id'] : null;
  8. $userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : null;
  9. $action = isset($_GET['action']) ? $_GET['action'] : null;
  10. $sessionId = api_get_session_id();
  11. if (empty($workId)) {
  12. api_not_allowed(true);
  13. }
  14. $my_folder_data = get_work_data_by_id($workId);
  15. if (empty($my_folder_data)) {
  16. api_not_allowed(true);
  17. }
  18. if (!api_is_allowed_to_edit()) {
  19. api_not_allowed(true);
  20. }
  21. $courseInfo = api_get_course_info();
  22. $interbreadcrumb[] = [
  23. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  24. 'name' => get_lang('Assignments'),
  25. ];
  26. $interbreadcrumb[] = [
  27. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId,
  28. 'name' => $my_folder_data['title'],
  29. ];
  30. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add a user')];
  31. switch ($action) {
  32. case 'add':
  33. $data = getUserToWork($userId, $workId, api_get_course_int_id());
  34. if (empty($data)) {
  35. addUserToWork($userId, $workId, api_get_course_int_id());
  36. }
  37. $url = api_get_path(WEB_CODE_PATH).'work/add_user.php?id='.$workId.'&'.api_get_cidreq();
  38. Display::addFlash(Display::return_message(get_lang('Added')));
  39. header('Location: '.$url);
  40. exit;
  41. break;
  42. case 'delete':
  43. if (!empty($workId) && !empty($userId)) {
  44. deleteUserToWork($userId, $workId, api_get_course_int_id());
  45. Display::addFlash(Display::return_message(get_lang('Deleted')));
  46. $url = api_get_path(WEB_CODE_PATH).'work/add_user.php?id='.$workId.'&'.api_get_cidreq();
  47. header('Location: '.$url);
  48. exit;
  49. }
  50. break;
  51. }
  52. Display::display_header(null);
  53. $items = getAllUserToWork($workId, api_get_course_int_id());
  54. $usersAdded = [];
  55. if (!empty($items)) {
  56. echo Display::page_subheader(get_lang('Users added'));
  57. echo '<ul class="list-group">';
  58. foreach ($items as $data) {
  59. $myUserId = $data['user_id'];
  60. $usersAdded[] = $myUserId;
  61. $userInfo = api_get_user_info($myUserId);
  62. $url = api_get_path(WEB_CODE_PATH).'work/add_user.php?action=delete&id='.$workId.'&user_id='.$myUserId;
  63. $link = Display::url(
  64. '<em class="fa fa-trash"></em> '.get_lang('Delete'),
  65. $url,
  66. ['class' => 'btn btn-danger btn-sm']
  67. );
  68. echo '<li class="list-group-item">'.
  69. $userInfo['complete_name_with_username'].'<div class="pull-right">'.$link.'</div></li>';
  70. }
  71. echo '</ul>';
  72. }
  73. $status = 0;
  74. if (empty($sessionId)) {
  75. $status = STUDENT;
  76. }
  77. $userList = CourseManager::get_user_list_from_course_code(
  78. $courseInfo['code'],
  79. $sessionId,
  80. null,
  81. null,
  82. $status
  83. );
  84. $userToAddList = [];
  85. foreach ($userList as $user) {
  86. if (!in_array($user['user_id'], $usersAdded)) {
  87. $userToAddList[] = $user;
  88. }
  89. }
  90. if (!empty($userToAddList)) {
  91. echo Display::page_subheader(get_lang('Users to add'));
  92. echo '<ul class="list-group">';
  93. foreach ($userToAddList as $user) {
  94. $userName = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') ';
  95. $url = api_get_path(WEB_CODE_PATH).'work/add_user.php?action=add&id='.$workId.'&user_id='.$user['user_id'];
  96. $link = Display::url(
  97. '<em class="fa fa-plus"></em> '.get_lang('Add'),
  98. $url,
  99. ['class' => 'btn btn-primary btn-sm']
  100. );
  101. echo '<li class="list-group-item">'.$userName.'<div class="pull-right"> '.$link.'</div></li>';
  102. }
  103. echo '</ul>';
  104. } else {
  105. echo Display::return_message(get_lang('NoUsers to add'), 'warning');
  106. }
  107. echo '<hr /><div class="clear"></div>';
  108. Display::display_footer();