subscribe_class.php 4.7 KB

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