dashboard_add_courses_to_user.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // resetting the course id
  8. $cidReset = true;
  9. require_once '../inc/global.inc.php';
  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' => 'index.php', '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[] = '
  88. <script type="text/javascript">
  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 = array();
  139. $msg = '';
  140. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  141. $courses_list = $_POST['CoursesList'];
  142. $affected_rows = CourseManager::subscribeCoursesToDrhManager($user_id, $courses_list);
  143. if ($affected_rows) {
  144. $msg = get_lang('AssignedCoursesHaveBeenUpdatedSuccessfully');
  145. }
  146. }
  147. // display header
  148. Display::display_header($tool_name);
  149. // actions
  150. echo '<div class="actions">
  151. <span style="float: right;margin:0px;padding:0px;">
  152. <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>
  153. <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>
  154. </div>';
  155. echo Display::page_header(sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  156. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  157. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  158. foreach ($assigned_courses_code as &$value) {
  159. $value = "'".$value."'";
  160. }
  161. $without_assigned_courses = '';
  162. if (count($assigned_courses_code) > 0) {
  163. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  164. }
  165. $needle = '%';
  166. $firstLetter = null;
  167. if (isset($_POST['firstLetterCourse'])) {
  168. $firstLetter = $_POST['firstLetterCourse'];
  169. $needle = Database::escape_string($firstLetter.'%');
  170. }
  171. if (api_is_multiple_url_enabled()) {
  172. $sql = " SELECT c.code, c.title
  173. FROM $tbl_course c
  174. LEFT JOIN $tbl_course_rel_access_url a
  175. ON (a.c_id = c.id)
  176. WHERE
  177. c.code LIKE '$needle' $without_assigned_courses AND
  178. access_url_id = ".api_get_current_access_url_id()."
  179. ORDER BY c.title";
  180. } else {
  181. $sql= " SELECT c.code, c.title
  182. FROM $tbl_course c
  183. WHERE c.code LIKE '$needle' $without_assigned_courses
  184. ORDER BY c.title";
  185. }
  186. $result = Database::query($sql);
  187. ?>
  188. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id ?>" style="margin:0px;">
  189. <input type="hidden" name="formSent" value="1" />
  190. <?php
  191. if(!empty($msg)) {
  192. Display::display_normal_message($msg); //main API
  193. }
  194. ?>
  195. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  196. <tr>
  197. <td align="left"></td>
  198. <td align="left"></td>
  199. <td width="" align="center"> &nbsp; </td>
  200. </tr>
  201. <tr>
  202. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  203. <td width="10%">&nbsp;</td>
  204. <td align="center" width="45%"><b>
  205. <?php
  206. if (UserManager::is_admin($user_id)) {
  207. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  208. } else if ($user_info['status'] == SESSIONADMIN) {
  209. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  210. } else {
  211. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  212. }
  213. ?>
  214. :</b></td>
  215. </tr>
  216. <?php if($add_type == 'multiple') { ?>
  217. <tr><td width="45%" align="center">
  218. <?php echo get_lang('FirstLetterCourse');?> :
  219. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  220. <option value="%">--</option>
  221. <?php
  222. echo Display :: get_alphabet_options($firstLetter);
  223. ?>
  224. </select>
  225. </td>
  226. <td>&nbsp;</td></tr>
  227. <?php } ?>
  228. <tr>
  229. <td width="45%" align="center">
  230. <div id="ajax_list_courses_multiple">
  231. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  232. <?php
  233. while ($enreg = Database::fetch_array($result)) {
  234. ?>
  235. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  236. <?php } ?>
  237. </select></div>
  238. </td>
  239. <td width="10%" valign="middle" align="center">
  240. <button class="btn-default" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  241. <i class="fa fa-arrow-right"></i>
  242. </button>
  243. <br /><br />
  244. <button class="btn-default" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  245. <i class="fa fa-arrow-left"></i>
  246. </button>
  247. <br /><br /><br /><br /><br /><br />
  248. <?php
  249. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  250. ?>
  251. </td>
  252. <td width="45%" align="center">
  253. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  254. <?php
  255. if (is_array($assigned_courses_to_hrm)) {
  256. foreach($assigned_courses_to_hrm as $enreg) {
  257. ?>
  258. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  259. <?php }
  260. }?>
  261. </select></td>
  262. </tr>
  263. </table>
  264. </form>
  265. <?php
  266. Display::display_footer();