user_add.php 14 KB

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