user_edit.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script(true);
  11. $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  12. api_protect_super_admin($user_id, null, true);
  13. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  14. $userInfo = api_get_user_info($user_id);
  15. $htmlHeadXtra[] = '
  16. <script>
  17. var is_platform_id = "'.$is_platform_admin.'";
  18. function enable_expiration_date() {
  19. document.user_edit.radio_expiration_date[0].checked=false;
  20. document.user_edit.radio_expiration_date[1].checked=true;
  21. }
  22. function password_switch_radio_button(){
  23. var input_elements = document.getElementsByTagName("input");
  24. for (var i = 0; i < input_elements.length; i++) {
  25. if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
  26. input_elements.item(i).checked = true;
  27. }
  28. }
  29. }
  30. function display_drh_list(){
  31. var $radios = $("input:radio[name=platform_admin]");
  32. if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  33. if (is_platform_id == 1)
  34. document.getElementById("id_platform_admin").style.display="block";
  35. } else if (document.getElementById("status_select").value=='.STUDENT.') {
  36. if (is_platform_id == 1)
  37. document.getElementById("id_platform_admin").style.display="none";
  38. $radios.filter("[value=0]").attr("checked", true);
  39. } else {
  40. if (is_platform_id == 1)
  41. document.getElementById("id_platform_admin").style.display="none";
  42. $radios.filter("[value=0]").attr("checked", true);
  43. }
  44. }
  45. function show_image(image,width,height) {
  46. width = parseInt(width) + 20;
  47. height = parseInt(height) + 20;
  48. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
  49. }
  50. function confirmation(name) {
  51. if (confirm("'.get_lang('AreYouSureToDeleteJS', '').' " + name + " ?")) {
  52. document.forms["profile"].submit();
  53. } else {
  54. return false;
  55. }
  56. }
  57. </script>';
  58. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  59. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  60. $tool_name = get_lang('ModifyUserInfo');
  61. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  62. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('UserList')];
  63. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  64. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  65. $sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u
  66. LEFT JOIN $table_admin a ON a.user_id = u.id
  67. WHERE u.id = '".$user_id."'";
  68. $res = Database::query($sql);
  69. if (Database::num_rows($res) != 1) {
  70. header('Location: user_list.php');
  71. exit;
  72. }
  73. $user_data = Database::fetch_array($res, 'ASSOC');
  74. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  75. $user_data['send_mail'] = 0;
  76. $user_data['old_password'] = $user_data['password'];
  77. //Convert the registration date of the user
  78. $user_data['registration_date'] = api_get_local_time($user_data['registration_date']);
  79. unset($user_data['password']);
  80. // Create the form
  81. $form = new FormValidator(
  82. 'user_edit',
  83. 'post',
  84. api_get_self().'?user_id='.$user_id,
  85. ''
  86. );
  87. $form->addElement('header', $tool_name);
  88. $form->addElement('hidden', 'user_id', $user_id);
  89. if (api_is_western_name_order()) {
  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. // Lastname
  96. $form->addElement('text', 'lastname', get_lang('LastName'));
  97. $form->applyFilter('lastname', 'html_filter');
  98. $form->applyFilter('lastname', 'trim');
  99. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  100. } else {
  101. // Lastname
  102. $form->addElement('text', 'lastname', get_lang('LastName'));
  103. $form->applyFilter('lastname', 'html_filter');
  104. $form->applyFilter('lastname', 'trim');
  105. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  106. // Firstname
  107. $form->addElement('text', 'firstname', get_lang('FirstName'));
  108. $form->applyFilter('firstname', 'html_filter');
  109. $form->applyFilter('firstname', 'trim');
  110. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  111. }
  112. // Official code
  113. $form->addElement('text', 'official_code', get_lang('OfficialCode'), ['size' => '40']);
  114. $form->applyFilter('official_code', 'html_filter');
  115. $form->applyFilter('official_code', 'trim');
  116. // Email
  117. $form->addElement('text', 'email', get_lang('Email'));
  118. $form->addRule('email', get_lang('EmailWrong'), 'email');
  119. if (api_get_setting('registration', 'email') == 'true') {
  120. $form->addRule('email', get_lang('EmailWrong'), 'required');
  121. }
  122. if (api_get_setting('login_is_email') == 'true') {
  123. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  124. $form->addRule('email', get_lang('UserTaken'), 'username_available', $user_data['username']);
  125. }
  126. // OpenID
  127. if (api_get_setting('openid_authentication') == 'true') {
  128. $form->addElement('text', 'openid', get_lang('OpenIDURL'));
  129. }
  130. // Phone
  131. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  132. // Picture
  133. $form->addFile(
  134. 'picture',
  135. get_lang('AddImage'),
  136. ['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '1 / 1']
  137. );
  138. $allowed_picture_types = api_get_supported_image_extensions(false);
  139. $form->addRule(
  140. 'picture',
  141. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  142. 'filetype',
  143. $allowed_picture_types
  144. );
  145. if (strlen($user_data['picture_uri']) > 0) {
  146. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  147. }
  148. // Username
  149. if (api_get_setting('login_is_email') != 'true') {
  150. $form->addElement('text', 'username', get_lang('LoginName'), ['maxlength' => USERNAME_MAX_LENGTH]);
  151. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  152. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  153. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  154. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  155. }
  156. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  157. $form->addLabel(
  158. get_lang('ExternalAuthentication'),
  159. $userInfo['auth_source']
  160. );
  161. }
  162. // Password
  163. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('DontResetPassword'), 0);
  164. $nb_ext_auth_source_added = 0;
  165. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  166. $auth_sources = [];
  167. foreach ($extAuthSource as $key => $info) {
  168. // @todo : make uniform external authentication configuration (ex : cas and external_login ldap)
  169. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  170. // extAuthSource always on for CAS even if not activated
  171. // same action for file user_add.php
  172. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  173. $auth_sources[$key] = $key;
  174. $nb_ext_auth_source_added++;
  175. }
  176. }
  177. if ($nb_ext_auth_source_added > 0) {
  178. // @todo check the radio button for external authentification and select the external authentication in the menu
  179. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('ExternalAuthentication').' ', 3);
  180. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  181. $group[] = $form->createElement('static', '', '', '<br />');
  182. $form->addGroup($group, 'password', null, null, false);
  183. }
  184. }
  185. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  186. $group = [];
  187. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('EnterPassword'), 2);
  188. $group[] = $form->createElement(
  189. 'password',
  190. 'password',
  191. null,
  192. ['onkeydown' => 'javascript: password_switch_radio_button();']
  193. );
  194. $form->addGroup($group, 'password', null, null, false);
  195. $form->addPasswordRule('password', 'password');
  196. // Status
  197. $status = [];
  198. $status[COURSEMANAGER] = get_lang('Teacher');
  199. $status[STUDENT] = get_lang('Learner');
  200. $status[DRH] = get_lang('Drh');
  201. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  202. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  203. $status[INVITEE] = get_lang('Invitee');
  204. $form->addElement(
  205. 'select',
  206. 'status',
  207. get_lang('Profile'),
  208. $status,
  209. [
  210. 'id' => 'status_select',
  211. 'onchange' => 'javascript: display_drh_list();',
  212. ]
  213. );
  214. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  215. // Platform admin
  216. if (api_is_platform_admin()) {
  217. $group = [];
  218. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  219. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  220. $user_data['status'] == 1 ? $display = 'block' : $display = 'none';
  221. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  222. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false);
  223. $form->addElement('html', '</div>');
  224. }
  225. //Language
  226. $form->addSelectLanguage('language', get_lang('Language'));
  227. // Send email
  228. $group = [];
  229. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  230. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  231. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), null, false);
  232. // Registration User and Date
  233. $creatorInfo = api_get_user_info($user_data['creator_id']);
  234. $date = sprintf(get_lang('CreatedByXYOnZ'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']);
  235. $form->addElement('label', get_lang('RegistrationDate'), $date);
  236. if (!$user_data['platform_admin']) {
  237. // Expiration Date
  238. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  239. $group = [];
  240. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  241. $group[] = $form->createElement(
  242. 'DateTimePicker',
  243. 'expiration_date',
  244. null,
  245. ['onchange' => 'javascript: enable_expiration_date();']
  246. );
  247. $form->addGroup($group, 'max_member_group', null, null, false);
  248. // Active account or inactive account
  249. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  250. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  251. }
  252. $studentBossList = UserManager::getStudentBossList($user_data['user_id']);
  253. $conditions = ['status' => STUDENT_BOSS];
  254. $studentBoss = UserManager::get_user_list($conditions);
  255. $studentBossToSelect = [];
  256. if ($studentBoss) {
  257. foreach ($studentBoss as $bossId => $userData) {
  258. $bossInfo = api_get_user_info($userData['user_id']);
  259. $studentBossToSelect[$bossInfo['user_id']] = $bossInfo['complete_name_with_username'];
  260. }
  261. }
  262. if (!empty($studentBossList)) {
  263. $studentBossList = array_column($studentBossList, 'boss_id');
  264. }
  265. $user_data['student_boss'] = array_values($studentBossList);
  266. $form->addElement('advmultiselect', 'student_boss', get_lang('StudentBoss'), $studentBossToSelect);
  267. // EXTRA FIELDS
  268. $extraField = new ExtraField('user');
  269. $returnParams = $extraField->addElements(
  270. $form,
  271. $user_data['user_id'],
  272. [],
  273. false,
  274. false,
  275. [],
  276. [],
  277. [],
  278. false,
  279. true
  280. );
  281. $jqueryReadyContent = $returnParams['jquery_ready_content'];
  282. $allowEmailTemplate = api_get_configuration_value('mail_template_system');
  283. if ($allowEmailTemplate) {
  284. $form->addEmailTemplate(['user_edit_content.tpl']);
  285. }
  286. // the $jqueryReadyContent variable collects all functions that will be load in the
  287. $htmlHeadXtra[] = '<script>
  288. $(function () {
  289. '.$jqueryReadyContent.'
  290. });
  291. </script>';
  292. // Freeze user conditions, admin cannot updated them
  293. $extraConditions = api_get_configuration_value('show_conditions_to_user');
  294. if ($extraConditions && isset($extraConditions['conditions'])) {
  295. $extraConditions = $extraConditions['conditions'];
  296. foreach ($extraConditions as $condition) {
  297. /** @var HTML_QuickForm_group $element */
  298. $element = $form->getElement('extra_'.$condition['variable']);
  299. if ($element) {
  300. $element->freeze();
  301. }
  302. }
  303. }
  304. // Submit button
  305. $form->addButtonSave(get_lang('Save'));
  306. // Set default values
  307. $user_data['reset_password'] = 0;
  308. $expiration_date = $user_data['expiration_date'];
  309. if (empty($expiration_date)) {
  310. $user_data['radio_expiration_date'] = 0;
  311. $user_data['expiration_date'] = api_get_local_time();
  312. } else {
  313. $user_data['radio_expiration_date'] = 1;
  314. $user_data['expiration_date'] = api_get_local_time($expiration_date);
  315. }
  316. $form->setDefaults($user_data);
  317. $error_drh = false;
  318. // Validate form
  319. if ($form->validate()) {
  320. $user = $form->getSubmitValues(1);
  321. $reset_password = intval($user['reset_password']);
  322. if ($reset_password == 2 && empty($user['password'])) {
  323. Display::addFlash(Display::return_message(get_lang('PasswordIsTooShort')));
  324. header('Location: '.api_get_self().'?user_id='.$user_id);
  325. exit();
  326. }
  327. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  328. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  329. $error_drh = true;
  330. } else {
  331. $picture_element = $form->getElement('picture');
  332. $picture = $picture_element->getValue();
  333. $picture_uri = $user_data['picture_uri'];
  334. if (isset($user['delete_picture']) && $user['delete_picture']) {
  335. $picture_uri = UserManager::deleteUserPicture($user_id);
  336. } elseif (!empty($picture['name'])) {
  337. $picture_uri = UserManager::update_user_picture(
  338. $user_id,
  339. $_FILES['picture']['name'],
  340. $_FILES['picture']['tmp_name'],
  341. $user['picture_crop_result']
  342. );
  343. }
  344. $lastname = $user['lastname'];
  345. $firstname = $user['firstname'];
  346. $password = $user['password'];
  347. $auth_source = isset($user['auth_source']) ? $user['auth_source'] : $userInfo['auth_source'];
  348. $official_code = $user['official_code'];
  349. $email = $user['email'];
  350. $phone = $user['phone'];
  351. $username = isset($user['username']) ? $user['username'] : $userInfo['username'];
  352. $status = intval($user['status']);
  353. $platform_admin = intval($user['platform_admin']);
  354. $send_mail = intval($user['send_mail']);
  355. $reset_password = intval($user['reset_password']);
  356. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
  357. $language = $user['language'];
  358. $address = isset($user['address']) ? $user['address'] : null;
  359. if (!$user_data['platform_admin'] && $user['radio_expiration_date'] == '1') {
  360. $expiration_date = $user['expiration_date'];
  361. } else {
  362. $expiration_date = null;
  363. }
  364. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  365. //If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
  366. if ($platform_admin == 1) {
  367. $status = COURSEMANAGER;
  368. }
  369. if (api_get_setting('login_is_email') == 'true') {
  370. $username = $email;
  371. }
  372. $template = isset($user['email_template_option']) ? $user['email_template_option'] : [];
  373. UserManager::update_user(
  374. $user_id,
  375. $firstname,
  376. $lastname,
  377. $username,
  378. $password,
  379. $auth_source,
  380. $email,
  381. $status,
  382. $official_code,
  383. $phone,
  384. $picture_uri,
  385. $expiration_date,
  386. $active,
  387. null,
  388. $hr_dept_id,
  389. null,
  390. $language,
  391. null,
  392. $send_mail,
  393. $reset_password,
  394. $address,
  395. $template
  396. );
  397. $studentBossListSent = isset($user['student_boss']) ? $user['student_boss'] : [];
  398. UserManager::subscribeUserToBossList(
  399. $user_id,
  400. $studentBossListSent,
  401. true
  402. );
  403. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  404. $up = UserManager::update_openid($user_id, $user['openid']);
  405. }
  406. $currentUserId = api_get_user_id();
  407. $userObj = api_get_user_entity($user_id);
  408. UserManager::add_user_as_admin($userObj);
  409. if ($user_id != $currentUserId) {
  410. if ($platform_admin == 1) {
  411. $userObj = api_get_user_entity($user_id);
  412. UserManager::add_user_as_admin($userObj);
  413. } else {
  414. UserManager::remove_user_admin($user_id);
  415. }
  416. }
  417. $extraFieldValue = new ExtraFieldValue('user');
  418. $extraFieldValue->saveFieldValues($user);
  419. $userInfo = api_get_user_info($user_id);
  420. $message = get_lang('UserUpdated').': '.Display::url(
  421. $userInfo['complete_name_with_username'],
  422. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  423. );
  424. Session::erase('system_timezone');
  425. Display::addFlash(Display::return_message($message, 'normal', false));
  426. header('Location: user_list.php');
  427. exit();
  428. }
  429. }
  430. if ($error_drh) {
  431. Display::addFlash(Display::return_message(get_lang('StatusCanNotBeChangedToHumanResourcesManager'), 'error'));
  432. }
  433. $actions = [
  434. Display::url(
  435. Display::return_icon(
  436. 'info.png',
  437. get_lang('Information'),
  438. [],
  439. ICON_SIZE_MEDIUM
  440. ),
  441. api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user_id
  442. ),
  443. Display::url(
  444. Display::return_icon(
  445. 'login_as.png',
  446. get_lang('LoginAs'),
  447. [],
  448. ICON_SIZE_MEDIUM
  449. ),
  450. api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=login_as&user_id='.$user_id.'&sec_token='.Security::getTokenFromSession()
  451. ),
  452. ];
  453. $content = Display::toolbarAction('toolbar-user-information', [implode(PHP_EOL, $actions)]);
  454. $bigImage = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_BIG);
  455. $normalImage = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_ORIGINAL);
  456. $content .= '<div class="row">';
  457. $content .= '<div class="col-md-10">';
  458. // Display form
  459. $content .= $form->returnForm();
  460. $content .= '</div>';
  461. $content .= '<div class="col-md-2">';
  462. $content .= '<a class="thumbnail expand-image" href="'.$bigImage.'" /><img src="'.$normalImage.'"></a>';
  463. $content .= '</div>';
  464. $tpl = new Template($tool_name);
  465. $tpl->assign('content', $content);
  466. $tpl->display_one_col_template();