dashboard_add_courses_to_user.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // create an ajax object
  18. $xajax = new xajax();
  19. $xajax -> registerFunction ('search_courses');
  20. // setting the section (for the tabs)
  21. $this_section = SECTION_PLATFORM_ADMIN;
  22. // Access restrictions
  23. api_protect_admin_script(true);
  24. // setting breadcrumbs
  25. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  26. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  27. // Database Table Definitions
  28. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  29. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  30. // setting the name of the tool
  31. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  32. // initializing variables
  33. $id_session=intval($_GET['id_session']);
  34. $hrm_id = intval($_GET['user']);
  35. $hrm_info = api_get_user_info($hrm_id);
  36. $user_anonymous = api_get_anonymous_id();
  37. $current_user_id = api_get_user_id();
  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()) {
  43. api_not_allowed(true);
  44. }
  45. function search_courses($needle,$type) {
  46. global $tbl_course, $tbl_course_rel_user, $hrm_id;
  47. $xajax_response = new XajaxResponse();
  48. $return = '';
  49. if(!empty($needle) && !empty($type)) {
  50. // xajax send utf8 datas... datas in db can be non-utf8 datas
  51. $charset = api_get_setting('platform_charset');
  52. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  53. $assigned_courses_to_hrm = CourseManager::get_assigned_courses_to_hr_manager($hrm_id);
  54. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  55. foreach ($assigned_courses_code as &$value) {
  56. $value = "'".$value."'";
  57. }
  58. $without_assigned_courses = '';
  59. if (count($assigned_courses_code) > 0) {
  60. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  61. }
  62. $sql = "SELECT c.code, c.title FROM $tbl_course c
  63. WHERE c.code LIKE '$needle%' $without_assigned_courses ";
  64. $rs = Database::query($sql);
  65. $course_list = array();
  66. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  67. while($course = Database :: fetch_array($rs)) {
  68. $course_list[] = $course['id'];
  69. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'],ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  70. }
  71. $return .= '</select>';
  72. $xajax_response -> addAssign('ajax_list_courses_multiple','innerHTML',api_utf8_encode($return));
  73. }
  74. $_SESSION['course_list'] = $course_list;
  75. return $xajax_response;
  76. }
  77. $xajax -> processRequests();
  78. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  79. $htmlHeadXtra[] = '
  80. <script type="text/javascript">
  81. <!--
  82. function moveItem(origin , destination) {
  83. for(var i = 0 ; i<origin.options.length ; i++) {
  84. if(origin.options[i].selected) {
  85. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  86. origin.options[i]=null;
  87. i = i-1;
  88. }
  89. }
  90. destination.selectedIndex = -1;
  91. sortOptions(destination.options);
  92. }
  93. function sortOptions(options) {
  94. var newOptions = new Array();
  95. for (i = 0 ; i<options.length ; i++) {
  96. newOptions[i] = options[i];
  97. }
  98. newOptions = newOptions.sort(mysort);
  99. options.length = 0;
  100. for(i = 0 ; i < newOptions.length ; i++){
  101. options[i] = newOptions[i];
  102. }
  103. }
  104. function mysort(a, b) {
  105. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  106. return 1;
  107. }
  108. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. function valide() {
  114. var options = document.getElementById("destination").options;
  115. for (i = 0 ; i<options.length ; i++) {
  116. options[i].selected = true;
  117. }
  118. document.forms.formulaire.submit();
  119. }
  120. function remove_item(origin) {
  121. for(var i = 0 ; i<origin.options.length ; i++) {
  122. if(origin.options[i].selected) {
  123. origin.options[i]=null;
  124. i = i-1;
  125. }
  126. }
  127. }
  128. -->
  129. </script>';
  130. $formSent=0;
  131. $errorMsg = $firstLetterCourse = '';
  132. $UserList = array();
  133. $msg = '';
  134. if (intval($_POST['formSent']) == 1) {
  135. $courses_list = $_POST['CoursesList'];
  136. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($hrm_id,$courses_list);
  137. if ($affected_rows) {
  138. $msg = get_lang('AssignedCoursesHasBeenUpdatedSuccesslly');
  139. }
  140. }
  141. // display the dokeos header
  142. Display::display_header($tool_name);
  143. echo '<div class="row"><div class="form_header">'.get_lang('AssignedCoursesTo').'&nbsp;'.api_get_person_name($hrm_info['firstname'], $hrm_info['lastname']).'</div></div><br />';
  144. // *******************
  145. $assigned_courses_to_hrm = CourseManager::get_assigned_courses_to_hr_manager($hrm_id);
  146. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  147. foreach ($assigned_courses_code as &$value) {
  148. $value = "'".$value."'";
  149. }
  150. $without_assigned_courses = '';
  151. if (count($assigned_courses_code) > 0) {
  152. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  153. }
  154. $needle = '%';
  155. if (isset($_POST['firstLetterCourse'])) {
  156. $needle = Database::escape_string($_POST['firstLetterCourse']);
  157. $needle = "$needle%";
  158. }
  159. $sql = " SELECT c.code, c.title FROM $tbl_course c
  160. WHERE c.code LIKE '$needle' $without_assigned_courses ";
  161. $result = Database::query($sql);
  162. ?>
  163. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $hrm_id ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  164. <input type="hidden" name="formSent" value="1" />
  165. <?php
  166. if(!empty($msg)) {
  167. Display::display_normal_message($msg); //main API
  168. }
  169. ?>
  170. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  171. <tr>
  172. <td align="left"></td>
  173. <td align="left"></td>
  174. <td width="" align="center"> &nbsp; </td>
  175. </tr>
  176. <tr>
  177. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  178. <td width="10%">&nbsp;</td>
  179. <td align="center" width="45%"><b><?php echo get_lang('AssignedCoursesListToHumanResourceManager') ?> :</b></td>
  180. </tr>
  181. <?php if($add_type == 'multiple') { ?>
  182. <tr><td width="45%" align="center">
  183. <?php echo get_lang('FirstLetterCourse');?> :
  184. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  185. <option value="%">--</option>
  186. <?php
  187. echo Display :: get_alphabet_options($_POST['firstLetterCourse']);
  188. ?>
  189. </select>
  190. </td>
  191. <td>&nbsp;</td></tr>
  192. <?php } ?>
  193. <tr>
  194. <td width="45%" align="center">
  195. <div id="ajax_list_courses_multiple">
  196. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  197. <?php
  198. while ($enreg = Database::fetch_array($result)) {
  199. ?>
  200. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  201. <?php } ?>
  202. </select></div>
  203. </td>
  204. <td width="10%" valign="middle" align="center">
  205. <?php
  206. if ($ajax_search) {
  207. ?>
  208. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  209. <?php
  210. }
  211. else
  212. {
  213. ?>
  214. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  215. <br /><br />
  216. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  217. <?php
  218. }
  219. ?>
  220. <br /><br /><br /><br /><br /><br />
  221. <?php
  222. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('AssignCoursesToHumanResourceManager').'</button>';
  223. ?>
  224. </td>
  225. <td width="45%" align="center">
  226. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  227. <?php
  228. if (is_array($assigned_courses_to_hrm)) {
  229. foreach($assigned_courses_to_hrm as $enreg) {
  230. ?>
  231. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  232. <?php }
  233. }?>
  234. </select></td>
  235. </tr>
  236. </table>
  237. </form>
  238. <?php
  239. /*
  240. ==============================================================================
  241. FOOTER
  242. ==============================================================================
  243. */
  244. Display::display_footer();
  245. ?>