dashboard_add_sessions_to_user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 '../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 $tbl_session_rel_access_url, $tbl_session, $user_id;
  48. $xajax_response = new xajaxResponse();
  49. $return = '';
  50. if (!empty($needle) && !empty($type)) {
  51. $needle = Database::escape_string($needle);
  52. $assigned_sessions_to_hrm = SessionManager::get_sessions_followed_by_drh($user_id);
  53. $assigned_sessions_id = array_keys($assigned_sessions_to_hrm);
  54. $without_assigned_sessions = '';
  55. if (count($assigned_sessions_id) > 0) {
  56. $without_assigned_sessions = " AND s.id NOT IN(".implode(',', $assigned_sessions_id).")";
  57. }
  58. if (api_is_multiple_url_enabled()) {
  59. $sql = " SELECT s.id, s.name FROM $tbl_session s
  60. LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
  61. WHERE s.name LIKE '$needle%' $without_assigned_sessions AND access_url_id = ".api_get_current_access_url_id()."";
  62. } else {
  63. $sql = "SELECT s.id, s.name FROM $tbl_session s
  64. WHERE s.name LIKE '$needle%' $without_assigned_sessions ";
  65. }
  66. $rs = Database::query($sql);
  67. $return .= '<select class="form-control" id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20">';
  68. while($session = Database :: fetch_array($rs)) {
  69. $return .= '<option value="'.$session['id'].'" title="'.htmlspecialchars($session['name'],ENT_QUOTES).'">'.$session['name'].'</option>';
  70. }
  71. $return .= '</select>';
  72. $xajax_response->addAssign('ajax_list_sessions_multiple','innerHTML',api_utf8_encode($return));
  73. }
  74. return $xajax_response;
  75. }
  76. $xajax->processRequests();
  77. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  78. $htmlHeadXtra[] = '
  79. <script type="text/javascript">
  80. function moveItem(origin , destination) {
  81. for(var i = 0 ; i<origin.options.length ; i++) {
  82. if(origin.options[i].selected) {
  83. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  84. origin.options[i]=null;
  85. i = i-1;
  86. }
  87. }
  88. destination.selectedIndex = -1;
  89. sortOptions(destination.options);
  90. }
  91. function sortOptions(options) {
  92. var newOptions = new Array();
  93. for (i = 0 ; i<options.length ; i++) {
  94. newOptions[i] = options[i];
  95. }
  96. newOptions = newOptions.sort(mysort);
  97. options.length = 0;
  98. for(i = 0 ; i < newOptions.length ; i++){
  99. options[i] = newOptions[i];
  100. }
  101. }
  102. function mysort(a, b) {
  103. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  104. return 1;
  105. }
  106. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. function valide() {
  112. var options = document.getElementById("destination").options;
  113. for (i = 0 ; i<options.length ; i++) {
  114. options[i].selected = true;
  115. }
  116. document.forms.formulaire.submit();
  117. }
  118. function remove_item(origin) {
  119. for(var i = 0 ; i<origin.options.length ; i++) {
  120. if(origin.options[i].selected) {
  121. origin.options[i]=null;
  122. i = i-1;
  123. }
  124. }
  125. }
  126. </script>';
  127. $formSent=0;
  128. $firstLetterSession = isset($_POST['firstLetterSession']) ? $_POST['firstLetterSession'] : null;
  129. $errorMsg = '';
  130. $UserList = array();
  131. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  132. $sessions_list = $_POST['SessionsList'];
  133. $userInfo = api_get_user_info($user_id);
  134. $affected_rows = SessionManager::suscribe_sessions_to_hr_manager(
  135. $userInfo,
  136. $sessions_list
  137. );
  138. if ($affected_rows) {
  139. Display::addFlash(
  140. Display::return_message(get_lang('AssignedSessionsHaveBeenUpdatedSuccessfully'))
  141. );
  142. }
  143. }
  144. // display header
  145. Display::display_header($tool_name);
  146. // actions
  147. if ($user_info['status'] != SESSIONADMIN) {
  148. $actionsLeft = '<a href="dashboard_add_users_to_user.php?user='.$user_id.'">' . Display::return_icon('add-user.png', get_lang('AssignUsers'), null, ICON_SIZE_MEDIUM ) . '</a>';
  149. $actionsLeft .= '<a href="dashboard_add_courses_to_user.php?user='.$user_id.'">' . Display::return_icon('course-add.png', get_lang('AssignCourses'), null, ICON_SIZE_MEDIUM) . '</a>';
  150. }
  151. echo Display::toolbarAction('toolbar-dashboard', array( 0 => $actionsLeft, 1 => ''));
  152. echo Display::page_header(sprintf(get_lang('AssignSessionsToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])), null, 'h3');
  153. $assigned_sessions_to_hrm = SessionManager::get_sessions_followed_by_drh($user_id);
  154. $assigned_sessions_id = array_keys($assigned_sessions_to_hrm);
  155. $without_assigned_sessions = '';
  156. if (count($assigned_sessions_id) > 0) {
  157. $without_assigned_sessions = " AND s.id NOT IN (".implode(',',$assigned_sessions_id).") ";
  158. }
  159. $needle = '%';
  160. if (!empty($firstLetterSession)) {
  161. $needle = Database::escape_string($firstLetterSession.'%');
  162. }
  163. if (api_is_multiple_url_enabled()) {
  164. $sql = "SELECT s.id, s.name
  165. FROM $tbl_session s
  166. LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
  167. WHERE
  168. s.name LIKE '$needle%' $without_assigned_sessions AND
  169. access_url_id = ".api_get_current_access_url_id()."
  170. ORDER BY s.name";
  171. } else {
  172. $sql = "SELECT s.id, s.name FROM $tbl_session s
  173. WHERE s.name LIKE '$needle%' $without_assigned_sessions
  174. ORDER BY s.name";
  175. }
  176. $result = Database::query($sql);
  177. ?>
  178. <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();"';}?>>
  179. <input type="hidden" name="formSent" value="1" />
  180. <div class="row">
  181. <div class="col-md-4">
  182. <h5><?php echo get_lang('SessionsListInPlatform') ?> :</h5>
  183. <div id="ajax_list_sessions_multiple">
  184. <select id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20" style="width:340px;">
  185. <?php
  186. while ($enreg = Database::fetch_array($result)) {
  187. ?>
  188. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'],ENT_QUOTES).'"';?>>
  189. <?php echo $enreg['name']; ?>
  190. </option>
  191. <?php } ?>
  192. </select>
  193. </div>
  194. </div>
  195. <div class="col-md-4">
  196. <div class="code-course">
  197. <?php if ($add_type == 'multiple') { ?>
  198. <p><?php echo get_lang('FirstLetterSession');?> :</p>
  199. <select class="selectpicker form-control" name="firstLetterSession" onchange = "xajax_search_sessions(this.value, 'multiple')">
  200. <option value="%">--</option>
  201. <?php echo Display :: get_alphabet_options($firstLetterSession); ?>
  202. </select>
  203. <?php } ?>
  204. </div>
  205. <div class="control-course">
  206. <?php
  207. if ($ajax_search) {
  208. ?>
  209. <div class="separate-action">
  210. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))">
  211. <em class="fa fa-arrow-left"></em>
  212. </button>
  213. </div>
  214. <?php
  215. }
  216. else
  217. {
  218. ?>
  219. <div class="separate-action">
  220. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  221. <em class="fa fa-arrow-right"></em>
  222. </button>
  223. </div>
  224. <div class="separate-action">
  225. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  226. <em class="fa fa-arrow-left"></em>
  227. </button>
  228. </div>
  229. <?php
  230. }
  231. ?>
  232. <?php
  233. echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  234. ?>
  235. </div>
  236. </div>
  237. <div class="col-md-4">
  238. <h5><?php
  239. if (UserManager::is_admin($user_id)) {
  240. echo get_lang('AssignedSessionsListToPlatformAdministrator');
  241. } else if ($user_info['status'] == SESSIONADMIN) {
  242. echo get_lang('AssignedSessionsListToSessionsAdministrator');
  243. } else {
  244. echo get_lang('AssignedSessionsListToHumanResourcesManager');
  245. }
  246. ?>
  247. :</h5>
  248. <select id='destination' name="SessionsList[]" multiple="multiple" size="20" style="width:320px;">
  249. <?php
  250. if (is_array($assigned_sessions_to_hrm)) {
  251. foreach($assigned_sessions_to_hrm as $enreg) {
  252. ?>
  253. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'],ENT_QUOTES).'"'; ?>>
  254. <?php echo $enreg['name'] ?>
  255. </option>
  256. <?php }
  257. }?>
  258. </select>
  259. </div>
  260. </div>
  261. </form>
  262. <?php
  263. Display::display_footer();