user_add.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. $result = api_sql_query("INSERT INTO $tbl_user
  110. SET lastname = '$lastname_form',
  111. firstname = '$firstname_form',
  112. username = '$username_form',
  113. password = '$pw',
  114. email = '$email_form',
  115. status = '$platformStatus',
  116. official_code = '$official_code_form',
  117. creator_id = '".$_user['user_id']."'");
  118. $userId = mysql_insert_id();
  119. if ($userId) $platformRegSucceed = true;
  120. }
  121. if($userId && $_cid)
  122. {
  123. /*
  124. Note : As we temporarly use this script in the platform administration
  125. section to also add user to the platform, We have to prevent course
  126. registration. That's why we check if $_cid is initialized, it gives us
  127. an hint about the use context of the script
  128. */
  129. /*---------------------------
  130. COURSE REGISTRATION
  131. ----------------------------*/
  132. /*
  133. * check the return value of the query
  134. * if 0, the user is already registered to the course
  135. */
  136. if (api_sql_query("INSERT INTO $tbl_courseUser
  137. SET user_id = '$userId',
  138. course_code = '$currentCourseID',
  139. status = '$admin_form',
  140. tutor_id = '$tutor_form'"))
  141. {
  142. $courseRegSucceed = true;
  143. }
  144. } // if $platformRegSucceed && $_cid
  145. /*---------------------------
  146. MAIL NOTIFICATION TO NEW USER
  147. ----------------------------*/
  148. if ($platformRegSucceed)
  149. {
  150. $emailto = "$lastname_form $firstname_form <$email_form>";
  151. $emailfromaddr = $administratorEmail;
  152. $emailfromname = api_get_setting('siteName');
  153. $emailsubject = get_lang('YourReg').' '.get_setting('siteName');
  154. $emailheaders = "From: ".get_setting('administratorSurname')." ".get_setting('administratorName')." <".$administratorEmail.">\n";
  155. $emailheaders .= "Reply-To: ".$administratorEmail."\n";
  156. $emailheaders .= "Return-Path: ".$administratorEmail."\n";
  157. $emailheaders .= "charset: ".$charset."\n";
  158. $emailheaders .= "X-Mailer: PHP/" . phpversion() . "\n";
  159. $emailheaders .= "X-Sender-IP: $REMOTE_ADDR"; // (small security precaution...)
  160. $recipient_name = $firstname_form.' '.$lastname_form;
  161. $sender_name = get_setting('administratorName').' '.get_setting('administratorSurname');
  162. $email_admin = get_setting('emailAdministrator');
  163. if ($courseRegSucceed)
  164. {
  165. $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').": ".$_configuration['root_web']."\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";
  166. $message = get_lang('TheU')." ".stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedToCourse')."<a href=\"user.php\">".get_lang('BackUser')."</a>\n";
  167. }
  168. else
  169. {
  170. $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').": ".$_configuration['root_web']."\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";
  171. $message = stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedU');
  172. }
  173. @api_mail($recipient_name, $email_form, $emailsubject, $emailbody, $sender_name,$email_admin);
  174. /*
  175. * remove <form> variables to prevent any pre-filled fields
  176. */
  177. unset($lastname_form, $firstname_form, $username_form, $password_form, $email_form, $admin_form, $tutor_form);
  178. } // end if ($platformRegSucceed)
  179. //else
  180. //{
  181. // $message = get_lang('UserAlreadyRegistered');
  182. //}
  183. } // end if register request
  184. $interbreadcrumb[] = array ("url"=>"user.php", "name"=> get_lang('Users'));
  185. $nameTools = get_lang('AddAU');
  186. Display::display_header($nameTools,"User");
  187. ?>
  188. <h3><?php echo get_lang('Users'); ?></h3>
  189. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  190. <tr>
  191. <td><h4><?php echo $nameTools; ?></h4></td>
  192. <td></td>
  193. </tr>
  194. </table>
  195. <?php
  196. /*==========================
  197. ADD ONE USER FORM
  198. ==========================*/
  199. ?>
  200. <?php echo get_lang('OneByOne'); ?>. <?php echo get_lang('UserOneByOneExplanation'); ?>
  201. <form method="post" action="<?php echo api_get_self(); ?>?register=yes">
  202. <table cellpadding="3" cellspacing="0" border="0">
  203. <?php
  204. if(!empty($message))
  205. {
  206. ?>
  207. <tr>
  208. <td colspan="2">
  209. <?php
  210. Display::display_normal_message($message); //main API
  211. ?>
  212. </td>
  213. </tr>
  214. <?php
  215. }
  216. ?>
  217. <tr>
  218. <td align="right"><?php echo get_lang('LastName'); ?> :</td>
  219. <td><input type="text" size="15" name="lastname_form" value="<?php echo htmlentities(stripslashes($lastname_form),ENT_QUOTES,$charset); ?>" /></td>
  220. </tr>
  221. <tr>
  222. <td align="right"><?php echo get_lang('FirstName'); ?> :</td>
  223. <td><input type="text" size="15" name="firstname_form" value="<?php echo htmlentities(stripslashes($firstname_form),ENT_QUOTES,$charset); ?>" /></td>
  224. </tr>
  225. <tr>
  226. <td align="right"><?php echo get_lang('OfficialCode'); ?> :</td>
  227. <td><input type="text" size="15" name="official_code_form" value="<?php echo htmlentities(stripslashes($official_code_form)); ?>" /></td>
  228. </tr>
  229. <tr>
  230. <td align="right"><?php echo get_lang('UserName') ?> :</td>
  231. <td><input type="text" size="15" name="username_form" value="<?php echo htmlentities(stripslashes($username_form)); ?>" /></td>
  232. </tr>
  233. <tr>
  234. <td align="right"><?php echo get_lang('Pass') ?> :</td>
  235. <td><input type="password" size="15" name="password_form" value="<?php echo htmlentities(stripslashes($password_form)) ?>" /></td>
  236. </tr>
  237. <tr>
  238. <td align="right"><?php echo get_lang('Email'); ?> :</td>
  239. <td><input type="text" size="15" name="email_form" value="<?php echo $email_form; ?>" /></td>
  240. </tr>
  241. <tr>
  242. <?php
  243. if ($_cid) // if we're inside a course, then it's a course registration
  244. {
  245. ?>
  246. <td align="right"><?php echo get_lang('Tutor'); ?> :</td>
  247. <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'); ?>
  248. <input class="checkbox" type="radio" name="tutor_form" value="1" <?php if($tutor_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes') ?></td>
  249. </tr>
  250. <tr>
  251. <td align="right"><?php echo get_lang('Manager') ?> :</td>
  252. <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') ?>
  253. <input class="checkbox" type="radio" name="admin_form" value="1" <?php if($admin_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes'); ?></td>
  254. </tr>
  255. <?php
  256. } // end if $_cid - for the case we're not in a course registration
  257. // but a platform registration
  258. else
  259. {
  260. ?>
  261. <tr>
  262. <td align="right"><?php echo get_lang('Status') ?> : </td>
  263. <td>
  264. <select name="platformStatus">
  265. <option value="<?php echo STUDENT ?>"><?php echo get_lang('RegStudent') ?></option>
  266. <option value="<?php echo COURSEMANAGER ?>"><?php echo get_lang('RegAdmin') ?></option>
  267. </select>
  268. </td>
  269. </tr>
  270. <?php
  271. } // end else if $_cid
  272. ?>
  273. <tr>
  274. <td>&nbsp;</td>
  275. <td><input type="submit" name="submit" value="<?php echo get_lang('Ok') ?>" /></td>
  276. </tr>
  277. </table>
  278. </form>
  279. <?php
  280. /*==========================
  281. IMPORT XML/CSV USER LIST
  282. ==========================*/
  283. if($is_platformAdmin)
  284. {
  285. echo "<a href=\"".$rootAdminWeb."importUserList.php\">".get_lang('UserMany')."</a>";
  286. } // if is_platformAdmin
  287. else
  288. {
  289. echo "<p>".get_lang('IfYouWantToAddManyUsers')."</p>";
  290. }
  291. Display::display_footer();
  292. ?>