user_add.php 17 KB

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