PageRenderTime 80ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/mods/_core/enrolment/lib/enroll.inc.php

https://github.com/harriswong/ATutor
PHP | 261 lines | 196 code | 44 blank | 21 comment | 50 complexity | d7138c107a5c7fe31eb52ba74d5ab0ee MD5 | raw file
  1. <?php
  2. /************************************************************************/
  3. /* ATutor */
  4. /************************************************************************/
  5. /* Copyright (c) 2002-2010 */
  6. /* Inclusive Design Institute */
  7. /* http://atutor.ca */
  8. /* */
  9. /* This program is free software. You can redistribute it and/or */
  10. /* modify it under the terms of the GNU General Public License */
  11. /* as published by the Free Software Foundation. */
  12. /************************************************************************/
  13. // $Id$
  14. function checkUserInfo($record) {
  15. global $db, $addslashes;
  16. static $email_list;
  17. if (empty($record['remove'])) {
  18. $record['remove'] = FALSE;
  19. }
  20. //error flags for this record
  21. $record['err_email'] = FALSE;
  22. $record['err_uname'] = FALSE;
  23. $record['exists'] = FALSE;
  24. $record['email'] = trim($record['email']);
  25. /* email check */
  26. if ($record['email'] == '') {
  27. $record['err_email'] = _AT('import_err_email_missing');
  28. } else if (!preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", $record['email'])) {
  29. $record['err_email'] = _AT('import_err_email_invalid');
  30. } else if (isset($email_list[$record['email']])) {
  31. $record['err_email'] = _AT('import_err_email_exists');
  32. } else {
  33. $record['email'] = $addslashes($record['email']);
  34. $sql="SELECT * FROM ".TABLE_PREFIX."members WHERE email LIKE '$record[email]'";
  35. $result = mysql_query($sql,$db);
  36. if (mysql_num_rows($result) != 0) {
  37. $row = mysql_fetch_assoc($result);
  38. $record['exists'] = _AT('import_err_email_exists');
  39. $record['fname'] = $row['first_name'];
  40. $record['lname'] = $row['last_name'];
  41. $record['email'] = $row['email'];
  42. $record['uname'] = $row['login'];
  43. $record['status'] = $row['status'];
  44. } else {
  45. // it's good, add it to the list
  46. $email_list[$record['email']] = true;
  47. }
  48. }
  49. /* username check */
  50. if (empty($record['uname'])) {
  51. $record['uname'] = stripslashes (strtolower (substr ($record['fname'], 0, 1).$_POST['sep_choice'].$record['lname']));
  52. }
  53. $record['uname'] = preg_replace("{[^a-zA-Z0-9._-]}","", trim($record['uname']));
  54. if (!(preg_match("/^[a-zA-Z0-9._-]([a-zA-Z0-9._-])*$/i", $record['uname']))) {
  55. $record['err_uname'] = _AT('import_err_username_invalid');
  56. }
  57. if (isset($record['status']) && $record['status'] == AT_STATUS_DISABLED) {
  58. $record['err_disabled'] = true;
  59. } else {
  60. $record['err_disabled'] = false;
  61. }
  62. $record['uname'] = $addslashes($record['uname']);
  63. $record['fname'] = $addslashes($record['fname']);
  64. $record['lname'] = $addslashes($record['lname']);
  65. $sql = "SELECT member_id FROM ".TABLE_PREFIX."members WHERE login='$record[uname]'";
  66. $result = mysql_query($sql,$db);
  67. if ((mysql_num_rows($result) != 0) && !$record['exists']) {
  68. $record['err_uname'] = _AT('import_err_username_exists');
  69. } else {
  70. $result = mysql_query("SELECT * FROM ".TABLE_PREFIX."admins WHERE login='$record[uname]'",$db);
  71. if (mysql_num_rows($result) != 0) {
  72. $record['err_uname'] = _AT('import_err_username_exists');
  73. }
  74. }
  75. $sql = "SELECT member_id FROM ".TABLE_PREFIX."members WHERE first_name='$record[fname]' AND last_name='$record[lname]' LIMIT 1";
  76. $result = mysql_query($sql,$db);
  77. if ((mysql_num_rows($result) != 0) && !$record['exists']) {
  78. $record['err_uname'] = _AT('import_err_full_name_exists');
  79. }
  80. /* removed record? */
  81. if ($record['remove']) {
  82. //unset errors
  83. $record['err_email'] = '';
  84. $record['err_uname'] = '';
  85. $record['err_disabled'] = '';
  86. }
  87. $record['fname'] = htmlspecialchars(stripslashes(trim($record['fname'])));
  88. $record['lname'] = htmlspecialchars(stripslashes(trim($record['lname'])));
  89. $record['email'] = htmlspecialchars(stripslashes(trim($record['email'])));
  90. $record['uname'] = htmlspecialchars(stripslashes(trim($record['uname'])));
  91. return $record;
  92. }
  93. function add_users($user_list, $enroll, $course) {
  94. global $db;
  95. global $msg;
  96. global $_config;
  97. global $addslashes;
  98. require_once(AT_INCLUDE_PATH.'classes/phpmailer/atutormailer.class.php');
  99. if (defined('AT_EMAIL_CONFIRMATION') && AT_EMAIL_CONFIRMATION) {
  100. $status = AT_STATUS_UNCONFIRMED;
  101. } else {
  102. $status = AT_STATUS_STUDENT;
  103. }
  104. foreach ($user_list as $student) {
  105. if (!$student['remove']) {
  106. $student['uname'] = $addslashes($student['uname']);
  107. $student['email'] = $addslashes($student['email']);
  108. $student['fname'] = $addslashes($student['fname']);
  109. $student['lname'] = $addslashes($student['lname']);
  110. if (!$student['exists']) {
  111. $sql = "INSERT INTO ".TABLE_PREFIX."members
  112. (login,
  113. password,
  114. email,
  115. first_name,
  116. last_name,
  117. gender,
  118. status,
  119. preferences,
  120. creation_date,
  121. language,
  122. inbox_notify,
  123. private_email)
  124. VALUES
  125. ('$student[uname]',
  126. '". sha1($student[uname]). "',
  127. '$student[email]',
  128. '$student[fname]',
  129. '$student[lname]',
  130. 'n',
  131. $status,
  132. '$_config[pref_defaults]',
  133. NOW(),
  134. '$_config[default_language]',
  135. $_config[pref_inbox_notify],
  136. 1)";
  137. $result = mysql_query($sql, $db);
  138. if (mysql_affected_rows($db) == 1) {
  139. $m_id = mysql_insert_id($db);
  140. $student['exists'] = _AT('import_err_email_exists');
  141. $sql = "INSERT INTO ".TABLE_PREFIX."course_enrollment (member_id, course_id, approved, last_cid) VALUES ($m_id, $course, '$enroll', 0)";
  142. if ($result = mysql_query($sql,$db)) {
  143. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  144. if (defined('AT_EMAIL_CONFIRMATION') && AT_EMAIL_CONFIRMATION) {
  145. $sql = "SELECT email, creation_date FROM ".TABLE_PREFIX."members WHERE member_id=$m_id";
  146. $result = mysql_query($sql, $db);
  147. $row = mysql_fetch_assoc($result);
  148. $code = substr(md5($row['email'] . $row['creation_date'] . $m_id), 0, 10);
  149. // send email here.
  150. $confirmation_link = AT_BASE_HREF . 'confirm.php?id='.$m_id.SEP.'m='.$code;
  151. $subject = $_config['site_name'].': '._AT('email_confirmation_subject');
  152. $body = _AT(array('new_account_enroll_confirm', $_SESSION['course_title'], $confirmation_link))."\n\n";
  153. } else {
  154. $subject = $_config['site_name'].': '._AT('account_information');
  155. $body = _AT(array('new_account_enroll',AT_BASE_HREF, $_SESSION['course_title']))."\n\n";
  156. }
  157. //$body .= SITE_NAME.': '._AT('account_information')."\n";
  158. $body .= _AT('web_site') .' : '.AT_BASE_HREF."\n";
  159. $body .= _AT('login_name') .' : '.$student['uname'] . "\n";
  160. $body .= _AT('password') .' : '.$student['uname'] . "\n";
  161. $mail = new ATutorMailer;
  162. $mail->From = $_config['contact_email'];
  163. $mail->AddAddress($student['email']);
  164. $mail->Subject = $subject;
  165. $mail->Body = $body;
  166. $mail->Send();
  167. unset($mail);
  168. } else {
  169. $already_enrolled .= '<li>' . $student['uname'] . '</li>';
  170. }
  171. } else {
  172. //$msg->addError('LIST_IMPORT_FAILED');
  173. }
  174. } else if (! $student['err_disabled']) {
  175. $sql = "SELECT member_id FROM ".TABLE_PREFIX."members WHERE email='$student[email]'";
  176. $result = mysql_query($sql, $db);
  177. if ($row = mysql_fetch_assoc($result)) {
  178. $m_id = $row['member_id'];
  179. $sql = "INSERT INTO ".TABLE_PREFIX."course_enrollment (member_id, course_id, approved, last_cid, role) VALUES ($m_id, $course, '$enroll', 0, '$role')";
  180. if($result = mysql_query($sql,$db)) {
  181. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  182. } else {
  183. $sql = "REPLACE INTO ".TABLE_PREFIX."course_enrollment (member_id, course_id, approved, last_cid, role) VALUES ($m_id, $course, '$enroll', 0, '$role')";
  184. $result = mysql_query($sql,$db);
  185. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  186. }
  187. $subject = $_config['site_name'].': '._AT('email_confirmation_subject');
  188. $body = _AT(array('enrol_message_approved',$_SESSION['course_title'],AT_BASE_HREF))."\n\n";
  189. $body .= _AT('web_site') .' : '.AT_BASE_HREF."\n";
  190. $body .= _AT('login_name') .' : '.$student['uname'] . "\n";
  191. $mail = new ATutorMailer;
  192. $mail->From = $_config['contact_email'];
  193. $mail->AddAddress($student['email']);
  194. $mail->Subject = $subject;
  195. $mail->Body = $body;
  196. $mail->Send();
  197. unset($mail);
  198. }
  199. } else if ($student['err_disabled']) {
  200. $not_enrolled_list .= '<li>' . $student['uname'] . '</li>';
  201. }
  202. }
  203. }
  204. if ($already_enrolled) {
  205. $feedback = array('ALREADY_ENROLLED', $already_enrolled);
  206. $msg->addFeedback($feedback);
  207. }
  208. if ($enrolled_list) {
  209. $feedback = array('ENROLLED', $enrolled_list);
  210. $msg->addFeedback($feedback);
  211. }
  212. if ($not_enrolled_list) {
  213. $feedback = array('NOT_ENROLLED', $not_enrolled_list);
  214. $msg->addFeedback($feedback);
  215. }
  216. }
  217. ?>