user_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. use Chamilo\CoreBundle\Framework\Container;
  7. use Chamilo\UserBundle\Entity\User;
  8. use Chamilo\UserBundle\Form\UserType;
  9. $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  10. api_protect_super_admin($user_id, null, true);
  11. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  12. $tool_name = get_lang('ModifyUserInfo');
  13. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  14. $interbreadcrumb[] = array('url' => "user_list.php", "name" => get_lang('UserList'));
  15. //$user = Container::getEntityManager()->getRepository('ChamiloUserBundle:User')->find($user_id);
  16. $user_data = api_get_user_info($user_id, false, true);
  17. $user_data['platform_admin'] = api_is_platform_admin_by_id($user_id);
  18. $user_data['send_mail'] = 0;
  19. $user_data['old_password'] = $user_data['password'];
  20. //Convert the registration date of the user
  21. //@todo remove the date_default_timezone_get() see UserManager::create_user function
  22. $user_data['registration_date'] = api_get_local_time(
  23. $user_data['registration_date'],
  24. null,
  25. date_default_timezone_get()
  26. );
  27. unset($user_data['password']);
  28. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  29. $user_data = array_merge($user_data, $extra_data);
  30. // Create the form
  31. $form = new FormValidator('user_edit', 'post', api_get_self().'?user_id='.$user_id);
  32. $form->addElement('header', '', $tool_name);
  33. $form->addElement('hidden', 'user_id', $user_id);
  34. if (api_is_western_name_order()) {
  35. // First name
  36. $form->addElement('text', 'firstname', get_lang('FirstName'));
  37. $form->applyFilter('firstname', 'html_filter');
  38. $form->applyFilter('firstname', 'trim');
  39. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  40. // Lastname
  41. $form->addElement('text', 'lastname', get_lang('LastName'));
  42. $form->applyFilter('lastname', 'html_filter');
  43. $form->applyFilter('lastname', 'trim');
  44. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  45. } else {
  46. // Lastname
  47. $form->addElement('text', 'lastname', get_lang('LastName'));
  48. $form->applyFilter('lastname', 'html_filter');
  49. $form->applyFilter('lastname', 'trim');
  50. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  51. // Firstname
  52. $form->addElement('text', 'firstname', get_lang('FirstName'));
  53. $form->applyFilter('firstname', 'html_filter');
  54. $form->applyFilter('firstname', 'trim');
  55. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  56. }
  57. // Official code
  58. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  59. $form->applyFilter('official_code', 'html_filter');
  60. $form->applyFilter('official_code', 'trim');
  61. // Email
  62. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  63. $form->addRule('email', get_lang('EmailWrong'), 'email');
  64. /*if (api_get_setting('registration', 'email') == 'true') {
  65. $form->addRule('email', get_lang('EmailWrong'), 'required');
  66. }*/
  67. if (api_get_setting('profile.login_is_email') == 'true') {
  68. $form->addRule(
  69. 'email',
  70. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  71. 'maxlength',
  72. USERNAME_MAX_LENGTH
  73. );
  74. $form->addRule('email', get_lang('UserTaken'), 'username_available', $user_data['username']);
  75. }
  76. // OpenID
  77. /*if (api_get_setting('openid_authentication') == 'true') {
  78. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => '40'));
  79. }*/
  80. // Phone
  81. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  82. // Picture
  83. $form->addElement('file', 'picture', get_lang('AddPicture'));
  84. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  85. $form->addRule(
  86. 'picture',
  87. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  88. 'filetype',
  89. $allowed_picture_types
  90. );
  91. if (strlen($user_data['picture_uri']) > 0) {
  92. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  93. }
  94. // Username
  95. if (api_get_setting('profile.login_is_email') != 'true') {
  96. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  97. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  98. $form->addRule(
  99. 'username',
  100. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  101. 'maxlength',
  102. USERNAME_MAX_LENGTH
  103. );
  104. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  105. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  106. }
  107. // Password
  108. $form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('DontResetPassword'), 0);
  109. $nb_ext_auth_source_added = 0;
  110. if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
  111. $auth_sources = array();
  112. foreach ($extAuthSource as $key => $info) {
  113. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  114. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  115. // extAuthSource always on for CAS even if not activated
  116. // same action for file user_add.php
  117. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  118. $auth_sources[$key] = $key;
  119. $nb_ext_auth_source_added++;
  120. }
  121. }
  122. if ($nb_ext_auth_source_added > 0) {
  123. // @todo check the radio button for external authentification and select the external authentification in the menu
  124. $group[] = $form->createElement('radio', 'reset_password', null, get_lang('ExternalAuthentication').' ', 3);
  125. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  126. $group[] = $form->createElement('static', '', '', '<br />');
  127. $form->addGroup($group, 'password', null, '', false);
  128. }
  129. }
  130. $form->addElement('radio', 'reset_password', null, get_lang('AutoGeneratePassword'), 1);
  131. // before giving the form to reset the password, check the corresponding param
  132. if (api_is_global_platform_admin() or api_get_setting('admins_can_set_users_pass')==='true') {
  133. $group = array();
  134. $group[] =$form->createElement('radio', 'reset_password', null, null, 2);
  135. $group[] =$form->createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button();'));
  136. $form->addGroup($group, 'password', null, '', false);
  137. }
  138. // Status.
  139. $status = api_get_user_roles();
  140. unset($status[ANONYMOUS]);
  141. $form->addElement(
  142. 'select',
  143. 'status',
  144. get_lang('Profile'),
  145. $status,
  146. array(
  147. 'id' => 'status_select',
  148. 'class' => 'chzn-select',
  149. 'multiple' => 'multiple'
  150. )
  151. );
  152. $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none';
  153. //Language
  154. $form->addElement('select_language', 'language', get_lang('Language'));
  155. // Send email
  156. $group = array();
  157. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  158. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  159. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;', false);
  160. // Registration Date
  161. $creatorInfo = api_get_user_info($user_data['creator_id']);
  162. $date = sprintf(
  163. get_lang('CreatedByXYOnZ'),
  164. 'user_information.php?user_id='.$user_data['creator_id'],
  165. $creatorInfo['username'],
  166. $user_data['registration_date']
  167. );
  168. $form->addElement(
  169. 'label',
  170. get_lang('RegistrationDate'),
  171. $date
  172. );
  173. // Expiration Date
  174. if (!$user_data['platform_admin']) {
  175. // Expiration Date
  176. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  177. $group = array ();
  178. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  179. $group[] = $form->createElement('date_time_picker', 'expiration_date', array('onchange' => 'javascript: enable_expiration_date();'));
  180. $form->addGroup($group, 'max_member_group', null, '', false);
  181. // Active account or inactive account
  182. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  183. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  184. }
  185. // Active account or inactive account
  186. // EXTRA FIELDS
  187. $extraField = new ExtraField('user');
  188. $return_params = $extraField->set_extra_fields_in_form($form, $extra_data, 'user_edit', true, $user_id);
  189. $jquery_ready_content = $return_params['jquery_ready_content'];
  190. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  191. $htmlHeadXtra[] = '<script>
  192. $(document).ready(function(){
  193. '.$jquery_ready_content.'
  194. });
  195. </script>';
  196. // Submit button
  197. $form->addElement('style_submit_button', 'submit', get_lang('ModifyInformation'), 'class="save"');
  198. // Set default values
  199. $user_data['reset_password'] = 0;
  200. $expiration_date = $user_data['expiration_date'];
  201. if ($expiration_date == '0000-00-00 00:00:00') {
  202. $user_data['radio_expiration_date'] = 0;
  203. } else {
  204. $user_data['radio_expiration_date'] = 1;
  205. }
  206. $user = Database::getManager()->getRepository('ChamiloUserBundle:User')->find($user_data['user_id']);
  207. $roles = $user->getGroups();
  208. $roleToArray = array();
  209. if (!empty($roles)) {
  210. foreach($roles as $role) {
  211. $roleId = $role->getId();
  212. $roleToArray[] = $roleId;
  213. }
  214. }
  215. $user_data['status'] = $roleToArray;
  216. $form->setDefaults($user_data);
  217. $error_drh = false;
  218. // Validate form
  219. if ($form->validate()) {
  220. $user = $form->getSubmitValues();
  221. $is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);
  222. if ($user['status'] == DRH && $is_user_subscribed_in_course) {
  223. $error_drh = true;
  224. } else {
  225. $picture_element = $form->getElement('picture');
  226. $picture = $picture_element->getValue();
  227. $picture_uri = $user_data['picture_uri'];
  228. if (isset($user['delete_picture']) && $user['delete_picture']) {
  229. $picture_uri = UserManager::delete_user_picture($user_id);
  230. } elseif (!empty($picture['name'])) {
  231. $picture_uri = UserManager::update_user_picture(
  232. $user_id,
  233. $_FILES['picture']['name'],
  234. $_FILES['picture']['tmp_name']
  235. );
  236. }
  237. $lastname = $user['lastname'];
  238. $firstname = $user['firstname'];
  239. $password = $user['password'];
  240. $auth_source = null;
  241. $official_code = $user['official_code'];
  242. $email = $user['email'];
  243. $phone = $user['phone'];
  244. $username = $user['username'];
  245. $status = $user['status'];
  246. $send_mail = intval($user['send_mail']);
  247. $reset_password = intval($user['reset_password']);
  248. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
  249. $language = $user['language'];
  250. if (isset($user['radio_expiration_date']) && $user['radio_expiration_date'] == '1') {
  251. $expiration_date = new \DateTime($user['expiration_date']);
  252. } else {
  253. $expiration_date = null;
  254. }
  255. $active = isset($user['active']) ? intval($user['active']) : 0;
  256. if (api_get_setting('profile.login_is_email') == 'true') {
  257. $username = $email;
  258. }
  259. UserManager::update_user(
  260. $user_id,
  261. $firstname,
  262. $lastname,
  263. $username,
  264. $password,
  265. $auth_source,
  266. $email,
  267. $status,
  268. $official_code,
  269. $phone,
  270. $picture_uri,
  271. $expiration_date,
  272. $active,
  273. null,
  274. $hr_dept_id,
  275. null,
  276. $language,
  277. null,
  278. $send_mail,
  279. $reset_password
  280. );
  281. /*if (api_get_setting('openid_authentication') == 'true' && !empty
  282. ($user['openid'])) {
  283. $up = UserManager::update_openid($user_id, $user['openid']);
  284. }*/
  285. // Using the extra field value obj
  286. $extraFieldValues = new ExtraFieldValue('user');
  287. $extraFieldValues->save_field_values($user);
  288. $tok = Security::get_token();
  289. /*
  290. header(
  291. 'Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')).'&sec_token='.$tok
  292. );
  293. exit();*/
  294. }
  295. }
  296. $message = null;
  297. if ($error_drh) {
  298. $err_msg = get_lang('StatusCanNotBeChangedToHumanResourcesManager');
  299. $message = Display::return_message($err_msg, 'error');
  300. }
  301. // USER PICTURE
  302. $image_path = UserManager::get_user_picture_path_by_id($user_id, 'web');
  303. $image_dir = $image_path['dir'];
  304. $image = $image_path['file'];
  305. $image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_IMG_PATH).'unknown.jpg');
  306. $image_size = api_getimagesize($image_file);
  307. // get the path,width and height from original picture
  308. $big_image = $image_dir.'big_'.$image;
  309. $big_image_size = api_getimagesize($big_image);
  310. $big_image_width = $big_image_size['width'];
  311. $big_image_height = $big_image_size['height'];
  312. $url_big_image = $big_image.'?rnd='.time();
  313. // Display form
  314. $content = $form->return_form();
  315. $em = Container::getEntityManager();
  316. $request = Container::getRequest();
  317. $user = new User();
  318. if (!empty($user_id)) {
  319. $user = $em->getRepository('ChamiloUserBundle:User')->find($user_id);
  320. }
  321. $builder = Container::getFormFactory()->createBuilder(
  322. new UserType(Container::getSecurity()),
  323. $user
  324. );
  325. $form = $builder->getForm();
  326. $form->handleRequest($request);
  327. if ($form->isValid()) {
  328. $em->flush();
  329. Container::addFlash(get_lang('Updated'));
  330. $url = Container::getRouter()->generate(
  331. 'main',
  332. array('name' => 'admin/user_list.php')
  333. );
  334. header('Location: '.$url);
  335. exit;
  336. }
  337. $urlAction = api_get_self().'?user_id='.$user_id;
  338. echo Container::getTemplate()->render(
  339. 'ChamiloCoreBundle:User:create.html.twig',
  340. array(
  341. 'form' => $form->createView(),
  342. 'url' => $urlAction
  343. )
  344. );