class.php 4.9 KB

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