subscribe_class.php 6.2 KB

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