user_fields.php 15 KB

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