user_add.php 14 KB

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