user_add.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. $langFile="registration";
  27. include("../inc/global.inc.php");
  28. $this_section=SECTION_COURSES;
  29. if (! ($is_courseAdmin || $is_platformAdmin)) die ("not allowed");
  30. $currentCourseID = $_course['sysCode'];
  31. $currentCourseName = $_course['official_code'];
  32. $tbl_user = "user";
  33. $tbl_courseUser = "course_rel_user";
  34. /*==========================
  35. DATA CHECKING
  36. ==========================*/
  37. if($register)
  38. {
  39. /*
  40. * Fields Checking
  41. */
  42. $lastname_form = trim($lastname_form);
  43. $firstname_form = trim($firstname_form);
  44. $password_form = trim($password_form);
  45. $username_form = trim($username_form);
  46. $email_form = trim($email_form);
  47. $official_code_form = trim($official_code_form);
  48. // empty field checking
  49. if(empty($lastname_form) || empty($firstname_form) || empty($password_form) || empty($username_form) || empty($email_form))
  50. {
  51. $dataChecked = false;
  52. $message = get_lang('Filled');
  53. }
  54. // valid mail address checking
  55. elseif(!eregi('^[0-9a-z_.-]+@([0-9a-z-]+\.)+([0-9a-z]){2,4}$',$email_form))
  56. {
  57. $dataChecked = false;
  58. $message = get_lang('EmailWrong');
  59. }
  60. else
  61. {
  62. $dataChecked = true;
  63. }
  64. // prevent conflict with existing user account
  65. if($dataChecked)
  66. {
  67. $result=api_sql_query("SELECT user_id,
  68. (username='$username_form') AS loginExists,
  69. (lastname='$lastname_form' AND firstname='$firstname_form' AND email='$email_form') AS userExists
  70. FROM $tbl_user
  71. WHERE username='$username_form' OR (lastname='$lastname_form' AND firstname='$firstname_form' AND email='$email_form')
  72. ORDER BY userExists DESC, loginExists DESC");
  73. if(mysql_num_rows($result))
  74. {
  75. while($user=mysql_fetch_array($result))
  76. {
  77. // check if the user is already registered to the platform
  78. if($user['userExists'])
  79. {
  80. $userExists = true;
  81. $userId = $user['user_id'];
  82. break;
  83. }
  84. // check if the login name choosen is already taken by another user
  85. if($user['loginExists'])
  86. {
  87. $loginExists = true;
  88. $userId = 0;
  89. $message = get_lang('UserNo')." (".stripslashes($username_form).") ".get_lang('Taken');
  90. break;
  91. }
  92. } // end while $result
  93. } // end if num rows
  94. } // end if datachecked
  95. /*=============================
  96. NEW USER REGISTRATION PROCESS
  97. =============================*/
  98. if($dataChecked && !$userExists && !$loginExists)
  99. {
  100. /*---------------------------
  101. PLATFORM REGISTRATION
  102. ----------------------------*/
  103. if ($_cid) $platformStatus = STUDENT; // course registrartion context...
  104. else $platformStatus = $platformStatus; // admin section of the platform context...
  105. if ($userPasswordCrypted) $pw = md5($password_form);
  106. else $pw = $password_form;
  107. $result = api_sql_query("INSERT INTO $tbl_user
  108. SET lastname = '$lastname_form',
  109. firstname = '$firstname_form',
  110. username = '$username_form',
  111. password = '$pw',
  112. email = '$email_form',
  113. status = '$platformStatus',
  114. official_code = '$official_code_form',
  115. creator_id = '$_uid'");
  116. $userId = mysql_insert_id();
  117. if ($userId) $platformRegSucceed = true;
  118. }
  119. if($userId && $_cid)
  120. {
  121. /*
  122. Note : As we temporarly use this script in the platform administration
  123. section to also add user to the platform, We have to prevent course
  124. registration. That's why we check if $_cid is initialized, it gives us
  125. an hint about the use context of the script
  126. */
  127. /*---------------------------
  128. COURSE REGISTRATION
  129. ----------------------------*/
  130. /*
  131. * check the return value of the query
  132. * if 0, the user is already registered to the course
  133. */
  134. if (api_sql_query("INSERT INTO $tbl_courseUser
  135. SET user_id = '$userId',
  136. course_code = '$currentCourseID',
  137. status = '$admin_form',
  138. tutor_id = '$tutor_form'"))
  139. {
  140. $courseRegSucceed = true;
  141. }
  142. } // if $platformRegSucceed && $_cid
  143. /*---------------------------
  144. MAIL NOTIFICATION TO NEW USER
  145. ----------------------------*/
  146. if ($platformRegSucceed)
  147. {
  148. $emailto = "$lastname_form $firstname_form <$email_form>";
  149. $emailfromaddr = $administratorEmail;
  150. $emailfromname = api_get_setting('siteName');
  151. $emailsubject = get_lang('YourReg').' '.get_setting('siteName');
  152. $emailheaders = "From: ".get_setting('administratorSurname')." ".get_setting('administratorName')." <".$administratorEmail.">\n";
  153. $emailheaders .= "Reply-To: ".$administratorEmail."\n";
  154. $emailheaders .= "X-Mailer: PHP/" . phpversion() . "\n";
  155. $emailheaders .= "X-Sender-IP: $REMOTE_ADDR"; // (small security precaution...)
  156. if ($courseRegSucceed)
  157. {
  158. $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').": $rootWeb\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";
  159. $message = get_lang('TheU')." ".stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedToCourse')."<a href=\"user.php\">".get_lang('BackUser')."</a>\n";
  160. }
  161. else
  162. {
  163. $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').": $rootWeb\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";
  164. $message = stripslashes("$firstname_form $lastname_form")." ".get_lang('AddedU');
  165. }
  166. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  167. /*
  168. * remove <form> variables to prevent any pre-filled fields
  169. */
  170. unset($lastname_form, $firstname_form, $username_form, $password_form, $email_form, $admin_form, $tutor_form);
  171. } // end if ($platformRegSucceed)
  172. //else
  173. //{
  174. // $message = get_lang('UserAlreadyRegistered');
  175. //}
  176. } // end if register request
  177. $interbreadcrumb[] = array ("url"=>"user.php", "name"=> get_lang('Users'));
  178. $nameTools = get_lang('AddAU');
  179. Display::display_header($nameTools,"User");
  180. ?>
  181. <h3><?php echo get_lang('Users'); ?></h3>
  182. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  183. <tr>
  184. <td><h4><?php echo $nameTools; ?></h4></td>
  185. <td></td>
  186. </tr>
  187. </table>
  188. <?php
  189. /*==========================
  190. ADD ONE USER FORM
  191. ==========================*/
  192. ?>
  193. <?php echo get_lang('OneByOne'); ?>. <?php echo get_lang('UserOneByOneExplanation'); ?>
  194. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?register=yes">
  195. <table cellpadding="3" cellspacing="0" border="0">
  196. <?php
  197. if(!empty($message))
  198. {
  199. ?>
  200. <tr>
  201. <td colspan="2">
  202. <?php
  203. Display::display_normal_message($message); //main API
  204. ?>
  205. </td>
  206. </tr>
  207. <?php
  208. }
  209. ?>
  210. <tr>
  211. <td align="right"><?php echo get_lang('Lastname'); ?> :</td>
  212. <td><input type="text" size="15" name="lastname_form" value="<?php echo htmlentities(stripslashes($lastname_form)); ?>" /></td>
  213. </tr>
  214. <tr>
  215. <td align="right"><?php echo get_lang('Firstname'); ?> :</td>
  216. <td><input type="text" size="15" name="firstname_form" value="<?php echo htmlentities(stripslashes($firstname_form)); ?>" /></td>
  217. </tr>
  218. <tr>
  219. <td align="right"><?php echo get_lang('OfficialCode'); ?> :</td>
  220. <td><input type="text" size="15" name="official_code_form" value="<?php echo htmlentities(stripslashes($official_code_form)); ?>" /></td>
  221. </tr>
  222. <tr>
  223. <td align="right"><?php echo get_lang('Username') ?> :</td>
  224. <td><input type="text" size="15" name="username_form" value="<?php echo htmlentities(stripslashes($username_form)); ?>" /></td>
  225. </tr>
  226. <tr>
  227. <td align="right"><?php echo get_lang('Pass') ?> :</td>
  228. <td><input type="password" size="15" name="password_form" value="<?php echo htmlentities(stripslashes($password_form)) ?>" /></td>
  229. </tr>
  230. <tr>
  231. <td align="right"><?php echo get_lang('Email'); ?> :</td>
  232. <td><input type="text" size="15" name="email_form" value="<?php echo $email_form; ?>" /></td>
  233. </tr>
  234. <tr>
  235. <?php
  236. if ($_cid) // if we're inside a course, then it's a course registration
  237. {
  238. ?>
  239. <td align="right"><?php echo get_lang('Tutor'); ?> :</td>
  240. <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'); ?>
  241. <input class="checkbox" type="radio" name="tutor_form" value="1" <?php if($tutor_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes') ?></td>
  242. </tr>
  243. <tr>
  244. <td align="right"><?php echo get_lang('Manager') ?> :</td>
  245. <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') ?>
  246. <input class="checkbox" type="radio" name="admin_form" value="1" <?php if($admin_form == 1) echo 'checked="checked"'; ?> /> <?php echo get_lang('Yes'); ?></td>
  247. </tr>
  248. <?php
  249. } // end if $_cid - for the case we're not in a course registration
  250. // but a platform registration
  251. else
  252. {
  253. ?>
  254. <tr>
  255. <td align="right"><?php echo get_lang('Status') ?> : </td>
  256. <td>
  257. <select name="platformStatus">
  258. <option value="<?php echo STUDENT ?>"><?php echo get_lang('RegStudent') ?></option>
  259. <option value="<?php echo COURSEMANAGER ?>"><?php echo get_lang('RegAdmin') ?></option>
  260. </select>
  261. </td>
  262. </tr>
  263. <?php
  264. } // end else if $_cid
  265. ?>
  266. <tr>
  267. <td>&nbsp;</td>
  268. <td><input type="submit" name="submit" value="<?php echo get_lang('Ok') ?>" /></td>
  269. </tr>
  270. </table>
  271. </form>
  272. <?php
  273. /*==========================
  274. IMPORT XML/CSV USER LIST
  275. ==========================*/
  276. if($is_platformAdmin)
  277. {
  278. echo "<a href=\"".$rootAdminWeb."importUserList.php\">".get_lang('UserMany')."</a>";
  279. } // if is_platformAdmin
  280. else
  281. {
  282. echo "<p>".get_lang('IfYouWantToAddManyUsers')."</p>";
  283. }
  284. Display::display_footer();
  285. ?>