dashboard_add_courses_to_user.php 10 KB

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