user_add.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * @package dokeos.user
  21. ==============================================================================
  22. */
  23. /*==========================
  24. INIT
  25. ==========================*/
  26. // name of the language file that needs to be included
  27. $language_file="registration";
  28. include("../inc/global.inc.php");
  29. require_once(api_get_path(INCLUDE_PATH).'lib/mail.lib.inc.php');
  30. $this_section=SECTION_COURSES;
  31. if (! ($is_courseAdmin || $is_platformAdmin)) api_not_allowed(true);
  32. $currentCourseID = $_course['sysCode'];
  33. $currentCourseName = $_course['official_code'];
  34. $tbl_user = "user";
  35. $tbl_courseUser = "course_rel_user";
  36. /*==========================
  37. DATA CHECKING
  38. ==========================*/
  39. if($register)
  40. {
  41. /*
  42. * Fields Checking
  43. */
  44. $lastname_form = trim($lastname_form);
  45. $firstname_form = trim($firstname_form);
  46. $password_form = trim($password_form);
  47. $username_form = trim($username_form);
  48. $email_form = trim($email_form);
  49. $official_code_form = trim($official_code_form);
  50. // empty field checking
  51. if(empty($lastname_form) || empty($firstname_form) || empty($password_form) || empty($username_form) || empty($email_form))
  52. {
  53. $dataChecked = false;
  54. $message = get_lang('Filled');
  55. }
  56. // valid mail address checking
  57. elseif(!eregi('^[0-9a-z_.-]+@([0-9a-z-]+\.)+([0-9a-z]){2,4}$',$email_form))
  58. {
  59. $dataChecked = false;
  60. $message = get_lang('EmailWrong');
  61. }
  62. else
  63. {
  64. $dataChecked = true;
  65. }
  66. // prevent conflict with existing user account
  67. if($dataChecked)
  68. {
  69. $result=api_sql_query("SELECT user_id,
  70. (username='$username_form') AS loginExists,
  71. (lastname='$lastname_form' AND firstname='$firstname_form' AND email='$email_form') AS userExists
  72. FROM $tbl_user
  73. WHERE username='$username_form' OR (lastname='$lastname_form' AND firstname='$firstname_form' AND email='$email_form')
  74. ORDER BY userExists DESC, loginExists DESC");
  75. if(mysql_num_rows($result))
  76. {
  77. while($user=mysql_fetch_array($result))
  78. {
  79. // check if the user is already registered to the platform
  80. if($user['userExists'])
  81. {
  82. $userExists = true;
  83. $userId = $user['user_id'];
  84. break;
  85. }
  86. // check if the login name choosen is already taken by another user
  87. if($user['loginExists'])
  88. {
  89. $loginExists = true;
  90. $userId = 0;
  91. $message = get_lang('UserNo')." (".stripslashes($username_form).") ".get_lang('Taken');
  92. break;
  93. }
  94. } // end while $result
  95. } // end if num rows
  96. } // end if datachecked
  97. /*=============================
  98. NEW USER REGISTRATION PROCESS
  99. =============================*/
  100. if($dataChecked && !$userExists && !$loginExists)
  101. {
  102. /*---------------------------
  103. PLATFORM REGISTRATION
  104. ----------------------------*/
  105. if ($_cid) $platformStatus = STUDENT; // course registrartion context...
  106. else $platformStatus = $platformStatus; // admin section of the platform context...
  107. //if ($userPasswordCrypted) $pw = md5($password_form);
  108. //else $pw = $password_form;
  109. $pw = api_get_encrypted_password($password_form);
  110. $result = api_sql_query("INSERT INTO $tbl_user
  111. SET lastname = '$lastname_form',
  112. firstname = '$firstname_form',
  113. username = '$username_form',
  114. password = '$pw',
  115. email = '$email_form',
  116. status = '$platformStatus',
  117. official_code = '$official_code_form',
  118. creator_id = '".$_user['user_id']."'");
  119. $userId = mysql_insert_id();
  120. if ($userId) $platformRegSucceed = true;
  121. }
  122. if($userId && $_cid)
  123. {
  124. /*
  125. Note : As we temporarly use this script in the platform administration
  126. section to also add user to the platform, We have to prevent course
  127. registration. That's why we check if $_cid is initialized, it gives us
  128. an hint about the use context of the script
  129. */
  130. /*---------------------------
  131. COURSE REGISTRATION
  132. ----------------------------*/
  133. /*
  134. * check the return value of the query
  135. * if 0, the user is already registered to the course
  136. */
  137. if (api_sql_query("INSERT INTO $tbl_courseUser
  138. SET user_id = '$userId',
  139. course_code = '$currentCourseID',
  140. status = '$admin_form',
  141. tutor_id = '$tutor_form'"))
  142. {
  143. $courseRegSucceed = true;
  144. }
  145. } // if $platformRegSucceed && $_cid
  146. /*---------------------------
  147. MAIL NOTIFICATION TO NEW USER
  148. ----------------------------*/
  149. if ($platformRegSucceed)
  150. {
  151. $emailto = "$lastname_form $firstname_form <$email_form>";
  152. $emailfromaddr = $administratorEmail;
  153. $emailfromname = api_get_setting('siteName');
  154. $emailsubject = get_lang('YourReg').' '.get_setting('siteName');
  155. $emailheaders = "From: ".get_setting('administratorSurname')." ".get_setting('administratorName')." <".$administratorEmail.">\n";
  156. $emailheaders .= "Reply-To: ".$administratorEmail."\n";
  157. $emailheaders .= "Return-Path: ".$administratorEmail."\n";
  158. $emailheaders .= "charset: ".$charset."\n";
  159. $emailheaders .= "X-Mailer: PHP/" . phpversion() . "\n";
  160. $emailheaders .= "X-Sender-IP: $REMOTE_ADDR"; // (small security precaution...)
  161. $recipient_name = $firstname_form.' '.$lastname_form;
  162. $sender_name = get_setting('administratorName').' '.get_setting('administratorSurname');
  163. $email_admin = get_setting('emailAdministrator');
  164. $portal_url = $_configuration['root_web'];
  165. if ($_configuration['multiple_access_urls']==true) {
  166. $access_url_id = api_get_current_access_url_id();
  167. if ($access_url_id != -1 ){
  168. $url = api_get_access_url($access_url_id);
  169. $portal_url = $url['url'];
  170. }
  171. }
  172. if ($courseRegSucceed)
  173. {
  174. $emailbody = get_lang('Dear')." ".stripslashes("$firstname_form $lastname_form").",\n".get_lang('OneResp')." $currentCourseName ".get_lang('RegYou')." ".get_setting('siteName')." ".get_lang('Settings')." $username_form\n".get_lang('Pass').": $password_form\n".get_lang('Address')." ".get_setting('siteName')." ".get_lang('Is').": ".$portal_url."\n".get_lang('Problem')."\n".get_lang('Formula').",\n".get_setting('administratorSurname')." ".get_setting('administratorName')."\n".get_lang('Manager')." ".get_setting('siteName')." \nT. ".get_setting('administratorTelephone')."\n".get_lang('Email').": ".get_setting('emailAdministrator')."\n";
  175. $message = get_lang('TheU')." ".stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedToCourse')."<a href=\"user.php\">".get_lang('BackUser')."</a>\n";
  176. }
  177. else
  178. {
  179. $emailbody = get_lang('Dear')." $firstname_form $lastname_form,\n ".get_lang('YouAreReg')." ".get_setting('siteName')." ".get_lang('Settings')." $username_form\n".get_lang('Pass').": $password_form\n".get_lang('Address')." ".get_setting('siteName')." ".get_lang('Is').": ".$portal_url."\n".get_lang('Problem')."\n".get_lang('Formula').",\n".get_setting('administratorSurname')." ".get_setting('administratorName')."\n".get_lang('Manager')." ".get_setting('siteName')." \nT. ".get_setting('administratorTelephone')."\n".get_lang('Email').": ".get_setting('emailAdministrator')."\n";
  180. $message = stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedU');
  181. }
  182. @api_mail($recipient_name, $email_form, $emailsubject, $emailbody, $sender_name,$email_admin);
  183. /*
  184. * remove <form> variables to prevent any pre-filled fields
  185. */
  186. unset($lastname_form, $firstname_form, $username_form, $password_form, $email_form, $admin_form, $tutor_form);
  187. } // end if ($platformRegSucceed)
  188. //else
  189. //{
  190. // $message = get_lang('UserAlreadyRegistered');
  191. //}
  192. } // end if register request
  193. $interbreadcrumb[] = array ("url"=>"user.php", "name"=> get_lang('Users'));
  194. $nameTools = get_lang('AddAU');
  195. Display::display_header($nameTools,"User");
  196. ?>
  197. <h3><?php echo get_lang('Users'); ?></h3>
  198. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  199. <tr>
  200. <td><h4><?php echo $nameTools; ?></h4></td>
  201. <td></td>
  202. </tr>
  203. </table>
  204. <?php
  205. /*==========================
  206. ADD ONE USER FORM
  207. ==========================*/
  208. ?>
  209. <?php echo get_lang('OneByOne'); ?>. <?php echo get_lang('UserOneByOneExplanation'); ?>
  210. <form method="post" action="<?php echo api_get_self(); ?>?register=yes">
  211. <table cellpadding="3" cellspacing="0" border="0">
  212. <?php
  213. if(!empty($message))
  214. {
  215. ?>
  216. <tr>
  217. <td colspan="2">
  218. <?php
  219. Display::display_normal_message($message); //main API
  220. ?>
  221. </td>
  222. </tr>
  223. <?php
  224. }
  225. ?>
  226. <tr>
  227. <td align="right"><?php echo get_lang('LastName'); ?> :</td>
  228. <td><input type="text" size="15" name="lastname_form" value="<?php echo htmlentities(stripslashes($lastname_form),ENT_QUOTES,$charset); ?>" /></td>
  229. </tr>
  230. <tr>
  231. <td align="right"><?php echo get_lang('FirstName'); ?> :</td>
  232. <td><input type="text" size="15" name="firstname_form" value="<?php echo htmlentities(stripslashes($firstname_form),ENT_QUOTES,$charset); ?>" /></td>
  233. </tr>
  234. <tr>
  235. <td align="right"><?php echo get_lang('OfficialCode'); ?> :</td>
  236. <td><input type="text" size="15" name="official_code_form" value="<?php echo htmlentities(stripslashes($official_code_form)); ?>" /></td>
  237. </tr>
  238. <tr>
  239. <td align="right"><?php echo get_lang('UserName') ?> :</td>
  240. <td><input type="text" size="15" name="username_form" value="<?php echo htmlentities(stripslashes($username_form)); ?>" /></td>
  241. </tr>
  242. <tr>
  243. <td align="right"><?php echo get_lang('Pass') ?> :</td>
  244. <td><input type="password" size="15" name="password_form" value="<?php echo htmlentities(stripslashes($password_form)) ?>" /></td>
  245. </tr>
  246. <tr>
  247. <td align="right"><?php echo get_lang('Email'); ?> :</td>
  248. <td><input type="text" size="15" name="email_form" value="<?php echo $email_form; ?>" /></td>
  249. </tr>
  250. <tr>
  251. <?php
  252. if ($_cid) // if we're inside a course, then it's a course registration
  253. {
  254. ?>
  255. <td align="right"><?php echo get_lang('Tutor'); ?> :</td>
  256. <td><input class="checkbox" type="radio" name="tutor_form" value="0" <?php if(!isset($tutor_form) || !$tutor_form) echo 'checked="checked"'; ?> /> <?php echo get_lang('No'); ?>
  257. <input class="checkbox" type="radio" name="tutor_form" value="1" <?php if($tutor_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes') ?></td>
  258. </tr>
  259. <tr>
  260. <td align="right"><?php echo get_lang('Manager') ?> :</td>
  261. <td><input class="checkbox" type="radio" name="admin_form" value="5" <?php if(!isset($admin_form) || $admin_form == 5) echo 'checked="checked"'; ?> /> <?php echo get_lang('No') ?>
  262. <input class="checkbox" type="radio" name="admin_form" value="1" <?php if($admin_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes'); ?></td>
  263. </tr>
  264. <?php
  265. } // end if $_cid - for the case we're not in a course registration
  266. // but a platform registration
  267. else
  268. {
  269. ?>
  270. <tr>
  271. <td align="right"><?php echo get_lang('Status') ?> : </td>
  272. <td>
  273. <select name="platformStatus">
  274. <option value="<?php echo STUDENT ?>"><?php echo get_lang('RegStudent') ?></option>
  275. <option value="<?php echo COURSEMANAGER ?>"><?php echo get_lang('RegAdmin') ?></option>
  276. </select>
  277. </td>
  278. </tr>
  279. <?php
  280. } // end else if $_cid
  281. ?>
  282. <tr>
  283. <td>&nbsp;</td>
  284. <td><input type="submit" name="submit" value="<?php echo get_lang('Ok') ?>" /></td>
  285. </tr>
  286. </table>
  287. </form>
  288. <?php
  289. /*==========================
  290. IMPORT XML/CSV USER LIST
  291. ==========================*/
  292. if($is_platformAdmin)
  293. {
  294. echo "<a href=\"".$rootAdminWeb."importUserList.php\">".get_lang('UserMany')."</a>";
  295. } // if is_platformAdmin
  296. else
  297. {
  298. echo "<p>".get_lang('IfYouWantToAddManyUsers')."</p>";
  299. }
  300. Display::display_footer();
  301. ?>