user_edit.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  7. api_protect_super_admin($user_id, null, true);
  8. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  9. $htmlHeadXtra[] = '<script>
  10. var is_platform_id = "'.$is_platform_admin.'";
  11. function enable_expiration_date() {
  12. document.user_edit.radio_expiration_date[0].checked=false;
  13. document.user_edit.radio_expiration_date[1].checked=true;
  14. }
  15. function password_switch_radio_button(){
  16. var input_elements = document.getElementsByTagName("input");
  17. for (var i = 0; i < input_elements.length; i++) {
  18. if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
  19. input_elements.item(i).checked = true;
  20. }
  21. }
  22. }
  23. function display_drh_list(){
  24. var $radios = $("input:radio[name=platform_admin]");
  25. if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  26. if (is_platform_id == 1)
  27. document.getElementById("id_platform_admin").style.display="block";
  28. } else if (document.getElementById("status_select").value=='.STUDENT.') {
  29. if (is_platform_id == 1)
  30. document.getElementById("id_platform_admin").style.display="none";
  31. $radios.filter("[value=0]").attr("checked", true);
  32. } else {
  33. if (is_platform_id == 1)
  34. document.getElementById("id_platform_admin").style.display="none";
  35. $radios.filter("[value=0]").attr("checked", true);
  36. }
  37. }
  38. function show_image(image,width,height) {
  39. width = parseInt(width) + 20;
  40. height = parseInt(height) + 20;
  41. window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
  42. }
  43. </script>';
  44. $noPHP_SELF = true;
  45. $tool_name = get_lang('ModifyUserInfo');
  46. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  47. $interbreadcrumb[] = array('url' => "user_list.php", "name" => get_lang('UserList'));
  48. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  49. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  50. $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."'";
  51. $res = Database::query($sql);
  52. if (Database::num_rows($res) != 1) {
  53. header('Location: user_list.php');
  54. exit;
  55. }
  56. $user_data = Database::fetch_array($res, 'ASSOC');
  57. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  58. $user_data['send_mail'] = 0;
  59. $user_data['old_password'] = $user_data['password'];
  60. //Convert the registration date of the user
  61. //@todo remove the date_default_timezone_get() see UserManager::create_user function
  62. $user_data['registration_date'] = api_get_local_time(
  63. $user_data['registration_date'],
  64. null,
  65. date_default_timezone_get()
  66. );
  67. unset($user_data['password']);
  68. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  69. $user_data = array_merge($user_data, $extra_data);
  70. // Create the form
  71. $form = new FormValidator('user_edit', 'post', '', '', array('style' => 'width: 60%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;')));
  72. $form->addElement('header', '', $tool_name);
  73. $form->addElement('hidden', 'user_id', $user_id);
  74. if (api_is_western_name_order()) {
  75. // Firstname
  76. $form->addElement('text', 'firstname', get_lang('FirstName'));
  77. $form->applyFilter('firstname', 'html_filter');
  78. $form->applyFilter('firstname', 'trim');
  79. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  80. // Lastname
  81. $form->addElement('text', 'lastname', get_lang('LastName'));
  82. $form->applyFilter('lastname', 'html_filter');
  83. $form->applyFilter('lastname', 'trim');
  84. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  85. } else {
  86. // Lastname
  87. $form->addElement('text', 'lastname', get_lang('LastName'));
  88. $form->applyFilter('lastname', 'html_filter');
  89. $form->applyFilter('lastname', 'trim');
  90. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  91. // Firstname
  92. $form->addElement('text', 'firstname', get_lang('FirstName'));
  93. $form->applyFilter('firstname', 'html_filter');
  94. $form->applyFilter('firstname', 'trim');
  95. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  96. }
  97. // Official code
  98. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  99. $form->applyFilter('official_code', 'html_filter');
  100. $form->applyFilter('official_code', 'trim');
  101. // Email
  102. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  103. $form->addRule('email', get_lang('EmailWrong'), 'email');
  104. if (api_get_setting('registration', 'email') == 'true') {
  105. $form->addRule('email', get_lang('EmailWrong'), 'required');
  106. }
  107. if (api_get_setting('login_is_email') == 'true') {
  108. $form->addRule(
  109. 'email',
  110. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  111. 'maxlength',
  112. USERNAME_MAX_LENGTH
  113. );
  114. $form->addRule('email', get_lang('UserTaken'), 'username_available', $user_data['username']);
  115. }
  116. // OpenID
  117. if (api_get_setting('openid_authentication') == 'true') {
  118. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => '40'));
  119. }
  120. // Phone
  121. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  122. // Picture
  123. $form->addElement('file', 'picture', get_lang('AddPicture'));
  124. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  125. $form->addRule(
  126. 'picture',
  127. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  128. 'filetype',
  129. $allowed_picture_types
  130. );
  131. if (strlen($user_data['picture_uri']) > 0) {
  132. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  133. }
  134. // Username
  135. if (api_get_setting('login_is_email') != 'true') {
  136. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  137. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  138. $form->addRule(
  139. 'username',
  140. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  141. 'maxlength',
  142. USERNAME_MAX_LENGTH
  143. );
  144. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  145. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  146. }
  147. // Password
  148. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('DontResetPassword'), 0);
  149. $nb_ext_auth_source_added = 0;
  150. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  151. $auth_sources = array();
  152. foreach ($extAuthSource as $key => $info) {
  153. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  154. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  155. // extAuthSource always on for CAS even if not activated
  156. // same action for file user_add.php
  157. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  158. $auth_sources[$key] = $key;
  159. $nb_ext_auth_source_added++;
  160. }
  161. }
  162. if ($nb_ext_auth_source_added > 0) {
  163. // @todo check the radio button for external authentification and select the external authentification in the menu
  164. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('ExternalAuthentication').' ', 3);
  165. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  166. $group[] = $form->createElement('static', '', '', '<br />');
  167. $form->addGroup($group, 'password', null, '', false);
  168. }
  169. }
  170. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  171. // before giving the form to reset the password, check the corresponding param
  172. if (api_is_global_platform_admin() or api_get_setting('admins_can_set_users_pass')==='true') {
  173. $group = array();
  174. $group[] =$form->createElement('radio', 'reset_password', null, null, 2);
  175. $group[] =$form->createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button();'));
  176. $form->addGroup($group, 'password', null, '', false);
  177. }
  178. // Status.
  179. $status = api_get_user_roles();
  180. unset($status[ANONYMOUS]);
  181. $form->addElement(
  182. 'select',
  183. 'status',
  184. get_lang('Profile'),
  185. $status,
  186. array(
  187. 'id' => 'status_select',
  188. 'onchange' => 'javascript: display_drh_list();',
  189. 'class' => 'chzn-select'
  190. )
  191. );
  192. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  193. /*
  194. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  195. $drh_select = $form->addElement('select', 'hr_dept_id', get_lang('Drh'), array(), 'id="drh_select"');
  196. $drh_list = UserManager :: get_user_list(array('status' => DRH), api_sort_by_first_name() ? array('firstname', 'lastname') : array('lastname', 'firstname'));
  197. if (count($drh_list) == 0) {
  198. $drh_select->addOption('- '.get_lang('ThereIsNotStillAResponsible', '').' -', 0);
  199. } else {
  200. $drh_select->addOption('- '.get_lang('SelectAResponsible').' -', 0);
  201. }
  202. foreach($drh_list as $drh) {
  203. $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']);
  204. }
  205. $form->addElement('html', '</div>');
  206. */
  207. //Language
  208. if (api_is_platform_admin()) {
  209. $group = array();
  210. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
  211. $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
  212. $user_data['status'] == 1 ? $display = 'block':$display = 'none';
  213. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
  214. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false);
  215. $form->addElement('html', '</div>');
  216. }
  217. //Language
  218. $form->addElement('select_language', 'language', get_lang('Language'));
  219. // Send email
  220. $group = array();
  221. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  222. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  223. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  224. // Registration Date
  225. $creatorInfo = api_get_user_info($user_data['creator_id']);
  226. $date = sprintf(get_lang('CreatedByXYOnZ'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']);
  227. $form->addElement('html', '<div class="control-group"><label class="control-label">'.get_lang('RegistrationDate').'</label><div class="controls">'.$date.'</div></div>');
  228. // Expiration Date
  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('form_name' => $form->getAttribute('name'), '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. // Active account or inactive account
  241. // EXTRA FIELDS
  242. $extraField = new ExtraField('user');
  243. $return_params = $extraField->set_extra_fields_in_form($form, $extra_data, 'user_edit', true, $user_id);
  244. $jquery_ready_content = $return_params['jquery_ready_content'];
  245. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  246. $htmlHeadXtra[] = '<script>
  247. $(document).ready(function(){
  248. '.$jquery_ready_content.'
  249. });
  250. </script>';
  251. // Submit button
  252. $form->addElement('style_submit_button', 'submit', get_lang('ModifyInformation'), 'class="save"');
  253. // Set default values
  254. $user_data['reset_password'] = 0;
  255. $expiration_date = $user_data['expiration_date'];
  256. if ($expiration_date == '0000-00-00 00:00:00') {
  257. $user_data['radio_expiration_date'] = 0;
  258. $user_data['expiration_date'] = array();
  259. $user_data['expiration_date']['d'] = date('d');
  260. $user_data['expiration_date']['F'] = date('m');
  261. $user_data['expiration_date']['Y'] = date('Y');
  262. } else {
  263. $user_data['radio_expiration_date'] = 1;
  264. $user_data['expiration_date'] = array();
  265. $user_data['expiration_date']['d'] = substr($expiration_date, 8, 2);
  266. $user_data['expiration_date']['F'] = substr($expiration_date, 5, 2);
  267. $user_data['expiration_date']['Y'] = substr($expiration_date, 0, 4);
  268. $user_data['expiration_date']['H'] = substr($expiration_date, 11, 2);
  269. $user_data['expiration_date']['i'] = substr($expiration_date, 14, 2);
  270. }
  271. $user = Database::getManager()->getRepository('ChamiloLMSCoreBundle:User')->find($user_data['user_id']);
  272. $roles = $user->getRoles();
  273. $role = array();
  274. if (!empty($roles)) {
  275. $role = current($roles);
  276. $role = $role->getId();
  277. }
  278. $user_data['status'] = $role;
  279. $form->setDefaults($user_data);
  280. $error_drh = false;
  281. // Validate form
  282. if ($form->validate()) {
  283. $user = $form->getSubmitValues();
  284. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  285. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  286. $error_drh = true;
  287. } else {
  288. $picture_element = $form->getElement('picture');
  289. $picture = $picture_element->getValue();
  290. $picture_uri = $user_data['picture_uri'];
  291. if ($user['delete_picture']) {
  292. $picture_uri = UserManager::delete_user_picture($user_id);
  293. } elseif (!empty($picture['name'])) {
  294. $picture_uri = UserManager::update_user_picture(
  295. $user_id,
  296. $_FILES['picture']['name'],
  297. $_FILES['picture']['tmp_name']
  298. );
  299. }
  300. $lastname = $user['lastname'];
  301. $firstname = $user['firstname'];
  302. $password = $user['password'];
  303. $auth_source = $user['auth_source'];
  304. $official_code = $user['official_code'];
  305. $email = $user['email'];
  306. $phone = $user['phone'];
  307. $username = $user['username'];
  308. $status = intval($user['status']);
  309. $send_mail = intval($user['send_mail']);
  310. $reset_password = intval($user['reset_password']);
  311. $hr_dept_id = intval($user['hr_dept_id']);
  312. $language = $user['language'];
  313. if ($user['radio_expiration_date'] == '1') {
  314. $expiration_date = Text::return_datetime_from_array($user['expiration_date']);
  315. } else {
  316. $expiration_date = null;
  317. }
  318. $active = intval($user['active']);
  319. if (api_get_setting('login_is_email') == 'true') {
  320. $username = $email;
  321. }
  322. UserManager::update_user(
  323. $user_id,
  324. $firstname,
  325. $lastname,
  326. $username,
  327. $password,
  328. $auth_source,
  329. $email,
  330. $status,
  331. $official_code,
  332. $phone,
  333. $picture_uri,
  334. $expiration_date,
  335. $active,
  336. null,
  337. $hr_dept_id,
  338. null,
  339. $language,
  340. null,
  341. $send_mail,
  342. $reset_password
  343. );
  344. if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
  345. $up = UserManager::update_openid($user_id, $user['openid']);
  346. }
  347. // Using the extra field value obj
  348. $extraFieldValues = new ExtraFieldValue('user');
  349. $extraFieldValues->save_field_values($user);
  350. $tok = Security::get_token();
  351. header(
  352. 'Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok
  353. );
  354. exit();
  355. }
  356. }
  357. $message = null;
  358. if ($error_drh) {
  359. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  360. $message = Display::return_message($err_msg, 'error');
  361. }
  362. // USER PICTURE
  363. $image_path = UserManager::get_user_picture_path_by_id($user_id, 'web');
  364. $image_dir = $image_path['dir'];
  365. $image = $image_path['file'];
  366. $image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_IMG_PATH).'unknown.jpg');
  367. $image_size = api_getimagesize($image_file);
  368. $img_attributes = 'src="'.$image_file.'?rand='.time().'" '
  369. .'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
  370. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  371. if ($image_size['width'] > 300) { //limit display width to 300px
  372. $img_attributes .= 'width="300" ';
  373. }
  374. // get the path,width and height from original picture
  375. $big_image = $image_dir.'big_'.$image;
  376. $big_image_size = api_getimagesize($big_image);
  377. $big_image_width = $big_image_size['width'];
  378. $big_image_height = $big_image_size['height'];
  379. $url_big_image = $big_image.'?rnd='.time();
  380. $content = null;
  381. if ($image == '') {
  382. $content .= '<img '.$img_attributes.' />';
  383. } else {
  384. $content .= '<input type="image" '.$img_attributes.' onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');"/>';
  385. }
  386. // Display form
  387. $content .= $form->return_form();
  388. $app['title'] = $tool_name;
  389. $tpl = $app['template'];
  390. $tpl->assign('message', $message);
  391. $tpl->assign('content', $content);
  392. $tpl->display_one_col_template();