user_add.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php // $Id: user_add.php 15105 2008-04-25 08:38:20Z elixir_inter $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue du Corbeau, 108, B-1000 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. // name of the language file that needs to be included
  18. $language_file = array('admin', 'registration');
  19. $cidReset = true;
  20. // including necessary libraries
  21. require '../inc/global.inc.php';
  22. $libpath = api_get_path(LIBRARY_PATH);
  23. require_once $libpath.'fileManage.lib.php';
  24. require_once $libpath.'fileUpload.lib.php';
  25. require_once $libpath.'usermanager.lib.php';
  26. require_once $libpath.'formvalidator/FormValidator.class.php';
  27. require_once $libpath.'image.lib.php';
  28. require_once $libpath.'mail.lib.inc.php';
  29. // user permissions
  30. api_protect_admin_script(true);
  31. api_block_anonymous_users();
  32. // Database table definitions
  33. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  34. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  35. $database = Database::get_main_database();
  36. $htmlHeadXtra[] = '
  37. <script type="text/javascript">
  38. <!--
  39. function enable_expiration_date() { //v2.0
  40. document.user_add.radio_expiration_date[0].checked=false;
  41. document.user_add.radio_expiration_date[1].checked=true;
  42. }
  43. function password_switch_radio_button(form, input){
  44. var NodeList = document.getElementsByTagName("input");
  45. for(var i=0; i< NodeList.length; i++){
  46. if(NodeList.item(i).name=="password[password_auto]" && NodeList.item(i).value=="0"){
  47. NodeList.item(i).checked=true;
  48. }
  49. }
  50. }
  51. function display_drh_list(){
  52. if(document.getElementById("status_select").value=='.STUDENT.')
  53. {
  54. document.getElementById("drh_list").style.display="block";
  55. }
  56. else
  57. {
  58. document.getElementById("drh_list").style.display="none";
  59. document.getElementById("drh_select").options[0].selected="selected";
  60. }
  61. }
  62. //-->
  63. </script>';
  64. if (!empty($_GET['message'])) {
  65. $message = urldecode($_GET['message']);
  66. }
  67. $id_session='';
  68. if (isset($_GET["id_session"]) && $_GET["id_session"] != "") {
  69. $id_session = Security::remove_XSS($_GET["id_session"]);
  70. //$interbreadcrumb[] = array ("url" => "session.php", "name" => get_lang('Sessions'));
  71. //$interbreadcrumb[] = array ("url" => "course.php?id_session=".$_GET["id_session"]."", "name" => get_lang('Cours'));
  72. }
  73. $interbreadcrumb[] = array ('url' => '../admin/index.php', 'name' => get_lang('PlatformAdmin'));
  74. $tool_name = get_lang('AddUsers');
  75. // Create the form
  76. $form = new FormValidator('user_add');
  77. if (api_is_western_name_order()) {
  78. // Firstname
  79. $form->addElement('text','firstname', get_lang('FirstName'));
  80. $form->applyFilter('firstname', 'html_filter');
  81. $form->applyFilter('firstname', 'trim');
  82. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  83. // Lastname
  84. $form->addElement('text','lastname', get_lang('LastName'));
  85. $form->applyFilter('lastname', 'html_filter');
  86. $form->applyFilter('lastname', 'trim');
  87. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  88. } else {
  89. // Lastname
  90. $form->addElement('text','lastname', get_lang('LastName'));
  91. $form->applyFilter('lastname', 'html_filter');
  92. $form->applyFilter('lastname', 'trim');
  93. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  94. // Firstname
  95. $form->addElement('text','firstname', get_lang('FirstName'));
  96. $form->applyFilter('firstname', 'html_filter');
  97. $form->applyFilter('firstname', 'trim');
  98. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  99. }
  100. // Official code
  101. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
  102. $form->applyFilter('official_code', 'html_filter');
  103. $form->applyFilter('official_code', 'trim');
  104. // Email
  105. $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
  106. $form->addRule('email', get_lang('EmailWrong'), 'email');
  107. $form->addRule('email', get_lang('EmailWrong'), 'required');
  108. // Phone
  109. $form->addElement('text', 'phone', get_lang('PhoneNumber'));
  110. // Picture
  111. $form->addElement('file', 'picture', get_lang('AddPicture'));
  112. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  113. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  114. // Username
  115. $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
  116. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  117. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  118. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  119. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  120. // Password
  121. $group = array();
  122. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  123. if (count($extAuthSource) > 0) {
  124. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  125. $auth_sources = array();
  126. foreach ($extAuthSource as $key => $info) {
  127. $auth_sources[$key] = $key;
  128. }
  129. $group[] =& HTML_QuickForm::createElement('select', 'auth_source', null, $auth_sources);
  130. $group[] =& HTML_QuickForm::createElement('static', '', '', '<br />');
  131. }
  132. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', get_lang('Password'), get_lang('AutoGeneratePassword').'<br />', 1);
  133. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', 'id="radio_user_password"', null, 0);
  134. $group[] =& HTML_QuickForm::createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button(document.user_add,"password[password_auto]");'));
  135. $form->addGroup($group, 'password', get_lang('Password'), '');
  136. // Send email
  137. $group = array();
  138. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'), 1);
  139. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'), 0);
  140. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  141. // Expiration Date
  142. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  143. $group = array ();
  144. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  145. $group[] = & $form->createElement('datepicker', 'expiration_date', null, array('form_name' => $form->getAttribute('name'), 'onchange' => 'javascript: enable_expiration_date();'));
  146. $form->addGroup($group, 'max_member_group', null, '', false);
  147. // Active account or inactive account
  148. $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
  149. $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
  150. //session list
  151. if (api_is_session_admin()) {
  152. $where = 'WHERE session_admin_id='.intval(api_get_user_id());
  153. $where .= ' AND ( (session.date_start <= CURDATE() AND session.date_end >= CURDATE()) OR session.date_start="0000-00-00" ) ';
  154. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  155. $sql="SELECT id,name,nbr_courses,date_start,date_end FROM $tbl_session $where ORDER BY name";
  156. $result = Database::query($sql,__FILE__,__LINE__);
  157. $a_sessions = Database::store_result($result);
  158. $session_list = array();
  159. $session_list[0] = get_lang('SelectSession');
  160. if (is_array($a_sessions)) {
  161. foreach ($a_sessions as $session) {
  162. $session_list[$session['id']]=$session['name'];
  163. }
  164. }
  165. //asort($session_list);
  166. //api_asort($session_list, SORT_STRING);
  167. api_natsort($session_list);
  168. $form->addElement('select', 'session_id', get_lang('Session'), $session_list);
  169. }
  170. // EXTRA FIELDS
  171. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  172. foreach ($extra as $id => $field_details) {
  173. switch ($field_details[2]) {
  174. case USER_FIELD_TYPE_TEXT:
  175. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
  176. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  177. $form->applyFilter('extra_'.$field_details[1], 'trim');
  178. break;
  179. case USER_FIELD_TYPE_TEXTAREA:
  180. $form->add_html_editor('extra_'.$field_details[1], $field_details[3], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
  181. //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
  182. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  183. $form->applyFilter('extra_'.$field_details[1], 'trim');
  184. break;
  185. case USER_FIELD_TYPE_RADIO:
  186. $group = array();
  187. foreach ($field_details[9] as $option_id => $option_details) {
  188. $options[$option_details[1]] = $option_details[2];
  189. $group[] =& HTML_QuickForm::createElement('radio', 'extra_'.$field_details[1], $option_details[1], $option_details[2].'<br />', $option_details[1]);
  190. }
  191. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
  192. break;
  193. case USER_FIELD_TYPE_SELECT:
  194. $options = array();
  195. foreach ($field_details[9] as $option_id => $option_details) {
  196. $options[$option_details[1]] = $option_details[2];
  197. }
  198. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, '');
  199. break;
  200. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  201. $options = array();
  202. foreach ($field_details[9] as $option_id => $option_details) {
  203. $options[$option_details[1]] = $option_details[2];
  204. }
  205. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
  206. break;
  207. case USER_FIELD_TYPE_DATE:
  208. $form->addElement('datepickerdate', 'extra_'.$field_details[1], $field_details[3]);
  209. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  210. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  211. $form -> setDefaults($defaults);
  212. $form->applyFilter('theme', 'trim');
  213. break;
  214. case USER_FIELD_TYPE_DATETIME:
  215. $form->addElement('datepicker', 'extra_'.$field_details[1], $field_details[3]);
  216. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  217. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  218. $form -> setDefaults($defaults);
  219. $form->applyFilter('theme', 'trim');
  220. break;
  221. }
  222. }
  223. // Set default values
  224. $defaults['admin']['platform_admin'] = 0;
  225. $defaults['mail']['send_mail'] = 0;
  226. $defaults['password']['password_auto'] = 1;
  227. $defaults['active'] = 1;
  228. $defaults['expiration_date'] = array();
  229. $days = api_get_setting('account_valid_duration');
  230. $time = strtotime('+'.$days.' day');
  231. $defaults['expiration_date']['d'] = date('d', $time);
  232. $defaults['expiration_date']['F'] = date('m', $time);
  233. $defaults['expiration_date']['Y'] = date('Y', $time);
  234. $defaults['radio_expiration_date'] = 0;
  235. $defaults['status'] = STUDENT;
  236. $defaults['session_id'] = api_get_session_id();
  237. $form->setDefaults($defaults);
  238. // Submit button
  239. $select_level = array ();
  240. $html_results_enabled[] = FormValidator :: createElement ('style_submit_button', 'submit_plus', get_lang('Add').'+', 'class="add"');
  241. $html_results_enabled[] = FormValidator :: createElement ('style_submit_button', 'submit', get_lang('Add'), 'class="add"');
  242. $form->addGroup($html_results_enabled);
  243. // Validate form
  244. if ($form->validate()) {
  245. $check = Security::check_token('post');
  246. if ($check) {
  247. $user = $form->exportValues();
  248. $picture_element = & $form->getElement('picture');
  249. $picture = $picture_element->getValue();
  250. $picture_uri = '';
  251. if (strlen($picture['name']) > 0) {
  252. if (!is_dir(api_get_path(SYS_CODE_PATH).'upload/users/')) {
  253. if (mkdir(api_get_path(SYS_CODE_PATH).'upload/users/')) {
  254. $perm = api_get_setting('permissions_for_new_directories');
  255. $perm = octdec(!empty($perm) ? $perm : '0770');
  256. chmod(api_get_path(SYS_CODE_PATH).'upload/users/');
  257. }
  258. }
  259. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  260. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  261. move_uploaded_file($picture['tmp_name'], $picture_location);
  262. }
  263. $lastname = $user['lastname'];
  264. $firstname = $user['firstname'];
  265. $official_code = $user['official_code'];
  266. $email = $user['email'];
  267. $phone = $user['phone'];
  268. $username = $user['username'];
  269. $status = intval($user['status']);
  270. $picture = $_FILES['picture'];
  271. $platform_admin = intval($user['admin']['platform_admin']);
  272. $send_mail = intval($user['mail']['send_mail']);
  273. $hr_dept_id = intval($user['hr_dept_id']);
  274. if (count($extAuthSource) > 0 && $user['password']['password_auto'] == '2') {
  275. $auth_source = $user['password']['auth_source'];
  276. $password = 'PLACEHOLDER';
  277. } else {
  278. $auth_source = PLATFORM_AUTH_SOURCE;
  279. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  280. }
  281. if ($user['radio_expiration_date'] == '1') {
  282. $expiration_date = $user['expiration_date'];
  283. } else {
  284. $expiration_date = '0000-00-00 00:00:00';
  285. }
  286. $active = intval($user['active']);
  287. // default status = student
  288. $status = 5;
  289. //create user
  290. $user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, api_get_setting('platformLanguage'), $phone, $picture_uri, $auth_source, $expiration_date, $active, $hr_dept_id);
  291. //adding to the session
  292. if (api_is_session_admin()) {
  293. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  294. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  295. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  296. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  297. $id_session = $user['session_id'];
  298. if ($id_session != 0) {
  299. $result = Database::query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'",__FILE__,__LINE__);
  300. $CourseList=array();
  301. while ($row = Database::fetch_array($result)) {
  302. $CourseList[] = $row['course_code'];
  303. }
  304. foreach ($CourseList as $enreg_course) {
  305. Database::query("INSERT INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$user_id')",__FILE__,__LINE__);
  306. // updating the total
  307. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  308. $rs = Database::query($sql, __FILE__, __LINE__);
  309. list($nbr_users) = Database::fetch_array($rs);
  310. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  311. }
  312. Database::query("INSERT INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$user_id')", __FILE__, __LINE__);
  313. $sql = "SELECT COUNT(nbr_users) as nbUsers FROM $tbl_session WHERE id='$id_session' ";
  314. $rs = Database::query($sql, __FILE__, __LINE__);
  315. list($nbr_users) = Database::fetch_array($rs);
  316. Database::query("UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ", __FILE__, __LINE__);
  317. }
  318. }
  319. $extras = array();
  320. foreach ($user as $key => $value) {
  321. if (substr($key, 0, 6) == 'extra_') {
  322. //an extra field
  323. $myres = UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
  324. }
  325. }
  326. if ($platform_admin) {
  327. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  328. Database::query($sql, __FILE__, __LINE__);
  329. }
  330. if (!empty ($email) && $send_mail) {
  331. $emailto = '"'.api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS).'" <'.$email.'>';
  332. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  333. $emailheaders = 'From: '.api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS).' <'.api_get_setting('emailAdministrator').">\n";
  334. $emailheaders .= 'Reply-To: '.api_get_setting('emailAdministrator');
  335. $portal_url = $_configuration['root_web'];
  336. if ($_configuration['multiple_access_urls']==true) {
  337. $access_url_id = api_get_current_access_url_id();
  338. if ($access_url_id != -1) {
  339. $url = api_get_access_url($access_url_id);
  340. $portal_url = $url['url'];
  341. }
  342. }
  343. $emailbody=get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ". api_get_setting('siteName') ." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ". $username ."\n". get_lang('Pass')." : ".stripslashes($password)."\n\n" .get_lang('Address') ." ". api_get_setting('siteName') ." ". get_lang('Is') ." : ".$portal_url."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n". get_lang('Manager'). " ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".api_get_setting('emailAdministrator');
  344. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  345. }
  346. Security::clear_token();
  347. if (isset($user['submit_plus'])) {
  348. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  349. $tok = Security::get_token();
  350. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')).'&sec_token='.$tok);
  351. exit ();
  352. } else {
  353. $tok = Security::get_token();
  354. header('Location: ../admin/user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')).'&sec_token='.$tok);
  355. exit ();
  356. }
  357. }
  358. } else {
  359. if (isset($_POST['submit'])) {
  360. Security::clear_token();
  361. }
  362. $token = Security::get_token();
  363. $form->addElement('hidden', 'sec_token');
  364. $form->setConstants(array('sec_token' => $token));
  365. }
  366. // Display form
  367. Display::display_header($tool_name);
  368. //api_display_tool_title($tool_name);
  369. if (!empty($message)) {
  370. Display::display_normal_message(stripslashes($message));
  371. }
  372. $form->display();
  373. /*
  374. ==============================================================================
  375. FOOTER
  376. ==============================================================================
  377. */
  378. Display::display_footer();