user_add.php 15 KB

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