dashboard_add_courses_to_user.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. require_once '../inc/global.inc.php';
  12. global $_configuration;
  13. $xajax = new xajax();
  14. $xajax->registerFunction('search_courses');
  15. // setting the section (for the tabs)
  16. $this_section = SECTION_PLATFORM_ADMIN;
  17. // Access restrictions
  18. api_protect_admin_script(true);
  19. // setting breadcrumbs
  20. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  21. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  22. // Database Table Definitions
  23. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  24. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  25. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  26. // initializing variables
  27. $user_id = intval($_GET['user']);
  28. $user_info = api_get_user_info($user_id);
  29. $user_anonymous = api_get_anonymous_id();
  30. $current_user_id = api_get_user_id();
  31. // setting the name of the tool
  32. if (UserManager::is_admin($user_id)) {
  33. $tool_name= get_lang('AssignCoursesToPlatformAdministrator');
  34. } else if ($user_info['status'] == SESSIONADMIN) {
  35. $tool_name= get_lang('AssignCoursesToSessionsAdministrator');
  36. } else {
  37. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  38. }
  39. $add_type = 'multiple';
  40. if(isset($_GET['add_type']) && $_GET['add_type']!='') {
  41. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  42. }
  43. if (!api_is_platform_admin()) {
  44. api_not_allowed(true);
  45. }
  46. function search_courses($needle, $type)
  47. {
  48. global $_configuration, $tbl_course, $tbl_course_rel_access_url,$user_id;
  49. $xajax_response = new xajaxResponse();
  50. $return = '';
  51. if (!empty($needle) && !empty($type)) {
  52. // xajax send utf8 datas... datas in db can be non-utf8 datas
  53. $charset = api_get_system_encoding();
  54. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  55. $needle = Database::escape_string($needle);
  56. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  57. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  58. foreach ($assigned_courses_code as &$value) {
  59. $value = "'".$value."'";
  60. }
  61. $without_assigned_courses = '';
  62. if (count($assigned_courses_code) > 0) {
  63. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  64. }
  65. if ($_configuration['multiple_access_urls']) {
  66. $sql = "SELECT c.code, c.title FROM $tbl_course c LEFT JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  67. WHERE c.code LIKE '$needle%' $without_assigned_courses AND access_url_id = ".api_get_current_access_url_id()."";
  68. } else {
  69. $sql = "SELECT c.code, c.title FROM $tbl_course c
  70. WHERE c.code LIKE '$needle%' $without_assigned_courses ";
  71. }
  72. $rs = Database::query($sql);
  73. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  74. while($course = Database :: fetch_array($rs)) {
  75. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'],ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  76. }
  77. $return .= '</select>';
  78. $xajax_response -> addAssign('ajax_list_courses_multiple','innerHTML',api_utf8_encode($return));
  79. }
  80. return $xajax_response;
  81. }
  82. $xajax->processRequests();
  83. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  84. $htmlHeadXtra[] = '
  85. <script type="text/javascript">
  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. </script>';
  133. $formSent=0;
  134. $errorMsg = $firstLetterCourse = '';
  135. $UserList = array();
  136. $msg = '';
  137. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  138. $courses_list = $_POST['CoursesList'];
  139. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($user_id,$courses_list);
  140. if ($affected_rows) {
  141. $msg = get_lang('AssignedCoursesHaveBeenUpdatedSuccessfully');
  142. }
  143. }
  144. // display header
  145. Display::display_header($tool_name);
  146. // actions
  147. echo '<div class="actions">
  148. <span style="float: right;margin:0px;padding:0px;">
  149. <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>
  150. <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>
  151. </div>';
  152. echo Display::page_header(sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  153. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  154. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  155. foreach ($assigned_courses_code as &$value) {
  156. $value = "'".$value."'";
  157. }
  158. $without_assigned_courses = '';
  159. if (count($assigned_courses_code) > 0) {
  160. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  161. }
  162. $needle = '%';
  163. $firstLetter = null;
  164. if (isset($_POST['firstLetterCourse'])) {
  165. $firstLetter = $_POST['firstLetterCourse'];
  166. $needle = Database::escape_string($firstLetter.'%');
  167. }
  168. if (api_is_multiple_url_enabled()) {
  169. $sql = " SELECT c.code, c.title
  170. FROM $tbl_course c
  171. LEFT JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  172. WHERE
  173. c.code LIKE '$needle' $without_assigned_courses AND
  174. access_url_id = ".api_get_current_access_url_id()."
  175. ORDER BY c.title";
  176. } else {
  177. $sql= " SELECT c.code, c.title FROM $tbl_course c
  178. WHERE c.code LIKE '$needle' $without_assigned_courses
  179. ORDER BY c.title";
  180. }
  181. $result = Database::query($sql);
  182. ?>
  183. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id ?>" style="margin:0px;">
  184. <input type="hidden" name="formSent" value="1" />
  185. <?php
  186. if(!empty($msg)) {
  187. Display::display_normal_message($msg); //main API
  188. }
  189. ?>
  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('CoursesListInPlatform') ?> :</b></td>
  198. <td width="10%">&nbsp;</td>
  199. <td align="center" width="45%"><b>
  200. <?php
  201. if (UserManager::is_admin($user_id)) {
  202. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  203. } else if ($user_info['status'] == SESSIONADMIN) {
  204. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  205. } else {
  206. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  207. }
  208. ?>
  209. :</b></td>
  210. </tr>
  211. <?php if($add_type == 'multiple') { ?>
  212. <tr><td width="45%" align="center">
  213. <?php echo get_lang('FirstLetterCourse');?> :
  214. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  215. <option value="%">--</option>
  216. <?php
  217. echo Display :: get_alphabet_options($firstLetter);
  218. ?>
  219. </select>
  220. </td>
  221. <td>&nbsp;</td></tr>
  222. <?php } ?>
  223. <tr>
  224. <td width="45%" align="center">
  225. <div id="ajax_list_courses_multiple">
  226. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  227. <?php
  228. while ($enreg = Database::fetch_array($result)) {
  229. ?>
  230. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  231. <?php } ?>
  232. </select></div>
  233. </td>
  234. <td width="10%" valign="middle" align="center">
  235. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  236. <br /><br />
  237. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  238. <br /><br /><br /><br /><br /><br />
  239. <?php
  240. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  241. ?>
  242. </td>
  243. <td width="45%" align="center">
  244. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  245. <?php
  246. if (is_array($assigned_courses_to_hrm)) {
  247. foreach($assigned_courses_to_hrm as $enreg) {
  248. ?>
  249. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  250. <?php }
  251. }?>
  252. </select></td>
  253. </tr>
  254. </table>
  255. </form>
  256. <?php
  257. Display::display_footer();