user_add.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. $libpath = api_get_path(LIBRARY_PATH);
  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('security.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('components/cropper/dist/cropper.min.css');
  40. $htmlHeadXtra[] = api_get_js('components/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. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin'));
  78. $interbreadcrumb[] = array("url" => 'user_list.php', "name" => get_lang('UserList'));
  79. $tool_name = get_lang('AddUsers');
  80. // Create the form
  81. $form = new FormValidator('user_add');
  82. $form->addElement('header', '', $tool_name);
  83. if (api_is_western_name_order()) {
  84. // Firstname
  85. $form->addElement('text', 'firstname', get_lang('FirstName'));
  86. $form->applyFilter('firstname', 'html_filter');
  87. $form->applyFilter('firstname', 'trim');
  88. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  89. // Lastname
  90. $form->addElement('text', 'lastname', get_lang('LastName'));
  91. $form->applyFilter('lastname', 'html_filter');
  92. $form->applyFilter('lastname', 'trim');
  93. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  94. } else {
  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. // Firstname
  101. $form->addElement('text', 'firstname', get_lang('FirstName'));
  102. $form->applyFilter('firstname', 'html_filter');
  103. $form->applyFilter('firstname', 'trim');
  104. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  105. }
  106. // Official code
  107. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  108. $form->applyFilter('official_code', 'html_filter');
  109. $form->applyFilter('official_code', 'trim');
  110. // Email
  111. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  112. $form->addRule('email', get_lang('EmailWrong'), 'email');
  113. if (api_get_setting_in_list('registration.required_profile_fields', 'email')) {
  114. $form->addRule('email', get_lang('EmailWrong'), 'required');
  115. }
  116. if (api_get_setting('profile.login_is_email') == 'true') {
  117. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  118. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  119. }
  120. // Phone
  121. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  122. // Picture
  123. $form->addFile(
  124. 'picture',
  125. get_lang('AddImage'),
  126. array('id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '1 / 1')
  127. );
  128. $allowed_picture_types = api_get_supported_image_extensions(false);
  129. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  130. // Username
  131. if (api_get_setting('profile.login_is_email') != 'true') {
  132. $form->addElement('text', 'username', get_lang('LoginName'), array('id'=> 'username', 'maxlength' => USERNAME_MAX_LENGTH, 'autocomplete' => 'off'));
  133. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  134. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  135. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  136. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  137. }
  138. // Password
  139. $group = array();
  140. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  141. $nb_ext_auth_source_added = 0;
  142. if (isset($extAuthSource) && count($extAuthSource) > 0) {
  143. $auth_sources = array();
  144. foreach ($extAuthSource as $key => $info) {
  145. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  146. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  147. // extAuthSource always on for CAS even if not activated
  148. // same action for file user_edit.php
  149. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  150. $auth_sources[$key] = $key;
  151. $nb_ext_auth_source_added++;
  152. }
  153. }
  154. if ($nb_ext_auth_source_added > 0) {
  155. $group[] = $form->createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  156. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  157. $group[] = $form->createElement('static', '', '', '<br />');
  158. }
  159. }
  160. $group[] = $form->createElement(
  161. 'radio',
  162. 'password_auto',
  163. get_lang('Password'),
  164. get_lang('AutoGeneratePassword').'<br />',
  165. 1
  166. );
  167. $group[] = $form->createElement(
  168. 'radio',
  169. 'password_auto',
  170. 'id="radio_user_password"',
  171. get_lang('EnterPassword'),
  172. 0
  173. );
  174. $group[] = $form->createElement(
  175. 'password',
  176. 'password',
  177. null,
  178. array(
  179. 'id' => 'password',
  180. 'autocomplete' => 'off',
  181. 'onkeydown' => 'javascript: password_switch_radio_button();',
  182. //'required' => 'required'
  183. )
  184. );
  185. $form->addGroup($group, 'password', get_lang('Password'));
  186. $form->addGroupRule('password', get_lang('EnterPassword'), 'required', null, 1);
  187. if ($checkPass) {
  188. $passwordStrengthLabels = '
  189. <div id="password-verdict"></div>
  190. <div id="password-errors"></div>
  191. <div id="password_progress" style="display:none"></div>
  192. ';
  193. $form->addElement('label', null, $passwordStrengthLabels);
  194. }
  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. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  202. $status[INVITEE] = get_lang('Invitee');
  203. $form->addElement(
  204. 'select',
  205. 'status',
  206. get_lang('Profile'),
  207. $status,
  208. array(
  209. 'id' => 'status_select',
  210. 'onchange' => 'javascript: display_drh_list();'
  211. )
  212. );
  213. //drh list (display only if student)
  214. $display = isset($_POST['status']) && $_POST['status'] == STUDENT || !isset($_POST['status']) ? 'block' : 'none';
  215. //@todo remove the drh list here. This code is unused
  216. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  217. if (isset($drh_list) && is_array($drh_list)) {
  218. foreach ($drh_list as $drh) {
  219. $drh_select->addOption(
  220. api_get_person_name($drh['firstname'], $drh['lastname']),
  221. $drh['user_id']
  222. );
  223. }
  224. }
  225. $form->addElement('html', '</div>');
  226. if (api_is_platform_admin()) {
  227. // Platform admin
  228. $group = array();
  229. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('Yes'), 1);
  230. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('No'), 0);
  231. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.';">');
  232. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'));
  233. $form->addElement('html', '</div>');
  234. }
  235. $form->addElement('select_language', 'language', get_lang('Language'), null);
  236. // Send email
  237. $group = array();
  238. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  239. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  240. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'));
  241. // Expiration Date
  242. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  243. $group = array ();
  244. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  245. $group[] = $form->createElement(
  246. 'DateTimePicker',
  247. 'expiration_date',
  248. null,
  249. array(
  250. 'onchange' => 'javascript: enable_expiration_date();'
  251. )
  252. );
  253. $form->addGroup($group, 'max_member_group', null, 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. $extraField = new ExtraField('user');
  258. $returnParams = $extraField->addElements($form);
  259. $jquery_ready_content = $returnParams['jquery_ready_content'];
  260. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  261. $htmlHeadXtra[] ='<script>
  262. $(document).ready(function(){
  263. '.$jquery_ready_content.'
  264. });
  265. </script>';
  266. // Set default values
  267. $defaults['admin']['platform_admin'] = 0;
  268. $defaults['mail']['send_mail'] = 1;
  269. $defaults['password']['password_auto'] = 1;
  270. $defaults['active'] = 1;
  271. $days = api_get_setting('profile.account_valid_duration');
  272. $defaults['expiration_date'] = api_get_local_time('+'.$days.' day');
  273. $defaults['extra_mail_notify_invitation'] = 1;
  274. $defaults['extra_mail_notify_message'] = 1;
  275. $defaults['extra_mail_notify_group_message'] = 1;
  276. $defaults['radio_expiration_date'] = 0;
  277. $defaults['status'] = STUDENT;
  278. $form->setDefaults($defaults);
  279. // Submit button
  280. $html_results_enabled[] = $form->createElement('button', 'submit', get_lang('Add'), 'plus', 'primary');
  281. $html_results_enabled[] = $form->createElement('button', 'submit_plus', get_lang('Add').'+', 'plus', 'primary');
  282. $form->addGroup($html_results_enabled);
  283. // Validate form
  284. if ($form->validate()) {
  285. $check = Security::check_token('post');
  286. if ($check) {
  287. $user = $form->exportValues();
  288. $lastname = $user['lastname'];
  289. $firstname = $user['firstname'];
  290. $official_code = $user['official_code'];
  291. $email = $user['email'];
  292. $phone = $user['phone'];
  293. $username = $user['username'];
  294. $status = intval($user['status']);
  295. $language = $user['language'];
  296. $picture = $_FILES['picture'];
  297. $platform_admin = intval($user['admin']['platform_admin']);
  298. $send_mail = intval($user['mail']['send_mail']);
  299. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : 0;
  300. if (isset($extAuthSource) && count($extAuthSource) > 0 &&
  301. $user['password']['password_auto'] == '2'
  302. ) {
  303. $auth_source = $user['password']['auth_source'];
  304. $password = 'PLACEHOLDER';
  305. } else {
  306. $auth_source = PLATFORM_AUTH_SOURCE;
  307. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  308. }
  309. if ($user['radio_expiration_date'] == '1') {
  310. $expiration_date = $user['expiration_date'];
  311. } else {
  312. $expiration_date = null;
  313. }
  314. $active = intval($user['active']);
  315. if (api_get_setting('profile.login_is_email') == 'true') {
  316. $username = $email;
  317. }
  318. $extra = array();
  319. foreach ($user as $key => $value) {
  320. if (substr($key, 0, 6) == 'extra_') { //an extra field
  321. $extra[substr($key, 6)] = $value;
  322. }
  323. }
  324. $user_id = UserManager::create_user(
  325. $firstname,
  326. $lastname,
  327. $status,
  328. $email,
  329. $username,
  330. $password,
  331. $official_code,
  332. $language,
  333. $phone,
  334. null,
  335. $auth_source,
  336. $expiration_date,
  337. $active,
  338. $hr_dept_id,
  339. $extra,
  340. null,
  341. $send_mail,
  342. $platform_admin
  343. );
  344. Security::clear_token();
  345. $tok = Security::get_token();
  346. if (!empty($user_id)) {
  347. if (!empty($picture['name'])) {
  348. $picture_uri = UserManager::update_user_picture(
  349. $user_id,
  350. $_FILES['picture']['name'],
  351. $_FILES['picture']['tmp_name'],
  352. $user['picture_crop_result']
  353. );
  354. UserManager::update_user(
  355. $user_id,
  356. $firstname,
  357. $lastname,
  358. $username,
  359. $password,
  360. $auth_source,
  361. $email,
  362. $status,
  363. $official_code,
  364. $phone,
  365. $picture_uri,
  366. $expiration_date,
  367. $active,
  368. null,
  369. $hr_dept_id,
  370. null,
  371. $language
  372. );
  373. }
  374. $extraFieldValues = new ExtraFieldValue('user');
  375. $user['item_id'] = $user_id;
  376. $extraFieldValues->saveFieldValues($user);
  377. $message = get_lang('UserAdded');
  378. }
  379. if (isset($user['submit_plus'])) {
  380. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  381. Display::addFlash(Display::return_message($message));
  382. header('Location: user_add.php?sec_token='.$tok);
  383. exit;
  384. } else {
  385. $tok = Security::get_token();
  386. Display::addFlash(Display::return_message($message));
  387. header('Location: user_list.php?sec_token='.$tok);
  388. exit;
  389. }
  390. }
  391. } else {
  392. if (isset($_POST['submit'])) {
  393. Security::clear_token();
  394. }
  395. $token = Security::get_token();
  396. $form->addElement('hidden', 'sec_token');
  397. $form->setConstants(array('sec_token' => $token));
  398. }
  399. $content = $form->returnForm();
  400. echo $content;