PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/atutor/ATutor
PHP | 260 lines | 196 code | 35 blank | 29 comment | 49 complexity | 5f76899a426ed9339b235f65cdf344da 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 %smembers WHERE email LIKE '%s'";
  35. $rows_members = queryDB($sql,array(TABLE_PREFIX, $record['email']), TRUE);
  36. if(count($rows_members) > 0){
  37. $record['exists'] = _AT('import_err_email_exists');
  38. $record['fname'] = $rows_members['first_name'];
  39. $record['lname'] = $rows_members['last_name'];
  40. $record['email'] = $rows_members['email'];
  41. $record['uname'] = $rows_members['login'];
  42. $record['status'] = $rows_members['status'];
  43. } else {
  44. // it's good, add it to the list
  45. $email_list[$record['email']] = true;
  46. }
  47. }
  48. /* username check */
  49. if (empty($record['uname'])) {
  50. $record['uname'] = stripslashes (strtolower (substr ($record['fname'], 0, 1).$_POST['sep_choice'].$record['lname']));
  51. }
  52. $record['uname'] = preg_replace("{[^a-zA-Z0-9._-]}","", trim($record['uname']));
  53. if (!(preg_match("/^[a-zA-Z0-9._-]([a-zA-Z0-9._-])*$/i", $record['uname']))) {
  54. $record['err_uname'] = _AT('import_err_username_invalid');
  55. }
  56. if (isset($record['status']) && $record['status'] == AT_STATUS_DISABLED) {
  57. $record['err_disabled'] = true;
  58. } else {
  59. $record['err_disabled'] = false;
  60. }
  61. $record['uname'] = $addslashes($record['uname']);
  62. $record['fname'] = $addslashes($record['fname']);
  63. $record['lname'] = $addslashes($record['lname']);
  64. $sql = "SELECT member_id FROM %smembers WHERE login='%s'";
  65. $rows_members = queryDB($sql,array(TABLE_PREFIX, $record['uname']),TRUE);
  66. if(count($rows_members) > 0 && !$record['exists']){
  67. $record['err_uname'] = _AT('import_err_username_exists');
  68. } else {
  69. $rows_admins = queryDB("SELECT * FROM %sadmins WHERE login='%s'", array(TABLE_PREFIX, $record['uname']), TRUE);
  70. if (count($rows_admins) != 0) {
  71. $record['err_uname'] = _AT('import_err_username_exists');
  72. }
  73. }
  74. // This prevent CVS import course list when a person with the same name exists.
  75. /*******
  76. $sql = "SELECT member_id FROM %smembers WHERE first_name='%s' AND last_name='%s' LIMIT 1";
  77. $rows_members = queryDB($sql, array(TABLE_PREFIX, $record['fname'], $record['lname']), TRUE);
  78. if(count($rows_members) != 0 && !$record['exists']){
  79. $record['err_uname'] = _AT('import_err_full_name_exists');
  80. }
  81. ******/
  82. /* removed record? */
  83. if ($record['remove']) {
  84. //unset errors
  85. $record['err_email'] = '';
  86. $record['err_uname'] = '';
  87. $record['err_disabled'] = '';
  88. }
  89. $record['fname'] = htmlspecialchars(stripslashes(trim($record['fname'])));
  90. $record['lname'] = htmlspecialchars(stripslashes(trim($record['lname'])));
  91. $record['email'] = htmlspecialchars(stripslashes(trim($record['email'])));
  92. $record['uname'] = htmlspecialchars(stripslashes(trim($record['uname'])));
  93. return $record;
  94. }
  95. function add_users($user_list, $enroll, $course) {
  96. global $db;
  97. global $msg;
  98. global $_config;
  99. global $addslashes;
  100. require_once(AT_INCLUDE_PATH.'classes/phpmailer/atutormailer.class.php');
  101. if (defined('AT_EMAIL_CONFIRMATION') && AT_EMAIL_CONFIRMATION) {
  102. $status = AT_STATUS_UNCONFIRMED;
  103. } else {
  104. $status = AT_STATUS_STUDENT;
  105. }
  106. foreach ($user_list as $student) {
  107. if ($student['remove'] == '') {
  108. $student['uname'] = $addslashes($student['uname']);
  109. $student['email'] = $addslashes($student['email']);
  110. $student['fname'] = $addslashes($student['fname']);
  111. $student['lname'] = $addslashes($student['lname']);
  112. if ($student['exists'] == '') {
  113. $sql = "INSERT INTO %smembers
  114. (login,
  115. password,
  116. email,
  117. first_name,
  118. last_name,
  119. gender,
  120. status,
  121. preferences,
  122. creation_date,
  123. language,
  124. inbox_notify,
  125. private_email)
  126. VALUES
  127. ('$student[uname]',
  128. '". sha1($student[uname]). "',
  129. '$student[email]',
  130. '$student[fname]',
  131. '$student[lname]',
  132. 'n',
  133. $status,
  134. '$_config[pref_defaults]',
  135. NOW(),
  136. '$_config[default_language]',
  137. 0,
  138. 1)";
  139. $result = queryDB($sql,array(TABLE_PREFIX));
  140. if ($result == 1) {
  141. $m_id = at_insert_id();
  142. $student['exists'] = _AT('import_err_email_exists');
  143. $role = "Student";
  144. $sql = "INSERT INTO %scourse_enrollment (member_id, course_id, approved, last_cid, role) VALUES (%d, %d, '%s', 0, '%s')";
  145. $result = queryDB($sql, array(TABLE_PREFIX, $m_id, $course, $enroll, $role));
  146. if($result > 0){
  147. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  148. if (defined('AT_EMAIL_CONFIRMATION') && AT_EMAIL_CONFIRMATION) {
  149. $sql = "SELECT email, creation_date FROM %smembers WHERE member_id=%d";
  150. $row = queryDB($sql, array(TABLE_PREFIX, $m_id), TRUE);
  151. $code = substr(md5($row['email'] . $row['creation_date'] . $m_id), 0, 10);
  152. // send email here.
  153. $confirmation_link = AT_BASE_HREF . 'confirm.php?id='.$m_id.SEP.'m='.$code;
  154. $subject = $_config['site_name'].': '._AT('email_confirmation_subject');
  155. $body = _AT(array('new_account_enroll_confirm', $_SESSION['course_title'], $confirmation_link))."\n\n";
  156. } else {
  157. $subject = $_config['site_name'].': '._AT('account_information');
  158. $body = _AT(array('new_account_enroll',AT_BASE_HREF, $_SESSION['course_title']))."\n\n";
  159. }
  160. //$body .= SITE_NAME.': '._AT('account_information')."\n";
  161. $body .= _AT('web_site') .' : '.AT_BASE_HREF."\n";
  162. $body .= _AT('login_name') .' : '.$student['uname'] . "\n";
  163. $body .= _AT('password') .' : '.$student['uname'] . "\n";
  164. $mail = new ATutorMailer;
  165. $mail->From = $_config['contact_email'];
  166. $mail->AddAddress($student['email']);
  167. $mail->Subject = $subject;
  168. $mail->Body = $body;
  169. $mail->Send();
  170. unset($mail);
  171. } else {
  172. $already_enrolled .= '<li>' . $student['uname'] . '</li>';
  173. }
  174. } else {
  175. //$msg->addError('LIST_IMPORT_FAILED');
  176. }
  177. } else if ($student['err_disabled'] == '') {
  178. $sql = "SELECT member_id FROM %smembers WHERE email='%s'";
  179. $rows_members = queryDB($sql, array(TABLE_PREFIX, $student['email']), TRUE);
  180. $role = "Student";
  181. if(count($rows_members) >0){
  182. $row = $rows_members;
  183. $m_id = $row['member_id'];
  184. $sql = "SELECT member_id FROM %smembers WHERE member_id =".$m_id;
  185. $result = queryDB($sql, array(TABLE_PREFIX, $m_id), TRUE);
  186. if(!is_array($result)){
  187. $sql = "INSERT INTO %scourse_enrollment (member_id, course_id, approved, last_cid, role) VALUES (%d, %d, '%s', 0, '%s')";
  188. $result = queryDB($sql, array(TABLE_PREFIX, $m_id, $course, $enroll, $role));
  189. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  190. } else {
  191. $sql = "REPLACE INTO %scourse_enrollment (member_id, course_id, approved, last_cid, role) VALUES (%d, %s, '%s', 0, '%s')";
  192. $result = queryDB($sql, array(TABLE_PREFIX, $m_id, $course, $enroll, $role));
  193. $enrolled_list .= '<li>' . $student['uname'] . '</li>';
  194. }
  195. $subject = $_config['site_name'].': '._AT('email_confirmation_subject');
  196. $body = _AT(array('enrol_message_approved',$_SESSION['course_title'],AT_BASE_HREF))."\n\n";
  197. $body .= _AT('web_site') .' : '.AT_BASE_HREF."\n";
  198. $body .= _AT('login_name') .' : '.$student['uname'] . "\n";
  199. $mail = new ATutorMailer;
  200. $mail->From = $_config['contact_email'];
  201. $mail->AddAddress($student['email']);
  202. $mail->Subject = $subject;
  203. $mail->Body = $body;
  204. $mail->Send();
  205. unset($mail);
  206. }
  207. } else if ($student['err_disabled'] != '') {
  208. $not_enrolled_list .= '<li>' . $student['uname'] . '</li>';
  209. }
  210. }
  211. }
  212. if ($already_enrolled) {
  213. $feedback = array('ALREADY_ENROLLED', $already_enrolled);
  214. $msg->addFeedback($feedback);
  215. }
  216. if ($enrolled_list) {
  217. $feedback = array('ENROLLED', $enrolled_list);
  218. $msg->addFeedback($feedback);
  219. }
  220. if ($not_enrolled_list) {
  221. $feedback = array('NOT_ENROLLED', $not_enrolled_list);
  222. $msg->addFeedback($feedback);
  223. }
  224. }
  225. ?>