subscribe_class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.user
  5. */
  6. /**
  7. * Code
  8. */
  9. // name of the language file that needs to be included
  10. $language_file = array('registration', 'admin');
  11. include ('../inc/global.inc.php');
  12. $this_section = SECTION_COURSES;
  13. if (!api_is_allowed_to_edit()) {
  14. api_not_allowed();
  15. exit;
  16. }
  17. /*
  18. MAIN CODE
  19. */
  20. $tool_name = get_lang("AddClassesToACourse");
  21. //extra entries in breadcrumb
  22. $interbreadcrumb[] = array("url" => "user.php", "name" => get_lang("ToolUser"));
  23. $interbreadcrumb[] = array("url" => "class.php", "name" => get_lang("Classes"));
  24. Display :: display_header($tool_name, "User");
  25. echo Display::page_header($tool_name);
  26. if (isset($_GET['register'])) {
  27. ClassManager::subscribe_to_course($_GET['class_id'], $_course['sysCode']);
  28. Display::display_normal_message(get_lang('ClassesSubscribed'));
  29. }
  30. if (isset($_POST['action'])) {
  31. switch ($_POST['action']) {
  32. case 'subscribe' :
  33. if (is_array($_POST['class'])) {
  34. foreach ($_POST['class'] as $index => $class_id) {
  35. ClassManager::subscribe_to_course($class_id, $_course['sysCode']);
  36. }
  37. Display::display_normal_message(get_lang('ClassesSubscribed'));
  38. }
  39. break;
  40. }
  41. }
  42. /*
  43. SHOW LIST OF USERS
  44. */
  45. /**
  46. * * Get the number of classes to display on the current page.
  47. */
  48. function get_number_of_classes() {
  49. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  50. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  51. $sql = "SELECT * FROM $course_class_table WHERE course_code = '" . $_SESSION['_course']['id'] . "'";
  52. $res = Database::query($sql);
  53. $subscribed_classes = array();
  54. while ($obj = Database::fetch_object($res)) {
  55. $subscribed_classes[] = $obj->class_id;
  56. }
  57. $sql = "SELECT c.id FROM $class_table c WHERE 1 = 1";
  58. if (isset($_GET['keyword'])) {
  59. $keyword = Database::escape_string(trim($_GET['keyword']));
  60. $sql .= " AND (c.name LIKE '%" . $keyword . "%')";
  61. }
  62. if (count($subscribed_classes) > 0) {
  63. $sql .= " AND c.id NOT IN ('" . implode("','", $subscribed_classes) . "')";
  64. }
  65. $res = Database::query($sql);
  66. $result = Database::num_rows($res);
  67. return $result;
  68. }
  69. /**
  70. * Get the classes to display on the current page.
  71. */
  72. function get_class_data($from, $number_of_items, $column, $direction) {
  73. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  74. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  75. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  76. $sql = "SELECT * FROM $course_class_table WHERE course_code = '" . $_SESSION['_course']['id'] . "'";
  77. $res = Database::query($sql);
  78. $subscribed_classes = array();
  79. while ($obj = Database::fetch_object($res)) {
  80. $subscribed_classes[] = $obj->class_id;
  81. }
  82. $sql = "SELECT
  83. c.id AS col0,
  84. c.name AS col1,
  85. COUNT(cu.user_id) AS col2,
  86. c.id AS col3
  87. FROM $class_table c
  88. ";
  89. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  90. $sql .= " WHERE 1 = 1";
  91. if (isset($_GET['keyword'])) {
  92. $keyword = Database::escape_string(trim($_GET['keyword']));
  93. $sql .= " AND (c.name LIKE '%" . $keyword . "%')";
  94. }
  95. if (count($subscribed_classes) > 0) {
  96. $sql .= " AND c.id NOT IN ('" . implode("','", $subscribed_classes) . "')";
  97. }
  98. $sql .= " GROUP BY c.id, c.name ";
  99. $sql .= " ORDER BY col$column $direction ";
  100. $sql .= " LIMIT $from,$number_of_items";
  101. $res = Database::query($sql);
  102. $classes = array();
  103. while ($class = Database::fetch_row($res)) {
  104. $classes[] = $class;
  105. }
  106. return $classes;
  107. }
  108. /**
  109. * Build the reg-column of the table
  110. * @param int $class_id The class id
  111. * @return string Some HTML-code
  112. */
  113. function reg_filter($class_id) {
  114. $result = "<a href=\"" . api_get_self() . "?register=yes&amp;class_id=" . $class_id . "\">" . get_lang("reg") . "</a>";
  115. return $result;
  116. }
  117. // Build search-form
  118. $form = new FormValidator('search_class', 'get', '', '', null, false);
  119. $form->add_textfield('keyword', '', false);
  120. $form->addElement('button', 'submit', get_lang('SearchButton'));
  121. // Build table
  122. $table = new SortableTable('users', 'get_number_of_classes', 'get_class_data', 1);
  123. $parameters['keyword'] = Security::remove_XSS($_GET['keyword']);
  124. $table->set_additional_parameters($parameters);
  125. $col = 0;
  126. $table->set_header($col++, '', false);
  127. $table->set_header($col++, get_lang('ClassName'));
  128. $table->set_header($col++, get_lang('NumberOfUsers'));
  129. $table->set_header($col++, get_lang('reg'), false);
  130. $table->set_column_filter($col - 1, 'reg_filter');
  131. $table->set_form_actions(array('subscribe' => get_lang('reg')), 'class');
  132. // Display form & table
  133. $form->display();
  134. $table->display();
  135. Display :: display_footer();