user_fields.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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">'.get_lang('AddUserField').'</a>');
  162. $form->display();
  163. // Create a sortable table with user-data
  164. $parameters['sec_token'] = Security::get_token();
  165. $column_show = array(1,1,1,1,1,1,1,1,0,0);
  166. $column_order = array(1,2,3,4,5,6,7,8,9,10);
  167. $extra_fields = UserManager::get_extra_fields(0,100,5,'ASC');
  168. $number_of_extra_fields = count($extra_fields);
  169. $table = new SortableTableFromArrayConfig($extra_fields, 5, 50, '', $column_show, $column_order, 'ASC');
  170. $table->set_additional_parameters($parameters);
  171. $table->set_header(0, '', false);
  172. $table->set_header(1, get_lang('FieldLabel'));
  173. $table->set_header(2, get_lang('FieldType'));
  174. $table->set_header(3, get_lang('FieldTitle'),false);
  175. $table->set_header(4, get_lang('FieldDefaultValue'),false);
  176. $table->set_header(5, get_lang('FieldOrder'));
  177. $table->set_header(6, get_lang('FieldVisibility'));
  178. $table->set_header(7, get_lang('FieldChangeability'));
  179. $table->set_header(8, get_lang('Modify'));
  180. $table->set_column_filter(5, 'order_filter');
  181. $table->set_column_filter(6, 'modify_visibility');
  182. $table->set_column_filter(7, 'modify_changeability');
  183. $table->set_column_filter(8, 'edit_filter');
  184. $table->set_column_filter(2, 'type_filter');
  185. $table->display();
  186. }
  187. /*
  188. ==============================================================================
  189. FOOTER
  190. ==============================================================================
  191. */
  192. Display::display_footer();
  193. //gateway functions to the UserManager methods (provided for SorteableTable callback mechanism)
  194. function get_number_of_extra_fields()
  195. {
  196. return UserManager::get_number_of_extra_fields();
  197. }
  198. function get_extra_fields($f,$n,$o,$d)
  199. {
  200. return UserManager::get_extra_fields($f,$n,$o,$d);
  201. }
  202. /**
  203. * This functions translates the id of the form type into a human readable description
  204. *
  205. * @param integer $type the id of the form type
  206. * @return string the huma readable description of the field type (text, date, select drop-down, ...)
  207. *
  208. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  209. * @version July 2008
  210. * @since Dokeos 1.8.6
  211. */
  212. function type_filter($type)
  213. {
  214. $types[USER_FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  215. $types[USER_FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  216. $types[USER_FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  217. $types[USER_FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  218. $types[USER_FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  219. $types[USER_FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  220. $types[USER_FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  221. $types[USER_FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  222. $types[USER_FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  223. return $types[$type];
  224. }
  225. /**
  226. * Modify the display order field into up and down arrows
  227. *
  228. * @param unknown_type $field_order
  229. * @param array Url parameters
  230. * @param array The results row
  231. * @return string The link
  232. *
  233. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  234. * @version July 2008
  235. * @since Dokeos 1.8.6
  236. */
  237. function order_filter($field_order,$url_params,$row)
  238. {
  239. global $number_of_extra_fields;
  240. // the up icon only has to appear when the row can be moved up (all but the first row)
  241. if ($row[5]<>1)
  242. {
  243. $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>';
  244. }
  245. else
  246. {
  247. $return .= Display::return_icon('blank.gif','',array('width'=>'21px'));
  248. }
  249. // the down icon only has to appear when the row can be moved down (all but the last row)
  250. if ($row[5]<>$number_of_extra_fields)
  251. {
  252. $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>';
  253. }
  254. return $return;
  255. }
  256. /**
  257. * Modify the visible field to show links and icons
  258. * @param int The current visibility
  259. * @param array Url parameters
  260. * @param array The results row
  261. * @return string The link
  262. */
  263. function modify_visibility($visibility,$url_params,$row)
  264. {
  265. return ($visibility?'<a href="'.api_get_self().'?action=hide_field&field_id='.$row[0].'&sec_token='.$_SESSION['sec_token'].'"><img src="'.api_get_path(WEB_IMG_PATH).'visible.gif" alt="'.get_lang('Hide').'" /></a>':'<a href="'.api_get_self().'?action=show_field&field_id='.$row[0].'&sec_token='.$_SESSION['sec_token'].'"><img src="'.api_get_path(WEB_IMG_PATH).'invisible.gif" alt="'.get_lang('Show').'" /></a>');
  266. }
  267. /**
  268. * Modify the changeability field to show links and icons
  269. * @param int The current changeability
  270. * @param array Url parameters
  271. * @param array The results row
  272. * @return string The link
  273. */
  274. function modify_changeability($changeability,$url_params,$row)
  275. {
  276. return ($changeability?'<a href="'.api_get_self().'?action=freeze_field&field_id='.$row[0].'&sec_token='.$_SESSION['sec_token'].'"><img src="'.api_get_path(WEB_IMG_PATH).'right.gif" alt="'.get_lang('MakeUnchangeable').'" /></a>':'<a href="'.api_get_self().'?action=thaw_field&field_id='.$row[0].'&sec_token='.$_SESSION['sec_token'].'"><img src="'.api_get_path(WEB_IMG_PATH).'wrong.gif" alt="'.get_lang('MakeChangeable').'" /></a>');
  277. }
  278. function edit_filter($id,$url_params,$row)
  279. {
  280. $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>';
  281. $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>';
  282. return $return;
  283. }
  284. /**
  285. * Move a user defined field up or down
  286. *
  287. * @param string $direction the direction we have to move the field to (up or down)
  288. * @param unknown_type $field_id
  289. *
  290. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  291. * @version July 2008
  292. * @since Dokeos 1.8.6
  293. */
  294. function move_user_field($direction,$field_id)
  295. {
  296. // Databse table definitions
  297. $table_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  298. // check the parameters
  299. if (!in_array($direction,array('moveup','movedown')) OR !is_numeric($field_id))
  300. {
  301. return false;
  302. }
  303. // determine the SQL sort direction
  304. if ($direction == 'moveup')
  305. {
  306. $sortdirection = 'DESC';
  307. }
  308. else
  309. {
  310. $sortdirection = 'ASC';
  311. }
  312. $found = false;
  313. $sql = "SELECT id, field_order FROM $table_user_field ORDER BY field_order $sortdirection";
  314. $result = api_sql_query($sql,__FILE__,__LINE__);
  315. while($row = Database::fetch_array($result))
  316. {
  317. if ($found)
  318. {
  319. $next_id = $row['id'];
  320. $next_order = $row['field_order'];
  321. break;
  322. }
  323. if ($field_id == $row['id'])
  324. {
  325. $this_id = $row['id'];
  326. $this_order = $row['field_order'];
  327. $found = true;
  328. }
  329. }
  330. $sql1 = "UPDATE ".$table_user_field." SET field_order = '".Database::escape_string($next_order)."' WHERE id = '".Database::escape_string($this_id)."'";
  331. $sql2 = "UPDATE ".$table_user_field." SET field_order = '".Database::escape_string($this_order)."' WHERE id = '".Database::escape_string($next_id)."'";
  332. api_sql_query($sql1,__FILE__,__LINE__);
  333. api_sql_query($sql2,__FILE__,__LINE__);
  334. return true;
  335. }
  336. /**
  337. * Delete a user field (and also the options and values entered by the users)
  338. *
  339. * @param integer $field_id the id of the field that has to be deleted
  340. * @return boolean true if the field has been deleted, false if the field could not be deleted (for whatever reason)
  341. *
  342. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  343. * @version July 2008
  344. * @since Dokeos 1.8.6
  345. */
  346. function delete_user_fields($field_id)
  347. {
  348. // Database table definitions
  349. $table_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  350. $table_user_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  351. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  352. // delete the fields
  353. $sql = "DELETE FROM $table_user_field WHERE id = '".Database::escape_string($field_id)."'";
  354. $result = api_sql_query($sql,__FILE__,__LINE__);
  355. if (Database::affected_rows() == 1)
  356. {
  357. // delete the field options
  358. $sql = "DELETE FROM $table_user_field_options WHERE field_id = '".Database::escape_string($field_id)."'";
  359. $result = api_sql_query($sql,__FILE__,__LINE__);
  360. // delete the field values
  361. $sql = "DELETE FROM $table_user_field_values WHERE field_id = '".Database::escape_string($field_id)."'";
  362. $result = api_sql_query($sql,__FILE__,__LINE__);
  363. // recalculate the field_order because the value is used to show/hide the up/down icon
  364. // and the field_order value cannot be bigger than the number of fields
  365. $sql = "SELECT * FROM $table_user_field ORDER BY field_order ASC";
  366. $result = api_sql_query($sql,__FILE__,__LINE__);
  367. $i = 1;
  368. while($row = Database::fetch_array($result))
  369. {
  370. $sql_reorder = "UPDATE $table_user_field SET field_order = '".Database::escape_string($i)."' WHERE id = '".Database::escape_string($row['id'])."'";
  371. $result_reorder = api_sql_query($sql_reorder,__FILE__,__LINE__);
  372. $i++;
  373. }
  374. // field was deleted so we return true
  375. return true;
  376. }
  377. else
  378. {
  379. // the field was not deleted so we return false
  380. return false;
  381. }
  382. }
  383. ?>