dashboard_add_sessions_to_user.php 12 KB

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