user_add.php 14 KB

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