user_fields_options.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // name of the language file that needs to be included
  7. $language_file = array('admin', 'registration');
  8. // resetting the course information
  9. $cidReset = true;
  10. // including the global library
  11. require '../inc/global.inc.php';
  12. // section for the tabs
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. // user permissions
  15. api_protect_admin_script();
  16. // breadcrumbs
  17. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  18. $interbreadcrumb[] = array('url' => 'user_fields.php', 'name' => get_lang('UserFields'));
  19. $interbreadcrumb[] = array('url' => 'user_fields_add.php?action=edit&field_id=' . Security::remove_XSS($_GET['field_id']) . '&amp;sec_token=' . Security::getCurrentToken(), 'name' => get_lang('EditUserFields'));
  20. // name of the tools
  21. $tool_name = get_lang('UserFieldsSortOptions');
  22. // display header
  23. Display::display_header($tool_name);
  24. if (isset($_GET['action'])) {
  25. $check = Security::check_token('get');
  26. if ($check) {
  27. switch ($_GET['action']) {
  28. case 'moveup' :
  29. if (api_is_platform_admin() && !empty($_GET['option_id'])) {
  30. if (move_user_field_option('moveup', $_GET['option_id'])) {
  31. Display :: display_confirmation_message(get_lang('FieldOptionMovedUp'));
  32. } else {
  33. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  34. }
  35. }
  36. break;
  37. case 'movedown' :
  38. if (api_is_platform_admin() && !empty($_GET['option_id'])) {
  39. if (move_user_field_option('movedown', $_GET['option_id'])) {
  40. Display :: display_confirmation_message(get_lang('FieldOptionMovedDown'));
  41. } else {
  42. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  43. }
  44. }
  45. break;
  46. }
  47. }
  48. }
  49. // getting all the information of the field
  50. $field_info = UserManager::get_extra_field_information($_GET['field_id']);
  51. echo Display::page_header($field_info['3']);
  52. // the total number of options (used in the actions_filter function but declared here for performance reasons)
  53. $number_of_options = get_number_of_options();
  54. // displaying the sortable table
  55. $parameters['sec_token'] = Security::get_token();
  56. $parameters['field_id'] = Security::remove_XSS($_GET['field_id']);
  57. $table = new SortableTable('options', 'get_number_of_options', 'get_options_data', 2);
  58. $table->set_additional_parameters($parameters);
  59. $table->set_header(0, get_lang('DisplayOrder'), false);
  60. $table->set_header(1, get_lang('OptionText'), false);
  61. $table->set_header(2, get_lang('Actions'), false);
  62. $table->set_column_filter(2, 'actions_filter');
  63. $table->display();
  64. // display footer
  65. Display::display_footer();
  66. function get_options_data($from, $number_of_items, $column, $direction) {
  67. // Database table definition
  68. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  69. // The sql statement
  70. $sql = "SELECT
  71. option_order AS col0,
  72. option_display_text AS col1,
  73. id AS col2
  74. FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' ORDER BY option_order ASC";
  75. $sql .= " LIMIT $from,$number_of_items";
  76. $res = Database::query($sql);
  77. $return = array();
  78. while ($option = Database::fetch_row($res)) {
  79. $return[] = $option;
  80. }
  81. return $return;
  82. }
  83. function get_number_of_options($from = null, $number_of_items = null, $column = null, $direction = null) {
  84. // Database table definition
  85. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  86. // The sql statement
  87. $sql = "SELECT count(id) as total FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' ";
  88. $res = Database::query($sql);
  89. $row = Database::fetch_row($res);
  90. return $row[0];
  91. }
  92. function actions_filter($option_id, $url_params, $row) {
  93. global $number_of_options;
  94. if ($row[0] <> 1) {
  95. $return .= '<a href="' . api_get_self() . '?action=moveup&amp;option_id=' . $option_id . '&amp;field_id=' . Security::remove_XSS($_GET['field_id']) . '&amp;sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('up.gif', get_lang('Up')) . '</a>';
  96. } else {
  97. $return .= Display::return_icon('blank.gif', '', array('width' => '21px'));
  98. }
  99. // the down icon only has to appear when the row can be moved down (all but the last row)
  100. if ($row[0] <> $number_of_options) {
  101. $return .= '<a href="' . api_get_self() . '?action=movedown&amp;option_id=' . $option_id . '&amp;field_id=' . Security::remove_XSS($_GET['field_id']) . '&amp;sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('down.gif', get_lang('Down')) . '</a>';
  102. }
  103. return $return;
  104. }
  105. /**
  106. * Move a user defined field option up or down
  107. *
  108. * @param string $direction the direction we have to move the field to (up or down)
  109. * @param unknown_type $field_id
  110. *
  111. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  112. * @version July 2008
  113. * @since Dokeos 1.8.6
  114. */
  115. function move_user_field_option($direction, $option_id) {
  116. // Database table definition
  117. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  118. // check the parameters
  119. if (!in_array($direction, array('moveup', 'movedown')) OR !is_numeric($option_id)) {
  120. return false;
  121. }
  122. // determine the SQL sort direction
  123. if ($direction == 'moveup') {
  124. $sortdirection = 'DESC';
  125. } else {
  126. $sortdirection = 'ASC';
  127. }
  128. $found = false;
  129. $sql = "SELECT id, option_order FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' ORDER BY option_order $sortdirection";
  130. $result = Database::query($sql);
  131. while ($row = Database::fetch_array($result)) {
  132. if ($found) {
  133. $next_id = $row['id'];
  134. $next_order = $row['option_order'];
  135. break;
  136. }
  137. if ($option_id == $row['id']) {
  138. $this_id = $row['id'];
  139. $this_order = $row['option_order'];
  140. $found = true;
  141. }
  142. }
  143. $sql1 = "UPDATE " . $table_userfields_options . " SET option_order = '" . Database::escape_string($next_order) . "' WHERE id = '" . Database::escape_string($this_id) . "'";
  144. $sql2 = "UPDATE " . $table_userfields_options . " SET option_order = '" . Database::escape_string($this_order) . "' WHERE id = '" . Database::escape_string($next_id) . "'";
  145. Database::query($sql1);
  146. Database::query($sql2);
  147. return true;
  148. }