subscribe_class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // $Id: subscribe_class.php 8213 2006-03-15 15:52:12Z turboke $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2005 Dokeos S.A.
  7. Copyright (c) Bart Mollet, Hogeschool Gent
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This script allows teachers to subscribe existing classes to their course.
  22. * @package dokeos.user
  23. ==============================================================================
  24. */
  25. /*
  26. ==============================================================================
  27. INIT SECTION
  28. ==============================================================================
  29. */
  30. // name of the language file that needs to be included
  31. $language_file = array('registration','admin');
  32. include ('../inc/global.inc.php');
  33. $this_section = SECTION_COURSES;
  34. if (!api_is_allowed_to_edit()) api_not_allowed(true);
  35. if(api_get_setting('use_session_mode')=='true')
  36. {
  37. api_not_allowed(true);
  38. }
  39. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  40. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  41. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  42. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  43. /*
  44. ==============================================================================
  45. MAIN CODE
  46. ==============================================================================
  47. */
  48. /*
  49. -----------------------------------------------------------
  50. Header
  51. -----------------------------------------------------------
  52. */
  53. $tool_name = get_lang("AddClassesToACourse");
  54. //extra entries in breadcrumb
  55. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  56. $interbreadcrumb[] = array ("url" => "class.php", "name" => get_lang("Classes"));
  57. Display :: display_header($tool_name, "User");
  58. api_display_tool_title($tool_name);
  59. /*
  60. ==============================================================================
  61. MAIN SECTION
  62. ==============================================================================
  63. */
  64. if (!api_is_allowed_to_edit())
  65. {
  66. api_not_allowed();
  67. exit;
  68. }
  69. if (isset ($_GET['register']))
  70. {
  71. ClassManager::subscribe_to_course($_GET['class_id'],$_course['sysCode']);
  72. Display::display_normal_message(get_lang('ClassesSubscribed'));
  73. }
  74. if (isset ($_POST['action']))
  75. {
  76. switch ($_POST['action'])
  77. {
  78. case 'subscribe' :
  79. if (is_array($_POST['class']))
  80. {
  81. foreach ($_POST['class'] as $index => $class_id)
  82. {
  83. ClassManager::subscribe_to_course($class_id,$_course['sysCode']);
  84. }
  85. Display::display_normal_message(get_lang('ClassesSubscribed'));
  86. }
  87. break;
  88. }
  89. }
  90. /*
  91. -----------------------------------------------------------
  92. SHOW LIST OF USERS
  93. -----------------------------------------------------------
  94. */
  95. /**
  96. * * Get the number of classes to display on the current page.
  97. */
  98. function get_number_of_classes()
  99. {
  100. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  101. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  102. $sql = "SELECT * FROM $course_class_table WHERE course_code = '".$_SESSION['_course']['id']."'";
  103. $res = Database::query($sql,__FILE__,__LINE__);
  104. $subscribed_classes = array();
  105. while($obj = Database::fetch_object($res))
  106. {
  107. $subscribed_classes[] = $obj->class_id;
  108. }
  109. $sql = "SELECT c.id FROM $class_table c WHERE 1 = 1";
  110. if (isset ($_GET['keyword']))
  111. {
  112. $keyword = Database::escape_string($_GET['keyword']);
  113. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  114. }
  115. if( count($subscribed_classes) > 0)
  116. {
  117. $sql .= " AND c.id NOT IN ('".implode("','",$subscribed_classes)."')";
  118. }
  119. $res = Database::query($sql, __FILE__, __LINE__);
  120. $result = Database::num_rows($res);
  121. return $result;
  122. }
  123. /**
  124. * Get the classes to display on the current page.
  125. */
  126. function get_class_data($from, $number_of_items, $column, $direction)
  127. {
  128. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  129. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  130. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  131. $sql = "SELECT * FROM $course_class_table WHERE course_code = '".$_SESSION['_course']['id']."'";
  132. $res = Database::query($sql,__FILE__,__LINE__);
  133. $subscribed_classes = array();
  134. while($obj = Database::fetch_object($res))
  135. {
  136. $subscribed_classes[] = $obj->class_id;
  137. }
  138. $sql = "SELECT
  139. c.id AS col0,
  140. c.name AS col1,
  141. COUNT(cu.user_id) AS col2,
  142. c.id AS col3
  143. FROM $class_table c
  144. ";
  145. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  146. $sql .= " WHERE 1 = 1";
  147. if (isset ($_GET['keyword']))
  148. {
  149. $keyword = Database::escape_string($_GET['keyword']);
  150. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  151. }
  152. if( count($subscribed_classes) > 0)
  153. {
  154. $sql .= " AND c.id NOT IN ('".implode("','",$subscribed_classes)."')";
  155. }
  156. $sql .= " GROUP BY c.id, c.name ";
  157. $sql .= " ORDER BY col$column $direction ";
  158. $sql .= " LIMIT $from,$number_of_items";
  159. $res = Database::query($sql, __FILE__, __LINE__);
  160. $classes = array ();
  161. while ($class = Database::fetch_row($res))
  162. {
  163. $classes[] = $class;
  164. }
  165. return $classes;
  166. }
  167. /**
  168. * Build the reg-column of the table
  169. * @param int $class_id The class id
  170. * @return string Some HTML-code
  171. */
  172. function reg_filter($class_id)
  173. {
  174. $result = "<a href=\"".api_get_self()."?register=yes&amp;class_id=".$class_id."\">".get_lang("reg")."</a>";
  175. return $result;
  176. }
  177. // Build search-form
  178. $form = new FormValidator('search_class', 'get','','',null,false);
  179. $renderer = & $form->defaultRenderer();
  180. $renderer->setElementTemplate('<span>{element}</span> ');
  181. $form->add_textfield('keyword', '', false);
  182. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  183. // Build table
  184. $table = new SortableTable('users', 'get_number_of_classes', 'get_class_data', 1);
  185. $parameters['keyword'] = $_GET['keyword'];
  186. $table->set_additional_parameters($parameters);
  187. $col = 0;
  188. $table->set_header($col ++, '', false);
  189. $table->set_header($col ++, get_lang('ClassName'));
  190. $table->set_header($col ++, get_lang('NumberOfUsers'));
  191. $table->set_header($col ++, get_lang('reg'), false);
  192. $table->set_column_filter($col -1, 'reg_filter');
  193. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'class');
  194. // Display form & table
  195. $form->display();
  196. echo '<br />';
  197. $table->display();
  198. /*
  199. ==============================================================================
  200. FOOTER
  201. ==============================================================================
  202. */
  203. Display :: display_footer();
  204. ?>