subscribe_class.php 5.0 KB

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