dashboard_add_courses_to_user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // create an ajax object
  16. $xajax = new xajax();
  17. $xajax -> registerFunction ('search_courses');
  18. // setting the section (for the tabs)
  19. $this_section = SECTION_PLATFORM_ADMIN;
  20. // Access restrictions
  21. api_protect_admin_script(true);
  22. // setting breadcrumbs
  23. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  24. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  25. // Database Table Definitions
  26. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  27. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  28. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  29. // initializing variables
  30. $id_session=intval($_GET['id_session']);
  31. $user_id = intval($_GET['user']);
  32. $user_info = api_get_user_info($user_id);
  33. $user_anonymous = api_get_anonymous_id();
  34. $current_user_id = api_get_user_id();
  35. // setting the name of the tool
  36. if (UserManager::is_admin($user_id)) {
  37. $tool_name= get_lang('AssignCoursesToPlatformAdministrator');
  38. } else if ($user_info['status'] == SESSIONADMIN) {
  39. $tool_name= get_lang('AssignCoursesToSessionsAdministrator');
  40. } else {
  41. $tool_name= get_lang('AssignCoursesToHumanResourcesManager');
  42. }
  43. $add_type = 'multiple';
  44. if(isset($_GET['add_type']) && $_GET['add_type']!=''){
  45. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  46. }
  47. if (!api_is_platform_admin()) {
  48. api_not_allowed(true);
  49. }
  50. function search_courses($needle,$type) {
  51. global $_configuration, $tbl_course, $tbl_course_rel_user, $tbl_course_rel_access_url,$user_id;
  52. $xajax_response = new XajaxResponse();
  53. $return = '';
  54. if(!empty($needle) && !empty($type)) {
  55. // xajax send utf8 datas... datas in db can be non-utf8 datas
  56. $charset = api_get_system_encoding();
  57. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  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. <!--
  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. -->
  136. </script>';
  137. $formSent=0;
  138. $errorMsg = $firstLetterCourse = '';
  139. $UserList = array();
  140. $msg = '';
  141. if (intval($_POST['formSent']) == 1) {
  142. $courses_list = $_POST['CoursesList'];
  143. $affected_rows = CourseManager::suscribe_courses_to_hr_manager($user_id,$courses_list);
  144. if ($affected_rows) {
  145. $msg = get_lang('AssignedCoursesHaveBeenUpdatedSuccessfully');
  146. }
  147. }
  148. // display header
  149. Display::display_header($tool_name);
  150. // actions
  151. echo '<div class="actions">
  152. <span style="float: right;margin:0px;padding:0px;">
  153. <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>
  154. <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>
  155. </div>';
  156. echo Display::page_header(sprintf(get_lang('AssignCoursesToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  157. $assigned_courses_to_hrm = CourseManager::get_courses_followed_by_drh($user_id);
  158. $assigned_courses_code = array_keys($assigned_courses_to_hrm);
  159. foreach ($assigned_courses_code as &$value) {
  160. $value = "'".$value."'";
  161. }
  162. $without_assigned_courses = '';
  163. if (count($assigned_courses_code) > 0) {
  164. $without_assigned_courses = " AND c.code NOT IN(".implode(',',$assigned_courses_code).")";
  165. }
  166. $needle = '%';
  167. if (isset($_POST['firstLetterCourse'])) {
  168. $needle = Database::escape_string($_POST['firstLetterCourse']);
  169. $needle = "$needle%";
  170. }
  171. if ($_configuration['multiple_access_urls']) {
  172. $sql = " SELECT c.code, c.title FROM $tbl_course c LEFT JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  173. WHERE c.code LIKE '$needle' $without_assigned_courses AND access_url_id = ".api_get_current_access_url_id()."
  174. ORDER BY c.title";
  175. } else {
  176. $sql = " SELECT c.code, c.title FROM $tbl_course c
  177. WHERE c.code LIKE '$needle' $without_assigned_courses
  178. ORDER BY c.title";
  179. }
  180. $result = Database::query($sql);
  181. ?>
  182. <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();"';}?>>
  183. <input type="hidden" name="formSent" value="1" />
  184. <?php
  185. if(!empty($msg)) {
  186. Display::display_normal_message($msg); //main API
  187. }
  188. ?>
  189. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  190. <tr>
  191. <td align="left"></td>
  192. <td align="left"></td>
  193. <td width="" align="center"> &nbsp; </td>
  194. </tr>
  195. <tr>
  196. <td width="45%" align="center"><b><?php echo get_lang('CoursesListInPlatform') ?> :</b></td>
  197. <td width="10%">&nbsp;</td>
  198. <td align="center" width="45%"><b>
  199. <?php
  200. if (UserManager::is_admin($user_id)) {
  201. echo get_lang('AssignedCoursesListToPlatformAdministrator');
  202. } else if ($user_info['status'] == SESSIONADMIN) {
  203. echo get_lang('AssignedCoursesListToSessionsAdministrator');
  204. } else {
  205. echo get_lang('AssignedCoursesListToHumanResourcesManager');
  206. }
  207. ?>
  208. :</b></td>
  209. </tr>
  210. <?php if($add_type == 'multiple') { ?>
  211. <tr><td width="45%" align="center">
  212. <?php echo get_lang('FirstLetterCourse');?> :
  213. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  214. <option value="%">--</option>
  215. <?php
  216. echo Display :: get_alphabet_options($_POST['firstLetterCourse']);
  217. ?>
  218. </select>
  219. </td>
  220. <td>&nbsp;</td></tr>
  221. <?php } ?>
  222. <tr>
  223. <td width="45%" align="center">
  224. <div id="ajax_list_courses_multiple">
  225. <select id="origin" name="NoAssignedCoursesList[]" multiple="multiple" size="20" style="width:340px;">
  226. <?php
  227. while ($enreg = Database::fetch_array($result)) {
  228. ?>
  229. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"';?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  230. <?php } ?>
  231. </select></div>
  232. </td>
  233. <td width="10%" valign="middle" align="center">
  234. <?php
  235. if ($ajax_search) {
  236. ?>
  237. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  238. <?php
  239. }
  240. else
  241. {
  242. ?>
  243. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  244. <br /><br />
  245. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  246. <?php
  247. }
  248. ?>
  249. <br /><br /><br /><br /><br /><br />
  250. <?php
  251. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  252. ?>
  253. </td>
  254. <td width="45%" align="center">
  255. <select id='destination' name="CoursesList[]" multiple="multiple" size="20" style="width:320px;">
  256. <?php
  257. if (is_array($assigned_courses_to_hrm)) {
  258. foreach($assigned_courses_to_hrm as $enreg) {
  259. ?>
  260. <option value="<?php echo $enreg['code']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'],ENT_QUOTES).'"'; ?>><?php echo $enreg['title'].' ('.$enreg['code'].')'; ?></option>
  261. <?php }
  262. }?>
  263. </select></td>
  264. </tr>
  265. </table>
  266. </form>
  267. <?php
  268. Display::display_footer();