user_add.php 15 KB

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