user_edit.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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(
  16. WEB_LIBRARY_PATH
  17. ).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  18. $htmlHeadXtra[] = '<link href="'.api_get_path(
  19. WEB_LIBRARY_PATH
  20. ).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  21. $htmlHeadXtra[] = '
  22. <script>
  23. var is_platform_id = "'.$is_platform_admin.'";
  24. <!--
  25. function enable_expiration_date() {
  26. document.user_edit.radio_expiration_date[0].checked=false;
  27. document.user_edit.radio_expiration_date[1].checked=true;
  28. }
  29. function password_switch_radio_button(){
  30. var input_elements = document.getElementsByTagName("input");
  31. for (var i = 0; i < input_elements.length; i++) {
  32. if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
  33. input_elements.item(i).checked = true;
  34. }
  35. }
  36. }
  37. function display_drh_list(){
  38. var $radios = $("input:radio[name=platform_admin]");
  39. if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  40. if (is_platform_id == 1)
  41. document.getElementById("id_platform_admin").style.display="block";
  42. } else if (document.getElementById("status_select").value=='.STUDENT.') {
  43. if (is_platform_id == 1)
  44. document.getElementById("id_platform_admin").style.display="none";
  45. $radios.filter("[value=0]").attr("checked", true);
  46. } else {
  47. if (is_platform_id == 1)
  48. document.getElementById("id_platform_admin").style.display="none";
  49. $radios.filter("[value=0]").attr("checked", true);
  50. }
  51. }
  52. function show_image(image,width,height) {
  53. width = parseInt(width) + 20;
  54. height = parseInt(height) + 20;
  55. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
  56. }
  57. //-->
  58. </script>';
  59. $noPHP_SELF = true;
  60. $tool_name = get_lang('ModifyUserInfo');
  61. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  62. $interbreadcrumb[] = array('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 LEFT JOIN $table_admin a ON a.user_id = u.user_id WHERE u.user_id = '".$user_id."'";
  66. $res = Database::query($sql);
  67. if (Database::num_rows($res) != 1) {
  68. header('Location: user_list.php');
  69. exit;
  70. }
  71. $user_data = Database::fetch_array($res, 'ASSOC');
  72. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  73. $user_data['send_mail'] = 0;
  74. $user_data['old_password'] = $user_data['password'];
  75. //Convert the registration date of the user
  76. //@todo remove the date_default_timezone_get() see UserManager::create_user function
  77. $user_data['registration_date'] = api_get_local_time(
  78. $user_data['registration_date'],
  79. null,
  80. date_default_timezone_get()
  81. );
  82. unset($user_data['password']);
  83. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  84. $user_data = array_merge($user_data, $extra_data);
  85. // Create the form
  86. $form = new FormValidator('user_edit', 'post', '', '', array('style' => 'width: 60%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')));
  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'), array('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'), array('size' => '40'));
  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(
  124. 'email',
  125. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  126. 'maxlength',
  127. USERNAME_MAX_LENGTH
  128. );
  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'), array('size' => '40'));
  134. }
  135. // Phone
  136. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  137. // Picture
  138. $form->addElement('file', 'picture', get_lang('AddPicture'));
  139. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  140. $form->addRule(
  141. 'picture',
  142. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  143. 'filetype',
  144. $allowed_picture_types
  145. );
  146. if (strlen($user_data['picture_uri']) > 0) {
  147. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  148. }
  149. // Username
  150. if (api_get_setting('login_is_email') != 'true') {
  151. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  152. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  153. $form->addRule(
  154. 'username',
  155. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  156. 'maxlength',
  157. USERNAME_MAX_LENGTH
  158. );
  159. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  160. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  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 (count($extAuthSource) > 0) {
  166. $auth_sources = array();
  167. foreach ($extAuthSource as $key => $info) {
  168. // @todo : make uniform external authentification 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 authentification 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, '', false);
  183. }
  184. }
  185. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  186. $group = array();
  187. $group[] = $form->createElement('radio', 'reset_password', null, null, 2);
  188. $group[] = $form->createElement(
  189. 'password',
  190. 'password',
  191. null,
  192. array('onkeydown' => 'javascript: password_switch_radio_button();')
  193. );
  194. $form->addGroup($group, 'password', null, '', false);
  195. // Status
  196. $status = array();
  197. $status[COURSEMANAGER] = get_lang('Teacher');
  198. $status[STUDENT] = get_lang('Learner');
  199. $status[DRH] = get_lang('Drh');
  200. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  201. $form->addElement(
  202. 'select',
  203. 'status',
  204. get_lang('Profile'),
  205. $status,
  206. array('id' => 'status_select', 'onchange' => 'javascript: display_drh_list();', 'class' => 'chzn-select')
  207. );
  208. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  209. /*
  210. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  211. $drh_select = $form->addElement('select', 'hr_dept_id', get_lang('Drh'), array(), 'id="drh_select"');
  212. $drh_list = UserManager :: get_user_list(array('status' => DRH), api_sort_by_first_name() ? array('firstname', 'lastname') : array('lastname', 'firstname'));
  213. if (count($drh_list) == 0) {
  214. $drh_select->addOption('- '.get_lang('ThereIsNotStillAResponsible', '').' -', 0);
  215. } else {
  216. $drh_select->addOption('- '.get_lang('SelectAResponsible').' -', 0);
  217. }
  218. foreach($drh_list as $drh) {
  219. $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']);
  220. }
  221. $form->addElement('html', '</div>');
  222. */
  223. // Platform admin
  224. if (api_is_platform_admin()) {
  225. $group = array();
  226. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  227. $group[] = $form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  228. $user_data['status'] == 1 ? $display = 'block' : $display = 'none';
  229. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  230. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false);
  231. $form->addElement('html', '</div>');
  232. }
  233. //Language
  234. $form->addElement('select_language', 'language', get_lang('Language'));
  235. // Send email
  236. $group = array();
  237. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  238. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  239. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  240. // Registration Date
  241. $form->addElement('static', 'registration_date', get_lang('RegistrationDate'), $user_data['registration_date']);
  242. if (!$user_data['platform_admin']) {
  243. // Expiration Date
  244. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  245. $group = array();
  246. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  247. $group[] = $form->createElement(
  248. 'datepicker',
  249. 'expiration_date',
  250. null,
  251. array('form_name' => $form->getAttribute('name'), 'onchange' => 'javascript: enable_expiration_date();')
  252. );
  253. $form->addGroup($group, 'max_member_group', null, '', false);
  254. // Active account or inactive account
  255. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  256. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  257. }
  258. // EXTRA FIELDS
  259. $return_params = ExtraField::set_extra_fields_in_form($form, $extra_data, 'user_edit', true, $user_id, 'user');
  260. $jquery_ready_content = $return_params['jquery_ready_content'];
  261. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  262. $htmlHeadXtra[] = '<script>
  263. $(document).ready(function(){
  264. '.$jquery_ready_content.'
  265. });
  266. </script>';
  267. // Submit button
  268. $form->addElement('style_submit_button', 'submit', get_lang('ModifyInformation'), 'class="save"');
  269. // Set default values
  270. $user_data['reset_password'] = 0;
  271. $expiration_date = $user_data['expiration_date'];
  272. if ($expiration_date == '0000-00-00 00:00:00') {
  273. $user_data['radio_expiration_date'] = 0;
  274. $user_data['expiration_date'] = array();
  275. $user_data['expiration_date']['d'] = date('d');
  276. $user_data['expiration_date']['F'] = date('m');
  277. $user_data['expiration_date']['Y'] = date('Y');
  278. } else {
  279. $user_data['radio_expiration_date'] = 1;
  280. $user_data['expiration_date'] = array();
  281. $user_data['expiration_date']['d'] = substr($expiration_date, 8, 2);
  282. $user_data['expiration_date']['F'] = substr($expiration_date, 5, 2);
  283. $user_data['expiration_date']['Y'] = substr($expiration_date, 0, 4);
  284. $user_data['expiration_date']['H'] = substr($expiration_date, 11, 2);
  285. $user_data['expiration_date']['i'] = substr($expiration_date, 14, 2);
  286. }
  287. $form->setDefaults($user_data);
  288. $error_drh = false;
  289. // Validate form
  290. if ($form->validate()) {
  291. $user = $form->getSubmitValues();
  292. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  293. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  294. $error_drh = true;
  295. } else {
  296. $picture_element = $form->getElement('picture');
  297. $picture = $picture_element->getValue();
  298. $picture_uri = $user_data['picture_uri'];
  299. if ($user['delete_picture']) {
  300. $picture_uri = UserManager::delete_user_picture($user_id);
  301. } elseif (!empty($picture['name'])) {
  302. $picture_uri = UserManager::update_user_picture(
  303. $user_id,
  304. $_FILES['picture']['name'],
  305. $_FILES['picture']['tmp_name']
  306. );
  307. }
  308. $lastname = $user['lastname'];
  309. $firstname = $user['firstname'];
  310. $password = $user['password'];
  311. $auth_source = $user['auth_source'];
  312. $official_code = $user['official_code'];
  313. $email = $user['email'];
  314. $phone = $user['phone'];
  315. $username = $user['username'];
  316. $status = intval($user['status']);
  317. $platform_admin = intval($user['platform_admin']);
  318. $send_mail = intval($user['send_mail']);
  319. $reset_password = intval($user['reset_password']);
  320. $hr_dept_id = intval($user['hr_dept_id']);
  321. $language = $user['language'];
  322. if ($user['radio_expiration_date'] == '1' && !$user_data['platform_admin']) {
  323. $expiration_date = Text::return_datetime_from_array($user['expiration_date']);
  324. } else {
  325. $expiration_date = '0000-00-00 00:00:00';
  326. }
  327. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  328. //If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
  329. if ($platform_admin == 1) {
  330. $status = COURSEMANAGER;
  331. }
  332. if (api_get_setting('login_is_email') == 'true') {
  333. $username = $email;
  334. }
  335. UserManager::update_user(
  336. $user_id,
  337. $firstname,
  338. $lastname,
  339. $username,
  340. $password,
  341. $auth_source,
  342. $email,
  343. $status,
  344. $official_code,
  345. $phone,
  346. $picture_uri,
  347. $expiration_date,
  348. $active,
  349. null,
  350. $hr_dept_id,
  351. null,
  352. $language,
  353. null,
  354. $send_mail,
  355. $reset_password
  356. );
  357. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  358. $up = UserManager::update_openid($user_id, $user['openid']);
  359. }
  360. if ($user_id != $_SESSION['_uid']) {
  361. if ($platform_admin == 1) {
  362. UserManager::add_user_as_admin($user_id);
  363. } else {
  364. UserManager::remove_user_admin($user_id);
  365. }
  366. }
  367. foreach ($user as $key => $value) {
  368. if (substr($key, 0, 6) == 'extra_') {
  369. //an extra field
  370. //@todo remove this as well as in the profile.php ad put it in a function
  371. if (is_array($value) && isset($value['Y']) && isset($value['F']) && isset($value['d'])) {
  372. if (isset($value['H']) && isset($value['i'])) {
  373. // extra field date time
  374. $time = mktime($value['H'], $value['i'], 0, $value['F'], $value['d'], $value['Y']);
  375. $value = date('Y-m-d H:i:s', $time);
  376. } else {
  377. // extra field date
  378. $time = mktime(0, 0, 0, $value['F'], $value['d'], $value['Y']);
  379. $value = date('Y-m-d', $time);
  380. }
  381. }
  382. UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
  383. }
  384. }
  385. $tok = Security::get_token();
  386. header(
  387. 'Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok
  388. );
  389. exit();
  390. }
  391. }
  392. $message = null;
  393. if ($error_drh) {
  394. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  395. $message = Display::return_message($err_msg, 'error');
  396. }
  397. // USER PICTURE
  398. $image_path = UserManager::get_user_picture_path_by_id($user_id, 'web');
  399. $image_dir = $image_path['dir'];
  400. $image = $image_path['file'];
  401. $image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_CODE_PATH).'img/unknown.jpg');
  402. $image_size = api_getimagesize($image_file);
  403. $img_attributes = 'src="'.$image_file.'?rand='.time().'" '
  404. .'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
  405. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  406. if ($image_size['width'] > 300) { //limit display width to 300px
  407. $img_attributes .= 'width="300" ';
  408. }
  409. // get the path,width and height from original picture
  410. $big_image = $image_dir.'big_'.$image;
  411. $big_image_size = api_getimagesize($big_image);
  412. $big_image_width = $big_image_size['width'];
  413. $big_image_height = $big_image_size['height'];
  414. $url_big_image = $big_image.'?rnd='.time();
  415. $content = null;
  416. if ($image == '') {
  417. $content .= '<img '.$img_attributes.' />';
  418. } else {
  419. $content .= '<input type="image" '.$img_attributes.' onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');"/>';
  420. }
  421. // Display form
  422. $content .= $form->return_form();
  423. $tpl = new Template($tool_name);
  424. $tpl->assign('message', $message);
  425. $tpl->assign('content', $content);
  426. $tpl->display_one_col_template();