subscribe_user2class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. */
  9. // name of the language file that needs to be included
  10. $language_file = 'admin';
  11. $cidReset = true;
  12. require_once '../inc/global.inc.php';
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. api_protect_admin_script();
  15. $course = $_GET['course'];
  16. $class_id = intval($_GET['idclass']);
  17. $form_sent = 0;
  18. $error_message = '';
  19. $first_letter_left = '';
  20. $first_letter_right = '';
  21. $left_user_list = array();
  22. $right_user_list = array();
  23. // Database table definitions
  24. $tbl_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  25. $tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  26. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  27. $sql = "SELECT name FROM $tbl_class WHERE id='$class_id'";
  28. $result = Database::query($sql);
  29. if (!list ($class_name) = Database::fetch_row($result)) {
  30. header('Location: class_list.php?filtreCours=' . urlencode($course));
  31. exit();
  32. }
  33. $noPHP_SELF = true;
  34. $tool_name = get_lang('AddUsersToAClass') . ' (' . $class_name . ')';
  35. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  36. $interbreadcrumb[] = array("url" => "class_list.php?filtreCours=" . urlencode($course), "name" => get_lang('AdminClasses'));
  37. if ($_POST['formSent']) {
  38. $form_sent = $_POST['formSent'];
  39. $first_letter_left = $_POST['firstLetterLeft'];
  40. $first_letter_right = $_POST['firstLetterRight'];
  41. $left_user_list = is_array($_POST['LeftUserList']) ? $_POST['LeftUserList'] : array();
  42. $right_user_list = is_array($_POST['RightUserList']) ? $_POST['RightUserList'] : array();
  43. $add_to_class = empty($_POST['addToClass']) ? 0 : 1;
  44. $remove_from_class = empty($_POST['removeFromClass']) ? 0 : 1;
  45. if ($form_sent == 1) {
  46. if ($add_to_class) {
  47. if (count($left_user_list) == 0) {
  48. $error_message = get_lang('AtLeastOneUser');
  49. } else {
  50. foreach ($left_user_list as $user_id) {
  51. ClassManager :: add_user($user_id, $class_id);
  52. }
  53. header('Location: class_list.php?filtreCours=' . urlencode($course));
  54. exit();
  55. }
  56. } elseif ($remove_from_class) {
  57. if (count($right_user_list) == 0)
  58. $error_message = get_lang('AtLeastOneUser');
  59. else {
  60. foreach ($right_user_list as $index => $user_id) {
  61. ClassManager :: unsubscribe_user($user_id, $class_id);
  62. }
  63. header('Location: class_list.php?filtreCours=' . urlencode($course));
  64. exit();
  65. }
  66. }
  67. }
  68. }
  69. Display :: display_header($tool_name);
  70. //api_display_tool_title($tool_name);
  71. $target_name = api_sort_by_first_name() ? 'firstname' : 'lastname';
  72. $sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u LEFT JOIN $tbl_class_user cu ON u.user_id=cu.user_id AND class_id='$class_id' WHERE " . $target_name . " LIKE '" . $first_letter_left . "%' AND class_id IS NULL ORDER BY " . (count($left_user_list) > 0 ? "(user_id IN(" . implode(',', $left_user_list) . ")) DESC," : "") . " " . $target_name;
  73. $result = Database::query($sql);
  74. $left_users = Database::store_result($result);
  75. $sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u,$tbl_class_user cu WHERE cu.user_id=u.user_id AND class_id='$class_id' AND " . $target_name . " LIKE '" . $first_letter_right . "%' ORDER BY " . (count($right_user_list) > 0 ? "(user_id IN(" . implode(',', $right_user_list) . ")) DESC," : "") . " " . $target_name;
  76. $result = Database::query($sql);
  77. $right_users = Database::store_result($result);
  78. if (!empty($error_message)) {
  79. Display :: display_normal_message($error_message);
  80. }
  81. ?>
  82. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?course=<?php echo urlencode($course); ?>&amp;idclass=<?php echo $class_id; ?>" style="margin:0px;">
  83. <input type="hidden" name="formSent" value="1"/>
  84. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  85. <tr>
  86. <td width="40%" align="center">
  87. <b><?php echo get_lang('UsersOutsideClass'); ?> :</b>
  88. <br/><br/>
  89. <?php echo get_lang('FirstLetterUser'); ?> :
  90. <select name="firstLetterLeft" onchange="javascript:document.formulaire.formSent.value='2'; document.formulaire.submit();">
  91. <option value="">--</option>
  92. <?php
  93. echo Display :: get_alphabet_options($first_letter_left);
  94. ?>
  95. </select>
  96. </td>
  97. <td width="20%">&nbsp;</td>
  98. <td width="40%" align="center">
  99. <b><?php echo get_lang('UsersInsideClass'); ?> :</b>
  100. <br/><br/>
  101. <?php echo get_lang('FirstLetterUser'); ?> :
  102. <select name="firstLetterRight" onchange="javascript:document.formulaire.formSent.value='2'; document.formulaire.submit();">
  103. <option value="">--</option>
  104. <?php
  105. echo Display :: get_alphabet_options($first_letter_right);
  106. ?>
  107. </select>
  108. </td>
  109. </tr>
  110. <tr>
  111. <td width="40%" align="center">
  112. <select name="LeftUserList[]" multiple="multiple" size="20" style="width:230px;">
  113. <?php
  114. foreach ($left_users as $user) {
  115. ?>
  116. <option value="<?php echo $user['user_id']; ?>" <?php if (in_array($user['user_id'], $left_user_list)) echo 'selected="selected"'; ?>><?php echo api_get_person_name($user['firstname'], $user['lastname']) . ' (' . $user['username'] . ')'; ?></option>
  117. <?php
  118. }
  119. ?>
  120. </select>
  121. </td>
  122. <td width="20%" valign="middle" align="center">
  123. <input type="submit" name="addToClass" value="<?php echo get_lang('AddToClass'); ?> &gt;&gt;"/>
  124. <br/><br/>
  125. <input type="submit" name="removeFromClass" value="&lt;&lt; <?php echo get_lang('RemoveFromClass'); ?>"/>
  126. </td>
  127. <td width="40%" align="center">
  128. <select name="RightUserList[]" multiple="multiple" size="20" style="width:230px;">
  129. <?php
  130. foreach ($right_users as $user) {
  131. ?>
  132. <option value="<?php echo $user['user_id']; ?>" <?php if (in_array($user['user_id'], $right_user_list)) echo 'selected="selected"'; ?>><?php echo api_get_person_name($user['firstname'], $user['lastname']) . ' (' . $user['username'] . ')'; ?></option>
  133. <?php
  134. }
  135. ?>
  136. </select>
  137. </td>
  138. </tr>
  139. </table>
  140. </form>
  141. <?php
  142. Display :: display_footer();