user_edit.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  7. api_protect_super_admin($user_id, null, true);
  8. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  9. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  10. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  11. $htmlHeadXtra[] = '<script>
  12. var is_platform_id = "'.$is_platform_admin.'";
  13. function enable_expiration_date() {
  14. document.user_edit.radio_expiration_date[0].checked=false;
  15. document.user_edit.radio_expiration_date[1].checked=true;
  16. }
  17. function password_switch_radio_button(){
  18. var input_elements = document.getElementsByTagName("input");
  19. for (var i = 0; i < input_elements.length; i++) {
  20. if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
  21. input_elements.item(i).checked = true;
  22. }
  23. }
  24. }
  25. function display_drh_list(){
  26. var $radios = $("input:radio[name=platform_admin]");
  27. if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  28. if (is_platform_id == 1)
  29. document.getElementById("id_platform_admin").style.display="block";
  30. } else if (document.getElementById("status_select").value=='.STUDENT.') {
  31. if (is_platform_id == 1)
  32. document.getElementById("id_platform_admin").style.display="none";
  33. $radios.filter("[value=0]").attr("checked", true);
  34. } else {
  35. if (is_platform_id == 1)
  36. document.getElementById("id_platform_admin").style.display="none";
  37. $radios.filter("[value=0]").attr("checked", true);
  38. }
  39. }
  40. function show_image(image,width,height) {
  41. width = parseInt(width) + 20;
  42. height = parseInt(height) + 20;
  43. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
  44. }
  45. </script>';
  46. $noPHP_SELF = true;
  47. $tool_name = get_lang('ModifyUserInfo');
  48. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  49. $interbreadcrumb[] = array('url' => "user_list.php", "name" => get_lang('UserList'));
  50. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  51. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  52. $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."'";
  53. $res = Database::query($sql);
  54. if (Database::num_rows($res) != 1) {
  55. header('Location: user_list.php');
  56. exit;
  57. }
  58. $user_data = Database::fetch_array($res, 'ASSOC');
  59. $user_data['send_mail'] = 0;
  60. $user_data['old_password'] = $user_data['password'];
  61. //Convert the registration date of the user
  62. //@todo remove the date_default_timezone_get() see UserManager::create_user function
  63. $user_data['registration_date'] = api_get_local_time(
  64. $user_data['registration_date'],
  65. null,
  66. date_default_timezone_get()
  67. );
  68. unset($user_data['password']);
  69. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  70. $user_data = array_merge($user_data, $extra_data);
  71. // Create the form
  72. $form = new FormValidator('user_edit', 'post', '', '', array('style' => 'width: 60%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')));
  73. $form->addElement('header', '', $tool_name);
  74. $form->addElement('hidden', 'user_id', $user_id);
  75. if (api_is_western_name_order()) {
  76. // Firstname
  77. $form->addElement('text', 'firstname', get_lang('FirstName'));
  78. $form->applyFilter('firstname', 'html_filter');
  79. $form->applyFilter('firstname', 'trim');
  80. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  81. // Lastname
  82. $form->addElement('text', 'lastname', get_lang('LastName'));
  83. $form->applyFilter('lastname', 'html_filter');
  84. $form->applyFilter('lastname', 'trim');
  85. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  86. } else {
  87. // Lastname
  88. $form->addElement('text', 'lastname', get_lang('LastName'));
  89. $form->applyFilter('lastname', 'html_filter');
  90. $form->applyFilter('lastname', 'trim');
  91. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  92. // Firstname
  93. $form->addElement('text', 'firstname', get_lang('FirstName'));
  94. $form->applyFilter('firstname', 'html_filter');
  95. $form->applyFilter('firstname', 'trim');
  96. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  97. }
  98. // Official code
  99. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  100. $form->applyFilter('official_code', 'html_filter');
  101. $form->applyFilter('official_code', 'trim');
  102. // Email
  103. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  104. $form->addRule('email', get_lang('EmailWrong'), 'email');
  105. if (api_get_setting('registration', 'email') == 'true') {
  106. $form->addRule('email', get_lang('EmailWrong'), 'required');
  107. }
  108. if (api_get_setting('login_is_email') == 'true') {
  109. $form->addRule(
  110. 'email',
  111. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  112. 'maxlength',
  113. USERNAME_MAX_LENGTH
  114. );
  115. $form->addRule('email', get_lang('UserTaken'), 'username_available', $user_data['username']);
  116. }
  117. // OpenID
  118. if (api_get_setting('openid_authentication') == 'true') {
  119. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => '40'));
  120. }
  121. // Phone
  122. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  123. // Picture
  124. $form->addElement('file', 'picture', get_lang('AddPicture'));
  125. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  126. $form->addRule(
  127. 'picture',
  128. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  129. 'filetype',
  130. $allowed_picture_types
  131. );
  132. if (strlen($user_data['picture_uri']) > 0) {
  133. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  134. }
  135. // Username
  136. if (api_get_setting('login_is_email') != 'true') {
  137. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  138. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  139. $form->addRule(
  140. 'username',
  141. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  142. 'maxlength',
  143. USERNAME_MAX_LENGTH
  144. );
  145. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  146. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  147. }
  148. // Password
  149. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('DontResetPassword'), 0);
  150. $nb_ext_auth_source_added = 0;
  151. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  152. $auth_sources = array();
  153. foreach ($extAuthSource as $key => $info) {
  154. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  155. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  156. // extAuthSource always on for CAS even if not activated
  157. // same action for file user_add.php
  158. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  159. $auth_sources[$key] = $key;
  160. $nb_ext_auth_source_added++;
  161. }
  162. }
  163. if ($nb_ext_auth_source_added > 0) {
  164. // @todo check the radio button for external authentification and select the external authentification in the menu
  165. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('ExternalAuthentication').' ', 3);
  166. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  167. $group[] = $form->createElement('static', '', '', '<br />');
  168. $form->addGroup($group, 'password', null, '', false);
  169. }
  170. }
  171. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  172. // before giving the form to reset the password, check the corresponding param
  173. if (api_is_global_platform_admin() or api_get_setting('admins_can_set_users_pass')==='true') {
  174. $group = array();
  175. $group[] =$form->createElement('radio', 'reset_password', null, null, 2);
  176. $group[] =$form->createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button();'));
  177. $form->addGroup($group, 'password', null, '', false);
  178. }
  179. // Status.
  180. $status = api_get_user_roles();
  181. unset($status[ANONYMOUS]);
  182. $form->addElement(
  183. 'select',
  184. 'status',
  185. get_lang('Profile'),
  186. $status,
  187. array(
  188. 'id' => 'status_select',
  189. 'onchange' => 'javascript: display_drh_list();',
  190. 'class' => 'chzn-select'
  191. )
  192. );
  193. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  194. /*
  195. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  196. $drh_select = $form->addElement('select', 'hr_dept_id', get_lang('Drh'), array(), 'id="drh_select"');
  197. $drh_list = UserManager :: get_user_list(array('status' => DRH), api_sort_by_first_name() ? array('firstname', 'lastname') : array('lastname', 'firstname'));
  198. if (count($drh_list) == 0) {
  199. $drh_select->addOption('- '.get_lang('ThereIsNotStillAResponsible', '').' -', 0);
  200. } else {
  201. $drh_select->addOption('- '.get_lang('SelectAResponsible').' -', 0);
  202. }
  203. foreach($drh_list as $drh) {
  204. $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']);
  205. }
  206. $form->addElement('html', '</div>');
  207. */
  208. //Language
  209. $form->addElement('select_language', 'language', get_lang('Language'));
  210. // Send email
  211. $group = array();
  212. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  213. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  214. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  215. // Registration Date
  216. $form->addElement('static', 'registration_date', get_lang('RegistrationDate'), $user_data['registration_date']);
  217. // Expiration Date
  218. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  219. $group = array();
  220. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  221. $group[] = $form->createElement(
  222. 'datepicker',
  223. 'expiration_date',
  224. null,
  225. array('form_name' => $form->getAttribute('name'), 'onchange' => 'javascript: enable_expiration_date();')
  226. );
  227. $form->addGroup($group, 'max_member_group', null, '', false);
  228. // Active account or inactive account
  229. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  230. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  231. // EXTRA FIELDS
  232. $extraField = new ExtraField('user');
  233. $return_params = $extraField->set_extra_fields_in_form($form, $extra_data, 'user_edit', true, $user_id);
  234. $jquery_ready_content = $return_params['jquery_ready_content'];
  235. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  236. $htmlHeadXtra[] = '<script>
  237. $(document).ready(function(){
  238. '.$jquery_ready_content.'
  239. });
  240. </script>';
  241. // Submit button
  242. $form->addElement('style_submit_button', 'submit', get_lang('ModifyInformation'), 'class="save"');
  243. // Set default values
  244. $user_data['reset_password'] = 0;
  245. $expiration_date = $user_data['expiration_date'];
  246. if ($expiration_date == '0000-00-00 00:00:00') {
  247. $user_data['radio_expiration_date'] = 0;
  248. $user_data['expiration_date'] = array();
  249. $user_data['expiration_date']['d'] = date('d');
  250. $user_data['expiration_date']['F'] = date('m');
  251. $user_data['expiration_date']['Y'] = date('Y');
  252. } else {
  253. $user_data['radio_expiration_date'] = 1;
  254. $user_data['expiration_date'] = array();
  255. $user_data['expiration_date']['d'] = substr($expiration_date, 8, 2);
  256. $user_data['expiration_date']['F'] = substr($expiration_date, 5, 2);
  257. $user_data['expiration_date']['Y'] = substr($expiration_date, 0, 4);
  258. $user_data['expiration_date']['H'] = substr($expiration_date, 11, 2);
  259. $user_data['expiration_date']['i'] = substr($expiration_date, 14, 2);
  260. }
  261. $user = $app['orm.em']->getRepository('Entity\User')->find($user_data['user_id']);
  262. $roles = $user->getRoles();
  263. $role = array();
  264. if (!empty($roles)) {
  265. $role = current($roles);
  266. $role = $role->getId();
  267. }
  268. $user_data['status'] = $role;
  269. $form->setDefaults($user_data);
  270. $error_drh = false;
  271. // Validate form
  272. if ($form->validate()) {
  273. $user = $form->getSubmitValues();
  274. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  275. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  276. $error_drh = true;
  277. } else {
  278. $picture_element = $form->getElement('picture');
  279. $picture = $picture_element->getValue();
  280. $picture_uri = $user_data['picture_uri'];
  281. if ($user['delete_picture']) {
  282. $picture_uri = UserManager::delete_user_picture($user_id);
  283. } elseif (!empty($picture['name'])) {
  284. $picture_uri = UserManager::update_user_picture(
  285. $user_id,
  286. $_FILES['picture']['name'],
  287. $_FILES['picture']['tmp_name']
  288. );
  289. }
  290. $lastname = $user['lastname'];
  291. $firstname = $user['firstname'];
  292. $password = $user['password'];
  293. $auth_source = $user['auth_source'];
  294. $official_code = $user['official_code'];
  295. $email = $user['email'];
  296. $phone = $user['phone'];
  297. $username = $user['username'];
  298. $status = intval($user['status']);
  299. $send_mail = intval($user['send_mail']);
  300. $reset_password = intval($user['reset_password']);
  301. $hr_dept_id = intval($user['hr_dept_id']);
  302. $language = $user['language'];
  303. if ($user['radio_expiration_date'] == '1') {
  304. $expiration_date = Text::return_datetime_from_array($user['expiration_date']);
  305. } else {
  306. $expiration_date = null;
  307. }
  308. $active = intval($user['active']);
  309. if (api_get_setting('login_is_email') == 'true') {
  310. $username = $email;
  311. }
  312. UserManager::update_user(
  313. $user_id,
  314. $firstname,
  315. $lastname,
  316. $username,
  317. $password,
  318. $auth_source,
  319. $email,
  320. $status,
  321. $official_code,
  322. $phone,
  323. $picture_uri,
  324. $expiration_date,
  325. $active,
  326. null,
  327. $hr_dept_id,
  328. null,
  329. $language,
  330. null,
  331. $send_mail,
  332. $reset_password
  333. );
  334. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  335. $up = UserManager::update_openid($user_id, $user['openid']);
  336. }
  337. // Using the extra field value obj
  338. $extraFieldValues = new ExtraFieldValue('user');
  339. $extraFieldValues->save_field_values($user);
  340. $tok = Security::get_token();
  341. header(
  342. 'Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok
  343. );
  344. exit();
  345. }
  346. }
  347. $message = null;
  348. if ($error_drh) {
  349. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  350. $message = Display::return_message($err_msg, 'error');
  351. }
  352. // USER PICTURE
  353. $image_path = UserManager::get_user_picture_path_by_id($user_id, 'web');
  354. $image_dir = $image_path['dir'];
  355. $image = $image_path['file'];
  356. $image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_CODE_PATH).'img/unknown.jpg');
  357. $image_size = api_getimagesize($image_file);
  358. $img_attributes = 'src="'.$image_file.'?rand='.time().'" '
  359. .'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
  360. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  361. if ($image_size['width'] > 300) { //limit display width to 300px
  362. $img_attributes .= 'width="300" ';
  363. }
  364. // get the path,width and height from original picture
  365. $big_image = $image_dir.'big_'.$image;
  366. $big_image_size = api_getimagesize($big_image);
  367. $big_image_width = $big_image_size['width'];
  368. $big_image_height = $big_image_size['height'];
  369. $url_big_image = $big_image.'?rnd='.time();
  370. $content = null;
  371. if ($image == '') {
  372. $content .= '<img '.$img_attributes.' />';
  373. } else {
  374. $content .= '<input type="image" '.$img_attributes.' onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');"/>';
  375. }
  376. // Display form
  377. $content .= $form->return_form();
  378. $app['title'] = $tool_name;
  379. $tpl = $app['template'];
  380. $tpl->assign('message', $message);
  381. $tpl->assign('content', $content);
  382. $tpl->display_one_col_template();