dashboard_add_courses_to_user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. require_once '../inc/lib/xajax/xajax.inc.php';
  14. global $_configuration;
  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. $user_id = intval($_GET['user']);
  30. $user_info = api_get_user_info($user_id);
  31. $user_anonymous = api_get_anonymous_id();
  32. $current_user_id = api_get_user_id();
  33. // setting the name of the tool
  34. if (UserManager::is_admin($user_id)) {
  35. $tool_name= get_lang('AssignCoursesToPlatformAdministrator');
  36. } else if ($user_info['status'] == SESSIONADMIN) {
  37. $tool_name= get_lang('AssignCoursesToSessionsAdministrator');
  38. } else {
  39. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  40. }
  41. $add_type = 'multiple';
  42. if(isset($_GET['add_type']) && $_GET['add_type']!='') {
  43. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  44. }
  45. if (!api_is_platform_admin()) {
  46. api_not_allowed(true);
  47. }
  48. function search_courses($needle, $type)
  49. {
  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. $needle = Database::escape_string($needle);
  58. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  59. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  60. foreach ($assigned_courses_code as &$value) {
  61. $value = "'".$value."'";
  62. }
  63. $without_assigned_courses = '';
  64. if (count($assigned_courses_code) > 0) {
  65. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  66. }
  67. if ($_configuration['multiple_access_urls']) {
  68. $sql = "SELECT c.code, c.title FROM $tbl_course c LEFT JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  69. WHERE c.code LIKE '$needle%' $without_assigned_courses AND access_url_id = ".api_get_current_access_url_id()."";
  70. } else {
  71. $sql = "SELECT c.code, c.title FROM $tbl_course c
  72. WHERE c.code LIKE '$needle%' $without_assigned_courses ";
  73. }
  74. $rs = Database::query($sql);
  75. $return .= '<select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  76. while($course = Database :: fetch_array($rs)) {
  77. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'],ENT_QUOTES).'">'.$course['title'].' ('.$course['code'].')</option>';
  78. }
  79. $return .= '</select>';
  80. $xajax_response -> addAssign('ajax_list_courses_multiple','innerHTML',api_utf8_encode($return));
  81. }
  82. return $xajax_response;
  83. }
  84. $xajax->processRequests();
  85. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  86. $htmlHeadXtra[] = '
  87. <script type="text/javascript">
  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 = $_POST['CoursesList'];
  141. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($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. echo '<div class="actions">
  150. <span style="float: right;margin:0px;padding:0px;">
  151. <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>
  152. <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>
  153. </div>';
  154. echo Display::page_header(sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  155. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  156. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  157. foreach ($assigned_courses_code as &$value) {
  158. $value = "'".$value."'";
  159. }
  160. $without_assigned_courses = '';
  161. if (count($assigned_courses_code) > 0) {
  162. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  163. }
  164. $needle = '%';
  165. $firstLetter = null;
  166. if (isset($_POST['firstLetterCourse'])) {
  167. $firstLetter = $_POST['firstLetterCourse'];
  168. $needle = Database::escape_string($firstLetter.'%');
  169. }
  170. if (api_is_multiple_url_enabled()) {
  171. $sql = " SELECT c.code, c.title
  172. FROM $tbl_course c
  173. LEFT JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  174. WHERE
  175. c.code LIKE '$needle' $without_assigned_courses AND
  176. access_url_id = ".api_get_current_access_url_id()."
  177. ORDER BY c.title";
  178. } else {
  179. $sql= " SELECT c.code, c.title FROM $tbl_course c
  180. WHERE c.code LIKE '$needle' $without_assigned_courses
  181. ORDER BY c.title";
  182. }
  183. $result = Database::query($sql);
  184. ?>
  185. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id ?>" style="margin:0px;">
  186. <input type="hidden" name="formSent" value="1" />
  187. <?php
  188. if(!empty($msg)) {
  189. Display::display_normal_message($msg); //main API
  190. }
  191. ?>
  192. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  193. <tr>
  194. <td align="left"></td>
  195. <td align="left"></td>
  196. <td width="" align="center"> &nbsp; </td>
  197. </tr>
  198. <tr>
  199. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  200. <td width="10%">&nbsp;</td>
  201. <td align="center" width="45%"><b>
  202. <?php
  203. if (UserManager::is_admin($user_id)) {
  204. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  205. } else if ($user_info['status'] == SESSIONADMIN) {
  206. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  207. } else {
  208. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  209. }
  210. ?>
  211. :</b></td>
  212. </tr>
  213. <?php if($add_type == 'multiple') { ?>
  214. <tr><td width="45%" align="center">
  215. <?php echo get_lang('FirstLetterCourse');?> :
  216. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  217. <option value="%">--</option>
  218. <?php
  219. echo Display :: get_alphabet_options($firstLetter);
  220. ?>
  221. </select>
  222. </td>
  223. <td>&nbsp;</td></tr>
  224. <?php } ?>
  225. <tr>
  226. <td width="45%" align="center">
  227. <div id="ajax_list_courses_multiple">
  228. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  229. <?php
  230. while ($enreg = Database::fetch_array($result)) {
  231. ?>
  232. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  233. <?php } ?>
  234. </select></div>
  235. </td>
  236. <td width="10%" valign="middle" align="center">
  237. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  238. <br /><br />
  239. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  240. <br /><br /><br /><br /><br /><br />
  241. <?php
  242. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  243. ?>
  244. </td>
  245. <td width="45%" align="center">
  246. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  247. <?php
  248. if (is_array($assigned_courses_to_hrm)) {
  249. foreach($assigned_courses_to_hrm as $enreg) {
  250. ?>
  251. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  252. <?php }
  253. }?>
  254. </select></td>
  255. </tr>
  256. </table>
  257. </form>
  258. <?php
  259. Display::display_footer();