subscribe_class.php 4.7 KB

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