user_add.php 12 KB

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