dashboard_add_sessions_to_user.php 12 KB

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