dashboard_add_sessions_to_user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20" style="width:340px;">';
  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. echo '<div class="actions">';
  148. if ($user_info['status'] != SESSIONADMIN) {
  149. echo '<span style="float: right;margin:0px;padding:0px;">
  150. <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>
  151. <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>
  152. </span>';
  153. }
  154. echo '</div>';
  155. echo Display::page_header(sprintf(get_lang('AssignSessionsToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  156. $assigned_sessions_to_hrm = SessionManager::get_sessions_followed_by_drh($user_id);
  157. $assigned_sessions_id = array_keys($assigned_sessions_to_hrm);
  158. $without_assigned_sessions = '';
  159. if (count($assigned_sessions_id) > 0) {
  160. $without_assigned_sessions = " AND s.id NOT IN (".implode(',',$assigned_sessions_id).") ";
  161. }
  162. $needle = '%';
  163. if (!empty($firstLetterSession)) {
  164. $needle = Database::escape_string($firstLetterSession.'%');
  165. }
  166. if (api_is_multiple_url_enabled()) {
  167. $sql = "SELECT s.id, s.name
  168. FROM $tbl_session s
  169. LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
  170. WHERE
  171. s.name LIKE '$needle%' $without_assigned_sessions AND
  172. access_url_id = ".api_get_current_access_url_id()."
  173. ORDER BY s.name";
  174. } else {
  175. $sql = "SELECT s.id, s.name FROM $tbl_session s
  176. WHERE s.name LIKE '$needle%' $without_assigned_sessions
  177. ORDER BY s.name";
  178. }
  179. $result = Database::query($sql);
  180. ?>
  181. <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();"';}?>>
  182. <input type="hidden" name="formSent" value="1" />
  183. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  184. <tr>
  185. <td align="left"></td>
  186. <td align="left"></td>
  187. <td width="" align="center"> &nbsp; </td>
  188. </tr>
  189. <tr>
  190. <td width="45%" align="center"><b><?php echo get_lang('SessionsListInPlatform') ?> :</b></td>
  191. <td width="10%">&nbsp;</td>
  192. <td align="center" width="45%">
  193. <b>
  194. <?php
  195. if (UserManager::is_admin($user_id)) {
  196. echo get_lang('AssignedSessionsListToPlatformAdministrator');
  197. } else if ($user_info['status'] == SESSIONADMIN) {
  198. echo get_lang('AssignedSessionsListToSessionsAdministrator');
  199. } else {
  200. echo get_lang('AssignedSessionsListToHumanResourcesManager');
  201. }
  202. ?>
  203. : </b></td>
  204. </tr>
  205. <?php if ($add_type == 'multiple') { ?>
  206. <tr><td width="45%" align="center">
  207. <?php echo get_lang('FirstLetterSession');?> :
  208. <select name="firstLetterSession" onchange = "xajax_search_sessions(this.value, 'multiple')">
  209. <option value="%">--</option>
  210. <?php
  211. echo Display :: get_alphabet_options($firstLetterSession);
  212. ?>
  213. </select>
  214. </td>
  215. <td>&nbsp;</td></tr>
  216. <?php } ?>
  217. <tr>
  218. <td width="45%" align="center">
  219. <div id="ajax_list_sessions_multiple">
  220. <select id="origin" name="NoAssignedSessionsList[]" multiple="multiple" size="20" style="width:340px;">
  221. <?php
  222. while ($enreg = Database::fetch_array($result)) {
  223. ?>
  224. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'],ENT_QUOTES).'"';?>>
  225. <?php echo $enreg['name']; ?>
  226. </option>
  227. <?php } ?>
  228. </select></div>
  229. </td>
  230. <td width="10%" valign="middle" align="center">
  231. <?php
  232. if ($ajax_search) {
  233. ?>
  234. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('destination'))">
  235. <i class="fa fa-arrow-left"></i>
  236. </button>
  237. <?php
  238. }
  239. else
  240. {
  241. ?>
  242. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  243. <i class="fa fa-arrow-right"></i>
  244. </button>
  245. <br /><br />
  246. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  247. <i class="fa fa-arrow-left"></i>
  248. </button>
  249. <?php
  250. }
  251. ?>
  252. <br /><br /><br /><br /><br /><br />
  253. <?php
  254. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  255. ?>
  256. </td>
  257. <td width="45%" align="center">
  258. <select id='destination' name="SessionsList[]" multiple="multiple" size="20" style="width:320px;">
  259. <?php
  260. if (is_array($assigned_sessions_to_hrm)) {
  261. foreach($assigned_sessions_to_hrm as $enreg) {
  262. ?>
  263. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'],ENT_QUOTES).'"'; ?>>
  264. <?php echo $enreg['name'] ?>
  265. </option>
  266. <?php }
  267. }?>
  268. </select></td>
  269. </tr>
  270. </table>
  271. </form>
  272. <?php
  273. Display::display_footer();