dashboard_add_sessions_to_user.php 11 KB

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