subscribe_user2course.php 13 KB

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