user_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once '../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('AreYouSureToDelete', '').' " + name + " ?")) {
  51. document.forms["profile"].submit();
  52. } else {
  53. return false;
  54. }
  55. }
  56. </script>';
  57. $libpath = api_get_path(LIBRARY_PATH);
  58. $noPHP_SELF = true;
  59. $tool_name = get_lang('ModifyUserInfo');
  60. $interbreadcrumb[] = array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  61. $interbreadcrumb[] = array('url' => "user_list.php","name" => get_lang('UserList'));
  62. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  63. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  64. $sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u
  65. LEFT JOIN $table_admin a ON a.user_id = u.id
  66. WHERE u.id = '".$user_id."'";
  67. $res = Database::query($sql);
  68. if (Database::num_rows($res) != 1) {
  69. header('Location: user_list.php');
  70. exit;
  71. }
  72. $user_data = Database::fetch_array($res, 'ASSOC');
  73. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  74. $user_data['send_mail'] = 0;
  75. $user_data['old_password'] = $user_data['password'];
  76. //Convert the registration date of the user
  77. $user_data['registration_date'] = api_get_local_time($user_data['registration_date']);
  78. unset($user_data['password']);
  79. // Create the form
  80. $form = new FormValidator(
  81. 'user_edit',
  82. 'post',
  83. api_get_self().'?user_id='.$user_id,
  84. ''
  85. );
  86. $form->addElement('header', '', $tool_name);
  87. $form->addElement('hidden', 'user_id', $user_id);
  88. if (api_is_western_name_order()) {
  89. // Firstname
  90. $form->addElement('text', 'firstname', get_lang('FirstName'));
  91. $form->applyFilter('firstname', 'html_filter');
  92. $form->applyFilter('firstname', 'trim');
  93. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  94. // Lastname
  95. $form->addElement('text', 'lastname', get_lang('LastName'));
  96. $form->applyFilter('lastname', 'html_filter');
  97. $form->applyFilter('lastname', 'trim');
  98. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  99. } else {
  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. // Firstname
  106. $form->addElement('text', 'firstname', get_lang('FirstName'));
  107. $form->applyFilter('firstname', 'html_filter');
  108. $form->applyFilter('firstname', 'trim');
  109. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  110. }
  111. // Official code
  112. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  113. $form->applyFilter('official_code', 'html_filter');
  114. $form->applyFilter('official_code', 'trim');
  115. // Email
  116. $form->addElement('text', 'email', get_lang('Email'));
  117. $form->addRule('email', get_lang('EmailWrong'), 'email');
  118. if (api_get_setting('registration', 'email') == 'true') {
  119. $form->addRule('email', get_lang('EmailWrong'), 'required');
  120. }
  121. if (api_get_setting('login_is_email') == 'true') {
  122. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  123. $form->addRule('email', get_lang('UserTaken'), 'username_available', $user_data['username']);
  124. }
  125. // OpenID
  126. if (api_get_setting('openid_authentication') == 'true') {
  127. $form->addElement('text', 'openid', get_lang('OpenIDURL'));
  128. }
  129. // Phone
  130. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  131. // Picture
  132. $form->addElement('file', 'picture', get_lang('AddPicture'));
  133. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  134. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  135. if (strlen($user_data['picture_uri']) > 0) {
  136. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  137. }
  138. // Username
  139. if (api_get_setting('login_is_email') != 'true') {
  140. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  141. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  142. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  143. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  144. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  145. }
  146. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  147. $form->addLabel(
  148. get_lang('ExternalAuthentication'),
  149. $userInfo['auth_source']
  150. );
  151. }
  152. // Password
  153. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('DontResetPassword'), 0);
  154. $nb_ext_auth_source_added = 0;
  155. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  156. $auth_sources = array();
  157. foreach ($extAuthSource as $key => $info) {
  158. // @todo : make uniform external authentication configuration (ex : cas and external_login ldap)
  159. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  160. // extAuthSource always on for CAS even if not activated
  161. // same action for file user_add.php
  162. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  163. $auth_sources[$key] = $key;
  164. $nb_ext_auth_source_added++;
  165. }
  166. }
  167. if ($nb_ext_auth_source_added > 0) {
  168. // @todo check the radio button for external authentification and select the external authentication in the menu
  169. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('ExternalAuthentication').' ', 3);
  170. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  171. $group[] = $form->createElement('static', '', '', '<br />');
  172. $form->addGroup($group, 'password', null, '', false);
  173. }
  174. }
  175. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  176. $group = array();
  177. $group[] =$form->createElement('radio', 'reset_password', null, get_lang('EnterPassword'), 2);
  178. $group[] = $form->createElement(
  179. 'password',
  180. 'password',
  181. null,
  182. array('onkeydown' => 'javascript: password_switch_radio_button();')
  183. );
  184. $form->addGroup($group, 'password', null, '', false);
  185. // Status
  186. $status = array();
  187. $status[COURSEMANAGER] = get_lang('Teacher');
  188. $status[STUDENT] = get_lang('Learner');
  189. $status[DRH] = get_lang('Drh');
  190. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  191. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  192. $status[INVITEE] = get_lang('Invitee');
  193. $form->addElement(
  194. 'select',
  195. 'status',
  196. get_lang('Profile'),
  197. $status,
  198. array(
  199. 'id' => 'status_select',
  200. 'onchange' => 'javascript: display_drh_list();',
  201. 'class' => 'chzn-select'
  202. )
  203. );
  204. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  205. // Platform admin
  206. if (api_is_platform_admin()) {
  207. $group = array();
  208. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  209. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  210. $user_data['status'] == 1 ? $display = 'block':$display = 'none';
  211. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  212. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false);
  213. $form->addElement('html', '</div>');
  214. }
  215. //Language
  216. $form->addElement('select_language', 'language', get_lang('Language'));
  217. // Send email
  218. $group = array();
  219. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  220. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  221. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  222. // Registration User and Date
  223. $creatorInfo = api_get_user_info($user_data['creator_id']);
  224. $date = sprintf(get_lang('CreatedByXYOnZ'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']);
  225. $form->addElement('label', get_lang('RegistrationDate'), $date);
  226. if (!$user_data['platform_admin']) {
  227. // Expiration Date
  228. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  229. $group = array ();
  230. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  231. $group[] = $form->createElement('DateTimePicker', 'expiration_date', null, array('onchange' => 'javascript: enable_expiration_date();'));
  232. $form->addGroup($group, 'max_member_group', null, '', false);
  233. // Active account or inactive account
  234. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  235. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  236. }
  237. // EXTRA FIELDS
  238. $extraField = new ExtraField('user');
  239. $returnParams = $extraField->addElements($form, $user_data['user_id']);
  240. $jquery_ready_content = $returnParams['jquery_ready_content'];
  241. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  242. $htmlHeadXtra[] ='<script>
  243. $(document).ready(function(){
  244. '.$jquery_ready_content.'
  245. });
  246. </script>';
  247. // Submit button
  248. $form->addButtonSave(get_lang('Save'));
  249. // Set default values
  250. $user_data['reset_password'] = 0;
  251. $expiration_date = $user_data['expiration_date'];
  252. if (empty($expiration_date)) {
  253. $user_data['radio_expiration_date'] = 0;
  254. $user_data['expiration_date'] = api_get_local_time();
  255. } else {
  256. $user_data['radio_expiration_date'] = 1;
  257. $user_data['expiration_date'] = api_get_local_time($expiration_date);
  258. }
  259. $form->setDefaults($user_data);
  260. $error_drh = false;
  261. // Validate form
  262. if ($form->validate()) {
  263. $user = $form->getSubmitValues(1);
  264. $reset_password = intval($user['reset_password']);
  265. if ($reset_password == 2 && empty($user['password'])) {
  266. Display::addFlash(Display::return_message(get_lang('PasswordIsTooShort')));
  267. header('Location: '.api_get_self().'?user_id='.$user_id);
  268. exit();
  269. }
  270. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  271. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  272. $error_drh = true;
  273. } else {
  274. $picture_element = $form->getElement('picture');
  275. $picture = $picture_element->getValue();
  276. $picture_uri = $user_data['picture_uri'];
  277. if (isset($user['delete_picture']) && $user['delete_picture']) {
  278. $picture_uri = UserManager::delete_user_picture($user_id);
  279. } elseif (!empty($picture['name'])) {
  280. $picture_uri = UserManager::update_user_picture(
  281. $user_id,
  282. $_FILES['picture']['name'],
  283. $_FILES['picture']['tmp_name']
  284. );
  285. }
  286. $lastname = $user['lastname'];
  287. $firstname = $user['firstname'];
  288. $password = $user['password'];
  289. $auth_source = isset($user['auth_source']) ? $user['auth_source'] : $userInfo['auth_source'];
  290. $official_code = $user['official_code'];
  291. $email = $user['email'];
  292. $phone = $user['phone'];
  293. $username = isset($user['username']) ? $user['username'] : $userInfo['username'];
  294. $status = intval($user['status']);
  295. $platform_admin = intval($user['platform_admin']);
  296. $send_mail = intval($user['send_mail']);
  297. $reset_password = intval($user['reset_password']);
  298. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
  299. $language = $user['language'];
  300. if ($user['radio_expiration_date'] == '1' && !$user_data['platform_admin']) {
  301. $expiration_date = $user['expiration_date'];
  302. } else {
  303. $expiration_date = null;
  304. }
  305. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  306. //If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
  307. if ($platform_admin == 1) {
  308. $status = COURSEMANAGER;
  309. }
  310. if (api_get_setting('login_is_email') == 'true') {
  311. $username = $email;
  312. }
  313. UserManager::update_user(
  314. $user_id,
  315. $firstname,
  316. $lastname,
  317. $username,
  318. $password,
  319. $auth_source,
  320. $email,
  321. $status,
  322. $official_code,
  323. $phone,
  324. $picture_uri,
  325. $expiration_date,
  326. $active,
  327. null,
  328. $hr_dept_id,
  329. null,
  330. $language,
  331. null,
  332. $send_mail,
  333. $reset_password
  334. );
  335. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  336. $up = UserManager::update_openid($user_id, $user['openid']);
  337. }
  338. $currentUserId = api_get_user_id();
  339. if ($user_id != $currentUserId) {
  340. if ($platform_admin == 1) {
  341. UserManager::add_user_as_admin($user_id);
  342. } else {
  343. UserManager::remove_user_admin($user_id);
  344. }
  345. }
  346. $extraFieldValue = new ExtraFieldValue('user');
  347. $extraFieldValue->saveFieldValues($user);
  348. $tok = Security::get_token();
  349. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok);
  350. exit();
  351. }
  352. }
  353. $message = null;
  354. if ($error_drh) {
  355. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  356. $message = Display::return_message($err_msg, 'error');
  357. }
  358. $content = null;
  359. $bigImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_BIG);
  360. $normalImage = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_ORIGINAL);
  361. $content .= '<a class="expand-image" href="'.$bigImage.'" /><img src="'.$normalImage.'"></a>';
  362. // Display form
  363. $content .= $form->returnForm();
  364. $tpl = new Template($tool_name);
  365. $tpl->assign('message', $message);
  366. $tpl->assign('content', $content);
  367. $tpl->display_one_col_template();