user_fields_options.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // Database table definitions
  17. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  18. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  19. $table_userfields = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  20. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  21. $table_userfields_values = Database :: get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  22. // breadcrumbs
  23. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  24. $interbreadcrumb[] = array ('url' => 'user_fields.php', 'name' => get_lang('UserFields'));
  25. $interbreadcrumb[] = array ('url' => 'user_fields_add.php?action=edit&field_id='.Security::remove_XSS($_GET['field_id']).'&amp;sec_token='.$_SESSION['sec_token'], 'name' => get_lang('EditUserFields'));
  26. // name of the tools
  27. $tool_name = get_lang('UserFieldsSortOptions');
  28. // display header
  29. Display::display_header($tool_name);
  30. if (isset ($_GET['action']))
  31. {
  32. $check = Security::check_token('get');
  33. if($check)
  34. {
  35. switch ($_GET['action'])
  36. {
  37. case 'moveup' :
  38. if (api_is_platform_admin() && !empty($_GET['option_id']))
  39. {
  40. if (move_user_field_option('moveup', $_GET['option_id']))
  41. {
  42. Display :: display_confirmation_message(get_lang('FieldOptionMovedUp'));
  43. }
  44. else
  45. {
  46. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  47. }
  48. }
  49. break;
  50. case 'movedown' :
  51. if (api_is_platform_admin() && !empty($_GET['option_id']))
  52. {
  53. if (move_user_field_option('movedown', $_GET['option_id']))
  54. {
  55. Display :: display_confirmation_message(get_lang('FieldOptionMovedDown'));
  56. }
  57. else
  58. {
  59. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  60. }
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. // getting all the information of the field
  67. $field_info = UserManager::get_extra_field_information($_GET['field_id']);
  68. echo '<strong>'.$field_info['3'].'</strong><br />';
  69. // the total number of options (used in the actions_filter function but declared here for performance reasons)
  70. $number_of_options = get_number_of_options();
  71. // displaying the sortable table
  72. $parameters['sec_token'] = Security::get_token();
  73. $parameters['field_id'] = Security::remove_XSS($_GET['field_id']);
  74. $table = new SortableTable('options', 'get_number_of_options', 'get_options_data',2);
  75. $table->set_additional_parameters($parameters);
  76. $table->set_header(0, get_lang('DisplayOrder'), false);
  77. $table->set_header(1, get_lang('OptionText'), false);
  78. $table->set_header(2, get_lang('Actions'), false);
  79. $table->set_column_filter(2, 'actions_filter');
  80. $table->display();
  81. // display footer
  82. Display::display_footer();
  83. function get_options_data($from, $number_of_items, $column, $direction)
  84. {
  85. // Database table definition
  86. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  87. // The sql statement
  88. $sql = "SELECT
  89. option_order AS col0,
  90. option_display_text AS col1,
  91. id AS col2
  92. FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ORDER BY option_order ASC";
  93. $sql .= " LIMIT $from,$number_of_items";
  94. $res = Database::query($sql);
  95. $return = array ();
  96. while ($option = Database::fetch_row($res))
  97. {
  98. $return[] = $option;
  99. }
  100. return $return;
  101. }
  102. function get_number_of_options($from=null, $number_of_items=null, $column=null, $direction=null)
  103. {
  104. // Database table definition
  105. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  106. // The sql statement
  107. $sql = "SELECT count(id) as total FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ";
  108. $res = Database::query($sql);
  109. $row = Database::fetch_row($res);
  110. return $row[0];
  111. }
  112. function actions_filter($option_id,$url_params,$row)
  113. {
  114. global $number_of_options;
  115. if ($row[0]<>1)
  116. {
  117. $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='.$_SESSION['sec_token'].'">'.Display::return_icon('up.gif', get_lang('Up')).'</a>';
  118. }
  119. else
  120. {
  121. $return .= Display::return_icon('blank.gif','',array('width'=>'21px'));
  122. }
  123. // the down icon only has to appear when the row can be moved down (all but the last row)
  124. if ($row[0]<>$number_of_options)
  125. {
  126. $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='.$_SESSION['sec_token'].'">'.Display::return_icon('down.gif', get_lang('Down')).'</a>';
  127. }
  128. return $return;
  129. }
  130. /**
  131. * Move a user defined field option up or down
  132. *
  133. * @param string $direction the direction we have to move the field to (up or down)
  134. * @param unknown_type $field_id
  135. *
  136. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  137. * @version July 2008
  138. * @since Dokeos 1.8.6
  139. */
  140. function move_user_field_option($direction,$option_id)
  141. {
  142. // Database table definition
  143. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  144. // check the parameters
  145. if (!in_array($direction,array('moveup','movedown')) OR !is_numeric($option_id))
  146. {
  147. return false;
  148. }
  149. // determine the SQL sort direction
  150. if ($direction == 'moveup')
  151. {
  152. $sortdirection = 'DESC';
  153. }
  154. else
  155. {
  156. $sortdirection = 'ASC';
  157. }
  158. $found = false;
  159. $sql = "SELECT id, option_order FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ORDER BY option_order $sortdirection";
  160. $result = Database::query($sql);
  161. while($row = Database::fetch_array($result))
  162. {
  163. if ($found)
  164. {
  165. $next_id = $row['id'];
  166. $next_order = $row['option_order'];
  167. break;
  168. }
  169. if ($option_id == $row['id'])
  170. {
  171. $this_id = $row['id'];
  172. $this_order = $row['option_order'];
  173. $found = true;
  174. }
  175. }
  176. $sql1 = "UPDATE ".$table_userfields_options." SET option_order = '".Database::escape_string($next_order)."' WHERE id = '".Database::escape_string($this_id)."'";
  177. $sql2 = "UPDATE ".$table_userfields_options." SET option_order = '".Database::escape_string($this_order)."' WHERE id = '".Database::escape_string($next_id)."'";
  178. Database::query($sql1);
  179. Database::query($sql2);
  180. return true;
  181. }
  182. ?>