subscribe_class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $em = Database::getManager();
  43. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  44. $courseId = api_get_course_int_id();
  45. $res = $em->getRepository('ChamiloCoreBundle:CourseRelClass')->findBy(['courseId' => $courseId]);
  46. $subscribed_classes = array();
  47. foreach ($res as $obj) {
  48. $subscribed_classes[] = $obj->getClassId();
  49. }
  50. $sql = "SELECT c.id FROM $class_table c WHERE 1 = 1";
  51. if (isset($_GET['keyword'])) {
  52. $keyword = Database::escape_string(trim($_GET['keyword']));
  53. $sql .= " AND (c.name LIKE '%" . $keyword . "%')";
  54. }
  55. if (count($subscribed_classes) > 0) {
  56. $sql .= " AND c.id NOT IN ('" . implode("','", $subscribed_classes) . "')";
  57. }
  58. $res = Database::query($sql);
  59. $result = Database::num_rows($res);
  60. return $result;
  61. }
  62. /**
  63. * Get the classes to display on the current page.
  64. */
  65. function get_class_data($from, $number_of_items, $column, $direction)
  66. {
  67. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  68. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  69. $courseId = api_get_course_int_id();
  70. $em = Database::getManager();
  71. $res = $em->getRepository('ChamiloCoreBundle:CourseRelClass')->findBy(['courseId' => $courseId]);
  72. $subscribed_classes = array();
  73. foreach ($res as $obj) {
  74. $subscribed_classes[] = $obj->getClassId();
  75. }
  76. $sql = "SELECT
  77. c.id AS col0,
  78. c.name AS col1,
  79. COUNT(cu.user_id) AS col2,
  80. c.id AS col3
  81. FROM $class_table c ";
  82. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  83. $sql .= " WHERE 1 = 1";
  84. if (isset($_GET['keyword'])) {
  85. $keyword = Database::escape_string(trim($_GET['keyword']));
  86. $sql .= " AND (c.name LIKE '%" . $keyword . "%')";
  87. }
  88. if (count($subscribed_classes) > 0) {
  89. $sql .= " AND c.id NOT IN ('" . implode("','", $subscribed_classes) . "')";
  90. }
  91. $sql .= " GROUP BY c.id, c.name ";
  92. $sql .= " ORDER BY col$column $direction ";
  93. $sql .= " LIMIT $from,$number_of_items";
  94. $res = Database::query($sql);
  95. $classes = array();
  96. while ($class = Database::fetch_row($res)) {
  97. $classes[] = $class;
  98. }
  99. return $classes;
  100. }
  101. /**
  102. * Build the reg-column of the table
  103. * @param int $class_id The class id
  104. * @return string Some HTML-code
  105. */
  106. function reg_filter($class_id) {
  107. $result = "<a href=\"" . api_get_self() . "?register=yes&amp;class_id=" . $class_id . "\">" . get_lang("reg") . "</a>";
  108. return $result;
  109. }
  110. // Build search-form
  111. $form = new FormValidator('search_class', 'get', '', '', null, false);
  112. $form->addText('keyword', '', false);
  113. $form->addButtonSearch(get_lang('SearchButton'));
  114. // Build table
  115. $table = new SortableTable('users', 'get_number_of_classes', 'get_class_data', 1);
  116. $parameters['keyword'] = Security::remove_XSS($_GET['keyword']);
  117. $table->set_additional_parameters($parameters);
  118. $col = 0;
  119. $table->set_header($col++, '', false);
  120. $table->set_header($col++, get_lang('ClassName'));
  121. $table->set_header($col++, get_lang('NumberOfUsers'));
  122. $table->set_header($col++, get_lang('reg'), false);
  123. $table->set_column_filter($col - 1, 'reg_filter');
  124. $table->set_form_actions(array('subscribe' => get_lang('reg')), 'class');
  125. // Display form & table
  126. $form->display();
  127. $table->display();
  128. Display :: display_footer();