user_add.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. $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[] = '<link href="'. api_get_path(WEB_PATH) .'web/assets/cropper/dist/cropper.min.css" rel="stylesheet">';
  41. $htmlHeadXtra[] = '<script src="'. api_get_path(WEB_PATH) .'web/assets/cropper/dist/cropper.min.js"></script>';
  42. $htmlHeadXtra[] = '<script>
  43. $(document).ready(function() {
  44. var $image = $("#previewImage");
  45. var $input = $("[name=\'cropResult\']");
  46. var $cropButton = $("#cropButton");
  47. var canvas = "";
  48. var imageWidth = "";
  49. var imageHeight = "";
  50. $("input:file").change(function() {
  51. var oFReader = new FileReader();
  52. oFReader.readAsDataURL(document.getElementById("picture").files[0]);
  53. oFReader.onload = function (oFREvent) {
  54. $image.attr("src", this.result);
  55. $("#labelCropImage").html("'.get_lang('Preview').'");
  56. $("#cropImage").addClass("thumbnail");
  57. $cropButton.removeClass("hidden");
  58. // Destroy cropper
  59. $image.cropper("destroy");
  60. $image.cropper({
  61. aspectRatio: 1 / 1,
  62. responsive : true,
  63. center : false,
  64. guides : false,
  65. movable: false,
  66. zoomable: false,
  67. rotatable: false,
  68. scalable: false,
  69. crop: function(e) {
  70. // Output the result data for cropping image.
  71. $input.val(e.x+","+e.y+","+e.width+","+e.height);
  72. }
  73. });
  74. };
  75. });
  76. $("#cropButton").on("click", function() {
  77. var canvas = $image.cropper("getCroppedCanvas");
  78. var dataUrl = canvas.toDataURL();
  79. $image.attr("src", dataUrl);
  80. $image.cropper("destroy");
  81. $cropButton.addClass("hidden");
  82. return false;
  83. });
  84. });
  85. </script>';
  86. $htmlHeadXtra[] = '
  87. <script>
  88. $("#status_select").ready(function() {
  89. if ($(this).attr("value") != '.STUDENT.') {
  90. $("#id_platform_admin").hide();
  91. }
  92. });
  93. function enable_expiration_date() { //v2.0
  94. document.user_add.radio_expiration_date[0].checked=false;
  95. document.user_add.radio_expiration_date[1].checked=true;
  96. }
  97. function password_switch_radio_button() {
  98. var input_elements = document.getElementsByTagName("input");
  99. for (var i = 0; i < input_elements.length; i++) {
  100. if (input_elements.item(i).name == "password[password_auto]" && input_elements.item(i).value == "0") {
  101. input_elements.item(i).checked = true;
  102. }
  103. }
  104. }
  105. var is_platform_id = "'.$is_platform_admin.'";
  106. function display_drh_list(){
  107. if (document.getElementById("status_select").value=='.STUDENT.') {
  108. document.getElementById("drh_list").style.display="block";
  109. if (is_platform_id == 1)
  110. document.getElementById("id_platform_admin").style.display="none";
  111. } else if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
  112. document.getElementById("drh_list").style.display="none";
  113. if (is_platform_id == 1)
  114. document.getElementById("id_platform_admin").style.display="block";
  115. } else {
  116. document.getElementById("drh_list").style.display="none";
  117. if (is_platform_id == 1)
  118. document.getElementById("id_platform_admin").style.display="none";
  119. }
  120. }
  121. </script>';
  122. if (!empty($_GET['message'])) {
  123. $message = urldecode($_GET['message']);
  124. }
  125. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  126. $interbreadcrumb[] = array("url" => 'user_list.php', "name" => get_lang('UserList'));
  127. $tool_name = get_lang('AddUsers');
  128. // Create the form
  129. $form = new FormValidator('user_add');
  130. $form->addElement('header', '', $tool_name);
  131. if (api_is_western_name_order()) {
  132. // Firstname
  133. $form->addElement('text', 'firstname', get_lang('FirstName'));
  134. $form->applyFilter('firstname', 'html_filter');
  135. $form->applyFilter('firstname', 'trim');
  136. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  137. // Lastname
  138. $form->addElement('text', 'lastname', get_lang('LastName'));
  139. $form->applyFilter('lastname', 'html_filter');
  140. $form->applyFilter('lastname', 'trim');
  141. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  142. } else {
  143. // Lastname
  144. $form->addElement('text', 'lastname', get_lang('LastName'));
  145. $form->applyFilter('lastname', 'html_filter');
  146. $form->applyFilter('lastname', 'trim');
  147. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  148. // Firstname
  149. $form->addElement('text', 'firstname', get_lang('FirstName'));
  150. $form->applyFilter('firstname', 'html_filter');
  151. $form->applyFilter('firstname', 'trim');
  152. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  153. }
  154. // Official code
  155. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  156. $form->applyFilter('official_code', 'html_filter');
  157. $form->applyFilter('official_code', 'trim');
  158. // Email
  159. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  160. $form->addRule('email', get_lang('EmailWrong'), 'email');
  161. if (api_get_setting('registration', 'email') == 'true') {
  162. $form->addRule('email', get_lang('EmailWrong'), 'required');
  163. }
  164. if (api_get_setting('login_is_email') == 'true') {
  165. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  166. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  167. }
  168. // Phone
  169. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  170. // Picture
  171. $form->addElement('file', 'picture', get_lang('AddImage'), array('id' => 'picture', 'class' => 'picture-form'));
  172. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  173. $form->addHtml(''
  174. . '<div class="form-group">'
  175. . '<label for="cropImage" id="labelCropImage" class="col-sm-2 control-label"></label>'
  176. . '<div class="col-sm-8">'
  177. . '<div id="cropImage" class="cropCanvas">'
  178. . '<img id="previewImage" >'
  179. . '</div>'
  180. . '<div>'
  181. . '<button class="btn btn-primary hidden" name="cropButton" id="cropButton" type="submit"><em class="fa fa-crop"></em> '.get_lang('CropYourPicture').'</button>'
  182. . '</div>'
  183. . '</div>'
  184. . '</div>'
  185. . '');
  186. $form->addHidden('cropResult', '');
  187. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  188. // Username
  189. if (api_get_setting('login_is_email') != 'true') {
  190. $form->addElement('text', 'username', get_lang('LoginName'), array('id'=> 'username', 'maxlength' => USERNAME_MAX_LENGTH, 'autocomplete' => 'off'));
  191. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  192. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  193. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  194. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  195. }
  196. // Password
  197. $group = array();
  198. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  199. $nb_ext_auth_source_added = 0;
  200. if (isset($extAuthSource) && count($extAuthSource) > 0) {
  201. $auth_sources = array();
  202. foreach ($extAuthSource as $key => $info) {
  203. // @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
  204. // Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
  205. // extAuthSource always on for CAS even if not activated
  206. // same action for file user_edit.php
  207. if (($key == CAS_AUTH_SOURCE && api_get_setting('cas_activate') === 'true') || ($key != CAS_AUTH_SOURCE)) {
  208. $auth_sources[$key] = $key;
  209. $nb_ext_auth_source_added++;
  210. }
  211. }
  212. if ($nb_ext_auth_source_added > 0) {
  213. $group[] = $form->createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  214. $group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
  215. $group[] = $form->createElement('static', '', '', '<br />');
  216. }
  217. }
  218. $group[] = $form->createElement(
  219. 'radio',
  220. 'password_auto',
  221. get_lang('Password'),
  222. get_lang('AutoGeneratePassword').'<br />',
  223. 1
  224. );
  225. $group[] = $form->createElement(
  226. 'radio',
  227. 'password_auto',
  228. 'id="radio_user_password"',
  229. get_lang('EnterPassword'),
  230. 0
  231. );
  232. $group[] = $form->createElement(
  233. 'password',
  234. 'password',
  235. null,
  236. array(
  237. 'id' => 'password',
  238. 'autocomplete' => 'off',
  239. 'onkeydown' => 'javascript: password_switch_radio_button();',
  240. //'required' => 'required'
  241. )
  242. );
  243. $form->addGroup($group, 'password', get_lang('Password'), '');
  244. $form->addGroupRule('password', get_lang('EnterPassword'), 'required', null, 1);
  245. if ($checkPass) {
  246. $passwordStrengthLabels = '
  247. <div id="password-verdict"></div>
  248. <div id="password-errors"></div>
  249. <div id="password_progress" style="display:none"></div>
  250. ';
  251. $form->addElement('label', null, $passwordStrengthLabels);
  252. }
  253. // Status
  254. $status = array();
  255. $status[COURSEMANAGER] = get_lang('Teacher');
  256. $status[STUDENT] = get_lang('Learner');
  257. $status[DRH] = get_lang('Drh');
  258. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  259. $status[STUDENT_BOSS] = get_lang('RoleStudentBoss');
  260. $status[INVITEE] = get_lang('Invitee');
  261. $form->addElement(
  262. 'select',
  263. 'status',
  264. get_lang('Profile'),
  265. $status,
  266. array(
  267. 'id' => 'status_select',
  268. 'onchange' => 'javascript: display_drh_list();'
  269. )
  270. );
  271. //drh list (display only if student)
  272. $display = isset($_POST['status']) && $_POST['status'] == STUDENT || !isset($_POST['status']) ? 'block' : 'none';
  273. //@todo remove the drh list here. This code is unused
  274. $form->addElement('html', '<div id="drh_list" style="display:'.$display.';">');
  275. if (isset($drh_list) && is_array($drh_list)) {
  276. foreach ($drh_list as $drh) {
  277. $drh_select->addOption(
  278. api_get_person_name($drh['firstname'], $drh['lastname']),
  279. $drh['user_id']
  280. );
  281. }
  282. }
  283. $form->addElement('html', '</div>');
  284. if (api_is_platform_admin()) {
  285. // Platform admin
  286. $group = array();
  287. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('Yes'), 1);
  288. $group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('No'), 0);
  289. $form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.';">');
  290. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  291. $form->addElement('html', '</div>');
  292. }
  293. $form->addElement('select_language', 'language', get_lang('Language'), null);
  294. // Send email
  295. $group = array();
  296. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  297. $group[] = $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  298. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  299. // Expiration Date
  300. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  301. $group = array ();
  302. $group[] = $form->createElement('radio', 'radio_expiration_date', null, get_lang('Enabled'), 1);
  303. $group[] = $form->createElement(
  304. 'DateTimePicker',
  305. 'expiration_date',
  306. null,
  307. array(
  308. 'onchange' => 'javascript: enable_expiration_date();'
  309. )
  310. );
  311. $form->addGroup($group, 'max_member_group', null, '', false);
  312. // Active account or inactive account
  313. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  314. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  315. $extraField = new ExtraField('user');
  316. $returnParams = $extraField->addElements($form);
  317. $jquery_ready_content = $returnParams['jquery_ready_content'];
  318. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  319. $htmlHeadXtra[] ='<script>
  320. $(document).ready(function(){
  321. '.$jquery_ready_content.'
  322. });
  323. </script>';
  324. // Set default values
  325. $defaults['admin']['platform_admin'] = 0;
  326. $defaults['mail']['send_mail'] = 1;
  327. $defaults['password']['password_auto'] = 1;
  328. $defaults['active'] = 1;
  329. $days = api_get_setting('account_valid_duration');
  330. $defaults['expiration_date'] = api_get_local_time('+'.$days.' day');
  331. $defaults['extra_mail_notify_invitation'] = 1;
  332. $defaults['extra_mail_notify_message'] = 1;
  333. $defaults['extra_mail_notify_group_message'] = 1;
  334. $defaults['radio_expiration_date'] = 0;
  335. $defaults['status'] = STUDENT;
  336. $form->setDefaults($defaults);
  337. // Submit button
  338. $html_results_enabled[] = $form->createElement('button', 'submit', get_lang('Add'), 'plus', 'primary');
  339. $html_results_enabled[] = $form->createElement('button', 'submit_plus', get_lang('Add').'+', 'plus', 'primary');
  340. $form->addGroup($html_results_enabled);
  341. // Validate form
  342. if ($form->validate()) {
  343. $check = Security::check_token('post');
  344. if ($check) {
  345. $user = $form->exportValues();
  346. $lastname = $user['lastname'];
  347. $firstname = $user['firstname'];
  348. $official_code = $user['official_code'];
  349. $email = $user['email'];
  350. $phone = $user['phone'];
  351. $username = $user['username'];
  352. $status = intval($user['status']);
  353. $language = $user['language'];
  354. $picture = $_FILES['picture'];
  355. $platform_admin = intval($user['admin']['platform_admin']);
  356. $send_mail = intval($user['mail']['send_mail']);
  357. $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : 0;
  358. if (isset($extAuthSource) && count($extAuthSource) > 0 &&
  359. $user['password']['password_auto'] == '2'
  360. ) {
  361. $auth_source = $user['password']['auth_source'];
  362. $password = 'PLACEHOLDER';
  363. } else {
  364. $auth_source = PLATFORM_AUTH_SOURCE;
  365. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  366. }
  367. if ($user['radio_expiration_date'] == '1') {
  368. $expiration_date = $user['expiration_date'];
  369. } else {
  370. $expiration_date = null;
  371. }
  372. $active = intval($user['active']);
  373. if (api_get_setting('login_is_email') == 'true') {
  374. $username = $email;
  375. }
  376. $extra = array();
  377. foreach ($user as $key => $value) {
  378. if (substr($key, 0, 6) == 'extra_') { //an extra field
  379. $extra[substr($key, 6)] = $value;
  380. }
  381. }
  382. $user_id = UserManager::create_user(
  383. $firstname,
  384. $lastname,
  385. $status,
  386. $email,
  387. $username,
  388. $password,
  389. $official_code,
  390. $language,
  391. $phone,
  392. null,
  393. $auth_source,
  394. $expiration_date,
  395. $active,
  396. $hr_dept_id,
  397. $extra,
  398. null,
  399. $send_mail,
  400. $platform_admin
  401. );
  402. Security::clear_token();
  403. $tok = Security::get_token();
  404. if ($user_id === false) {
  405. //If any error ocurred during user creation, print it (api_failureList
  406. // stores values as separate words, so rework it
  407. $message = '';
  408. $message_bits = explode(' ',api_get_last_failure());
  409. foreach ($message_bits as $bit) {
  410. $message .= ucfirst($bit);
  411. }
  412. } else {
  413. if (!empty($picture['name'])) {
  414. $picture_uri = UserManager::update_user_picture(
  415. $user_id,
  416. $_FILES['picture']['name'],
  417. $_FILES['picture']['tmp_name'],
  418. $user['cropResult']
  419. );
  420. UserManager::update_user(
  421. $user_id,
  422. $firstname,
  423. $lastname,
  424. $username,
  425. $password,
  426. $auth_source,
  427. $email,
  428. $status,
  429. $official_code,
  430. $phone,
  431. $picture_uri,
  432. $expiration_date,
  433. $active,
  434. null,
  435. $hr_dept_id,
  436. null,
  437. $language
  438. );
  439. }
  440. $extraFieldValues = new ExtraFieldValue('user');
  441. $user['item_id'] = $user_id;
  442. $extraFieldValues->saveFieldValues($user);
  443. $message = get_lang('UserAdded');
  444. }
  445. if (isset($user['submit_plus'])) {
  446. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  447. header('Location: user_add.php?message='.urlencode($message).'&sec_token='.$tok);
  448. exit ();
  449. } else {
  450. $tok = Security::get_token();
  451. header('Location: user_list.php?action=show_message&message='.urlencode($message).'&sec_token='.$tok);
  452. exit ();
  453. }
  454. }
  455. } else {
  456. if (isset($_POST['submit'])) {
  457. Security::clear_token();
  458. }
  459. $token = Security::get_token();
  460. $form->addElement('hidden', 'sec_token');
  461. $form->setConstants(array('sec_token' => $token));
  462. }
  463. if (!empty($message)){
  464. $message = Display::return_message(stripslashes($message));
  465. }
  466. $content = $form->returnForm();
  467. $tpl = new Template($tool_name);
  468. //$tpl->assign('actions', $actions);
  469. $tpl->assign('message', $message);
  470. $tpl->assign('content', $content);
  471. $tpl->display_one_col_template();