user_add.php 15 KB

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