subscribe_user2course.php 12 KB

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