class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // $Id: class.php 9119 2006-08-18 07:10:23Z bmol $
  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 manage subscribed
  22. *classes in their course.
  23. * @package dokeos.user
  24. ==============================================================================
  25. */
  26. /*
  27. ==============================================================================
  28. INIT SECTION
  29. ==============================================================================
  30. */
  31. // name of the language file that needs to be included
  32. $language_file = array('registration','admin');
  33. include ('../inc/global.inc.php');
  34. $this_section = SECTION_COURSES;
  35. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  36. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  37. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  38. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  39. /*
  40. ==============================================================================
  41. MAIN CODE
  42. ==============================================================================
  43. */
  44. api_protect_course_script();
  45. if(api_get_setting('use_session_mode')=='true')
  46. {
  47. api_not_allowed();
  48. }
  49. /*
  50. -----------------------------------------------------------
  51. Header
  52. -----------------------------------------------------------
  53. */
  54. $tool_name = get_lang("Classes");
  55. //extra entries in breadcrumb
  56. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  57. Display :: display_header($tool_name, "User");
  58. api_display_tool_title($tool_name);
  59. if (api_is_allowed_to_edit())
  60. {
  61. echo '<a href="subscribe_class.php?'.api_get_cidreq().'">'.get_lang("AddClassesToACourse").'</a><br /><br />';
  62. }
  63. /*
  64. ==============================================================================
  65. MAIN SECTION
  66. ==============================================================================
  67. */
  68. if(api_is_allowed_to_edit())
  69. {
  70. if (isset ($_GET['unsubscribe']))
  71. {
  72. ClassManager::unsubscribe_from_course($_GET['class_id'],$_course['sysCode']);
  73. Display::display_normal_message(get_lang('ClassesUnSubscribed'));
  74. }
  75. if (isset ($_POST['action']))
  76. {
  77. switch ($_POST['action'])
  78. {
  79. case 'unsubscribe' :
  80. if (is_array($_POST['class']))
  81. {
  82. foreach ($_POST['class'] as $index => $class_id)
  83. {
  84. ClassManager::unsubscribe_from_course($class_id,$_course['sysCode']);
  85. }
  86. Display::display_normal_message(get_lang('ClassesUnSubscribed'));
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. /*
  93. -----------------------------------------------------------
  94. SHOW LIST OF CLASSES
  95. -----------------------------------------------------------
  96. */
  97. /**
  98. * * Get the number of classes to display on the current page.
  99. */
  100. function get_number_of_classes()
  101. {
  102. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  103. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  104. $sql = "SELECT c.id FROM $class_table c, $course_class_table cc WHERE cc.class_id = c.id AND cc.course_code ='".$_SESSION['_course']['id']."'";
  105. if (isset ($_GET['keyword']))
  106. {
  107. $keyword = mysql_real_escape_string($_GET['keyword']);
  108. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  109. }
  110. $res = api_sql_query($sql, __FILE__, __LINE__);
  111. $result = mysql_num_rows($res);
  112. return $result;
  113. }
  114. /**
  115. * Get the classes to display on the current page.
  116. */
  117. function get_class_data($from, $number_of_items, $column, $direction)
  118. {
  119. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  120. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  121. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  122. $sql = "SELECT
  123. c.id AS col0,
  124. c.name AS col1,
  125. COUNT(cu.user_id) AS col2";
  126. if (api_is_allowed_to_edit())
  127. {
  128. $sql .= " ,c.id AS col3";
  129. }
  130. // '(' and ')' in next query is necessary (see http://bugs.mysql.com/bug.php?id=19053)
  131. $sql .= " FROM ($class_table c, $course_class_table cc)";
  132. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  133. $sql .= " WHERE c.id = cc.class_id AND cc.course_code = '".$_SESSION['_course']['id']."'";
  134. if (isset ($_GET['keyword']))
  135. {
  136. $keyword = mysql_real_escape_string($_GET['keyword']);
  137. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  138. }
  139. $sql .= " GROUP BY c.id, c.name ";
  140. $sql .= " ORDER BY col$column $direction ";
  141. $sql .= " LIMIT $from,$number_of_items";
  142. $res = api_sql_query($sql, __FILE__, __LINE__);
  143. $classes = array ();
  144. while ($class = mysql_fetch_row($res))
  145. {
  146. $classes[] = $class;
  147. }
  148. return $classes;
  149. }
  150. /**
  151. * Build the reg-column of the table
  152. * @param int $class_id The class id
  153. * @return string Some HTML-code
  154. */
  155. function reg_filter($class_id)
  156. {
  157. global $charset;
  158. $result = '<a href="'.api_get_self().'?'.api_get_cidreq().'&unsubscribe=yes&amp;class_id='.$class_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)).'\')) return false;"><img src="../img/delete.gif"/></a>';
  159. return $result;
  160. }
  161. // Build search-form
  162. $form = new FormValidator('search_class', 'get','','',null,false);
  163. $renderer = & $form->defaultRenderer();
  164. $renderer->setElementTemplate('<span>{element}</span> ');
  165. $form->add_textfield('keyword', '', false);
  166. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  167. // Build table
  168. $table = new SortableTable('users', 'get_number_of_classes', 'get_class_data', 1);
  169. $parameters['keyword'] = $_GET['keyword'];
  170. $table->set_additional_parameters($parameters);
  171. $col = 0;
  172. $table->set_header($col ++, '', false);
  173. $table->set_header($col ++, get_lang('ClassName'));
  174. $table->set_header($col ++, get_lang('NumberOfUsers'));
  175. if (api_is_allowed_to_edit())
  176. {
  177. $table->set_header($col ++, '', false);
  178. $table->set_column_filter($col -1, 'reg_filter');
  179. $table->set_form_actions(array ('unsubscribe' => get_lang('Unreg')), 'class');
  180. }
  181. // Display form & table
  182. $form->display();
  183. echo '<br />';
  184. $table->display();
  185. /*
  186. ==============================================================================
  187. FOOTER
  188. ==============================================================================
  189. */
  190. Display :: display_footer();
  191. ?>