class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.user
  5. */
  6. /**
  7. * INIT SECTION
  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. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  14. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  15. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  16. /**
  17. * MAIN CODE
  18. */
  19. api_protect_course_script();
  20. if (api_get_setting('use_session_mode')=='true') {
  21. api_not_allowed();
  22. }
  23. $tool_name = get_lang("Classes");
  24. //extra entries in breadcrumb
  25. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("ToolUser"));
  26. Display :: display_header($tool_name, "User");
  27. api_display_tool_title($tool_name);
  28. if (api_is_allowed_to_edit()) {
  29. echo '<a href="subscribe_class.php?'.api_get_cidreq().'">'.get_lang("AddClassesToACourse").'</a><br /><br />';
  30. }
  31. /* MAIN SECTION*/
  32. if(api_is_allowed_to_edit())
  33. {
  34. if (isset ($_GET['unsubscribe']))
  35. {
  36. ClassManager::unsubscribe_from_course($_GET['class_id'],$_course['sysCode']);
  37. Display::display_normal_message(get_lang('ClassesUnSubscribed'));
  38. }
  39. if (isset ($_POST['action']))
  40. {
  41. switch ($_POST['action'])
  42. {
  43. case 'unsubscribe' :
  44. if (is_array($_POST['class']))
  45. {
  46. foreach ($_POST['class'] as $index => $class_id)
  47. {
  48. ClassManager::unsubscribe_from_course($class_id,$_course['sysCode']);
  49. }
  50. Display::display_normal_message(get_lang('ClassesUnSubscribed'));
  51. }
  52. break;
  53. }
  54. }
  55. }
  56. /*
  57. SHOW LIST OF CLASSES
  58. */
  59. /**
  60. * * Get the number of classes to display on the current page.
  61. */
  62. function get_number_of_classes()
  63. {
  64. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  65. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  66. $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']."'";
  67. if (isset ($_GET['keyword']))
  68. {
  69. $keyword = Database::escape_string(trim($_GET['keyword']));
  70. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  71. }
  72. $res = Database::query($sql);
  73. $result = Database::num_rows($res);
  74. return $result;
  75. }
  76. /**
  77. * Get the classes to display on the current page.
  78. */
  79. function get_class_data($from, $number_of_items, $column, $direction)
  80. {
  81. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  82. $course_class_table = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  83. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  84. $sql = "SELECT
  85. c.id AS col0,
  86. c.name AS col1,
  87. COUNT(cu.user_id) AS col2";
  88. if (api_is_allowed_to_edit())
  89. {
  90. $sql .= " ,c.id AS col3";
  91. }
  92. // '(' and ')' in next query is necessary (see http://bugs.mysql.com/bug.php?id=19053)
  93. $sql .= " FROM ($class_table c, $course_class_table cc)";
  94. $sql .= " LEFT JOIN $class_user_table cu ON cu.class_id = c.id";
  95. $sql .= " WHERE c.id = cc.class_id AND cc.course_code = '".$_SESSION['_course']['id']."'";
  96. if (isset ($_GET['keyword']))
  97. {
  98. $keyword = Database::escape_string(trim($_GET['keyword']));
  99. $sql .= " AND (c.name LIKE '%".$keyword."%')";
  100. }
  101. $sql .= " GROUP BY c.id, c.name ";
  102. $sql .= " ORDER BY col$column $direction ";
  103. $sql .= " LIMIT $from,$number_of_items";
  104. $res = Database::query($sql);
  105. $classes = array ();
  106. while ($class = Database::fetch_row($res))
  107. {
  108. $classes[] = $class;
  109. }
  110. return $classes;
  111. }
  112. /**
  113. * Build the reg-column of the table
  114. * @param int $class_id The class id
  115. * @return string Some HTML-code
  116. */
  117. function reg_filter($class_id)
  118. {
  119. global $charset;
  120. $result = '<a href="'.api_get_self().'?'.api_get_cidreq().'&unsubscribe=yes&amp;class_id='.$class_id.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)).'\')) return false;"><img src="../img/delete.gif"/></a>';
  121. return $result;
  122. }
  123. // Build search-form
  124. $form = new FormValidator('search_class', 'get','','',null,false);
  125. $renderer = & $form->defaultRenderer();
  126. $renderer->setElementTemplate('<span>{element}</span> ');
  127. $form->add_textfield('keyword', '', false);
  128. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  129. // Build table
  130. $table = new SortableTable('user_class', 'get_number_of_classes', 'get_class_data', 1);
  131. $parameters['keyword'] = $_GET['keyword'];
  132. $table->set_additional_parameters($parameters);
  133. $col = 0;
  134. $table->set_header($col ++, '', false);
  135. $table->set_header($col ++, get_lang('ClassName'));
  136. $table->set_header($col ++, get_lang('NumberOfUsers'));
  137. if (api_is_allowed_to_edit())
  138. {
  139. $table->set_header($col ++, '', false);
  140. $table->set_column_filter($col -1, 'reg_filter');
  141. $table->set_form_actions(array ('unsubscribe' => get_lang('Unreg')), 'class');
  142. }
  143. // Display form & table
  144. $form->display();
  145. echo '<br />';
  146. $table->display();
  147. Display :: display_footer();