user_add.php 18 KB

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