dashboard_add_sessions_to_user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Interface for assigning sessions to Human Resources Manager.
  5. *
  6. * @package chamilo.admin
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. // create an ajax object
  12. $xajax = new xajax();
  13. $xajax->registerFunction('search_sessions');
  14. // setting the section (for the tabs)
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. // Access restrictions
  17. api_protect_admin_script(true);
  18. // setting breadcrumbs
  19. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  20. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
  21. // Database Table Definitions
  22. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  23. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  24. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  25. // Initializing variables
  26. $user_id = isset($_GET['user']) ? intval($_GET['user']) : null;
  27. $user_info = api_get_user_info($user_id);
  28. $user_anonymous = api_get_anonymous_id();
  29. $current_user_id = api_get_user_id();
  30. $ajax_search = false;
  31. // Setting the name of the tool
  32. if (UserManager::is_admin($user_id)) {
  33. $tool_name = get_lang('AssignSessionsToAdministrationistrator');
  34. } elseif ($user_info['status'] == SESSIONADMIN) {
  35. $tool_name = get_lang('assign sessions to sessions administrator');
  36. } else {
  37. $tool_name = get_lang('Assign sessions to Human Resources manager');
  38. }
  39. $add_type = 'multiple';
  40. if (isset($_GET['add_type']) && $_GET['add_type'] != '') {
  41. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  42. }
  43. if (!api_is_platform_admin() && !api_is_session_admin()) {
  44. api_not_allowed(true);
  45. }
  46. function search_sessions($needle, $type)
  47. {
  48. global $user_id;
  49. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  50. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  51. $xajax_response = new xajaxResponse();
  52. $return = '';
  53. if (!empty($needle) && !empty($type)) {
  54. $needle = Database::escape_string($needle);
  55. $assigned_sessions_to_hrm = SessionManager::get_sessions_followed_by_drh($user_id);
  56. $assigned_sessions_id = array_keys($assigned_sessions_to_hrm);
  57. $without_assigned_sessions = '';
  58. if (count($assigned_sessions_id) > 0) {
  59. $without_assigned_sessions = " AND s.id NOT IN(".implode(',', $assigned_sessions_id).")";
  60. }
  61. if (api_is_multiple_url_enabled()) {
  62. $sql = " SELECT s.id, s.name FROM $tbl_session s
  63. LEFT JOIN $tbl_session_rel_access_url a
  64. ON (s.id = a.session_id)
  65. WHERE
  66. s.name LIKE '$needle%' $without_assigned_sessions AND
  67. access_url_id = ".api_get_current_access_url_id();
  68. } else {
  69. $sql = "SELECT s.id, s.name FROM $tbl_session s
  70. WHERE s.name LIKE '$needle%' $without_assigned_sessions ";
  71. }
  72. $rs = Database::query($sql);
  73. $return .= '<select class="form-control" id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20">';
  74. while ($session = Database :: fetch_array($rs)) {
  75. $return .= '<option value="'.$session['id'].'" title="'.htmlspecialchars($session['name'], ENT_QUOTES).'">'.$session['name'].'</option>';
  76. }
  77. $return .= '</select>';
  78. $xajax_response->addAssign(
  79. 'ajax_list_sessions_multiple',
  80. 'innerHTML',
  81. api_utf8_encode($return)
  82. );
  83. }
  84. return $xajax_response;
  85. }
  86. $xajax->processRequests();
  87. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  88. $htmlHeadXtra[] = '<script>
  89. function moveItem(origin , destination) {
  90. for(var i = 0 ; i<origin.options.length ; i++) {
  91. if(origin.options[i].selected) {
  92. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  93. origin.options[i]=null;
  94. i = i-1;
  95. }
  96. }
  97. destination.selectedIndex = -1;
  98. sortOptions(destination.options);
  99. }
  100. function sortOptions(options) {
  101. var newOptions = new Array();
  102. for (i = 0 ; i<options.length ; i++) {
  103. newOptions[i] = options[i];
  104. }
  105. newOptions = newOptions.sort(mysort);
  106. options.length = 0;
  107. for(i = 0 ; i < newOptions.length ; i++){
  108. options[i] = newOptions[i];
  109. }
  110. }
  111. function mysort(a, b) {
  112. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  113. return 1;
  114. }
  115. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. function valide() {
  121. var options = document.getElementById("destination").options;
  122. for (i = 0 ; i<options.length ; i++) {
  123. options[i].selected = true;
  124. }
  125. document.forms.formulaire.submit();
  126. }
  127. function remove_item(origin) {
  128. for(var i = 0 ; i<origin.options.length ; i++) {
  129. if(origin.options[i].selected) {
  130. origin.options[i]=null;
  131. i = i-1;
  132. }
  133. }
  134. }
  135. </script>';
  136. $formSent = 0;
  137. $firstLetterSession = isset($_POST['firstLetterSession']) ? $_POST['firstLetterSession'] : null;
  138. $errorMsg = '';
  139. $UserList = [];
  140. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  141. $sessions_list = $_POST['SessionsList'];
  142. $userInfo = api_get_user_info($user_id);
  143. $affected_rows = SessionManager::subscribeSessionsToDrh(
  144. $userInfo,
  145. $sessions_list
  146. );
  147. if ($affected_rows) {
  148. Display::addFlash(
  149. Display::return_message(get_lang('The assigned sessions have been updated'))
  150. );
  151. }
  152. }
  153. // display header
  154. Display::display_header($tool_name);
  155. // Actions
  156. if ($user_info['status'] != SESSIONADMIN) {
  157. $actionsLeft = '<a href="dashboard_add_users_to_user.php?user='.$user_id.'">'.
  158. Display::return_icon('add-user.png', get_lang('Assign users'), null, ICON_SIZE_MEDIUM).'</a>';
  159. $actionsLeft .= '<a href="dashboard_add_courses_to_user.php?user='.$user_id.'">'.
  160. Display::return_icon('course-add.png', get_lang('Assign courses'), null, ICON_SIZE_MEDIUM).'</a>';
  161. echo Display::toolbarAction('toolbar-dashboard', [$actionsLeft]);
  162. }
  163. echo Display::page_header(
  164. sprintf(get_lang('Assign sessions to %s'), api_get_person_name($user_info['firstname'], $user_info['lastname'])),
  165. null,
  166. 'h3'
  167. );
  168. $assigned_sessions_to_hrm = SessionManager::get_sessions_followed_by_drh($user_id);
  169. $assigned_sessions_id = array_keys($assigned_sessions_to_hrm);
  170. $without_assigned_sessions = '';
  171. if (count($assigned_sessions_id) > 0) {
  172. $without_assigned_sessions = " AND s.id NOT IN (".implode(',', $assigned_sessions_id).") ";
  173. }
  174. $needle = '%';
  175. if (!empty($firstLetterSession)) {
  176. $needle = Database::escape_string($firstLetterSession.'%');
  177. }
  178. if (api_is_multiple_url_enabled()) {
  179. $sql = "SELECT s.id, s.name
  180. FROM $tbl_session s
  181. LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
  182. WHERE
  183. s.name LIKE '$needle%' $without_assigned_sessions AND
  184. access_url_id = ".api_get_current_access_url_id()."
  185. ORDER BY s.name";
  186. } else {
  187. $sql = "SELECT s.id, s.name FROM $tbl_session s
  188. WHERE s.name LIKE '$needle%' $without_assigned_sessions
  189. ORDER BY s.name";
  190. }
  191. $result = Database::query($sql);
  192. ?>
  193. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id; ?>" style="margin:0px;" <?php if ($ajax_search) {
  194. echo ' onsubmit="valide();"';
  195. }?>>
  196. <input type="hidden" name="formSent" value="1" />
  197. <div class="row">
  198. <div class="col-md-4">
  199. <h5><?php echo get_lang('List of sessions on the platform'); ?> :</h5>
  200. <div id="ajax_list_sessions_multiple">
  201. <select id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20" style="width:340px;">
  202. <?php
  203. while ($enreg = Database::fetch_array($result)) {
  204. ?>
  205. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'], ENT_QUOTES).'"'; ?>>
  206. <?php echo $enreg['name']; ?>
  207. </option>
  208. <?php
  209. } ?>
  210. </select>
  211. </div>
  212. </div>
  213. <div class="col-md-4">
  214. <div class="code-course">
  215. <?php if ($add_type == 'multiple') {
  216. ?>
  217. <p><?php echo get_lang('Session title\'s first letter'); ?> :</p>
  218. <select class="selectpicker form-control" name="firstLetterSession" onchange = "xajax_search_sessions(this.value, 'multiple')">
  219. <option value="%">--</option>
  220. <?php echo Display :: get_alphabet_options($firstLetterSession); ?>
  221. </select>
  222. <?php
  223. } ?>
  224. </div>
  225. <div class="control-course">
  226. <?php
  227. if ($ajax_search) {
  228. ?>
  229. <div class="separate-action">
  230. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))">
  231. <em class="fa fa-arrow-left"></em>
  232. </button>
  233. </div>
  234. <?php
  235. } else {
  236. ?>
  237. <div class="separate-action">
  238. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  239. <em class="fa fa-arrow-right"></em>
  240. </button>
  241. </div>
  242. <div class="separate-action">
  243. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  244. <em class="fa fa-arrow-left"></em>
  245. </button>
  246. </div>
  247. <?php
  248. }
  249. echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  250. ?>
  251. </div>
  252. </div>
  253. <div class="col-md-4">
  254. <h5>
  255. <?php
  256. if (UserManager::is_admin($user_id)) {
  257. echo get_lang('AssignedSessionsListToAdministrationistrator');
  258. } elseif ($user_info['status'] == SESSIONADMIN) {
  259. echo get_lang('Assigned sessions list to sessions administrator');
  260. } else {
  261. echo get_lang('List of sessions assigned to the Human Resources manager');
  262. }
  263. ?>
  264. :</h5>
  265. <select id='destination' name="SessionsList[]" multiple="multiple" size="20" style="width:320px;">
  266. <?php
  267. if (is_array($assigned_sessions_to_hrm)) {
  268. foreach ($assigned_sessions_to_hrm as $enreg) {
  269. ?>
  270. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'], ENT_QUOTES).'"'; ?>>
  271. <?php echo $enreg['name']; ?>
  272. </option>
  273. <?php
  274. }
  275. }?>
  276. </select>
  277. </div>
  278. </div>
  279. </form>
  280. <?php
  281. Display::display_footer();