user_edit.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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('Edit user information');
  61. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  62. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
  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('First name'));
  92. $form->applyFilter('firstname', 'html_filter');
  93. $form->applyFilter('firstname', 'trim');
  94. $form->addRule('firstname', get_lang('Required field'), 'required');
  95. // Lastname
  96. $form->addElement('text', 'lastname', get_lang('Last name'));
  97. $form->applyFilter('lastname', 'html_filter');
  98. $form->applyFilter('lastname', 'trim');
  99. $form->addRule('lastname', get_lang('Required field'), 'required');
  100. } else {
  101. // Lastname
  102. $form->addElement('text', 'lastname', get_lang('Last name'));
  103. $form->applyFilter('lastname', 'html_filter');
  104. $form->applyFilter('lastname', 'trim');
  105. $form->addRule('lastname', get_lang('Required field'), 'required');
  106. // Firstname
  107. $form->addElement('text', 'firstname', get_lang('First name'));
  108. $form->applyFilter('firstname', 'html_filter');
  109. $form->applyFilter('firstname', 'trim');
  110. $form->addRule('firstname', get_lang('Required field'), 'required');
  111. }
  112. // Official code
  113. $form->addElement('text', 'official_code', get_lang('Code'), ['size' => '40']);
  114. $form->applyFilter('official_code', 'html_filter');
  115. $form->applyFilter('official_code', 'trim');
  116. // e-mail
  117. $form->addElement('text', 'email', get_lang('e-mail'));
  118. $form->addRule('email', get_lang('e-mailWrong'), 'email');
  119. if (api_get_setting('registration', 'email') == 'true') {
  120. $form->addRule('email', get_lang('e-mailWrong'), 'required');
  121. }
  122. if (api_get_setting('login_is_email') == 'true') {
  123. $form->addRule('email', sprintf(get_lang('The login needs to be maximum %s characters long'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  124. $form->addRule('email', get_lang('This login is already in use'), 'username_available', $user_data['username']);
  125. }
  126. // Phone
  127. $form->addElement('text', 'phone', get_lang('Phone number'));
  128. // Picture
  129. $form->addFile(
  130. 'picture',
  131. get_lang('Add image'),
  132. ['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '1 / 1']
  133. );
  134. $allowed_picture_types = api_get_supported_image_extensions(false);
  135. $form->addRule(
  136. 'picture',
  137. get_lang('Only PNG, JPG or GIF images allowed').' ('.implode(',', $allowed_picture_types).')',
  138. 'filetype',
  139. $allowed_picture_types
  140. );
  141. if (strlen($user_data['picture_uri']) > 0) {
  142. $form->addElement('checkbox', 'delete_picture', '', get_lang('Remove picture'));
  143. }
  144. // Username
  145. if (api_get_setting('login_is_email') != 'true') {
  146. $form->addElement('text', 'username', get_lang('Login'), ['maxlength' => USERNAME_MAX_LENGTH]);
  147. $form->addRule('username', get_lang('Required field'), 'required');
  148. $form->addRule('username', sprintf(get_lang('The login needs to be maximum %s characters long'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  149. $form->addRule('username', get_lang('Only letters and numbers allowed'), 'username');
  150. $form->addRule('username', get_lang('This login is already in use'), 'username_available', $user_data['username']);
  151. }
  152. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  153. $form->addLabel(
  154. get_lang('External authentification'),
  155. $userInfo['auth_source']
  156. );
  157. }
  158. // Password
  159. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('Don\'t reset password'), 0);
  160. $nb_ext_auth_source_added = 0;
  161. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  162. $auth_sources = [];
  163. foreach ($extAuthSource as $key => $info) {
  164. // @todo : make uniform external authentication configuration (ex : cas and external_login ldap)
  165. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  166. // extAuthSource always on for CAS even if not activated
  167. // same action for file user_add.php
  168. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  169. $auth_sources[$key] = $key;
  170. $nb_ext_auth_source_added++;
  171. }
  172. }
  173. if ($nb_ext_auth_source_added > 0) {
  174. // @todo check the radio button for external authentification and select the external authentication in the menu
  175. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('External authentification').' ', 3);
  176. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  177. $group[] = $form->createElement('static', '', '', '<br />');
  178. $form->addGroup($group, 'password', null, null, false);
  179. }
  180. }
  181. $form->addElement('radio', 'reset_password', null, get_lang('Automatically generate a new password'), 1);
  182. $group = [];
  183. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('Enter password'), 2);
  184. $group[] = $form->createElement(
  185. 'password',
  186. 'password',
  187. null,
  188. ['onkeydown' => 'javascript: password_switch_radio_button();']
  189. );
  190. $form->addGroup($group, 'password', null, null, false);
  191. $form->addPasswordRule('password', 'password');
  192. // Status
  193. $status = [];
  194. $status[COURSEMANAGER] = get_lang('Trainer');
  195. $status[STUDENT] = get_lang('Learner');
  196. $status[DRH] = get_lang('Human Resources Manager');
  197. $status[SESSIONADMIN] = get_lang('Sessions administrator');
  198. $status[STUDENT_BOSS] = get_lang('Student\'s superior');
  199. $status[INVITEE] = get_lang('Invitee');
  200. $form->addElement(
  201. 'select',
  202. 'status',
  203. get_lang('Profile'),
  204. $status,
  205. [
  206. 'id' => 'status_select',
  207. 'onchange' => 'javascript: display_drh_list();',
  208. ]
  209. );
  210. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  211. // Platform admin
  212. if (api_is_platform_admin()) {
  213. $group = [];
  214. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  215. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  216. $user_data['status'] == 1 ? $display = 'block' : $display = 'none';
  217. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  218. $form->addGroup($group, 'admin', get_lang('Administration'), null, false);
  219. $form->addElement('html', '</div>');
  220. }
  221. //Language
  222. $form->addSelectLanguage('language', get_lang('Language'));
  223. // Send email
  224. $group = [];
  225. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  226. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  227. $form->addGroup($group, 'mail', get_lang('Send mail to new user'), null, false);
  228. // Registration User and Date
  229. $creatorInfo = api_get_user_info($user_data['creator_id']);
  230. $date = sprintf(get_lang('Create by <a href="%s">%s</a> on %s'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']);
  231. $form->addElement('label', get_lang('Registration date'), $date);
  232. if (!$user_data['platform_admin']) {
  233. // Expiration Date
  234. $form->addElement('radio', 'radio_expiration_date', get_lang('Expiration date'), get_lang('Never expires'), 0);
  235. $group = [];
  236. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  237. $group[] = $form->createElement(
  238. 'DateTimePicker',
  239. 'expiration_date',
  240. null,
  241. ['onchange' => 'javascript: enable_expiration_date();']
  242. );
  243. $form->addGroup($group, 'max_member_group', null, null, false);
  244. // active account or inactive account
  245. $form->addElement('radio', 'active', get_lang('Account'), get_lang('active'), 1);
  246. $form->addElement('radio', 'active', '', get_lang('inactive'), 0);
  247. }
  248. $studentBossList = UserManager::getStudentBossList($user_data['user_id']);
  249. $conditions = ['status' => STUDENT_BOSS];
  250. $studentBoss = UserManager::get_user_list($conditions);
  251. $studentBossToSelect = [];
  252. if ($studentBoss) {
  253. foreach ($studentBoss as $bossId => $userData) {
  254. $bossInfo = api_get_user_info($userData['user_id']);
  255. $studentBossToSelect[$bossInfo['user_id']] = $bossInfo['complete_name_with_username'];
  256. }
  257. }
  258. if (!empty($studentBossList)) {
  259. $studentBossList = array_column($studentBossList, 'boss_id');
  260. }
  261. $user_data['student_boss'] = array_values($studentBossList);
  262. $form->addElement('advmultiselect', 'student_boss', get_lang('Superior (n+1)'), $studentBossToSelect);
  263. // EXTRA FIELDS
  264. $extraField = new ExtraField('user');
  265. $returnParams = $extraField->addElements(
  266. $form,
  267. $user_data['user_id'],
  268. [],
  269. false,
  270. false,
  271. [],
  272. [],
  273. [],
  274. false,
  275. true
  276. );
  277. $jqueryReadyContent = $returnParams['jquery_ready_content'];
  278. $allowEmailTemplate = api_get_configuration_value('mail_template_system');
  279. if ($allowEmailTemplate) {
  280. $form->addEmailTemplate(['user_edit_content.tpl']);
  281. }
  282. // the $jqueryReadyContent variable collects all functions that will be load in the
  283. $htmlHeadXtra[] = '<script>
  284. $(function () {
  285. '.$jqueryReadyContent.'
  286. });
  287. </script>';
  288. // Freeze user conditions, admin cannot updated them
  289. $extraConditions = api_get_configuration_value('show_conditions_to_user');
  290. if ($extraConditions && isset($extraConditions['conditions'])) {
  291. $extraConditions = $extraConditions['conditions'];
  292. foreach ($extraConditions as $condition) {
  293. /** @var HTML_QuickForm_group $element */
  294. $element = $form->getElement('extra_'.$condition['variable']);
  295. if ($element) {
  296. $element->freeze();
  297. }
  298. }
  299. }
  300. // Submit button
  301. $form->addButtonSave(get_lang('Save'));
  302. // Set default values
  303. $user_data['reset_password'] = 0;
  304. $expiration_date = $user_data['expiration_date'];
  305. if (empty($expiration_date)) {
  306. $user_data['radio_expiration_date'] = 0;
  307. $user_data['expiration_date'] = api_get_local_time();
  308. } else {
  309. $user_data['radio_expiration_date'] = 1;
  310. $user_data['expiration_date'] = api_get_local_time($expiration_date);
  311. }
  312. $form->setDefaults($user_data);
  313. $error_drh = false;
  314. // Validate form
  315. if ($form->validate()) {
  316. $user = $form->getSubmitValues(1);
  317. $reset_password = intval($user['reset_password']);
  318. if ($reset_password == 2 && empty($user['password'])) {
  319. Display::addFlash(Display::return_message(get_lang('The password is too short')));
  320. header('Location: '.api_get_self().'?user_id='.$user_id);
  321. exit();
  322. }
  323. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  324. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  325. $error_drh = true;
  326. } else {
  327. $picture_element = $form->getElement('picture');
  328. $picture = $picture_element->getValue();
  329. $picture_uri = $user_data['picture_uri'];
  330. if (isset($user['delete_picture']) && $user['delete_picture']) {
  331. $picture_uri = UserManager::deleteUserPicture($user_id);
  332. } elseif (!empty($picture['name'])) {
  333. $picture_uri = UserManager::update_user_picture(
  334. $user_id,
  335. $_FILES['picture']['name'],
  336. $_FILES['picture']['tmp_name'],
  337. $user['picture_crop_result']
  338. );
  339. }
  340. $lastname = $user['lastname'];
  341. $firstname = $user['firstname'];
  342. $password = $user['password'];
  343. $auth_source = isset($user['auth_source']) ? $user['auth_source'] : $userInfo['auth_source'];
  344. $official_code = $user['official_code'];
  345. $email = $user['email'];
  346. $phone = $user['phone'];
  347. $username = isset($user['username']) ? $user['username'] : $userInfo['username'];
  348. $status = intval($user['status']);
  349. $platform_admin = intval($user['platform_admin']);
  350. $send_mail = intval($user['send_mail']);
  351. $reset_password = intval($user['reset_password']);
  352. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
  353. $language = $user['language'];
  354. $address = isset($user['address']) ? $user['address'] : null;
  355. if (!$user_data['platform_admin'] && $user['radio_expiration_date'] == '1') {
  356. $expiration_date = $user['expiration_date'];
  357. } else {
  358. $expiration_date = null;
  359. }
  360. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  361. //If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
  362. if ($platform_admin == 1) {
  363. $status = COURSEMANAGER;
  364. }
  365. if (api_get_setting('login_is_email') == 'true') {
  366. $username = $email;
  367. }
  368. $template = isset($user['email_template_option']) ? $user['email_template_option'] : [];
  369. UserManager::update_user(
  370. $user_id,
  371. $firstname,
  372. $lastname,
  373. $username,
  374. $password,
  375. $auth_source,
  376. $email,
  377. $status,
  378. $official_code,
  379. $phone,
  380. $picture_uri,
  381. $expiration_date,
  382. $active,
  383. null,
  384. $hr_dept_id,
  385. null,
  386. $language,
  387. null,
  388. $send_mail,
  389. $reset_password,
  390. $address,
  391. $template
  392. );
  393. $studentBossListSent = isset($user['student_boss']) ? $user['student_boss'] : [];
  394. UserManager::subscribeUserToBossList(
  395. $user_id,
  396. $studentBossListSent,
  397. true
  398. );
  399. $currentUserId = api_get_user_id();
  400. $userObj = api_get_user_entity($user_id);
  401. UserManager::add_user_as_admin($userObj);
  402. if ($user_id != $currentUserId) {
  403. if ($platform_admin == 1) {
  404. $userObj = api_get_user_entity($user_id);
  405. UserManager::add_user_as_admin($userObj);
  406. } else {
  407. UserManager::remove_user_admin($user_id);
  408. }
  409. }
  410. $extraFieldValue = new ExtraFieldValue('user');
  411. $extraFieldValue->saveFieldValues($user);
  412. $userInfo = api_get_user_info($user_id);
  413. $message = get_lang('User updated').': '.Display::url(
  414. $userInfo['complete_name_with_username'],
  415. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  416. );
  417. Session::erase('system_timezone');
  418. Display::addFlash(Display::return_message($message, 'normal', false));
  419. header('Location: user_list.php');
  420. exit();
  421. }
  422. }
  423. if ($error_drh) {
  424. Display::addFlash(Display::return_message(get_lang('The status of this user cannot be changed to human resources manager.'), 'error'));
  425. }
  426. $actions = [
  427. Display::url(
  428. Display::return_icon(
  429. 'info.png',
  430. get_lang('Information'),
  431. [],
  432. ICON_SIZE_MEDIUM
  433. ),
  434. api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user_id
  435. ),
  436. Display::url(
  437. Display::return_icon(
  438. 'login_as.png',
  439. get_lang('Login as'),
  440. [],
  441. ICON_SIZE_MEDIUM
  442. ),
  443. api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=login_as&user_id='.$user_id.'&sec_token='.Security::getTokenFromSession()
  444. ),
  445. ];
  446. $content = Display::toolbarAction('toolbar-user-information', [implode(PHP_EOL, $actions)]);
  447. $bigImage = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_BIG);
  448. $normalImage = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_ORIGINAL);
  449. $content .= '<div class="row">';
  450. $content .= '<div class="col-md-10">';
  451. // Display form
  452. $content .= $form->returnForm();
  453. $content .= '</div>';
  454. $content .= '<div class="col-md-2">';
  455. $content .= '<a class="thumbnail expand-image" href="'.$bigImage.'" /><img src="'.$normalImage.'"></a>';
  456. $content .= '</div>';
  457. $tpl = new Template($tool_name);
  458. $tpl->assign('content', $content);
  459. $tpl->display_one_col_template();