user_add.php 13 KB

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