subscribe_user2course.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. *
  11. * @todo use formvalidator for the form
  12. */
  13. $cidReset = true;
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. api_protect_admin_script();
  17. $form_sent = 0;
  18. $first_letter_user = '';
  19. $first_letter_course = '';
  20. $courses = [];
  21. $users = [];
  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('Add users to course');
  26. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  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('Enrolment by classes')).get_lang('Enrolment by classes').'</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 = [];
  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[] = [
  49. 'name' => $extra_field[3],
  50. 'type' => $extra_field[2],
  51. 'variable' => $extra_field[1],
  52. 'data' => $extra_field[9],
  53. ];
  54. }
  55. if ($extra_field[8] == 1 && $extra_field[2] == ExtraField::FIELD_TYPE_TAG) {
  56. $options = UserManager::get_extra_user_data_for_tags($extra_field[1]);
  57. $new_field_list[] = [
  58. 'name' => $extra_field[3],
  59. 'type' => $extra_field[2],
  60. 'variable' => $extra_field[1],
  61. 'data' => $options['options'],
  62. ];
  63. }
  64. }
  65. }
  66. /* React on POSTed request */
  67. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  68. $form_sent = $_POST['form_sent'];
  69. $users = isset($_POST['User list']) && is_array($_POST['User list']) ? $_POST['User list'] : [];
  70. $courses = isset($_POST['Course list']) && is_array($_POST['Course list']) ? $_POST['Course list'] : [];
  71. $first_letter_user = Database::escape_string($_POST['firstLetterUser']);
  72. $first_letter_course = Database::escape_string($_POST['firstLetterCourse']);
  73. foreach ($users as $key => $value) {
  74. $users[$key] = intval($value);
  75. }
  76. if ($form_sent == 1) {
  77. if (count($users) == 0 || count($courses) == 0) {
  78. echo Display::return_message(get_lang('You must select at least one user and one course'), 'error');
  79. } else {
  80. $errorDrh = 0;
  81. foreach ($courses as $course_code) {
  82. foreach ($users as $user_id) {
  83. $user = api_get_user_info($user_id);
  84. if ($user['status'] != DRH) {
  85. CourseManager::subscribeUser($user_id, $course_code);
  86. } else {
  87. $errorDrh = 1;
  88. }
  89. }
  90. }
  91. if ($errorDrh == 0) {
  92. echo Display::return_message(get_lang('The selected users are subscribed to the selected course'), 'confirm');
  93. } else {
  94. echo Display::return_message(get_lang('Human resources managers should not be registered to courses. The corresponding users you selected have not been subscribed.'), 'error');
  95. }
  96. }
  97. }
  98. }
  99. /* Display GUI */
  100. if (empty($first_letter_user)) {
  101. $sql = "SELECT count(*) as nb_users FROM $tbl_user";
  102. $result = Database::query($sql);
  103. $num_row = Database::fetch_array($result);
  104. if ($num_row['nb_users'] > 1000) {
  105. //if there are too much users to gracefully handle with the HTML select list,
  106. // assign a default filter on users names
  107. $first_letter_user = 'A';
  108. }
  109. unset($result);
  110. }
  111. $where_filter = null;
  112. $extra_field_result = [];
  113. //Filter by Extra Fields
  114. $use_extra_fields = false;
  115. if (is_array($extra_field_list)) {
  116. if (is_array($new_field_list) && count($new_field_list) > 0) {
  117. $result_list = [];
  118. foreach ($new_field_list as $new_field) {
  119. $varname = 'field_'.$new_field['variable'];
  120. $fieldtype = $new_field['type'];
  121. if (UserManager::is_extra_field_available($new_field['variable'])) {
  122. if (isset($_POST[$varname]) && $_POST[$varname] != '0') {
  123. $use_extra_fields = true;
  124. if ($fieldtype == ExtraField::FIELD_TYPE_TAG) {
  125. $extra_field_result[] = UserManager::get_extra_user_data_by_tags(
  126. intval($_POST['field_id']),
  127. $_POST[$varname]
  128. );
  129. } else {
  130. $extra_field_result[] = UserManager::get_extra_user_data_by_value(
  131. $new_field['variable'],
  132. $_POST[$varname]
  133. );
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. if ($use_extra_fields) {
  141. $final_result = [];
  142. if (count($extra_field_result) > 1) {
  143. for ($i = 0; $i < count($extra_field_result) - 1; $i++) {
  144. if (is_array($extra_field_result[$i + 1])) {
  145. $final_result = array_intersect($extra_field_result[$i], $extra_field_result[$i + 1]);
  146. }
  147. }
  148. } else {
  149. $final_result = $extra_field_result[0];
  150. }
  151. if (api_is_multiple_url_enabled()) {
  152. if (is_array($final_result) && count($final_result) > 0) {
  153. $where_filter = " AND u.user_id IN ('".implode("','", $final_result)."') ";
  154. } else {
  155. //no results
  156. $where_filter = " AND u.user_id = -1";
  157. }
  158. } else {
  159. if (is_array($final_result) && count($final_result) > 0) {
  160. $where_filter = " AND user_id IN ('".implode("','", $final_result)."') ";
  161. } else {
  162. //no results
  163. $where_filter = " AND user_id = -1";
  164. }
  165. }
  166. }
  167. $target_name = 'lastname';
  168. $orderBy = $target_name;
  169. $showOfficialCode = false;
  170. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  171. if ($orderListByOfficialCode === 'true') {
  172. $showOfficialCode = true;
  173. $orderBy = " official_code, lastname, firstname";
  174. }
  175. $sql = "SELECT user_id, lastname, firstname, username, official_code
  176. FROM $tbl_user
  177. WHERE user_id<>2 AND ".$target_name." LIKE '".$first_letter_user."%' $where_filter
  178. ORDER BY ".(count($users) > 0 ? "(user_id IN(".implode(',', $users).")) DESC," : "")." ".$orderBy;
  179. if (api_is_multiple_url_enabled()) {
  180. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  181. $access_url_id = api_get_current_access_url_id();
  182. if ($access_url_id != -1) {
  183. $sql = "SELECT u.user_id,lastname,firstname,username, official_code
  184. FROM $tbl_user u
  185. INNER JOIN $tbl_user_rel_access_url user_rel_url
  186. ON (user_rel_url.user_id = u.user_id)
  187. WHERE
  188. u.user_id<>2 AND
  189. access_url_id = $access_url_id AND
  190. (".$target_name." LIKE '".$first_letter_user."%' )
  191. $where_filter
  192. ORDER BY ".(count($users) > 0 ? "(u.user_id IN(".implode(',', $users).")) DESC," : "")." ".$orderBy;
  193. }
  194. }
  195. $result = Database::query($sql);
  196. $db_users = Database::store_result($result);
  197. unset($result);
  198. $sql = "SELECT code,visual_code,title
  199. FROM $tbl_course
  200. WHERE visual_code LIKE '".$first_letter_course."%'
  201. ORDER BY ".(count($courses) > 0 ? "(code IN('".implode("','", $courses)."')) DESC," : "")." visual_code";
  202. if (api_is_multiple_url_enabled()) {
  203. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  204. $access_url_id = api_get_current_access_url_id();
  205. if ($access_url_id != -1) {
  206. $sql = "SELECT code, visual_code, title
  207. FROM $tbl_course as course
  208. INNER JOIN $tbl_course_rel_access_url course_rel_url
  209. ON (course_rel_url.c_id = course.id)
  210. WHERE
  211. access_url_id = $access_url_id AND
  212. (visual_code LIKE '".$first_letter_course."%' )
  213. ORDER BY ".(count($courses) > 0 ? "(code IN('".implode("','", $courses)."')) DESC," : "")." visual_code";
  214. }
  215. }
  216. $result = Database::query($sql);
  217. $db_courses = Database::store_result($result);
  218. unset($result);
  219. ?>
  220. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  221. <?php
  222. if (is_array($extra_field_list)) {
  223. if (is_array($new_field_list) && count($new_field_list) > 0) {
  224. echo '<h3>'.get_lang('Filter users').'</h3>';
  225. foreach ($new_field_list as $new_field) {
  226. echo $new_field['name'];
  227. $varname = 'field_'.$new_field['variable'];
  228. $fieldtype = $new_field['type'];
  229. echo '&nbsp;<select name="'.$varname.'">';
  230. echo '<option value="0">--'.get_lang('Select').'--</option>';
  231. foreach ($new_field['data'] as $option) {
  232. $checked = '';
  233. if ($fieldtype == ExtraField::FIELD_TYPE_TAG) {
  234. if (isset($_POST[$varname])) {
  235. if ($_POST[$varname] == $option['tag']) {
  236. $checked = 'selected="true"';
  237. }
  238. }
  239. echo '<option value="'.$option['tag'].'" '.$checked.'>'.$option['tag'].'</option>';
  240. } else {
  241. if (isset($_POST[$varname])) {
  242. if ($_POST[$varname] == $option[1]) {
  243. $checked = 'selected="true"';
  244. }
  245. }
  246. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[2].'</option>';
  247. }
  248. }
  249. echo '</select>';
  250. $extraHidden = $fieldtype == ExtraField::FIELD_TYPE_TAG ? '<input type="hidden" name="field_id" value="'.$option['field_id'].'" />' : '';
  251. echo $extraHidden;
  252. echo '&nbsp;&nbsp;';
  253. }
  254. echo '<input class="btn btn-primary" type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" ></input>';
  255. echo '<br /><br />';
  256. }
  257. }
  258. ?>
  259. <input type="hidden" name="form_sent" value="1"/>
  260. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  261. <tr>
  262. <td width="40%" align="center">
  263. <b><?php echo get_lang('User list'); ?></b>
  264. <br/><br/>
  265. <?php echo get_lang('First letter (last name)'); ?> :
  266. <select name="firstLetterUser"
  267. onchange="javascript:document.formulaire.form_sent.value='2'; document.formulaire.submit();"
  268. aria-label="<?php echo get_lang('First letter (last name)'); ?>">
  269. <option value="">--</option>
  270. <?php
  271. echo Display :: get_alphabet_options($first_letter_user);
  272. ?>
  273. </select>
  274. </td>
  275. <td width="20%">&nbsp;</td>
  276. <td width="40%" align="center">
  277. <b><?php echo get_lang('Course list'); ?> :</b>
  278. <br/><br/>
  279. <?php echo get_lang('First letter (code)'); ?> :
  280. <select name="firstLetterCourse"
  281. onchange="javascript:document.formulaire.form_sent.value='2'; document.formulaire.submit();"
  282. aria-label="<?php echo get_lang('First letter (code)'); ?>">
  283. <option value="">--</option>
  284. <?php
  285. echo Display :: get_alphabet_options($first_letter_course);
  286. ?>
  287. </select>
  288. </td>
  289. </tr>
  290. <tr>
  291. <td width="40%" align="center">
  292. <select name="User list[]" multiple="multiple" size="20" style="width:300px;">
  293. <?php foreach ($db_users as $user) {
  294. ?>
  295. <option value="<?php echo $user['user_id']; ?>" <?php if (in_array($user['user_id'], $users)) {
  296. echo 'selected="selected"';
  297. } ?>>
  298. <?php
  299. $userName = $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')';
  300. if ($showOfficialCode) {
  301. $officialCode = !empty($user['official_code']) ? $user['official_code'].' - ' : '? - ';
  302. $userName = $officialCode.$userName;
  303. }
  304. echo $userName; ?>
  305. </option>
  306. <?php
  307. } ?>
  308. </select>
  309. </td>
  310. <td width="20%" valign="middle" align="center">
  311. <button type="submit" class="btn btn-primary" value="<?php echo get_lang('Add to the course(s)'); ?> &gt;&gt;">
  312. <em class="fa fa-plus"></em> <?php echo get_lang('Add to the course(s)'); ?>
  313. </button>
  314. </td>
  315. <td width="40%" align="center">
  316. <select name="Course list[]" multiple="multiple" size="20" style="width:300px;">
  317. <?php foreach ($db_courses as $course) {
  318. ?>
  319. <option value="<?php echo $course['code']; ?>" <?php if (in_array($course['code'], $courses)) {
  320. echo 'selected="selected"';
  321. } ?>>
  322. <?php echo '('.$course['visual_code'].') '.$course['title']; ?>
  323. </option>
  324. <?php
  325. } ?>
  326. </select>
  327. </td>
  328. </tr>
  329. </table>
  330. </form>
  331. <?php
  332. Display :: display_footer();