user_add.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php // $Id: user_add.php 14923 2008-04-16 14:06:02Z elixir_inter $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. Copyright (c) Bart Mollet, Hogeschool Gent
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, rue du Corbeau, 108, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * @package dokeos.admin
  23. ==============================================================================
  24. */
  25. // name of the language file that needs to be included
  26. $language_file = array('admin','registration');
  27. $cidReset = true;
  28. // including necessary libraries
  29. require ('../inc/global.inc.php');
  30. $libpath = api_get_path(LIBRARY_PATH);
  31. include_once ($libpath.'fileManage.lib.php');
  32. include_once ($libpath.'fileUpload.lib.php');
  33. include_once ($libpath.'usermanager.lib.php');
  34. require_once ($libpath.'formvalidator/FormValidator.class.php');
  35. // section for the tabs
  36. $this_section=SECTION_PLATFORM_ADMIN;
  37. // user permissions
  38. api_protect_admin_script();
  39. // Database table definitions
  40. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  41. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  42. $htmlHeadXtra[] = '
  43. <script language="JavaScript" type="text/JavaScript">
  44. <!--
  45. function enable_expiration_date() { //v2.0
  46. document.user_add.radio_expiration_date[0].checked=false;
  47. document.user_add.radio_expiration_date[1].checked=true;
  48. }
  49. function password_switch_radio_button(form, input){
  50. var NodeList = document.getElementsByTagName("input");
  51. for(var i=0; i< NodeList.length; i++){
  52. if(NodeList.item(i).name=="password[password_auto]" && NodeList.item(i).value=="0"){
  53. NodeList.item(i).checked=true;
  54. }
  55. }
  56. }
  57. function display_drh_list(){
  58. if(document.getElementById("status_select").value=='.STUDENT.')
  59. {
  60. document.getElementById("drh_list").style.display="block";
  61. }
  62. else
  63. {
  64. document.getElementById("drh_list").style.display="none";
  65. document.getElementById("drh_select").options[0].selected="selected";
  66. }
  67. }
  68. //-->
  69. </script>';
  70. if(!empty($_GET['message'])){
  71. $message = urldecode($_GET['message']);
  72. }
  73. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  74. $tool_name = get_lang('AddUsers');
  75. // Create the form
  76. $form = new FormValidator('user_add');
  77. // Lastname
  78. $form->addElement('text','lastname',get_lang('LastName'));
  79. $form->applyFilter('lastname','html_filter');
  80. $form->applyFilter('lastname','trim');
  81. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  82. // Firstname
  83. $form->addElement('text','firstname',get_lang('FirstName'));
  84. $form->applyFilter('firstname','html_filter');
  85. $form->applyFilter('firstname','trim');
  86. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  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. // Phone
  96. $form->addElement('text','phone',get_lang('PhoneNumber'));
  97. // Picture
  98. $form->addElement('file', 'picture', get_lang('AddPicture'));
  99. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  100. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  101. // Username
  102. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  103. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  104. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  105. $form->addRule('username', '', 'maxlength',20);
  106. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  107. // Password
  108. $group = array();
  109. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  110. if(count($extAuthSource) > 0)
  111. {
  112. $group[] =& HTML_QuickForm::createElement('radio','password_auto',null,get_lang('ExternalAuthentication').' ',2);
  113. $auth_sources = array();
  114. foreach($extAuthSource as $key => $info)
  115. {
  116. $auth_sources[$key] = $key;
  117. }
  118. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  119. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  120. }
  121. $group[] =& HTML_QuickForm::createElement('radio','password_auto',get_lang('Password'),get_lang('AutoGeneratePassword').'<br />',1);
  122. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto','id="radio_user_password"',null,0);
  123. $group[] =& HTML_QuickForm::createElement('password', 'password',null,'onkeydown=password_switch_radio_button(document.user_add,"password[password_auto]")');
  124. $form->addGroup($group, 'password', get_lang('Password'), '');
  125. // Status
  126. $status = array();
  127. $status[COURSEMANAGER] = get_lang('Teacher');
  128. $status[STUDENT] = get_lang('Learner');
  129. $status[DRH] = get_lang('Drh');
  130. $status[SESSIONADMIN] = get_lang('SessionsAdmin');
  131. $form->addElement('select','status',get_lang('Status'),$status,'id="status_select" onchange="display_drh_list()"');
  132. //drh list (display only if student)
  133. $display = $_POST['status'] == STUDENT || !isset($_POST['status']) ? 'block' : 'none';
  134. $form->addElement('html','<div id="drh_list" style="display:'.$display.';">');
  135. $drh_select = $form->addElement('select','hr_dept_id',get_lang('Drh'),array(),'id="drh_select"');
  136. $drh_list = UserManager :: get_user_list(array('status'=>DRH),array('lastname','firstname'));
  137. $drh_select->addOption('---',0);
  138. if(is_array($drh_list))
  139. {
  140. foreach($drh_list as $drh)
  141. {
  142. $drh_select->addOption($drh['lastname'].' '.$drh['firstname'],$drh['user_id']);
  143. }
  144. }
  145. $form->addElement('html', '</div>');
  146. // Platform admin
  147. $group = array();
  148. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  149. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  150. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  151. // Send email
  152. $group = array();
  153. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  154. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  155. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  156. // Expiration Date
  157. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  158. $group = array ();
  159. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  160. $group[] = & $form->createElement('datepicker','expiration_date', null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  161. $form->addGroup($group, 'max_member_group', null, '', false);
  162. // Active account or inactive account
  163. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  164. $form->addElement('radio','active','',get_lang('Inactive'),0);
  165. // EXTRA FIELDS
  166. $extra = UserManager::get_extra_fields(0,50,5,'ASC');
  167. foreach($extra as $id => $field_details)
  168. {
  169. if($field_details[6] == 0)
  170. {
  171. continue;
  172. }
  173. switch($field_details[2])
  174. {
  175. case USER_FIELD_TYPE_TEXT:
  176. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
  177. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  178. $form->applyFilter('extra_'.$field_details[1], 'trim');
  179. break;
  180. case USER_FIELD_TYPE_TEXTAREA:
  181. $form->add_html_editor('extra_'.$field_details[1], $field_details[3], false);
  182. //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
  183. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  184. $form->applyFilter('extra_'.$field_details[1], 'trim');
  185. break;
  186. case USER_FIELD_TYPE_RADIO:
  187. $group = array();
  188. foreach($field_details[8] as $option_id => $option_details)
  189. {
  190. $options[$option_details[1]] = $option_details[2];
  191. $group[] =& HTML_QuickForm::createElement('radio', 'extra_'.$field_details[1], $option_details[1],$option_details[2].'<br />',$option_details[1]);
  192. }
  193. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
  194. break;
  195. case USER_FIELD_TYPE_SELECT:
  196. $options = array();
  197. foreach($field_details[8] as $option_id => $option_details)
  198. {
  199. $options[$option_details[1]] = $option_details[2];
  200. }
  201. $form->addElement('select','extra_'.$field_details[1],$field_details[3],$options,'');
  202. break;
  203. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  204. $options = array();
  205. foreach($field_details[8] as $option_id => $option_details)
  206. {
  207. $options[$option_details[1]] = $option_details[2];
  208. }
  209. $form->addElement('select','extra_'.$field_details[1],$field_details[3],$options,array('multiple' => 'multiple'));
  210. break;
  211. case USER_FIELD_TYPE_DATE:
  212. $form->addElement('datepickerdate', 'extra_'.$field_details[1], $field_details[3]);
  213. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear',1900);
  214. $form->applyFilter('theme', 'trim');
  215. break;
  216. case USER_FIELD_TYPE_DATETIME:
  217. $form->addElement('datepicker', 'extra_'.$field_details[1], $field_details[3]);
  218. $form->applyFilter('theme', 'trim');
  219. break;
  220. }
  221. }
  222. // Set default values
  223. $defaults['admin']['platform_admin'] = 0;
  224. $defaults['mail']['send_mail'] = 1;
  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. $form->setDefaults($defaults);
  236. // Submit button
  237. $form->addElement('submit', 'submit', get_lang('Add'));
  238. $form->addElement('submit', 'submit_plus', get_lang('Add').'+');
  239. // Validate form
  240. if( $form->validate())
  241. {
  242. $check = Security::check_token('post');
  243. if($check)
  244. {
  245. $user = $form->exportValues();
  246. $picture_element = & $form->getElement('picture');
  247. $picture = $picture_element->getValue();
  248. $picture_uri = '';
  249. if (strlen($picture['name']) > 0)
  250. {
  251. if(!is_dir(api_get_path(SYS_CODE_PATH).'upload/users/')){
  252. if(mkdir(api_get_path(SYS_CODE_PATH).'upload/users/'))
  253. {
  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. {
  276. $auth_source = $user['password']['auth_source'];
  277. $password = 'PLACEHOLDER';
  278. }
  279. else
  280. {
  281. $auth_source = PLATFORM_AUTH_SOURCE;
  282. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  283. }
  284. if ($user['radio_expiration_date']=='1' )
  285. {
  286. $expiration_date=$user['expiration_date'];
  287. }
  288. else
  289. {
  290. $expiration_date='0000-00-00 00:00:00';
  291. }
  292. $active = intval($user['active']);
  293. $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);
  294. $extras = array();
  295. foreach($user as $key => $value)
  296. {
  297. if(substr($key,0,6)=='extra_') //an extra field
  298. {
  299. $myres = UserManager::update_extra_field_value($user_id,substr($key,6),$value);
  300. }
  301. }
  302. if ($platform_admin)
  303. {
  304. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  305. api_sql_query($sql,__FILE__,__LINE__);
  306. }
  307. if (!empty ($email) && $send_mail)
  308. {
  309. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  310. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  311. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  312. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  313. $emailbody=get_lang('Dear')." ".stripslashes("$firstname $lastname").",\n\n".get_lang('YouAreReg')." ". get_setting('siteName') ." ".get_lang('Settings')." ". $username ."\n". get_lang('Pass')." : ".stripslashes($password)."\n\n" .get_lang('Address') ." ". get_setting('siteName') ." ". get_lang('Is') ." : ". $_configuration['root_web'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  314. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  315. }
  316. Security::clear_token();
  317. if(isset($user['submit_plus']))
  318. {
  319. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  320. $tok = Security::get_token();
  321. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded').'&sec_token='.$tok));
  322. exit ();
  323. }
  324. else
  325. {
  326. $tok = Security::get_token();
  327. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded').'&sec_token='.$tok));
  328. exit ();
  329. }
  330. }
  331. }else{
  332. if(isset($_POST['submit'])){
  333. Security::clear_token();
  334. }
  335. $token = Security::get_token();
  336. $form->addElement('hidden','sec_token');
  337. $form->setConstants(array('sec_token' => $token));
  338. }
  339. // Display form
  340. Display::display_header($tool_name);
  341. //api_display_tool_title($tool_name);
  342. if(!empty($message)){
  343. Display::display_normal_message(stripslashes($message));
  344. }
  345. $form->display();
  346. /*
  347. ==============================================================================
  348. FOOTER
  349. ==============================================================================
  350. */
  351. Display::display_footer();
  352. ?>