subscribe_class.php 5.0 KB

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