subscribe_user2course.php 11 KB

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