dashboard_add_courses_to_user.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. ==============================================================================
  5. * Interface for assigning courses to Human Resources Manager
  6. * @package chamilo.admin
  7. ==============================================================================
  8. */
  9. // name of the language file that needs to be included
  10. $language_file='admin';
  11. // resetting the course id
  12. $cidReset=true;
  13. // including some necessary dokeos files
  14. require_once '../inc/global.inc.php';
  15. require_once '../inc/lib/xajax/xajax.inc.php';
  16. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  17. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  18. // create an ajax object
  19. $xajax = new xajax();
  20. $xajax -> registerFunction ('search_courses');
  21. // setting the section (for the tabs)
  22. $this_section = SECTION_PLATFORM_ADMIN;
  23. // Access restrictions
  24. api_protect_admin_script(true);
  25. // setting breadcrumbs
  26. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  27. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  28. // Database Table Definitions
  29. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  30. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  31. // initializing variables
  32. $id_session=intval($_GET['id_session']);
  33. $user_id = intval($_GET['user']);
  34. $user_info = api_get_user_info($user_id);
  35. $user_anonymous = api_get_anonymous_id();
  36. $current_user_id = api_get_user_id();
  37. // setting the name of the tool
  38. if (UserManager::is_admin($user_id)) {
  39. $tool_name= get_lang('AssignCoursesToPlatformAdministrator');
  40. } else if ($user_info['status'] == SESSIONADMIN) {
  41. $tool_name= get_lang('AssignCoursesToSessionsAdministrator');
  42. } else {
  43. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  44. }
  45. $add_type = 'multiple';
  46. if(isset($_GET['add_type']) && $_GET['add_type']!=''){
  47. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  48. }
  49. if (!api_is_platform_admin()) {
  50. api_not_allowed(true);
  51. }
  52. function search_courses($needle,$type) {
  53. global $tbl_course, $tbl_course_rel_user, $user_id;
  54. $xajax_response = new XajaxResponse();
  55. $return = '';
  56. if(!empty($needle) && !empty($type)) {
  57. // xajax send utf8 datas... datas in db can be non-utf8 datas
  58. $charset = api_get_setting('platform_charset');
  59. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  60. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  61. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  62. foreach ($assigned_courses_code as &$value) {
  63. $value = "'".$value."'";
  64. }
  65. $without_assigned_courses = '';
  66. if (count($assigned_courses_code) > 0) {
  67. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  68. }
  69. $sql = "SELECT c.code, c.title FROM $tbl_course c
  70. WHERE c.code LIKE '$needle%' $without_assigned_courses ";
  71. $rs = Database::query($sql);
  72. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  73. while($course = Database :: fetch_array($rs)) {
  74. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'],ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  75. }
  76. $return .= '</select>';
  77. $xajax_response -> addAssign('ajax_list_courses_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. <!--
  86. function moveItem(origin , destination) {
  87. for(var i = 0 ; i<origin.options.length ; i++) {
  88. if(origin.options[i].selected) {
  89. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  90. origin.options[i]=null;
  91. i = i-1;
  92. }
  93. }
  94. destination.selectedIndex = -1;
  95. sortOptions(destination.options);
  96. }
  97. function sortOptions(options) {
  98. var newOptions = new Array();
  99. for (i = 0 ; i<options.length ; i++) {
  100. newOptions[i] = options[i];
  101. }
  102. newOptions = newOptions.sort(mysort);
  103. options.length = 0;
  104. for(i = 0 ; i < newOptions.length ; i++){
  105. options[i] = newOptions[i];
  106. }
  107. }
  108. function mysort(a, b) {
  109. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  110. return 1;
  111. }
  112. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. function valide() {
  118. var options = document.getElementById("destination").options;
  119. for (i = 0 ; i<options.length ; i++) {
  120. options[i].selected = true;
  121. }
  122. document.forms.formulaire.submit();
  123. }
  124. function remove_item(origin) {
  125. for(var i = 0 ; i<origin.options.length ; i++) {
  126. if(origin.options[i].selected) {
  127. origin.options[i]=null;
  128. i = i-1;
  129. }
  130. }
  131. }
  132. -->
  133. </script>';
  134. $formSent=0;
  135. $errorMsg = $firstLetterCourse = '';
  136. $UserList = array();
  137. $msg = '';
  138. if (intval($_POST['formSent']) == 1) {
  139. $courses_list = $_POST['CoursesList'];
  140. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($user_id,$courses_list);
  141. if ($affected_rows) {
  142. $msg = get_lang('AssignedCoursesHaveBeenUpdatedSuccessfully');
  143. }
  144. }
  145. // display header
  146. Display::display_header($tool_name);
  147. // actions
  148. echo '<div class="actions" style="height:22px;">
  149. <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_sessions_to_user.php?user='.$user_id.'">'.Display::return_icon('view_more_stats.gif', get_lang('AssignSessions'), array('style'=>'vertical-align:middle')).' '.get_lang('AssignSessions').'</a></span>
  152. <span style="vertical-align:middle">'.sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])).'</span></div>';
  153. // *******************
  154. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  155. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  156. foreach ($assigned_courses_code as &$value) {
  157. $value = "'".$value."'";
  158. }
  159. $without_assigned_courses = '';
  160. if (count($assigned_courses_code) > 0) {
  161. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  162. }
  163. $needle = '%';
  164. if (isset($_POST['firstLetterCourse'])) {
  165. $needle = Database::escape_string($_POST['firstLetterCourse']);
  166. $needle = "$needle%";
  167. }
  168. $sql = " SELECT c.code, c.title FROM $tbl_course c
  169. WHERE c.code LIKE '$needle' $without_assigned_courses ";
  170. $result = Database::query($sql);
  171. ?>
  172. <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();"';}?>>
  173. <input type="hidden" name="formSent" value="1" />
  174. <?php
  175. if(!empty($msg)) {
  176. Display::display_normal_message($msg); //main API
  177. }
  178. ?>
  179. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  180. <tr>
  181. <td align="left"></td>
  182. <td align="left"></td>
  183. <td width="" align="center"> &nbsp; </td>
  184. </tr>
  185. <tr>
  186. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  187. <td width="10%">&nbsp;</td>
  188. <td align="center" width="45%"><b>
  189. <?php
  190. if (UserManager::is_admin($user_id)) {
  191. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  192. } else if ($user_info['status'] == SESSIONADMIN) {
  193. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  194. } else {
  195. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  196. }
  197. ?>
  198. :</b></td>
  199. </tr>
  200. <?php if($add_type == 'multiple') { ?>
  201. <tr><td width="45%" align="center">
  202. <?php echo get_lang('FirstLetterCourse');?> :
  203. <select name="firstLetterCourse" onchange = "xajax_search_course(this.value,'multiple')">
  204. <option value="%">--</option>
  205. <?php
  206. echo Display :: get_alphabet_options($_POST['firstLetterCourse']);
  207. ?>
  208. </select>
  209. </td>
  210. <td>&nbsp;</td></tr>
  211. <?php } ?>
  212. <tr>
  213. <td width="45%" align="center">
  214. <div id="ajax_list_courses_multiple">
  215. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  216. <?php
  217. while ($enreg = Database::fetch_array($result)) {
  218. ?>
  219. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  220. <?php } ?>
  221. </select></div>
  222. </td>
  223. <td width="10%" valign="middle" align="center">
  224. <?php
  225. if ($ajax_search) {
  226. ?>
  227. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  228. <?php
  229. }
  230. else
  231. {
  232. ?>
  233. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  234. <br /><br />
  235. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  236. <?php
  237. }
  238. ?>
  239. <br /><br /><br /><br /><br /><br />
  240. <?php
  241. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  242. ?>
  243. </td>
  244. <td width="45%" align="center">
  245. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  246. <?php
  247. if (is_array($assigned_courses_to_hrm)) {
  248. foreach($assigned_courses_to_hrm as $enreg) {
  249. ?>
  250. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  251. <?php }
  252. }?>
  253. </select></td>
  254. </tr>
  255. </table>
  256. </form>
  257. <?php
  258. /*
  259. ==============================================================================
  260. FOOTER
  261. ==============================================================================
  262. */
  263. Display::display_footer();
  264. ?>