user_add.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // Language files that should be included
  7. $language_file = array('admin', 'registration');
  8. $cidReset = true;
  9. // Including necessary libraries.
  10. require_once '../inc/global.inc.php';
  11. // Section for the tabs
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. // User permissions
  14. api_protect_admin_script(true);
  15. $is_platform_admin = api_is_platform_admin() ? 1 : 0;
  16. $message = null;
  17. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#password');
  18. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  19. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  20. if (isset($_configuration['allow_strength_pass_checker']) && $_configuration['allow_strength_pass_checker']) {
  21. $htmlHeadXtra[] = '
  22. <script>
  23. $(document).ready(function() {
  24. $("input[name=\'password[password_auto]\']").each(function(index, value) {
  25. $(this).click(function() {
  26. var value = $(this).attr("value");
  27. if (value == 0) {
  28. $("#password_progress").show();
  29. $(".password-verdict").show();
  30. $(".error-list").show();
  31. } else {
  32. $("#password_progress").hide();
  33. $(".password-verdict").hide();
  34. $(".error-list").hide();
  35. }
  36. });
  37. });
  38. });
  39. </script>';
  40. }
  41. $htmlHeadXtra[] = '
  42. <script>
  43. function enable_expiration_date() { //v2.0
  44. document.user_add.radio_expiration_date[0].checked=false;
  45. document.user_add.radio_expiration_date[1].checked=true;
  46. }
  47. function password_switch_radio_button() {
  48. var input_elements = document.getElementsByTagName("input");
  49. for (var i = 0; i < input_elements.length; i++) {
  50. if (input_elements.item(i).name == "password[password_auto]" && input_elements.item(i).value == "0") {
  51. input_elements.item(i).checked = true;
  52. }
  53. }
  54. }
  55. var is_platform_id = "'.$is_platform_admin.'";
  56. function display_drh_list(){
  57. if(document.getElementById("status_select").value=='.STUDENT.') {
  58. document.getElementById("drh_list").style.display="block";
  59. if (is_platform_id == 1)
  60. document.getElementById("id_platform_admin").style.display="none";
  61. } else if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  62. document.getElementById("drh_list").style.display="none";
  63. if (is_platform_id == 1)
  64. document.getElementById("id_platform_admin").style.display="block";
  65. } else {
  66. document.getElementById("drh_list").style.display="none";
  67. if (is_platform_id == 1)
  68. document.getElementById("id_platform_admin").style.display="none";
  69. }
  70. }
  71. </script>';
  72. if (!empty($_GET['message'])) {
  73. $message = urldecode($_GET['message']);
  74. }
  75. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  76. $interbreadcrumb[] = array("url" => 'user_list.php', "name" => get_lang('UserList'));
  77. $tool_name = get_lang('AddUsers');
  78. // Create the form
  79. $form = new FormValidator('user_add');
  80. $form->addElement('header', '', $tool_name);
  81. if (api_is_western_name_order()) {
  82. // Firstname
  83. $form->addElement('text', 'firstname', get_lang('FirstName'));
  84. $form->applyFilter('firstname', 'html_filter');
  85. $form->applyFilter('firstname', 'trim');
  86. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  87. // Lastname
  88. $form->addElement('text', 'lastname', get_lang('LastName'));
  89. $form->applyFilter('lastname', 'html_filter');
  90. $form->applyFilter('lastname', 'trim');
  91. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  92. } else {
  93. // Lastname
  94. $form->addElement('text', 'lastname', get_lang('LastName'));
  95. $form->applyFilter('lastname', 'html_filter');
  96. $form->applyFilter('lastname', 'trim');
  97. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  98. // Firstname
  99. $form->addElement('text', 'firstname', get_lang('FirstName'));
  100. $form->applyFilter('firstname', 'html_filter');
  101. $form->applyFilter('firstname', 'trim');
  102. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  103. }
  104. // Official code
  105. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  106. $form->applyFilter('official_code', 'html_filter');
  107. $form->applyFilter('official_code', 'trim');
  108. // Email
  109. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  110. $form->addRule('email', get_lang('EmailWrong'), 'email');
  111. if (api_get_setting('registration', 'email') == 'true') {
  112. $form->addRule('email', get_lang('EmailWrong'), 'required');
  113. }
  114. if (api_get_setting('login_is_email') == 'true') {
  115. $form->addRule(
  116. 'email',
  117. sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH),
  118. 'maxlength',
  119. USERNAME_MAX_LENGTH
  120. );
  121. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  122. }
  123. // Phone
  124. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  125. // Picture
  126. $form->addElement('file', 'picture', get_lang('AddPicture'));
  127. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  128. $form->addRule(
  129. 'picture',
  130. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  131. 'filetype',
  132. $allowed_picture_types
  133. );
  134. // Username
  135. if (api_get_setting('login_is_email') != 'true') {
  136. $form->addElement('text', 'username', get_lang('LoginName'), array('id'=> 'username', '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');
  146. }
  147. // Password
  148. $group = array();
  149. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  150. $nb_ext_auth_source_added = 0;
  151. if (isset($extAuthSource) && count($extAuthSource) > 0) {
  152. $auth_sources = array();
  153. foreach ($extAuthSource as $key => $info) {
  154. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  155. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  156. // extAuthSource always on for CAS even if not activated
  157. // same action for file user_edit.php
  158. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  159. $auth_sources[$key] = $key;
  160. $nb_ext_auth_source_added++;
  161. }
  162. }
  163. if ($nb_ext_auth_source_added > 0) {
  164. $group[] = $form->createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  165. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  166. $group[] = $form->createElement('static', '', '', '<br />');
  167. }
  168. }
  169. $group[] = $form->createElement(
  170. 'radio',
  171. 'password_auto',
  172. get_lang('Password'),
  173. get_lang('AutoGeneratePassword').'<br />',
  174. 1
  175. );
  176. $group[] = $form->createElement('radio', 'password_auto', 'id="radio_user_password"', null, 0);
  177. $group[] = $form->createElement('password', 'password', null, array('id'=> 'password', 'onkeydown' => 'javascript: password_switch_radio_button();'));
  178. $form->addGroup($group, 'password', get_lang('Password'), '');
  179. if (isset($_configuration['allow_strength_pass_checker']) && $_configuration['allow_strength_pass_checker']) {
  180. $form->addElement('label', null, '<div id="password_progress" style="display:none"></div>');
  181. }
  182. // Status
  183. $status = api_get_user_roles();
  184. $form->addElement(
  185. 'select',
  186. 'status',
  187. get_lang('Profile'),
  188. $status,
  189. array('id' => 'status_select', 'class' => 'chzn-select', 'onchange' => 'javascript: display_drh_list();')
  190. );
  191. //drh list (display only if student)
  192. $display = isset($_POST['status']) && $_POST['status'] == STUDENT || !isset($_POST['status']) ? 'block' : 'none';
  193. //@todo remove the drh list here. This code is unused
  194. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  195. if (isset($drh_list) && is_array($drh_list)) {
  196. foreach ($drh_list as $drh) {
  197. $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']);
  198. }
  199. }
  200. $form->addElement('html', '</div>');
  201. /*
  202. if (api_is_platform_admin()) {
  203. // Platform admin
  204. $group = array();
  205. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('Yes'), 1);
  206. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('No'), 0);
  207. //$display = ($_POST['status'] == STUDENT || !isset($_POST['status'])) ? 'none' : 'block';
  208. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.';">');
  209. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  210. $form->addElement('html', '</div>');
  211. }*/
  212. $form->addElement('select_language', 'language', get_lang('Language'), null);
  213. // Send email
  214. $group = array();
  215. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  216. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  217. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  218. // Expiration Date
  219. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  220. $group = array();
  221. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  222. $group[] = $form->createElement(
  223. 'datepicker',
  224. 'expiration_date',
  225. null,
  226. array('form_name' => $form->getAttribute('name'), 'onchange' => 'javascript: enable_expiration_date();')
  227. );
  228. $form->addGroup($group, 'max_member_group', null, '', false);
  229. // Active account or inactive account
  230. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  231. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  232. $extra_data = UserManager::get_extra_user_data(0, true);
  233. $extraField = new ExtraField('user');
  234. $return_params = $extraField->set_extra_fields_in_form($form, $extra_data, 'user_add', false, null);
  235. $jquery_ready_content = $return_params['jquery_ready_content'];
  236. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  237. $htmlHeadXtra[] = '<script>
  238. $(document).ready(function(){
  239. '.$jquery_ready_content.'
  240. });
  241. </script>';
  242. // Set default values
  243. $defaults['admin']['platform_admin'] = 0;
  244. $defaults['mail']['send_mail'] = 1;
  245. $defaults['password']['password_auto'] = 1;
  246. $defaults['active'] = 1;
  247. $defaults['expiration_date'] = array();
  248. $days = api_get_setting('account_valid_duration');
  249. $time = strtotime('+'.$days.' day');
  250. $defaults['expiration_date']['d'] = date('d', $time);
  251. $defaults['expiration_date']['F'] = date('m', $time);
  252. $defaults['expiration_date']['Y'] = date('Y', $time);
  253. $defaults['radio_expiration_date'] = 0;
  254. $defaults['status'] = STUDENT;
  255. $defaults = array_merge($defaults, $extra_data);
  256. $form->setDefaults($defaults);
  257. // Submit button
  258. $html_results_enabled[] = $form->createElement(
  259. 'style_submit_button',
  260. 'submit_plus',
  261. get_lang('Add').'+',
  262. 'class="add"'
  263. );
  264. $html_results_enabled[] = $form->createElement('style_submit_button', 'submit', get_lang('Add'), 'class="add"');
  265. $form->addGroup($html_results_enabled);
  266. // Validate form
  267. if ($form->validate()) {
  268. $check = Security::check_token('post');
  269. if ($check) {
  270. $user = $form->exportValues();
  271. $lastname = $user['lastname'];
  272. $firstname = $user['firstname'];
  273. $official_code = $user['official_code'];
  274. $email = $user['email'];
  275. $phone = $user['phone'];
  276. $username = $user['username'];
  277. $status = intval($user['status']);
  278. $language = $user['language'];
  279. $picture = $_FILES['picture'];
  280. $platform_admin = intval($user['admin']['platform_admin']);
  281. $send_mail = intval($user['mail']['send_mail']);
  282. $hr_dept_id = intval($user['hr_dept_id']);
  283. if (count($extAuthSource) > 0 && $user['password']['password_auto'] == '2') {
  284. $auth_source = $user['password']['auth_source'];
  285. $password = 'PLACEHOLDER';
  286. } else {
  287. $auth_source = PLATFORM_AUTH_SOURCE;
  288. $password = $user['password']['password_auto'] == '1' ? api_generate_password(
  289. ) : $user['password']['password'];
  290. }
  291. if ($user['radio_expiration_date'] == '1') {
  292. $expiration_date = $user['expiration_date'];
  293. } else {
  294. $expiration_date = '0000-00-00 00:00:00';
  295. }
  296. $active = intval($user['active']);
  297. if (api_get_setting('login_is_email') == 'true') {
  298. $username = $email;
  299. }
  300. $user_id = UserManager::create_user(
  301. $firstname,
  302. $lastname,
  303. $status,
  304. $email,
  305. $username,
  306. $password,
  307. $official_code,
  308. $language,
  309. $phone,
  310. null,
  311. $auth_source,
  312. $expiration_date,
  313. $active,
  314. $hr_dept_id,
  315. null,
  316. null,
  317. $send_mail
  318. );
  319. Security::clear_token();
  320. $tok = Security::get_token();
  321. if ($user_id === false) {
  322. //If any error ocurred during user creation, print it (api_failureList
  323. // stores values as separate words, so rework it
  324. $message = '';
  325. $message_bits = explode(' ', api_get_last_failure());
  326. foreach ($message_bits as $bit) {
  327. $message .= ucfirst($bit);
  328. }
  329. } else {
  330. if (!empty($picture['name'])) {
  331. $picture_uri = UserManager::update_user_picture(
  332. $user_id,
  333. $_FILES['picture']['name'],
  334. $_FILES['picture']['tmp_name']
  335. );
  336. UserManager::update_user(
  337. $user_id,
  338. $firstname,
  339. $lastname,
  340. $username,
  341. $password,
  342. $auth_source,
  343. $email,
  344. $status,
  345. $official_code,
  346. $phone,
  347. $picture_uri,
  348. $expiration_date,
  349. $active,
  350. null,
  351. $hr_dept_id,
  352. null,
  353. $language
  354. );
  355. }
  356. foreach ($user as $key => $value) {
  357. if (substr($key, 0, 6) == 'extra_') { //an extra field
  358. UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
  359. }
  360. }
  361. $message = get_lang('UserAdded');
  362. }
  363. if (isset($user['submit_plus'])) {
  364. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  365. header('Location: user_add.php?message='.urlencode($message).'&sec_token='.$tok);
  366. exit ();
  367. } else {
  368. $tok = Security::get_token();
  369. header('Location: user_list.php?action=show_message&message='.urlencode($message).'&sec_token='.$tok);
  370. exit ();
  371. }
  372. }
  373. } else {
  374. if (isset($_POST['submit'])) {
  375. Security::clear_token();
  376. }
  377. $token = Security::get_token();
  378. $form->addElement('hidden', 'sec_token');
  379. $form->setConstants(array('sec_token' => $token));
  380. }
  381. if (!empty($message)) {
  382. $message = Display::return_message(stripslashes($message));
  383. }
  384. $content = $form->return_form();
  385. $app['title'] = $tool_name;
  386. $tpl = $app['template'];
  387. $tpl->assign('message', $message);
  388. $tpl->assign('content', $content);
  389. $tpl->display_one_col_template();