user_fields_options.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php // $Id: user_fields_add.php 15740 2008-07-07 09:30:44Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  14. ==============================================================================
  15. */
  16. /**
  17. ==============================================================================
  18. * @package dokeos.admin
  19. ==============================================================================
  20. */
  21. // name of the language file that needs to be included
  22. $language_file = array('admin','registration');
  23. // resetting the course information
  24. $cidReset = true;
  25. // including the global library
  26. require ('../inc/global.inc.php');
  27. // including additional libraries
  28. $libpath = api_get_path(LIBRARY_PATH);
  29. include_once ($libpath.'usermanager.lib.php');
  30. require_once ($libpath.'formvalidator/FormValidator.class.php');
  31. // section for the tabs
  32. $this_section=SECTION_PLATFORM_ADMIN;
  33. // user permissions
  34. api_protect_admin_script();
  35. // Database table definitions
  36. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  37. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  38. $table_userfields = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  39. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  40. $table_userfields_values = Database :: get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  41. // breadcrumbs
  42. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  43. $interbreadcrumb[] = array ('url' => 'user_fields.php', 'name' => get_lang('UserFields'));
  44. $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'));
  45. // name of the tools
  46. $tool_name = get_lang('UserFieldsSortOptions');
  47. // display header
  48. Display::display_header($tool_name);
  49. if (isset ($_GET['action']))
  50. {
  51. $check = Security::check_token('get');
  52. if($check)
  53. {
  54. switch ($_GET['action'])
  55. {
  56. case 'moveup' :
  57. if (api_is_platform_admin() && !empty($_GET['option_id']))
  58. {
  59. if (move_user_field_option('moveup', $_GET['option_id']))
  60. {
  61. Display :: display_confirmation_message(get_lang('FieldOptionMovedUp'));
  62. }
  63. else
  64. {
  65. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  66. }
  67. }
  68. break;
  69. case 'movedown' :
  70. if (api_is_platform_admin() && !empty($_GET['option_id']))
  71. {
  72. if (move_user_field_option('movedown', $_GET['option_id']))
  73. {
  74. Display :: display_confirmation_message(get_lang('FieldOptionMovedDown'));
  75. }
  76. else
  77. {
  78. Display :: display_error_message(get_lang('CannotMoveFieldOption'));
  79. }
  80. }
  81. break;
  82. }
  83. }
  84. }
  85. // getting all the information of the field
  86. $field_info = UserManager::get_extra_field_information($_GET['field_id']);
  87. echo '<strong>'.$field_info['3'].'</strong><br />';
  88. // the total number of options (used in the actions_filter function but declared here for performance reasons)
  89. $number_of_options = get_number_of_options();
  90. // displaying the sortable table
  91. $parameters['sec_token'] = Security::get_token();
  92. $parameters['field_id'] = Security::remove_XSS($_GET['field_id']);
  93. $table = new SortableTable('options', 'get_number_of_options', 'get_options_data',2);
  94. $table->set_additional_parameters($parameters);
  95. $table->set_header(0, get_lang('DisplayOrder'), false);
  96. $table->set_header(1, get_lang('OptionText'), false);
  97. $table->set_header(2, get_lang('Actions'), false);
  98. $table->set_column_filter(2, 'actions_filter');
  99. $table->display();
  100. // display footer
  101. Display::display_footer();
  102. function get_options_data($from, $number_of_items, $column, $direction)
  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
  108. option_order AS col0,
  109. option_display_text AS col1,
  110. id AS col2
  111. FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ORDER BY option_order ASC";
  112. $sql .= " LIMIT $from,$number_of_items";
  113. $res = api_sql_query($sql, __FILE__, __LINE__);
  114. $return = array ();
  115. while ($option = Database::fetch_row($res))
  116. {
  117. $return[] = $option;
  118. }
  119. return $return;
  120. }
  121. function get_number_of_options($from=null, $number_of_items=null, $column=null, $direction=null)
  122. {
  123. // Database table definition
  124. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  125. // The sql statement
  126. $sql = "SELECT count(id) as total FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ";
  127. $res = api_sql_query($sql, __FILE__, __LINE__);
  128. $row = Database::fetch_row($res);
  129. return $row[0];
  130. }
  131. function actions_filter($option_id,$url_params,$row)
  132. {
  133. global $number_of_options;
  134. if ($row[0]<>1)
  135. {
  136. $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>';
  137. }
  138. else
  139. {
  140. $return .= Display::return_icon('blank.gif','',array('width'=>'21px'));
  141. }
  142. // the down icon only has to appear when the row can be moved down (all but the last row)
  143. if ($row[0]<>$number_of_options)
  144. {
  145. $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>';
  146. }
  147. return $return;
  148. }
  149. /**
  150. * Move a user defined field option up or down
  151. *
  152. * @param string $direction the direction we have to move the field to (up or down)
  153. * @param unknown_type $field_id
  154. *
  155. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  156. * @version July 2008
  157. * @since Dokeos 1.8.6
  158. */
  159. function move_user_field_option($direction,$option_id)
  160. {
  161. // Database table definition
  162. $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  163. // check the parameters
  164. if (!in_array($direction,array('moveup','movedown')) OR !is_numeric($option_id))
  165. {
  166. return false;
  167. }
  168. // determine the SQL sort direction
  169. if ($direction == 'moveup')
  170. {
  171. $sortdirection = 'DESC';
  172. }
  173. else
  174. {
  175. $sortdirection = 'ASC';
  176. }
  177. $found = false;
  178. $sql = "SELECT id, option_order FROM $table_userfields_options WHERE field_id='".Database::escape_string($_GET['field_id'])."' ORDER BY option_order $sortdirection";
  179. $result = api_sql_query($sql,__FILE__,__LINE__);
  180. while($row = Database::fetch_array($result))
  181. {
  182. if ($found)
  183. {
  184. $next_id = $row['id'];
  185. $next_order = $row['option_order'];
  186. break;
  187. }
  188. if ($option_id == $row['id'])
  189. {
  190. $this_id = $row['id'];
  191. $this_order = $row['option_order'];
  192. $found = true;
  193. }
  194. }
  195. $sql1 = "UPDATE ".$table_userfields_options." SET option_order = '".Database::escape_string($next_order)."' WHERE id = '".Database::escape_string($this_id)."'";
  196. $sql2 = "UPDATE ".$table_userfields_options." SET option_order = '".Database::escape_string($this_order)."' WHERE id = '".Database::escape_string($next_id)."'";
  197. api_sql_query($sql1,__FILE__,__LINE__);
  198. api_sql_query($sql2,__FILE__,__LINE__);
  199. return true;
  200. }
  201. ?>