user_edit.php 16 KB

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