user_add.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. // Including necessary libraries.
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. // Section for the tabs
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. // User permissions
  12. api_protect_admin_script(true);
  13. api_protect_limit_for_session_admin();
  14. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  15. $message = null;
  16. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#password');
  17. $checkPass = api_get_setting('allow_strength_pass_checker');
  18. if ($checkPass == 'true') {
  19. $htmlHeadXtra[] = '
  20. <script>
  21. $(document).ready(function() {
  22. $("#password").keypress(function() {
  23. $("#password").each(function(index, value) {
  24. var value = $(this).attr("value");
  25. if (value == 0) {
  26. $("#password_progress").show();
  27. $(".password-verdict").show();
  28. $(".error-list").show();
  29. } else {
  30. $("#password_progress").hide();
  31. $(".password-verdict").hide();
  32. $(".error-list").hide();
  33. }
  34. });
  35. });
  36. });
  37. </script>';
  38. }
  39. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  40. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  41. $htmlHeadXtra[] = '
  42. <script>
  43. $("#status_select").ready(function() {
  44. if ($(this).attr("value") != '.STUDENT.') {
  45. $("#id_platform_admin").hide();
  46. }
  47. });
  48. function enable_expiration_date() { //v2.0
  49. document.user_add.radio_expiration_date[0].checked=false;
  50. document.user_add.radio_expiration_date[1].checked=true;
  51. }
  52. function password_switch_radio_button() {
  53. var input_elements = document.getElementsByTagName("input");
  54. for (var i = 0; i < input_elements.length; i++) {
  55. if (input_elements.item(i).name == "password[password_auto]" && input_elements.item(i).value == "0") {
  56. input_elements.item(i).checked = true;
  57. }
  58. }
  59. }
  60. var is_platform_id = "'.$is_platform_admin.'";
  61. function display_drh_list(){
  62. if (document.getElementById("status_select").value=='.STUDENT.') {
  63. document.getElementById("drh_list").style.display="block";
  64. if (is_platform_id == 1)
  65. document.getElementById("id_platform_admin").style.display="none";
  66. } else if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  67. document.getElementById("drh_list").style.display="none";
  68. if (is_platform_id == 1)
  69. document.getElementById("id_platform_admin").style.display="block";
  70. } else {
  71. document.getElementById("drh_list").style.display="none";
  72. if (is_platform_id == 1)
  73. document.getElementById("id_platform_admin").style.display="none";
  74. }
  75. }
  76. </script>';
  77. if (!empty($_GET['message'])) {
  78. $message = urldecode($_GET['message']);
  79. }
  80. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  81. $interbreadcrumb[] = ["url" => 'user_list.php', "name" => get_lang('UserList')];
  82. $tool_name = get_lang('AddUsers');
  83. // Create the form
  84. $form = new FormValidator('user_add');
  85. $form->addElement('header', '', $tool_name);
  86. if (api_is_western_name_order()) {
  87. // Firstname
  88. $form->addElement(
  89. 'text',
  90. 'firstname',
  91. get_lang('FirstName'),
  92. [
  93. 'id' => 'firstname',
  94. ]
  95. );
  96. $form->applyFilter('firstname', 'html_filter');
  97. $form->applyFilter('firstname', 'trim');
  98. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  99. // Lastname
  100. $form->addElement(
  101. 'text',
  102. 'lastname',
  103. get_lang('LastName'),
  104. [
  105. 'id' => 'lastname',
  106. ]
  107. );
  108. $form->applyFilter('lastname', 'html_filter');
  109. $form->applyFilter('lastname', 'trim');
  110. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  111. } else {
  112. // Lastname
  113. $form->addElement(
  114. 'text',
  115. 'lastname',
  116. get_lang('LastName'),
  117. [
  118. 'id' => 'lastname',
  119. ]
  120. );
  121. $form->applyFilter('lastname', 'html_filter');
  122. $form->applyFilter('lastname', 'trim');
  123. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  124. // Firstname
  125. $form->addElement(
  126. 'text',
  127. 'firstname',
  128. get_lang('FirstName'),
  129. [
  130. 'id' => 'firstname',
  131. ]
  132. );
  133. $form->applyFilter('firstname', 'html_filter');
  134. $form->applyFilter('firstname', 'trim');
  135. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  136. }
  137. // Official code
  138. $form->addElement(
  139. 'text',
  140. 'official_code',
  141. get_lang('OfficialCode'),
  142. [
  143. 'size' => '40',
  144. 'id' => 'official_code',
  145. ]
  146. );
  147. $form->applyFilter('official_code', 'html_filter');
  148. $form->applyFilter('official_code', 'trim');
  149. // Email
  150. $form->addElement('text', 'email', get_lang('Email'), ['size' => '40', 'autocomplete' => 'off', 'id' => 'email']);
  151. $form->addRule('email', get_lang('EmailWrong'), 'email');
  152. if (api_get_setting('registration', 'email') == 'true') {
  153. $form->addRule('email', get_lang('EmailWrong'), 'required');
  154. }
  155. if (api_get_setting('login_is_email') == 'true') {
  156. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  157. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  158. }
  159. // Phone
  160. $form->addElement('text', 'phone', get_lang('PhoneNumber'), ['autocomplete' => 'off', 'id' => 'phone']);
  161. // Picture
  162. $form->addFile(
  163. 'picture',
  164. get_lang('AddImage'),
  165. ['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '1 / 1']
  166. );
  167. $allowed_picture_types = api_get_supported_image_extensions(false);
  168. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  169. // Username
  170. if (api_get_setting('login_is_email') != 'true') {
  171. $form->addElement('text', 'username', get_lang('LoginName'), ['id' => 'username', 'maxlength' => USERNAME_MAX_LENGTH, 'autocomplete' => 'off']);
  172. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  173. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string) USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  174. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  175. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  176. }
  177. // Password
  178. $group = [];
  179. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  180. $nb_ext_auth_source_added = 0;
  181. if (isset($extAuthSource) && count($extAuthSource) > 0) {
  182. $auth_sources = [];
  183. foreach ($extAuthSource as $key => $info) {
  184. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  185. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  186. // extAuthSource always on for CAS even if not activated
  187. // same action for file user_edit.php
  188. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  189. $auth_sources[$key] = $key;
  190. $nb_ext_auth_source_added++;
  191. }
  192. }
  193. if ($nb_ext_auth_source_added > 0) {
  194. $group[] = $form->createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  195. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  196. $group[] = $form->createElement('static', '', '', '<br />');
  197. }
  198. }
  199. $group[] = $form->createElement(
  200. 'radio',
  201. 'password_auto',
  202. get_lang('Password'),
  203. get_lang('AutoGeneratePassword').'<br />',
  204. 1
  205. );
  206. $group[] = $form->createElement(
  207. 'radio',
  208. 'password_auto',
  209. 'id="radio_user_password"',
  210. get_lang('EnterPassword'),
  211. 0
  212. );
  213. $group[] = $form->createElement(
  214. 'password',
  215. 'password',
  216. null,
  217. [
  218. 'id' => 'password',
  219. 'autocomplete' => 'off',
  220. 'onkeydown' => 'javascript: password_switch_radio_button();',
  221. //'required' => 'required'
  222. ]
  223. );
  224. $form->addGroup($group, 'password', get_lang('Password'));
  225. $form->addPasswordRule('password', 'password');
  226. $form->addGroupRule('password', get_lang('EnterPassword'), 'required', null, 1);
  227. if ($checkPass) {
  228. $passwordStrengthLabels = '
  229. <div id="password-verdict"></div>
  230. <div id="password-errors"></div>
  231. <div id="password_progress" style="display:none"></div>
  232. ';
  233. $form->addElement('label', null, $passwordStrengthLabels);
  234. }
  235. // Status
  236. $status = [];
  237. $status[COURSEMANAGER] = get_lang('Teacher');
  238. $status[STUDENT] = get_lang('Learner');
  239. $status[DRH] = get_lang('Drh');
  240. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  241. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  242. $status[INVITEE] = get_lang('Invitee');
  243. $form->addElement(
  244. 'select',
  245. 'status',
  246. get_lang('Profile'),
  247. $status,
  248. [
  249. 'id' => 'status_select',
  250. 'onchange' => 'javascript: display_drh_list();',
  251. ]
  252. );
  253. //drh list (display only if student)
  254. $display = isset($_POST['status']) && $_POST['status'] == STUDENT || !isset($_POST['status']) ? 'block' : 'none';
  255. //@todo remove the drh list here. This code is unused
  256. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  257. if (isset($drh_list) && is_array($drh_list)) {
  258. foreach ($drh_list as $drh) {
  259. $drh_select->addOption(
  260. api_get_person_name($drh['firstname'], $drh['lastname']),
  261. $drh['user_id']
  262. );
  263. }
  264. }
  265. $form->addElement('html', '</div>');
  266. if (api_is_platform_admin()) {
  267. // Platform admin
  268. $group = [];
  269. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('Yes'), 1);
  270. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('No'), 0);
  271. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.';">');
  272. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'));
  273. $form->addElement('html', '</div>');
  274. }
  275. $form->addSelectLanguage('language', get_lang('Language'), null);
  276. // Send email
  277. $group = [];
  278. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  279. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  280. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'));
  281. // Expiration Date
  282. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  283. $group = [];
  284. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  285. $group[] = $form->createElement(
  286. 'DateTimePicker',
  287. 'expiration_date',
  288. null,
  289. [
  290. 'onchange' => 'javascript: enable_expiration_date();',
  291. ]
  292. );
  293. $form->addGroup($group, 'max_member_group', null, null, false);
  294. // Active account or inactive account
  295. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  296. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  297. $extraField = new ExtraField('user');
  298. $returnParams = $extraField->addElements(
  299. $form,
  300. null,
  301. [],
  302. false,
  303. false,
  304. [],
  305. [],
  306. true
  307. );
  308. $jquery_ready_content = $returnParams['jquery_ready_content'];
  309. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  310. $htmlHeadXtra[] = '<script>
  311. $(document).ready(function(){
  312. '.$jquery_ready_content.'
  313. });
  314. </script>';
  315. // Set default values
  316. $defaults['admin']['platform_admin'] = 0;
  317. $defaults['mail']['send_mail'] = 1;
  318. $defaults['password']['password_auto'] = 1;
  319. $defaults['active'] = 1;
  320. $days = api_get_setting('account_valid_duration');
  321. $defaults['expiration_date'] = api_get_local_time('+'.$days.' day');
  322. $defaults['extra_mail_notify_invitation'] = 1;
  323. $defaults['extra_mail_notify_message'] = 1;
  324. $defaults['extra_mail_notify_group_message'] = 1;
  325. $defaults['radio_expiration_date'] = 0;
  326. $defaults['status'] = STUDENT;
  327. $form->setDefaults($defaults);
  328. // Submit button
  329. $html_results_enabled[] = $form->createElement('button', 'submit', get_lang('Add'), 'plus', 'primary');
  330. $html_results_enabled[] = $form->createElement('button', 'submit_plus', get_lang('Add').'+', 'plus', 'primary');
  331. $form->addGroup($html_results_enabled);
  332. // Validate form
  333. if ($form->validate()) {
  334. $check = Security::check_token('post');
  335. if ($check) {
  336. $user = $form->exportValues();
  337. $lastname = $user['lastname'];
  338. $firstname = $user['firstname'];
  339. $official_code = $user['official_code'];
  340. $email = $user['email'];
  341. $phone = $user['phone'];
  342. $username = $user['username'];
  343. $status = intval($user['status']);
  344. $language = $user['language'];
  345. $picture = $_FILES['picture'];
  346. $platform_admin = intval($user['admin']['platform_admin']);
  347. $send_mail = intval($user['mail']['send_mail']);
  348. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : 0;
  349. if (isset($extAuthSource) && count($extAuthSource) > 0 &&
  350. $user['password']['password_auto'] == '2'
  351. ) {
  352. $auth_source = $user['password']['auth_source'];
  353. $password = 'PLACEHOLDER';
  354. } else {
  355. $auth_source = PLATFORM_AUTH_SOURCE;
  356. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  357. }
  358. if ($user['radio_expiration_date'] == '1') {
  359. $expiration_date = $user['expiration_date'];
  360. } else {
  361. $expiration_date = null;
  362. }
  363. $active = intval($user['active']);
  364. if (api_get_setting('login_is_email') == 'true') {
  365. $username = $email;
  366. }
  367. $extra = [];
  368. foreach ($user as $key => $value) {
  369. if (substr($key, 0, 6) == 'extra_') { //an extra field
  370. $extra[substr($key, 6)] = $value;
  371. }
  372. }
  373. $user_id = UserManager::create_user(
  374. $firstname,
  375. $lastname,
  376. $status,
  377. $email,
  378. $username,
  379. $password,
  380. $official_code,
  381. $language,
  382. $phone,
  383. null,
  384. $auth_source,
  385. $expiration_date,
  386. $active,
  387. $hr_dept_id,
  388. $extra,
  389. null,
  390. $send_mail,
  391. $platform_admin
  392. );
  393. Security::clear_token();
  394. $tok = Security::get_token();
  395. if (!empty($user_id)) {
  396. if (!empty($picture['name'])) {
  397. $picture_uri = UserManager::update_user_picture(
  398. $user_id,
  399. $_FILES['picture']['name'],
  400. $_FILES['picture']['tmp_name'],
  401. $user['picture_crop_result']
  402. );
  403. UserManager::update_user(
  404. $user_id,
  405. $firstname,
  406. $lastname,
  407. $username,
  408. $password,
  409. $auth_source,
  410. $email,
  411. $status,
  412. $official_code,
  413. $phone,
  414. $picture_uri,
  415. $expiration_date,
  416. $active,
  417. null,
  418. $hr_dept_id,
  419. null,
  420. $language
  421. );
  422. }
  423. $extraFieldValues = new ExtraFieldValue('user');
  424. $user['item_id'] = $user_id;
  425. $extraFieldValues->saveFieldValues($user);
  426. $message = get_lang('UserAdded').': '.
  427. Display::url(
  428. api_get_person_name($firstname, $lastname),
  429. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  430. );
  431. }
  432. Display::addFlash(Display::return_message($message, 'normal', false));
  433. if (isset($_POST['submit_plus'])) {
  434. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  435. header('Location: user_add.php?sec_token='.$tok);
  436. exit;
  437. } else {
  438. $tok = Security::get_token();
  439. header('Location: user_list.php?sec_token='.$tok);
  440. exit;
  441. }
  442. }
  443. } else {
  444. if (isset($_POST['submit'])) {
  445. Security::clear_token();
  446. }
  447. $token = Security::get_token();
  448. $form->addElement('hidden', 'sec_token');
  449. $form->setConstants(['sec_token' => $token]);
  450. }
  451. if (!empty($message)) {
  452. $message = Display::return_message(stripslashes($message));
  453. }
  454. $content = $form->returnForm();
  455. $tpl = new Template($tool_name);
  456. $tpl->assign('message', $message);
  457. $tpl->assign('content', $content);
  458. $tpl->display_one_col_template();