class.php 4.5 KB

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