user_add.php 15 KB

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