user_fields_options.php 6.7 KB

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