user_edit.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php // $Id: user_edit.php 14955 2008-04-20 03:20:36Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. Copyright (c) Bart Mollet, Hogeschool Gent
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * @package dokeos.admin
  23. ==============================================================================
  24. */
  25. // name of the language file that needs to be included
  26. $language_file=array('admin','registration');
  27. $cidReset=true;
  28. include('../inc/global.inc.php');
  29. $this_section=SECTION_PLATFORM_ADMIN;
  30. api_protect_admin_script();
  31. $htmlHeadXtra[] = '
  32. <script language="JavaScript" type="text/JavaScript">
  33. <!--
  34. function enable_expiration_date() { //v2.0
  35. document.user_add.radio_expiration_date[0].checked=false;
  36. document.user_add.radio_expiration_date[1].checked=true;
  37. }
  38. function display_drh_list(){
  39. if(document.getElementById("status_select").value=='.STUDENT.')
  40. {
  41. document.getElementById("drh_list").style.display="block";
  42. }
  43. else
  44. {
  45. document.getElementById("drh_list").style.display="none";
  46. document.getElementById("drh_select").options[0].selected="selected";
  47. }
  48. }
  49. //-->
  50. </script>';
  51. include(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  52. include(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  53. include(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  54. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  55. $user_id=isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  56. $noPHP_SELF=true;
  57. $tool_name=get_lang('ModifyUserInfo');
  58. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  59. $interbreadcrumb[]=array('url' => "user_list.php","name" => get_lang('UserList'));
  60. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  61. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  62. $sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u LEFT JOIN $table_admin a ON a.user_id = u.user_id WHERE u.user_id = '".$user_id."'";
  63. $res = api_sql_query($sql,__FILE__,__LINE__);
  64. if(mysql_num_rows($res) != 1)
  65. {
  66. header('Location: user_list.php');
  67. exit;
  68. }
  69. $user_data = mysql_fetch_array($res,MYSQL_ASSOC);
  70. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  71. $user_data['send_mail'] = 0;
  72. $user_data['old_password'] = $user_data['password'];
  73. unset($user_data['password']);
  74. $user_data = array_merge($user_data, Usermanager :: get_extra_user_data($user_id,true));
  75. // Create the form
  76. $form = new FormValidator('user_add','post','','',array('style' => 'width: 60%; float: '.($text_dir=='rtl'?'right;':'left;')));
  77. $form->addElement('hidden','user_id',$user_id);
  78. // Lastname
  79. $form->addElement('text','lastname',get_lang('LastName'));
  80. $form->applyFilter('lastname','html_filter');
  81. $form->applyFilter('lastname','trim');
  82. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  83. // Firstname
  84. $form->addElement('text','firstname',get_lang('FirstName'));
  85. $form->applyFilter('firstname','html_filter');
  86. $form->applyFilter('firstname','trim');
  87. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  88. // Official code
  89. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  90. $form->applyFilter('official_code','html_filter');
  91. $form->applyFilter('official_code','trim');
  92. // Email
  93. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  94. $form->addRule('email', get_lang('EmailWrong'), 'email');
  95. $form->addRule('email', get_lang('EmailWrong'), 'required');
  96. // OpenID
  97. if(api_get_setting('openid_authentication')=='true')
  98. {
  99. $form->addElement('text', 'openid', get_lang('OpenIDURL'),array('size' => '40'));
  100. }
  101. // Phone
  102. $form->addElement('text','phone',get_lang('PhoneNumber'));
  103. // Picture
  104. $form->addElement('file', 'picture', get_lang('AddPicture'));
  105. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  106. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  107. if (strlen($user_data['picture_uri']) > 0 )
  108. {
  109. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  110. }
  111. // Username
  112. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  113. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  114. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  115. $form->addRule('username', '', 'maxlength',20);
  116. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  117. // Password
  118. $form->addElement('radio','reset_password',get_lang('Password'),get_lang('DontResetPassword'),0);
  119. if(count($extAuthSource) > 0)
  120. {
  121. $group[] =& HTML_QuickForm::createElement('radio','reset_password',null,get_lang('ExternalAuthentication').' ',3);
  122. $auth_sources = array();
  123. foreach($extAuthSource as $key => $info)
  124. {
  125. $auth_sources[$key] = $key;
  126. }
  127. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  128. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  129. $form->addGroup($group, 'password', null, '',false);
  130. }
  131. $form->addElement('radio','reset_password',null,get_lang('AutoGeneratePassword'),1);
  132. $group = array();
  133. $group[] =& HTML_QuickForm::createElement('radio', 'reset_password',null,null,2);
  134. $group[] =& HTML_QuickForm::createElement('password', 'password',null,null);
  135. $form->addGroup($group, 'password', null, '',false);
  136. // Status
  137. $status = api_get_status_langvars();
  138. $form->addElement('select','status',get_lang('Status'),$status,'id="status_select" onchange="display_drh_list()"');
  139. $display = $user_data['status'] == STUDENT || $_POST['status'] == STUDENT ? 'block' : 'none';
  140. $form->addElement('html','<div id="drh_list" style="display:'.$display.';">');
  141. $drh_select = $form->addElement('select','hr_dept_id',get_lang('Drh'),array(),'id="drh_select"');
  142. $drh_list = UserManager :: get_user_list(array('status'=>DRH),array('lastname','firstname'));
  143. $drh_select->addOption('---',0);
  144. foreach($drh_list as $drh)
  145. {
  146. $drh_select->addOption($drh['lastname'].' '.$drh['firstname'],$drh['user_id']);
  147. }
  148. $form->addElement('html', '</div>');
  149. // Platform admin
  150. // Only when changing another user!
  151. if($user_id != $_SESSION['_uid'])
  152. {
  153. $group = array();
  154. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  155. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  156. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;',false);
  157. }
  158. // Send email
  159. $group = array();
  160. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  161. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  162. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;',false);
  163. // Registration Date
  164. $form->addElement('static','registration_date', get_lang('RegistrationDate'), $user_data['registration_date']);
  165. if(! $user_data['platform_admin'] )
  166. {
  167. // Expiration Date
  168. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  169. $group = array ();
  170. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  171. $group[] = & $form->createElement('datepicker', 'expiration_date',null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  172. $form->addGroup($group, 'max_member_group', null, '', false);
  173. // Active account or inactive account
  174. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  175. $form->addElement('radio','active','',get_lang('Inactive'),0);
  176. }
  177. // EXTRA FIELDS
  178. $extra = UserManager::get_extra_fields(0,50,5,'ASC');
  179. foreach($extra as $id => $field_details)
  180. {
  181. if($field_details[6] == 0)
  182. {
  183. continue;
  184. }
  185. switch($field_details[2])
  186. {
  187. case USER_FIELD_TYPE_TEXT:
  188. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
  189. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  190. $form->applyFilter('extra_'.$field_details[1], 'trim');
  191. break;
  192. case USER_FIELD_TYPE_TEXTAREA:
  193. $form->add_html_editor('extra_'.$field_details[1], $field_details[3], false);
  194. //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
  195. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  196. $form->applyFilter('extra_'.$field_details[1], 'trim');
  197. break;
  198. case USER_FIELD_TYPE_RADIO:
  199. $group = array();
  200. foreach($field_details[8] as $option_id => $option_details)
  201. {
  202. $options[$option_details[1]] = $option_details[2];
  203. $group[] =& HTML_QuickForm::createElement('radio', 'extra_'.$field_details[1], $option_details[1],$option_details[2].'<br />',$option_details[1]);
  204. }
  205. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
  206. break;
  207. case USER_FIELD_TYPE_SELECT:
  208. $options = array();
  209. foreach($field_details[8] as $option_id => $option_details)
  210. {
  211. $options[$option_details[1]] = $option_details[2];
  212. }
  213. $form->addElement('select','extra_'.$field_details[1],$field_details[3],$options,'');
  214. break;
  215. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  216. $options = array();
  217. foreach($field_details[8] as $option_id => $option_details)
  218. {
  219. $options[$option_details[1]] = $option_details[2];
  220. }
  221. $form->addElement('select','extra_'.$field_details[1],$field_details[3],$options,array('multiple' => 'multiple'));
  222. break;
  223. case USER_FIELD_TYPE_DATE:
  224. $form->addElement('datepickerdate', 'extra_'.$field_details[1], $field_details[3]);
  225. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear',1900);
  226. $form->applyFilter('theme', 'trim');
  227. break;
  228. case USER_FIELD_TYPE_DATETIME:
  229. $form->addElement('datepicker', 'extra_'.$field_details[1], $field_details[3]);
  230. $form->applyFilter('theme', 'trim');
  231. break;
  232. }
  233. }
  234. // Submit button
  235. $form->addElement('submit', 'submit', 'OK');
  236. // Set default values
  237. $expiration_date=$user_data['expiration_date'];
  238. if ($expiration_date=='0000-00-00 00:00:00')
  239. {
  240. $user_data['radio_expiration_date']=0;
  241. $user_data['expiration_date']=array();
  242. $user_data['expiration_date']['d']=date('d');
  243. $user_data['expiration_date']['F']=date('m');
  244. $user_data['expiration_date']['Y']=date('Y');
  245. }
  246. else
  247. {
  248. $user_data['radio_expiration_date']=1;
  249. $user_data['expiration_date']=array();
  250. $user_data['expiration_date']['d']=substr($expiration_date,8,2);
  251. $user_data['expiration_date']['F']=substr($expiration_date,5,2);
  252. $user_data['expiration_date']['Y']=substr($expiration_date,0,4);
  253. }
  254. $form->setDefaults($user_data);
  255. // Validate form
  256. if( $form->validate())
  257. {
  258. $user = $form->exportValues();
  259. $picture_element = & $form->getElement('picture');
  260. $picture = $picture_element->getValue();
  261. $picture_uri = '';
  262. if (strlen($picture['name']) > 0)
  263. {
  264. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  265. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  266. move_uploaded_file($picture['tmp_name'], $picture_location);
  267. }
  268. elseif(isset($user['delete_picture']))
  269. {
  270. @unlink('../upload/users/'.$user_data['picture_uri']);
  271. }
  272. if (strlen($picture['name']) == 0){
  273. $picture_uri = $user_data['picture_uri'];
  274. }
  275. $lastname = $user['lastname'];
  276. $firstname = $user['firstname'];
  277. $official_code = $user['official_code'];
  278. $email = $user['email'];
  279. $phone = $user['phone'];
  280. $username = $user['username'];
  281. $status = intval($user['status']);
  282. $picture = $_FILES['picture'];
  283. $platform_admin = intval($user['platform_admin']);
  284. $send_mail = intval($user['send_mail']);
  285. $reset_password = intval($user['reset_password']);
  286. $hr_dept_id = intval($user['hr_dept_id']);
  287. if ($user['radio_expiration_date']=='1' && ! $user_data['platform_admin'] )
  288. {
  289. $expiration_date=$user['expiration_date'];
  290. }
  291. else
  292. {
  293. $expiration_date='0000-00-00 00:00:00';
  294. }
  295. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  296. if( $reset_password == 0)
  297. {
  298. $password = null;
  299. $auth_source = $user_data['auth_source'];
  300. }
  301. elseif($reset_password == 1)
  302. {
  303. $password = api_generate_password();
  304. $auth_source = PLATFORM_AUTH_SOURCE;
  305. }
  306. elseif($reset_password == 2)
  307. {
  308. $password = $user['password'];
  309. $auth_source = PLATFORM_AUTH_SOURCE;
  310. }
  311. elseif($reset_password == 3)
  312. {
  313. $password = $user['password'];
  314. $auth_source = $user['auth_source'];
  315. }
  316. UserManager::update_user($user_id,$firstname,$lastname,$username,$password,$auth_source,$email,$status,$official_code,$phone,$picture_uri,$expiration_date, $active, null, $hr_dept_id);
  317. if(api_get_setting('openid_authentication')=='true' && !empty($user['openid']))
  318. {
  319. $up = UserManager::update_openid($user_id,$user['openid']);
  320. }
  321. if($user_id != $_SESSION['_uid'])
  322. {
  323. if($platform_admin == 1)
  324. {
  325. $sql = "INSERT IGNORE INTO $table_admin SET user_id = '".$user_id."'";
  326. api_sql_query($sql,__FILE__,__LINE__);
  327. }
  328. else
  329. {
  330. $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'";
  331. api_sql_query($sql,__FILE__,__LINE__);
  332. }
  333. }
  334. $extras = array();
  335. foreach($user as $key => $value)
  336. {
  337. if(substr($key,0,6)=='extra_') //an extra field
  338. {
  339. $myres = UserManager::update_extra_field_value($user_id,substr($key,6),$value);
  340. }
  341. }
  342. if (!empty ($email) && $send_mail)
  343. {
  344. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  345. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  346. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  347. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  348. $emailbody = get_lang('Dear')." ".stripslashes("$firstname $lastname").",\n\n".get_lang('YouAreReg')." ". get_setting('siteName') ." ".get_lang('Settings')." ". $username;
  349. if($reset_password != 0 && !$userPasswordCrypted )
  350. {
  351. $emailbody .= "\n".get_lang('Pass')." : ".stripslashes($password);
  352. }
  353. $emailbody .= "\n\n" .get_lang('Address') ." ". get_setting('siteName') ." ". get_lang('Is') ." : ". $_configuration['root_web'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  354. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  355. }
  356. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')));
  357. exit();
  358. }
  359. Display::display_header($tool_name);
  360. //api_display_tool_title($tool_name);
  361. // Show the users picture
  362. if (strlen($user_data['picture_uri']) > 0)
  363. {
  364. $picture_url = api_get_path(WEB_CODE_PATH).'upload/users/'.$user_data['picture_uri'];
  365. }
  366. else
  367. {
  368. $picture_url = api_get_path(WEB_CODE_PATH)."img/unknown.jpg";
  369. }
  370. $image_size = @getimagesize($picture_url);
  371. $img_attributes = 'src="'.$picture_url.'?rand='.time().'" '
  372. .'alt="'.$user_data['lastname'].' '.$user_data['firstname'].'" '
  373. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  374. if ($image_size[0] > 200) //limit display width to 300px
  375. $img_attributes .= 'width="200" ';
  376. echo '<img '.$img_attributes.'/>';
  377. // Display form
  378. $form->display();
  379. /*
  380. ==============================================================================
  381. FOOTER
  382. ==============================================================================
  383. */
  384. Display::display_footer();
  385. ?>