user_add.php 14 KB

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