dashboard_add_courses_to_user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Interface for assigning courses to Human Resources Manager.
  5. *
  6. * @package chamilo.admin
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $xajax = new xajax();
  12. $xajax->registerFunction('search_courses');
  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[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  19. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
  20. // Database Table Definitions
  21. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  22. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  23. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  24. // initializing variables
  25. $user_id = intval($_GET['user']);
  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. // setting the name of the tool
  30. if (UserManager::is_admin($user_id)) {
  31. $tool_name = get_lang('AssignCoursesToAdministrationistrator');
  32. } elseif ($user_info['status'] == SESSIONADMIN) {
  33. $tool_name = get_lang('Assign courses to session\'s administrator');
  34. } else {
  35. $tool_name = get_lang('Assign courses to HR manager');
  36. }
  37. $add_type = 'multiple';
  38. if (isset($_GET['add_type']) && $_GET['add_type'] != '') {
  39. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  40. }
  41. if (!api_is_platform_admin()) {
  42. api_not_allowed(true);
  43. }
  44. function search_courses($needle, $type)
  45. {
  46. global $tbl_course, $tbl_course_rel_access_url, $user_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. $needle = Database::escape_string($needle);
  52. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  53. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  54. foreach ($assigned_courses_code as &$value) {
  55. $value = "'".$value."'";
  56. }
  57. $without_assigned_courses = '';
  58. if (count($assigned_courses_code) > 0) {
  59. $without_assigned_courses = " AND c.code NOT IN(".implode(',', $assigned_courses_code).")";
  60. }
  61. if (api_is_multiple_url_enabled()) {
  62. $sql = "SELECT c.code, c.title
  63. FROM $tbl_course c
  64. LEFT JOIN $tbl_course_rel_access_url a
  65. ON (a.c_id = c.id)
  66. WHERE
  67. c.code LIKE '$needle%' $without_assigned_courses AND
  68. access_url_id = ".api_get_current_access_url_id();
  69. } else {
  70. $sql = "SELECT c.code, c.title
  71. FROM $tbl_course c
  72. WHERE
  73. c.code LIKE '$needle%'
  74. $without_assigned_courses ";
  75. }
  76. $rs = Database::query($sql);
  77. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" >';
  78. while ($course = Database :: fetch_array($rs)) {
  79. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'], ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  80. }
  81. $return .= '</select>';
  82. $xajax_response->addAssign('ajax_list_courses_multiple', 'innerHTML', api_utf8_encode($return));
  83. }
  84. return $xajax_response;
  85. }
  86. $xajax->processRequests();
  87. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  88. $htmlHeadXtra[] = '<script>
  89. function moveItem(origin , destination) {
  90. for(var i = 0 ; i<origin.options.length ; i++) {
  91. if(origin.options[i].selected) {
  92. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  93. origin.options[i]=null;
  94. i = i-1;
  95. }
  96. }
  97. destination.selectedIndex = -1;
  98. sortOptions(destination.options);
  99. }
  100. function sortOptions(options) {
  101. var newOptions = new Array();
  102. for (i = 0 ; i<options.length ; i++) {
  103. newOptions[i] = options[i];
  104. }
  105. newOptions = newOptions.sort(mysort);
  106. options.length = 0;
  107. for(i = 0 ; i < newOptions.length ; i++){
  108. options[i] = newOptions[i];
  109. }
  110. }
  111. function mysort(a, b) {
  112. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  113. return 1;
  114. }
  115. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. function valide() {
  121. var options = document.getElementById("destination").options;
  122. for (i = 0 ; i<options.length ; i++) {
  123. options[i].selected = true;
  124. }
  125. document.forms.formulaire.submit();
  126. }
  127. function remove_item(origin) {
  128. for(var i = 0 ; i<origin.options.length ; i++) {
  129. if(origin.options[i].selected) {
  130. origin.options[i]=null;
  131. i = i-1;
  132. }
  133. }
  134. }
  135. </script>';
  136. $formSent = 0;
  137. $errorMsg = $firstLetterCourse = '';
  138. $UserList = [];
  139. $msg = '';
  140. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  141. $courses_list = isset($_POST['CoursesList']) ? $_POST['CoursesList'] : [];
  142. $affected_rows = CourseManager::subscribeCoursesToDrhManager($user_id, $courses_list);
  143. if ($affected_rows) {
  144. $msg = get_lang('The assigned courses have been updated');
  145. }
  146. }
  147. // display header
  148. Display::display_header($tool_name);
  149. // actions
  150. $actionsLeft = '<a href="dashboard_add_users_to_user.php?user='.$user_id.'">'.
  151. Display::return_icon('add-user.png', get_lang('Assign users'), null, ICON_SIZE_MEDIUM).'</a>';
  152. $actionsLeft .= '<a href="dashboard_add_sessions_to_user.php?user='.$user_id.'">'.
  153. Display::return_icon('session-add.png', get_lang('Assign sessions'), null, ICON_SIZE_MEDIUM).'</a>';
  154. echo $html = Display::toolbarAction('toolbar-dashboard', [$actionsLeft]);
  155. echo Display::page_header(
  156. sprintf(get_lang('Assign courses to %s'), api_get_person_name($user_info['firstname'], $user_info['lastname'])),
  157. null,
  158. 'h3'
  159. );
  160. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  161. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  162. foreach ($assigned_courses_code as &$value) {
  163. $value = "'".$value."'";
  164. }
  165. $without_assigned_courses = '';
  166. if (count($assigned_courses_code) > 0) {
  167. $without_assigned_courses = " AND c.code NOT IN(".implode(',', $assigned_courses_code).")";
  168. }
  169. $needle = '%';
  170. $firstLetter = null;
  171. if (isset($_POST['firstLetterCourse'])) {
  172. $firstLetter = $_POST['firstLetterCourse'];
  173. $needle = Database::escape_string($firstLetter.'%');
  174. }
  175. if (api_is_multiple_url_enabled()) {
  176. $sql = " SELECT c.code, c.title
  177. FROM $tbl_course c
  178. LEFT JOIN $tbl_course_rel_access_url a
  179. ON (a.c_id = c.id)
  180. WHERE
  181. c.code LIKE '$needle' $without_assigned_courses AND
  182. access_url_id = ".api_get_current_access_url_id()."
  183. ORDER BY c.title";
  184. } else {
  185. $sql = " SELECT c.code, c.title
  186. FROM $tbl_course c
  187. WHERE c.code LIKE '$needle' $without_assigned_courses
  188. ORDER BY c.title";
  189. }
  190. $result = Database::query($sql);
  191. ?>
  192. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id; ?>" style="margin:0px;">
  193. <input type="hidden" name="formSent" value="1" />
  194. <?php
  195. if (!empty($msg)) {
  196. echo Display::return_message($msg, 'normal'); //main API
  197. }
  198. ?>
  199. <div class="row">
  200. <div class="col-md-4">
  201. <h5><?php echo get_lang('Platform courses list'); ?> :</h5>
  202. <div id="ajax_list_courses_multiple">
  203. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  204. <?php while ($enreg = Database::fetch_array($result)) {
  205. ?>
  206. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'], ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  207. <?php
  208. } ?>
  209. </select>
  210. </div>
  211. </div>
  212. <div class="col-md-4">
  213. <div class="code-course">
  214. <?php if ($add_type == 'multiple') {
  215. ?>
  216. <p><?php echo get_lang('First letter (code)'); ?> :</p>
  217. <select name="firstLetterCourse" class="selectpicker form-control" onchange = "xajax_search_courses(this.value,'multiple')">
  218. <option value="%">--</option>
  219. <?php echo Display :: get_alphabet_options($firstLetter); ?>
  220. </select>
  221. <?php
  222. } ?>
  223. </div>
  224. <div class="control-course">
  225. <div class="separate-action">
  226. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  227. <em class="fa fa-arrow-right"></em>
  228. </button>
  229. </div>
  230. <div class="separate-action">
  231. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  232. <em class="fa fa-arrow-left"></em>
  233. </button>
  234. </div>
  235. <div class="separate-action">
  236. <?php echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>'; ?>
  237. </div>
  238. </div>
  239. </div>
  240. <div class="col-md-4">
  241. <h5><?php
  242. if (UserManager::is_admin($user_id)) {
  243. echo get_lang('AssignedCoursesListToAdministrationistrator');
  244. } elseif ($user_info['status'] == SESSIONADMIN) {
  245. echo get_lang('Assigned courses list to sessions administrator');
  246. } else {
  247. echo get_lang('Courses assigned to the HR manager');
  248. }
  249. ?>: </h5>
  250. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  251. <?php
  252. if (is_array($assigned_courses_to_hrm)) {
  253. foreach ($assigned_courses_to_hrm as $enreg) {
  254. ?>
  255. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'], ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  256. <?php
  257. }
  258. }
  259. ?>
  260. </select>
  261. </div>
  262. </div>
  263. </form>
  264. <?php
  265. Display::display_footer();