user_edit.php 19 KB

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