user_edit.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // Language files that should be included
  7. $language_file = array('admin', 'registration');
  8. $cidReset = true;
  9. require_once '../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script(true);
  12. $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  13. api_protect_super_admin($user_id, null, true);
  14. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  15. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  16. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  17. $htmlHeadXtra[] = '
  18. <script>
  19. var is_platform_id = "'.$is_platform_admin.'";
  20. function enable_expiration_date() {
  21. document.user_edit.radio_expiration_date[0].checked=false;
  22. document.user_edit.radio_expiration_date[1].checked=true;
  23. }
  24. function password_switch_radio_button(){
  25. var input_elements = document.getElementsByTagName("input");
  26. for (var i = 0; i < input_elements.length; i++) {
  27. if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
  28. input_elements.item(i).checked = true;
  29. }
  30. }
  31. }
  32. function display_drh_list(){
  33. var $radios = $("input:radio[name=platform_admin]");
  34. if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  35. if (is_platform_id == 1)
  36. document.getElementById("id_platform_admin").style.display="block";
  37. } else if (document.getElementById("status_select").value=='.STUDENT.') {
  38. if (is_platform_id == 1)
  39. document.getElementById("id_platform_admin").style.display="none";
  40. $radios.filter("[value=0]").attr("checked", true);
  41. } else {
  42. if (is_platform_id == 1)
  43. document.getElementById("id_platform_admin").style.display="none";
  44. $radios.filter("[value=0]").attr("checked", true);
  45. }
  46. }
  47. function show_image(image,width,height) {
  48. width = parseInt(width) + 20;
  49. height = parseInt(height) + 20;
  50. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
  51. }
  52. function confirmation(name) {
  53. if (confirm("'.get_lang('AreYouSureToDelete', '').' " + name + " ?")) {
  54. document.forms["profile"].submit();
  55. } else {
  56. return false;
  57. }
  58. }
  59. </script>';
  60. $libpath = api_get_path(LIBRARY_PATH);
  61. $noPHP_SELF = true;
  62. $tool_name = get_lang('ModifyUserInfo');
  63. $interbreadcrumb[] = array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  64. $interbreadcrumb[] = array('url' => "user_list.php","name" => get_lang('UserList'));
  65. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  66. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  67. $sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u
  68. LEFT JOIN $table_admin a ON a.user_id = u.user_id
  69. WHERE u.user_id = '".$user_id."'";
  70. $res = Database::query($sql);
  71. if (Database::num_rows($res) != 1) {
  72. header('Location: user_list.php');
  73. exit;
  74. }
  75. $user_data = Database::fetch_array($res, 'ASSOC');
  76. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  77. $user_data['send_mail'] = 0;
  78. $user_data['old_password'] = $user_data['password'];
  79. //Convert the registration date of the user
  80. $user_data['registration_date'] = api_get_local_time($user_data['registration_date']);
  81. unset($user_data['password']);
  82. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  83. $user_data = array_merge($user_data, $extra_data);
  84. // Create the form
  85. $form = new FormValidator('user_edit', 'post', '', '', array('style' => 'width: 60%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')));
  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'), array('size' => '40'));
  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'), array('size' => '40'));
  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. // 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. $group = array();
  171. $group[] =$form->createElement('radio', 'reset_password', null, null, 2);
  172. $group[] =$form->createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button();'));
  173. $form->addGroup($group, 'password', null, '', false);
  174. // Status
  175. $status = array();
  176. $status[COURSEMANAGER] = get_lang('Teacher');
  177. $status[STUDENT] = get_lang('Learner');
  178. $status[DRH] = get_lang('Drh');
  179. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  180. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  181. $status[INVITEE] = get_lang('Invitee');
  182. $form->addElement(
  183. 'select',
  184. 'status',
  185. get_lang('Profile'),
  186. $status,
  187. array(
  188. 'id' => 'status_select',
  189. 'onchange' => 'javascript: display_drh_list();',
  190. 'class' => 'chzn-select'
  191. )
  192. );
  193. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  194. /*
  195. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  196. $drh_select = $form->addElement('select', 'hr_dept_id', get_lang('Drh'), array(), 'id="drh_select"');
  197. $drh_list = UserManager :: get_user_list(array('status' => DRH), api_sort_by_first_name() ? array('firstname', 'lastname') : array('lastname', 'firstname'));
  198. if (count($drh_list) == 0) {
  199. $drh_select->addOption('- '.get_lang('ThereIsNotStillAResponsible', '').' -', 0);
  200. } else {
  201. $drh_select->addOption('- '.get_lang('SelectAResponsible').' -', 0);
  202. }
  203. foreach($drh_list as $drh) {
  204. $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']);
  205. }
  206. $form->addElement('html', '</div>');
  207. */
  208. // Platform admin
  209. if (api_is_platform_admin()) {
  210. $group = array();
  211. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  212. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  213. $user_data['status'] == 1 ? $display = 'block':$display = 'none';
  214. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  215. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false);
  216. $form->addElement('html', '</div>');
  217. }
  218. //Language
  219. $form->addElement('select_language', 'language', get_lang('Language'));
  220. // Send email
  221. $group = array();
  222. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  223. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  224. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  225. // Registration User and Date
  226. $creatorInfo = api_get_user_info($user_data['creator_id']);
  227. $date = sprintf(get_lang('CreatedByXYOnZ'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']);
  228. $form->addElement('html', '<div class="control-group"><label class="control-label">'.get_lang('RegistrationDate').'</label><div class="controls">'.$date.'</div></div>');
  229. if (!$user_data['platform_admin']) {
  230. // Expiration Date
  231. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  232. $group = array ();
  233. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  234. $group[] = $form->createElement('DatePicker', 'expiration_date', null, array('onchange' => 'javascript: enable_expiration_date();'));
  235. $form->addGroup($group, 'max_member_group', null, '', false);
  236. // Active account or inactive account
  237. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  238. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  239. }
  240. // EXTRA FIELDS
  241. $return_params = UserManager::set_extra_fields_in_form($form, $extra_data, 'user_edit', true, $user_id);
  242. $jquery_ready_content = $return_params['jquery_ready_content'];
  243. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  244. $htmlHeadXtra[] ='<script>
  245. $(document).ready(function(){
  246. '.$jquery_ready_content.'
  247. });
  248. </script>';
  249. // Submit button
  250. $form->addElement('style_submit_button', 'submit', get_lang('ModifyInformation'), 'class="save"');
  251. // Set default values
  252. $user_data['reset_password'] = 0;
  253. $expiration_date = $user_data['expiration_date'];
  254. if (empty($expiration_date)) {
  255. $user_data['radio_expiration_date'] = 0;
  256. $user_data['expiration_date'] = date('Y-m-d');
  257. } else {
  258. $user_data['radio_expiration_date'] = 1;
  259. $user_data['expiration_date'] = $expiration_date;
  260. }
  261. $form->setDefaults($user_data);
  262. $error_drh = false;
  263. // Validate form
  264. if ($form->validate()) {
  265. $user = $form->getSubmitValues(1);
  266. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  267. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  268. $error_drh = true;
  269. } else {
  270. $userInfo = api_get_user_info($user_id);
  271. $picture_element = $form->getElement('picture');
  272. $picture = $picture_element->getValue();
  273. $picture_uri = $user_data['picture_uri'];
  274. if (isset($user['delete_picture']) && $user['delete_picture']) {
  275. $picture_uri = UserManager::delete_user_picture($user_id);
  276. } elseif (!empty($picture['name'])) {
  277. $picture_uri = UserManager::update_user_picture($user_id, $_FILES['picture']['name'], $_FILES['picture']['tmp_name']);
  278. }
  279. $lastname = $user['lastname'];
  280. $firstname = $user['firstname'];
  281. $password = $user['password'];
  282. $auth_source = isset($user['auth_source']) ? $user['auth_source'] : $userInfo['auth_source'];
  283. $official_code = $user['official_code'];
  284. $email = $user['email'];
  285. $phone = $user['phone'];
  286. $username = isset($user['username']) ? $user['username'] : $userInfo['username'];
  287. $status = intval($user['status']);
  288. $platform_admin = intval($user['platform_admin']);
  289. $send_mail = intval($user['send_mail']);
  290. $reset_password = intval($user['reset_password']);
  291. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
  292. $language = $user['language'];
  293. if ($user['radio_expiration_date'] == '1' && !$user_data['platform_admin']) {
  294. $expiration_date = $user['expiration_date'];
  295. } else {
  296. $expiration_date = null;
  297. }
  298. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  299. //If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
  300. if ($platform_admin == 1) {
  301. $status = COURSEMANAGER;
  302. }
  303. if (api_get_setting('login_is_email') == 'true') {
  304. $username = $email;
  305. }
  306. UserManager::update_user(
  307. $user_id,
  308. $firstname,
  309. $lastname,
  310. $username,
  311. $password,
  312. $auth_source,
  313. $email,
  314. $status,
  315. $official_code,
  316. $phone,
  317. $picture_uri,
  318. $expiration_date,
  319. $active,
  320. null,
  321. $hr_dept_id,
  322. null,
  323. $language,
  324. null,
  325. $send_mail,
  326. $reset_password
  327. );
  328. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  329. $up = UserManager::update_openid($user_id,$user['openid']);
  330. }
  331. $currentUserId = api_get_user_id();
  332. if ($user_id != $currentUserId) {
  333. if ($platform_admin == 1) {
  334. UserManager::add_user_as_admin($user_id);
  335. } else {
  336. UserManager::remove_user_admin($user_id);
  337. }
  338. }
  339. foreach ($user as $key => $value) {
  340. if (substr($key, 0, 6) == 'extra_') {
  341. //an extra field
  342. //@todo remove this as well as in the profile.php ad put it in a function
  343. if (is_array($value) && isset($value['Y']) && isset($value['F']) && isset($value['d'])) {
  344. if (isset($value['H']) && isset($value['i'])) {
  345. // extra field date time
  346. $time = mktime($value['H'],$value['i'],0,$value['F'],$value['d'],$value['Y']);
  347. $value = date('Y-m-d H:i:s',$time);
  348. } else {
  349. // extra field date
  350. $time = mktime(0,0,0,$value['F'],$value['d'],$value['Y']);
  351. $value = date('Y-m-d',$time);
  352. }
  353. }
  354. // For array $value -> if exists key 'tmp_name' then must not be empty
  355. // This avoid delete from user field value table when doesn't upload a file
  356. if (is_array($value)) {
  357. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  358. //Nothing to do
  359. } else {
  360. if (array_key_exists('tmp_name', $value)) {
  361. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  362. }
  363. if (array_key_exists('name', $value)) {
  364. $value['name'] = Security::filter_filename($value['name']);
  365. }
  366. UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
  367. }
  368. } else {
  369. UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
  370. }
  371. } elseif (strpos($key, 'remove_extra') !== false) {
  372. $extra_value = Security::filter_filename(urldecode(key($value)));
  373. // To remove from user_field_value and folder
  374. UserManager::update_extra_field_value($user_id, substr($key,13), $extra_value);
  375. }
  376. }
  377. $tok = Security::get_token();
  378. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok);
  379. exit();
  380. }
  381. }
  382. $message = null;
  383. if ($error_drh) {
  384. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  385. $message = Display::return_message($err_msg, 'error');
  386. }
  387. // USER PICTURE
  388. $image_path = UserManager::get_user_picture_path_by_id($user_id,'web');
  389. $image_dir = $image_path['dir'];
  390. $image = $image_path['file'];
  391. $image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_CODE_PATH).'img/unknown.jpg');
  392. $image_size = api_getimagesize($image_file);
  393. $img_attributes = 'src="'.$image_file.'?rand='.time().'" '
  394. .'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
  395. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  396. if ($image_size['width'] > 300) { //limit display width to 300px
  397. $img_attributes .= 'width="300" ';
  398. }
  399. // get the path,width and height from original picture
  400. $big_image = $image_dir.'big_'.$image;
  401. $big_image_size = api_getimagesize($big_image);
  402. $big_image_width = $big_image_size['width'];
  403. $big_image_height = $big_image_size['height'];
  404. $url_big_image = $big_image.'?rnd='.time();
  405. $content = null;
  406. if ($image == '') {
  407. $content .= '<img '.$img_attributes.' />';
  408. } else {
  409. $content .= '<input type="image" '.$img_attributes.' onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');"/>';
  410. }
  411. // Display form
  412. $content .= $form->return_form();
  413. $tpl = new Template($tool_name);
  414. $tpl->assign('message', $message);
  415. $tpl->assign('content', $content);
  416. $tpl->display_one_col_template();