dashboard_add_courses_to_user.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Interface for assigning courses 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. // including some necessary dokeos files
  12. require_once '../inc/global.inc.php';
  13. global $_configuration;
  14. // create an ajax object
  15. $xajax = new xajax();
  16. $xajax -> registerFunction ('search_courses');
  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_course = Database::get_main_table(TABLE_MAIN_COURSE);
  26. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  27. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  28. // initializing variables
  29. $id_session=intval($_GET['id_session']);
  30. $user_id = intval($_GET['user']);
  31. $user_info = api_get_user_info($user_id);
  32. $user_anonymous = api_get_anonymous_id();
  33. $current_user_id = api_get_user_id();
  34. // setting the name of the tool
  35. if (UserManager::is_admin($user_id)) {
  36. $tool_name= get_lang('AssignCoursesToPlatformAdministrator');
  37. } else if ($user_info['status'] == SESSIONADMIN) {
  38. $tool_name= get_lang('AssignCoursesToSessionsAdministrator');
  39. } else {
  40. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  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()) {
  47. api_not_allowed(true);
  48. }
  49. function search_courses($needle,$type) {
  50. global $_configuration, $tbl_course, $tbl_course_rel_access_url,$user_id;
  51. $xajax_response = new XajaxResponse();
  52. $return = '';
  53. if(!empty($needle) && !empty($type)) {
  54. // xajax send utf8 datas... datas in db can be non-utf8 datas
  55. $charset = api_get_system_encoding();
  56. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  57. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  58. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  59. foreach ($assigned_courses_code as &$value) {
  60. $value = "'".$value."'";
  61. }
  62. $without_assigned_courses = '';
  63. if (count($assigned_courses_code) > 0) {
  64. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  65. }
  66. if ($_configuration['multiple_access_urls']) {
  67. $sql = "SELECT c.code, c.title FROM $tbl_course c LEFT JOIN $tbl_course_rel_access_url a ON (a.c_id = c.id)
  68. WHERE c.code LIKE '$needle%' $without_assigned_courses AND access_url_id = ".api_get_current_access_url_id()."";
  69. } else {
  70. $sql = "SELECT c.code, c.title FROM $tbl_course c
  71. WHERE c.code LIKE '$needle%' $without_assigned_courses ";
  72. }
  73. $rs = Database::query($sql);
  74. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  75. while($course = Database :: fetch_array($rs)) {
  76. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'],ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  77. }
  78. $return .= '</select>';
  79. $xajax_response -> addAssign('ajax_list_courses_multiple','innerHTML',api_utf8_encode($return));
  80. }
  81. return $xajax_response;
  82. }
  83. $xajax -> processRequests();
  84. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  85. $htmlHeadXtra[] = '<script>
  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 mysort(a, b) {
  98. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  99. return 1;
  100. }
  101. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. function valide() {
  107. var options = document.getElementById("destination").options;
  108. for (i = 0 ; i<options.length ; i++) {
  109. options[i].selected = true;
  110. }
  111. document.forms.formulaire.submit();
  112. }
  113. function remove_item(origin) {
  114. for(var i = 0 ; i<origin.options.length ; i++) {
  115. if(origin.options[i].selected) {
  116. origin.options[i]=null;
  117. i = i-1;
  118. }
  119. }
  120. }
  121. </script>';
  122. $formSent=0;
  123. $errorMsg = $firstLetterCourse = '';
  124. $UserList = array();
  125. $msg = '';
  126. if (intval($_POST['formSent']) == 1) {
  127. $courses_list = $_POST['CoursesList'];
  128. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($user_id, $courses_list);
  129. if ($affected_rows) {
  130. $msg = get_lang('AssignedCoursesHaveBeenUpdatedSuccessfully');
  131. }
  132. }
  133. // Display header
  134. Display::display_header($tool_name);
  135. // actions
  136. echo '<div class="actions">
  137. <span style="float: right;margin:0px;padding:0px;">
  138. <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>
  139. <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>
  140. </div>';
  141. echo Display::page_header(sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  142. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  143. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  144. foreach ($assigned_courses_code as &$value) {
  145. $value = "'".$value."'";
  146. }
  147. $without_assigned_courses = '';
  148. if (count($assigned_courses_code) > 0) {
  149. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  150. }
  151. $needle = '%';
  152. if (isset($_POST['firstLetterCourse'])) {
  153. $needle = Database::escape_string($_POST['firstLetterCourse']);
  154. $needle = "$needle%";
  155. }
  156. if (api_is_multiple_url_enabled()) {
  157. $sql = " SELECT c.code, c.title FROM $tbl_course c LEFT JOIN $tbl_course_rel_access_url a ON (a.c_id = c.id)
  158. WHERE c.code LIKE '$needle' $without_assigned_courses AND access_url_id = ".api_get_current_access_url_id()."
  159. ORDER BY c.title";
  160. } else {
  161. $sql = " SELECT c.code, c.title, id as real_id FROM $tbl_course c
  162. WHERE c.code LIKE '$needle' $without_assigned_courses
  163. ORDER BY c.title";
  164. }
  165. $result = Database::query($sql);
  166. ?>
  167. <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();"';}?>>
  168. <input type="hidden" name="formSent" value="1" />
  169. <?php
  170. if(!empty($msg)) {
  171. Display::display_normal_message($msg); //main API
  172. }
  173. ?>
  174. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  175. <tr>
  176. <td align="left"></td>
  177. <td align="left"></td>
  178. <td width="" align="center"> &nbsp; </td>
  179. </tr>
  180. <tr>
  181. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  182. <td width="10%">&nbsp;</td>
  183. <td align="center" width="45%"><b>
  184. <?php
  185. if (UserManager::is_admin($user_id)) {
  186. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  187. } else if ($user_info['status'] == SESSIONADMIN) {
  188. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  189. } else {
  190. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  191. }
  192. ?>
  193. :</b></td>
  194. </tr>
  195. <?php if($add_type == 'multiple') { ?>
  196. <tr><td width="45%" align="center">
  197. <?php echo get_lang('FirstLetterCourse');?> :
  198. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  199. <option value="%">--</option>
  200. <?php
  201. echo Display :: get_alphabet_options($_POST['firstLetterCourse']);
  202. ?>
  203. </select>
  204. </td>
  205. <td>&nbsp;</td></tr>
  206. <?php } ?>
  207. <tr>
  208. <td width="45%" align="center">
  209. <div id="ajax_list_courses_multiple">
  210. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  211. <?php
  212. while ($enreg = Database::fetch_array($result)) {
  213. ?>
  214. <option value="<?php echo $enreg['real_id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  215. <?php } ?>
  216. </select></div>
  217. </td>
  218. <td width="10%" valign="middle" align="center">
  219. <?php
  220. if ($ajax_search) {
  221. ?>
  222. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  223. <?php
  224. }
  225. else
  226. {
  227. ?>
  228. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  229. <br /><br />
  230. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  231. <?php
  232. }
  233. ?>
  234. <br /><br /><br /><br /><br /><br />
  235. <?php
  236. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  237. ?>
  238. </td>
  239. <td width="45%" align="center">
  240. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  241. <?php
  242. if (is_array($assigned_courses_to_hrm)) {
  243. foreach($assigned_courses_to_hrm as $enreg) {
  244. ?>
  245. <option value="<?php echo $enreg['real_id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  246. <?php }
  247. }?>
  248. </select></td>
  249. </tr>
  250. </table>
  251. </form>
  252. <?php
  253. Display::display_footer();