subscribe_user2course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script allows platform admins to add users to courses.
  5. * It displays a list of users and a list of courses;
  6. * you can select multiple users and courses and then click on
  7. * 'Add to this(these) course(s)'.
  8. *
  9. * @package chamilo.admin
  10. * @todo use formvalidator for the form
  11. */
  12. /* INIT SECTION */
  13. $cidReset = true;
  14. require_once '../inc/global.inc.php';
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. api_protect_admin_script();
  17. /* Global constants and variables */
  18. $form_sent = 0;
  19. $first_letter_user = '';
  20. $first_letter_course = '';
  21. $courses = array ();
  22. $users = array();
  23. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  24. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  25. /* Header */
  26. $tool_name = get_lang('AddUsersToACourse');
  27. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  28. $htmlHeadXtra[] = '
  29. <script type="text/javascript">
  30. function validate_filter() {
  31. document.formulaire.form_sent.value=0;
  32. document.formulaire.submit();
  33. }
  34. </script>';
  35. // displaying the header
  36. Display :: display_header($tool_name);
  37. $link_add_group = '<a href="usergroups.php">'.Display::return_icon('multiple.gif',get_lang('RegistrationByUsersGroups')).get_lang('RegistrationByUsersGroups').'</a>';
  38. echo '<div class="actions">'.$link_add_group.'</div>';
  39. $form = new FormValidator('subscribe_user2course');
  40. $form->addElement('header', '', $tool_name);
  41. $form->display();
  42. //checking for extra field with filter on
  43. $extra_field_list= UserManager::get_extra_fields();
  44. $new_field_list = array();
  45. if (is_array($extra_field_list)) {
  46. foreach ($extra_field_list as $extra_field) {
  47. //if is enabled to filter and is a "<select>" field type
  48. if ($extra_field[8]==1 && $extra_field[2]==4 ) {
  49. $new_field_list[] = array('name'=> $extra_field[3], 'variable'=>$extra_field[1], 'data'=> $extra_field[9]);
  50. }
  51. }
  52. }
  53. /* React on POSTed request */
  54. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  55. $form_sent = $_POST['form_sent'];
  56. $users = isset($_POST['UserList']) && is_array($_POST['UserList']) ? $_POST['UserList'] : array() ;
  57. $courses = isset($_POST['CourseList']) && is_array($_POST['CourseList']) ? $_POST['CourseList'] : array() ;
  58. $first_letter_user = $_POST['firstLetterUser'];
  59. $first_letter_course = $_POST['firstLetterCourse'];
  60. foreach ($users as $key => $value) {
  61. $users[$key] = intval($value);
  62. }
  63. if ($form_sent == 1) {
  64. if (count($users) == 0 || count($courses) == 0) {
  65. Display :: display_error_message(get_lang('AtLeastOneUserAndOneCourse'));
  66. } else {
  67. $errorDrh = 0;
  68. foreach ($courses as $course_code) {
  69. foreach ($users as $user_id) {
  70. $user = api_get_user_info($user_id);
  71. if ($user['status'] <> DRH) {
  72. CourseManager::subscribe_user($user_id, $course_code);
  73. } else {
  74. $errorDrh = 1;
  75. }
  76. }
  77. }
  78. if ($errorDrh == 0) {
  79. Display :: display_confirmation_message(get_lang('UsersAreSubscibedToCourse'));
  80. } else {
  81. Display :: display_error_message(get_lang('HumanResourcesManagerShouldNotBeRegisteredToCourses'));
  82. }
  83. }
  84. }
  85. }
  86. /* Display GUI */
  87. if (empty($first_letter_user)) {
  88. $sql = "SELECT count(*) as nb_users FROM $tbl_user";
  89. $result = Database::query($sql);
  90. $num_row = Database::fetch_array($result);
  91. if ($num_row['nb_users']>1000) {
  92. //if there are too much users to gracefully handle with the HTML select list,
  93. // assign a default filter on users names
  94. $first_letter_user = 'A';
  95. }
  96. unset($result);
  97. }
  98. $where_filter = null;
  99. //Filter by Extra Fields
  100. $use_extra_fields = false;
  101. if (is_array($extra_field_list)) {
  102. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  103. $result_list=array();
  104. foreach ($new_field_list as $new_field) {
  105. $varname = 'field_'.$new_field['variable'];
  106. if (UserManager::is_extra_field_available($new_field['variable'])) {
  107. if (isset($_POST[$varname]) && $_POST[$varname]!='0') {
  108. $use_extra_fields = true;
  109. $extra_field_result[]= UserManager::get_extra_user_data_by_value(
  110. $new_field['variable'],
  111. $_POST[$varname]
  112. );
  113. }
  114. }
  115. }
  116. }
  117. }
  118. if ($use_extra_fields) {
  119. $final_result = array();
  120. if (count($extra_field_result)>1) {
  121. for($i=0;$i<count($extra_field_result)-1;$i++) {
  122. if (is_array($extra_field_result[$i+1])) {
  123. $final_result = array_intersect($extra_field_result[$i],$extra_field_result[$i+1]);
  124. }
  125. }
  126. } else {
  127. $final_result = $extra_field_result[0];
  128. }
  129. if (api_is_multiple_url_enabled()) {
  130. if (is_array($final_result) && count($final_result)>0) {
  131. $where_filter = " AND u.user_id IN ('".implode("','",$final_result)."') ";
  132. } else {
  133. //no results
  134. $where_filter = " AND u.user_id = -1";
  135. }
  136. } else {
  137. if (is_array($final_result) && count($final_result)>0) {
  138. $where_filter = " AND user_id IN ('".implode("','",$final_result)."') ";
  139. } else {
  140. //no results
  141. $where_filter = " AND user_id = -1";
  142. }
  143. }
  144. }
  145. $target_name = api_sort_by_first_name() ? 'firstname' : 'lastname';
  146. $orderBy = $target_name;
  147. $showOfficialCode = false;
  148. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  149. if ($orderListByOfficialCode === 'true') {
  150. $showOfficialCode = true;
  151. $orderBy = " official_code, firstname, lastname";
  152. }
  153. $sql = "SELECT user_id, lastname, firstname, username, official_code
  154. FROM $tbl_user
  155. WHERE user_id<>2 AND ".$target_name." LIKE '".$first_letter_user."%' $where_filter
  156. ORDER BY ". (count($users) > 0 ? "(user_id IN(".implode(',', $users).")) DESC," : "")." ".$orderBy;
  157. if (api_is_multiple_url_enabled()) {
  158. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  159. $access_url_id = api_get_current_access_url_id();
  160. if ($access_url_id != -1){
  161. $sql = "SELECT u.user_id,lastname,firstname,username, official_code
  162. FROM ".$tbl_user ." u
  163. INNER JOIN $tbl_user_rel_access_url user_rel_url
  164. ON (user_rel_url.user_id = u.user_id)
  165. WHERE
  166. u.user_id<>2 AND
  167. access_url_id = $access_url_id AND
  168. (".$target_name." LIKE '".$first_letter_user."%' )
  169. $where_filter
  170. ORDER BY ". (count($users) > 0 ? "(u.user_id IN(".implode(',', $users).")) DESC," : "")." ".$orderBy;
  171. }
  172. }
  173. $result = Database::query($sql);
  174. $db_users = Database::store_result($result);
  175. unset($result);
  176. $sql = "SELECT code,visual_code,title
  177. FROM $tbl_course
  178. WHERE visual_code LIKE '".$first_letter_course."%'
  179. ORDER BY ". (count($courses) > 0 ? "(code IN('".implode("','", $courses)."')) DESC," : "")." visual_code";
  180. if (api_is_multiple_url_enabled()) {
  181. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  182. $access_url_id = api_get_current_access_url_id();
  183. if ($access_url_id != -1){
  184. $sql = "SELECT code, visual_code, title
  185. FROM $tbl_course as course
  186. INNER JOIN $tbl_course_rel_access_url course_rel_url
  187. ON (course_rel_url.c_id = course.id)
  188. WHERE
  189. access_url_id = $access_url_id AND
  190. (visual_code LIKE '".$first_letter_course."%' )
  191. ORDER BY ". (count($courses) > 0 ? "(code IN('".implode("','", $courses)."')) DESC," : "")." visual_code";
  192. }
  193. }
  194. $result = Database::query($sql);
  195. $db_courses = Database::store_result($result);
  196. unset($result);
  197. ?>
  198. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  199. <?php
  200. if (is_array($extra_field_list)) {
  201. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  202. echo '<h3>'.get_lang('FilterUsers').'</h3>';
  203. foreach ($new_field_list as $new_field) {
  204. echo $new_field['name'];
  205. $varname = 'field_'.$new_field['variable'];
  206. echo '&nbsp;<select name="'.$varname.'">';
  207. echo '<option value="0">--'.get_lang('Select').'--</option>';
  208. foreach ($new_field['data'] as $option) {
  209. $checked='';
  210. if (isset($_POST[$varname])) {
  211. if ($_POST[$varname]==$option[1]) {
  212. $checked = 'selected="true"';
  213. }
  214. }
  215. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  216. }
  217. echo '</select>';
  218. echo '&nbsp;&nbsp;';
  219. }
  220. echo '<input class="btn btn-primary" type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" ></input>';
  221. echo '<br /><br />';
  222. }
  223. }
  224. ?>
  225. <input type="hidden" name="form_sent" value="1"/>
  226. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  227. <tr>
  228. <td width="40%" align="center">
  229. <b><?php echo get_lang('UserList'); ?></b>
  230. <br/><br/>
  231. <?php echo get_lang('FirstLetterUser'); ?> :
  232. <select name="firstLetterUser" onchange="javascript:document.formulaire.form_sent.value='2'; document.formulaire.submit();">
  233. <option value="">--</option>
  234. <?php
  235. echo Display :: get_alphabet_options($first_letter_user);
  236. ?>
  237. </select>
  238. </td>
  239. <td width="20%">&nbsp;</td>
  240. <td width="40%" align="center">
  241. <b><?php echo get_lang('CourseList'); ?> :</b>
  242. <br/><br/>
  243. <?php echo get_lang('FirstLetterCourse'); ?> :
  244. <select name="firstLetterCourse" onchange="javascript:document.formulaire.form_sent.value='2'; document.formulaire.submit();">
  245. <option value="">--</option>
  246. <?php
  247. echo Display :: get_alphabet_options($first_letter_course);
  248. ?>
  249. </select>
  250. </td>
  251. </tr>
  252. <tr>
  253. <td width="40%" align="center">
  254. <select name="UserList[]" multiple="multiple" size="20" style="width:300px;">
  255. <?php
  256. foreach ($db_users as $user) {
  257. ?>
  258. <option value="<?php echo $user['user_id']; ?>" <?php if(in_array($user['user_id'],$users)) echo 'selected="selected"'; ?>>
  259. <?php
  260. $userName = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  261. if ($showOfficialCode) {
  262. $officialCode = !empty($user['official_code']) ? $user['official_code'].' - ' : '? - ';
  263. $userName = $officialCode.$userName;
  264. }
  265. echo $userName;
  266. ?>
  267. </option>
  268. <?php
  269. }
  270. ?>
  271. </select>
  272. </td>
  273. <td width="20%" valign="middle" align="center">
  274. <button type="submit" class="btn btn-primary" value="<?php echo get_lang('AddToThatCourse'); ?> &gt;&gt;">
  275. <i class="fa fa-plus"></i> <?php echo get_lang('AddToThatCourse'); ?>
  276. </button>
  277. </td>
  278. <td width="40%" align="center">
  279. <select name="CourseList[]" multiple="multiple" size="20" style="width:300px;">
  280. <?php
  281. foreach ($db_courses as $course) {
  282. ?>
  283. <option value="<?php echo $course['code']; ?>" <?php if(in_array($course['code'],$courses)) echo 'selected="selected"'; ?>>
  284. <?php echo '('.$course['visual_code'].') '.$course['title']; ?>
  285. </option>
  286. <?php
  287. }
  288. ?>
  289. </select>
  290. </td>
  291. </tr>
  292. </table>
  293. </form>
  294. <?php
  295. Display :: display_footer();