user_fields.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. $cidReset = true;
  9. // including necessary libraries
  10. require '../inc/global.inc.php';
  11. // section for the tabs
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. // user permissions
  14. api_protect_admin_script();
  15. // Database table definitions
  16. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  17. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  18. $table_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  19. $table_uf_opt = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  20. $table_uf_val = Database :: get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  21. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  22. // Display form
  23. $tool_name = get_lang('UserFields');
  24. Display :: display_header($tool_name);
  25. if (isset($_GET['action'])) {
  26. $check = Security::check_token('get');
  27. if ($check) {
  28. switch ($_GET['action']) {
  29. case 'show_message' :
  30. Display :: display_normal_message($_GET['message']);
  31. break;
  32. case 'show_field' :
  33. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_visible' => '1'))) {
  34. Display :: display_confirmation_message(get_lang('FieldShown'));
  35. } else {
  36. Display :: display_error_message(get_lang('CannotShowField'));
  37. }
  38. break;
  39. case 'hide_field' :
  40. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_visible' => '0'))) {
  41. Display :: display_confirmation_message(get_lang('FieldHidden'));
  42. } else {
  43. Display :: display_error_message(get_lang('CannotHideField'));
  44. }
  45. break;
  46. case 'thaw_field' :
  47. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_changeable' => '1'))) {
  48. Display :: display_confirmation_message(get_lang('FieldMadeChangeable'));
  49. } else {
  50. Display :: display_error_message(get_lang('CannotMakeFieldChangeable'));
  51. }
  52. break;
  53. case 'freeze_field' :
  54. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_changeable' => '0'))) {
  55. Display :: display_confirmation_message(get_lang('FieldMadeUnchangeable'));
  56. } else {
  57. Display :: display_error_message(get_lang('CannotMakeFieldUnchangeable'));
  58. }
  59. break;
  60. case 'moveup' :
  61. if (api_is_platform_admin() && !empty($_GET['field_id'])) {
  62. if (move_user_field('moveup', $_GET['field_id'])) {
  63. Display :: display_confirmation_message(get_lang('FieldMovedUp'));
  64. } else {
  65. Display :: display_error_message(get_lang('CannotMoveField'));
  66. }
  67. }
  68. break;
  69. case 'movedown' :
  70. if (api_is_platform_admin() && !empty($_GET['field_id'])) {
  71. if (move_user_field('movedown', $_GET['field_id'])) {
  72. Display :: display_confirmation_message(get_lang('FieldMovedDown'));
  73. } else {
  74. Display :: display_error_message(get_lang('CannotMoveField'));
  75. }
  76. }
  77. break;
  78. case 'filter_on' :
  79. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_filter' => '1'))) {
  80. Display :: display_confirmation_message(get_lang('FieldFilterSetOn'));
  81. } else {
  82. Display :: display_error_message(get_lang('CannotShowField'));
  83. }
  84. break;
  85. case 'filter_off' :
  86. if (api_is_platform_admin() && !empty($_GET['field_id']) && UserManager :: update_extra_field($_GET['field_id'], array('field_filter' => '0'))) {
  87. Display :: display_confirmation_message(get_lang('FieldFilterSetOff'));
  88. } else {
  89. Display :: display_error_message(get_lang('CannotShowField'));
  90. }
  91. break;
  92. case 'delete':
  93. if (api_is_platform_admin() && !empty($_GET['field_id'])) {
  94. if (delete_user_fields($_GET['field_id'])) {
  95. Display :: display_confirmation_message(get_lang('FieldDeleted'));
  96. } else {
  97. Display :: display_error_message(get_lang('CannotDeleteField'));
  98. }
  99. }
  100. break;
  101. }
  102. Security::clear_token();
  103. }
  104. }
  105. if (isset($_POST['action'])) {
  106. $check = Security::check_token('get');
  107. if ($check) {
  108. switch ($_POST['action']) {
  109. default:
  110. break;
  111. }
  112. Security::clear_token();
  113. }
  114. }
  115. // Create an add-field box
  116. $form = new FormValidator('add_field', 'post', '', '', null, false);
  117. $renderer = & $form->defaultRenderer();
  118. $renderer->setElementTemplate('<span>{element}</span> ');
  119. //$form->addElement('text','label',get_lang('FieldLabel'));
  120. //$form->addElement('text','type',get_lang('FieldType'));
  121. //$form->addElement('text','title',get_lang('FieldTitle'));
  122. //$form->addElement('text','default',get_lang('FieldDefaultValue'));
  123. //$form->addElement('submit','submit',get_lang('Search'));
  124. $form->addElement('static', 'search_advanced_link', null, '<a href="user_fields_add.php?action=fill">' . Display::return_icon('add_user_fields.png', get_lang('AddUserField'), '', ICON_SIZE_MEDIUM) . '</a>');
  125. echo '<div class="actions">';
  126. $form->display();
  127. echo '</div>';
  128. // Create a sortable table with user-data
  129. $parameters['sec_token'] = Security::get_token();
  130. //$column_show = array(1,1,1,1,1,1,1,1,1,0,0);
  131. //$column_order = array(1,2,3,4,5,6,7,8,9,10,11);
  132. $extra_fields = UserManager::get_extra_fields();
  133. $number_of_extra_fields = count($extra_fields);
  134. $table = new SortableTable('user_field', array('UserManager', 'get_number_of_extra_fields'), array('UserManager', 'get_extra_fields'), 5);
  135. $table->set_additional_parameters($parameters);
  136. $table->set_header(0, '', false);
  137. $table->set_header(1, get_lang('FieldLabel'), false);
  138. $table->set_header(2, get_lang('FieldType'), false);
  139. $table->set_header(3, get_lang('FieldTitle'), false);
  140. $table->set_header(4, get_lang('FieldDefaultValue'), false);
  141. $table->set_header(5, get_lang('FieldOrder'), false);
  142. $table->set_header(6, get_lang('FieldVisibility'), false);
  143. $table->set_header(7, get_lang('FieldChangeability'), false);
  144. $table->set_header(8, get_lang('FieldFilter'), false);
  145. $table->set_header(9, get_lang('Modify'), false);
  146. $table->set_column_filter(5, 'order_filter');
  147. $table->set_column_filter(6, 'modify_visibility');
  148. $table->set_column_filter(7, 'modify_changeability');
  149. $table->set_column_filter(8, 'modify_field_filter');
  150. $table->set_column_filter(9, 'edit_filter');
  151. $table->set_column_filter(2, 'type_filter');
  152. $table->display();
  153. Display::display_footer();
  154. //gateway functions to the UserManager methods (provided for SorteableTable callback mechanism)
  155. function get_number_of_extra_fields() {
  156. return UserManager::get_number_of_extra_fields();
  157. }
  158. function get_extra_fields($f, $n, $o, $d) {
  159. return UserManager::get_extra_fields($f, $n, $o, $d);
  160. }
  161. /**
  162. * This functions translates the id of the form type into a human readable description
  163. *
  164. * @param integer $type the id of the form type
  165. * @return string the huma readable description of the field type (text, date, select drop-down, ...)
  166. *
  167. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  168. * @version July 2008
  169. * @since Dokeos 1.8.6
  170. */
  171. function type_filter($type) {
  172. $types = UserManager::get_user_field_types();
  173. return $types[$type];
  174. }
  175. /**
  176. * Modify the display order field into up and down arrows
  177. *
  178. * @param unknown_type $field_order
  179. * @param array Url parameters
  180. * @param array The results row
  181. * @return string The link
  182. *
  183. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  184. * @version July 2008
  185. * @since Dokeos 1.8.6
  186. */
  187. function order_filter($field_order, $url_params, $row) {
  188. global $number_of_extra_fields;
  189. $return = '';
  190. // the up icon only has to appear when the row can be moved up (all but the first row)
  191. if ($row[5] <> 1) {
  192. $return .= '<a href="' . api_get_self() . '?action=moveup&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('up.gif', get_lang('Up')) . '</a>';
  193. } else {
  194. $return .= Display::return_icon('blank.gif', '', array('width' => '21px'));
  195. }
  196. // the down icon only has to appear when the row can be moved down (all but the last row)
  197. if ($row[5] <> $number_of_extra_fields) {
  198. $return .= '<a href="' . api_get_self() . '?action=movedown&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('down.gif', get_lang('Down')) . '</a>';
  199. }
  200. return $return;
  201. }
  202. /**
  203. * Modify the visible field to show links and icons
  204. * @param int The current visibility
  205. * @param array Url parameters
  206. * @param array The results row
  207. * @return string The link
  208. */
  209. function modify_visibility($visibility, $url_params, $row) {
  210. return ($visibility ? '<a href="' . api_get_self() . '?action=hide_field&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('visible.gif', get_lang('Hide')) . '</a>' : '<a href="' . api_get_self() . '?action=show_field&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('invisible.gif', get_lang('Show')) . '</a>');
  211. }
  212. /**
  213. * Modify the changeability field to show links and icons
  214. * @param int The current changeability
  215. * @param array Url parameters
  216. * @param array The results row
  217. * @return string The link
  218. */
  219. function modify_changeability($changeability, $url_params, $row) {
  220. return ($changeability ? '<a href="' . api_get_self() . '?action=freeze_field&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('right.gif', get_lang('MakeUnchangeable')) . '</a>' : '<a href="' . api_get_self() . '?action=thaw_field&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('wrong.gif', get_lang('MakeChangeable')) . '</a>');
  221. }
  222. function modify_field_filter($changeability, $url_params, $row) {
  223. return ($changeability ? '<a href="' . api_get_self() . '?action=filter_off&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('right.gif', get_lang('FilterOff')) . '</a>' : '' .
  224. '<a href="' . api_get_self() . '?action=filter_on&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('wrong.gif', get_lang('FilterOn')) . '</a>');
  225. }
  226. function edit_filter($id, $url_params, $row) {
  227. global $charset;
  228. $return = '<a href="user_fields_add.php?action=edit&field_id=' . $row[0] . '&field_type=' . $row[2] . '&sec_token=' . Security::getCurrentToken() . '">' . Display::return_icon('edit.png', get_lang('Edit')) . '</a>';
  229. $return .= ' <a href="' . api_get_self() . '?action=delete&field_id=' . $row[0] . '&sec_token=' . Security::getCurrentToken() . '" onclick="javascript:if(!confirm(' . "'" . addslashes(get_lang("ConfirmYourChoice")) . "'" . ')) return false;">' .
  230. Display::return_icon('delete.png', get_lang('Delete')) . '</a>';
  231. return $return;
  232. }
  233. /**
  234. * Move a user defined field up or down
  235. *
  236. * @param string $direction the direction we have to move the field to (up or down)
  237. * @param unknown_type $field_id
  238. *
  239. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  240. * @version July 2008
  241. * @since Dokeos 1.8.6
  242. */
  243. function move_user_field($direction, $field_id) {
  244. // Databse table definitions
  245. $table_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  246. // check the parameters
  247. if (!in_array($direction, array('moveup', 'movedown')) OR !is_numeric($field_id)) {
  248. return false;
  249. }
  250. // determine the SQL sort direction
  251. if ($direction == 'moveup') {
  252. $sortdirection = 'DESC';
  253. } else {
  254. $sortdirection = 'ASC';
  255. }
  256. // first reorder user_fields
  257. reorder_user_fields();
  258. $found = false;
  259. $sql = "SELECT id, field_order FROM $table_user_field ORDER BY field_order $sortdirection";
  260. $result = Database::query($sql);
  261. while ($row = Database::fetch_array($result)) {
  262. if ($found) {
  263. $next_id = $row['id'];
  264. $next_order = $row['field_order'];
  265. break;
  266. }
  267. if ($field_id == $row['id']) {
  268. $this_id = $row['id'];
  269. $this_order = $row['field_order'];
  270. $found = true;
  271. }
  272. }
  273. $sql1 = "UPDATE " . $table_user_field . " SET field_order = '" . intval($next_order) . "' WHERE id = '" . intval($this_id) . "'";
  274. $sql2 = "UPDATE " . $table_user_field . " SET field_order = '" . intval($this_order) . "' WHERE id = '" . intval($next_id) . "'";
  275. Database::query($sql1);
  276. Database::query($sql2);
  277. return true;
  278. }
  279. /**
  280. * Re-order user fields
  281. */
  282. function reorder_user_fields() {
  283. // Database table definition
  284. $t_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  285. $sql = "SELECT * FROM $t_user_field ORDER by field_order ASC";
  286. $res = Database::query($sql);
  287. $i = 1;
  288. while ($row = Database::fetch_array($res)) {
  289. $sql_reorder = "UPDATE $t_user_field SET field_order = $i WHERE id = '" . $row['id'] . "'";
  290. Database::query($sql_reorder);
  291. $i++;
  292. }
  293. }
  294. /**
  295. * Delete a user field (and also the options and values entered by the users)
  296. *
  297. * @param integer $field_id the id of the field that has to be deleted
  298. * @return boolean true if the field has been deleted, false if the field could not be deleted (for whatever reason)
  299. *
  300. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  301. * @version July 2008
  302. * @since Dokeos 1.8.6
  303. */
  304. function delete_user_fields($field_id) {
  305. // Database table definitions
  306. $table_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  307. $table_user_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  308. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  309. // delete the fields
  310. $sql = "DELETE FROM $table_user_field WHERE id = '" . Database::escape_string($field_id) . "'";
  311. $result = Database::query($sql);
  312. if (Database::affected_rows($result) == 1) {
  313. // delete the field options
  314. $sql = "DELETE FROM $table_user_field_options WHERE field_id = '" . Database::escape_string($field_id) . "'";
  315. $result = Database::query($sql);
  316. // delete the field values
  317. $sql = "DELETE FROM $table_user_field_values WHERE field_id = '" . Database::escape_string($field_id) . "'";
  318. $result = Database::query($sql);
  319. // recalculate the field_order because the value is used to show/hide the up/down icon
  320. // and the field_order value cannot be bigger than the number of fields
  321. $sql = "SELECT * FROM $table_user_field ORDER BY field_order ASC";
  322. $result = Database::query($sql);
  323. $i = 1;
  324. while ($row = Database::fetch_array($result)) {
  325. $sql_reorder = "UPDATE $table_user_field SET field_order = '" . Database::escape_string($i) . "' WHERE id = '" . Database::escape_string($row['id']) . "'";
  326. $result_reorder = Database::query($sql_reorder);
  327. $i++;
  328. }
  329. // field was deleted so we return true
  330. return true;
  331. } else {
  332. // the field was not deleted so we return false
  333. return false;
  334. }
  335. }