subscribe_class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $langFile = array('registration','admin');
  31. include ('../inc/global.inc.php');
  32. $this_section = SECTION_COURSES;
  33. api_protect_course_script();
  34. if(api_get_setting('use_session_mode')=='true')
  35. {
  36. api_not_allowed();
  37. }
  38. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  39. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  40. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  41. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. /*
  43. ==============================================================================
  44. MAIN CODE
  45. ==============================================================================
  46. */
  47. /*
  48. -----------------------------------------------------------
  49. Header
  50. -----------------------------------------------------------
  51. */
  52. $tool_name = get_lang("AddClassesToACourse");
  53. //extra entries in breadcrumb
  54. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  55. $interbreadcrumb[] = array ("url" => "class.php", "name" => get_lang("Classes"));
  56. Display :: display_header($tool_name, "User");
  57. api_display_tool_title($tool_name);
  58. /*
  59. ==============================================================================
  60. MAIN SECTION
  61. ==============================================================================
  62. */
  63. if (!api_is_allowed_to_edit())
  64. {
  65. api_not_allowed();
  66. exit;
  67. }
  68. if (isset ($_GET['register']))
  69. {
  70. ClassManager::subscribe_to_course($_GET['class_id'],$_course['sysCode']);
  71. Display::display_normal_message(get_lang('ClassesSubscribed'));
  72. }
  73. if (isset ($_POST['action']))
  74. {
  75. switch ($_POST['action'])
  76. {
  77. case 'subscribe' :
  78. if (is_array($_POST['class']))
  79. {
  80. foreach ($_POST['class'] as $index => $class_id)
  81. {
  82. ClassManager::subscribe_to_course($class_id,$_course['sysCode']);
  83. }
  84. Display::display_normal_message(get_lang('ClassesSubscribed'));
  85. }
  86. break;
  87. }
  88. }
  89. /*
  90. -----------------------------------------------------------
  91. SHOW LIST OF USERS
  92. -----------------------------------------------------------
  93. */
  94. /**
  95. * * Get the number of classes to display on the current page.
  96. */
  97. function get_number_of_classes()
  98. {
  99. $class_table = Database :: get_main_table(MAIN_CLASS_TABLE);
  100. $course_class_table = Database :: get_main_table(MAIN_COURSE_CLASS_TABLE);
  101. $sql = "SELECT * FROM $course_class_table WHERE course_code = '".$_SESSION['_course']['id']."'";
  102. $res = api_sql_query($sql,__FILE__,__LINE__);
  103. $subscribed_classes = array();
  104. while($obj = mysql_fetch_object($res))
  105. {
  106. $subscribed_classes[] = $obj->class_id;
  107. }
  108. $sql = "SELECT c.id FROM $class_table c WHERE 1 = 1";
  109. if (isset ($_GET['keyword']))
  110. {
  111. $keyword = mysql_real_escape_string($_GET['keyword']);
  112. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  113. }
  114. if( count($subscribed_classes) > 0)
  115. {
  116. $sql .= " AND c.id NOT IN ('".implode("','",$subscribed_classes)."')";
  117. }
  118. $res = api_sql_query($sql, __FILE__, __LINE__);
  119. $result = mysql_num_rows($res);
  120. return $result;
  121. }
  122. /**
  123. * Get the classes to display on the current page.
  124. */
  125. function get_class_data($from, $number_of_items, $column, $direction)
  126. {
  127. $class_table = Database :: get_main_table(MAIN_CLASS_TABLE);
  128. $course_class_table = Database :: get_main_table(MAIN_COURSE_CLASS_TABLE);
  129. $class_user_table = Database :: get_main_table(MAIN_CLASS_USER_TABLE);
  130. $sql = "SELECT * FROM $course_class_table WHERE course_code = '".$_SESSION['_course']['id']."'";
  131. $res = api_sql_query($sql,__FILE__,__LINE__);
  132. $subscribed_classes = array();
  133. while($obj = mysql_fetch_object($res))
  134. {
  135. $subscribed_classes[] = $obj->class_id;
  136. }
  137. $sql = "SELECT
  138. c.id AS col0,
  139. c.name AS col1,
  140. COUNT(cu.user_id) AS col2,
  141. c.id AS col3
  142. FROM $class_table c
  143. ";
  144. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  145. $sql .= " WHERE 1 = 1";
  146. if (isset ($_GET['keyword']))
  147. {
  148. $keyword = mysql_real_escape_string($_GET['keyword']);
  149. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  150. }
  151. if( count($subscribed_classes) > 0)
  152. {
  153. $sql .= " AND c.id NOT IN ('".implode("','",$subscribed_classes)."')";
  154. }
  155. $sql .= " GROUP BY c.id, c.name ";
  156. $sql .= " ORDER BY col$column $direction ";
  157. $sql .= " LIMIT $from,$number_of_items";
  158. $res = api_sql_query($sql, __FILE__, __LINE__);
  159. $classes = array ();
  160. while ($class = mysql_fetch_row($res))
  161. {
  162. $classes[] = $class;
  163. }
  164. return $classes;
  165. }
  166. /**
  167. * Build the reg-column of the table
  168. * @param int $class_id The class id
  169. * @return string Some HTML-code
  170. */
  171. function reg_filter($class_id)
  172. {
  173. $result = "<a href=\"".$_SERVER['PHP_SELF']."?register=yes&amp;class_id=".$class_id."\">".get_lang("reg")."</a>";
  174. return $result;
  175. }
  176. // Build search-form
  177. $form = new FormValidator('search_class', 'get','','',null,false);
  178. $renderer = & $form->defaultRenderer();
  179. $renderer->setElementTemplate('<span>{element}</span> ');
  180. $form->add_textfield('keyword', '', false);
  181. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  182. // Build table
  183. $table = new SortableTable('users', 'get_number_of_classes', 'get_class_data', 1);
  184. $parameters['keyword'] = $_GET['keyword'];
  185. $table->set_additional_parameters($parameters);
  186. $col = 0;
  187. $table->set_header($col ++, '', false);
  188. $table->set_header($col ++, get_lang('ClassName'));
  189. $table->set_header($col ++, get_lang('NumberOfUsers'));
  190. $table->set_header($col ++, get_lang('reg'), false);
  191. $table->set_column_filter($col -1, 'reg_filter');
  192. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'class');
  193. // Display form & table
  194. $form->display();
  195. echo '<br />';
  196. $table->display();
  197. /*
  198. ==============================================================================
  199. FOOTER
  200. ==============================================================================
  201. */
  202. Display :: display_footer();
  203. ?>