dashboard_add_courses_to_user.php 10 KB

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