user_add.php 15 KB

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