dashboard_add_sessions_to_user.php 12 KB

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