user_fields.php 15 KB

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