PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/enrol/authorize/uploadcsv.php

https://github.com/nadavkav/Moodle-RTL--Shenkar-Translation-Team-
PHP | 238 lines | 191 code | 28 blank | 19 comment | 50 complexity | 56490a5c54d8a31edfc8c7a22f22018f MD5 | raw file
  1. <?php // $Id: uploadcsv.php,v 1.12.2.2 2008/02/07 16:27:53 ethem Exp $
  2. /// Load libraries
  3. require_once('../../config.php');
  4. require_once($CFG->libdir.'/uploadlib.php');
  5. require_once($CFG->dirroot.'/enrol/authorize/const.php');
  6. require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
  7. /// Require capabilites
  8. require_login();
  9. require_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id));
  10. /// Print header
  11. $struploadcsv = get_string('uploadcsv', 'enrol_authorize');
  12. $navlinks = array();
  13. $navlinks[] = array('name' => $struploadcsv, 'link' => "uploadcsv.php", 'type' => 'misc');
  14. $navigation = build_navigation($navlinks);
  15. print_header_simple($struploadcsv, "", $navigation);
  16. print_heading_with_help($struploadcsv, 'uploadcsv', 'enrol/authorize');
  17. /// Handle CSV file
  18. if (($form = data_submitted()) && confirm_sesskey()) {
  19. $um = new upload_manager('csvfile', false, false, null, false, 0);
  20. if ($um->preprocess_files()) {
  21. $filename = $um->files['csvfile']['tmp_name'];
  22. // Fix mac/dos newlines
  23. $text = file_get_contents($filename);
  24. $text = preg_replace('!\r\n?!', "\n", $text);
  25. $fp = fopen($filename, "w");
  26. fwrite($fp, $text);
  27. fclose($fp);
  28. authorize_process_csv($filename);
  29. }
  30. }
  31. /// Print submit form
  32. $maxuploadsize = get_max_upload_file_size();
  33. echo '<center><form method="post" enctype="multipart/form-data" action="uploadcsv.php">
  34. <input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />
  35. <input type="hidden" name="sesskey" value="'.$USER->sesskey.'">';
  36. upload_print_form_fragment(1, array('csvfile'), array(get_string('file')));
  37. echo '<input type="submit" value="'.get_string('upload').'" />';
  38. echo '</form></center><br />';
  39. /// Print footer
  40. print_footer();
  41. ?><?php
  42. function authorize_process_csv($filename)
  43. {
  44. global $CFG, $SITE;
  45. /// We need these fields
  46. $myfields = array(
  47. 'Transaction ID', // enrol_authorize.transid or enrol_authorize_refunds.transid; See: Reference Transaction ID
  48. 'Transaction Status', // Under Review,Approved Review,Review Failed,Settled Successfully
  49. 'Transaction Type', // Authorization w/ Auto Capture, Authorization Only, Capture Only, Credit, Void, Prior Authorization Capture
  50. 'Settlement Amount', //
  51. 'Settlement Currency', //
  52. 'Settlement Date/Time', //
  53. 'Authorization Amount', //
  54. 'Authorization Currency', //
  55. 'Submit Date/Time', // timecreated
  56. 'Reference Transaction ID', // enrol_authorize.transid if Transaction Type = Credit
  57. 'Total Amount', // enrol_authorize.cost
  58. 'Currency', // enrol_authorize.currency
  59. 'Invoice Number', // enrol_authorize.id: Don't trust this! Backup/Restore changes this
  60. 'Customer ID' // enrol_authorize.userid
  61. );
  62. /// Open the file and get first line
  63. $handle = fopen($filename, "r");
  64. if (!$handle) {
  65. error('CANNOT OPEN CSV FILE');
  66. }
  67. $firstline = fgetcsv($handle, 8192, ",");
  68. $numfields = count($firstline);
  69. if ($numfields != 49 && $numfields != 70) {
  70. @fclose($handle);
  71. error('INVALID CSV FILE; Each line must include 49 or 70 fields');
  72. }
  73. /// Re-sort fields
  74. $csvfields = array();
  75. foreach ($myfields as $myfield) {
  76. $csvindex = array_search($myfield, $firstline);
  77. if ($csvindex === false) {
  78. $csvfields = array();
  79. break;
  80. }
  81. $csvfields[$myfield] = $csvindex;
  82. }
  83. if (empty($csvfields)) {
  84. @fclose($handle);
  85. error("<b>INVALID CSV FILE:</b> First line must include 'Header Fields' and
  86. the file must be type of <br />'Expanded Fields/Comma Separated'<br />or<br />
  87. 'Expanded Fields with CAVV Result Code/Comma Separated'");
  88. }
  89. /// Read lines
  90. $sendem = array();
  91. $ignoredlines = '';
  92. $imported = 0;
  93. $updated = 0;
  94. $ignored = 0;
  95. while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
  96. if (count($data) != $numfields) {
  97. $ignored++; // ignore empty lines
  98. continue;
  99. }
  100. $transid = $data[$csvfields['Transaction ID']];
  101. $transtype = $data[$csvfields['Transaction Type']];
  102. $transstatus = $data[$csvfields['Transaction Status']];
  103. $reftransid = $data[$csvfields['Reference Transaction ID']];
  104. $settlementdate = strtotime($data[$csvfields['Settlement Date/Time']]);
  105. if ($transstatus == 'Approved Review' || $transstatus == 'Review Failed') {
  106. if (($order = get_record('enrol_authorize', 'transid', $transid))) {
  107. $order->status = ($transstatus == 'Approved Review') ? AN_STATUS_APPROVEDREVIEW : AN_STATUS_REVIEWFAILED;
  108. update_record('enrol_authorize', $order);
  109. $updated++; // Updated order status
  110. }
  111. continue;
  112. }
  113. if (!empty($reftransid) && is_numeric($reftransid) && 'Settled Successfully' == $transstatus && 'Credit' == $transtype) {
  114. if (($order = get_record('enrol_authorize', 'transid', $reftransid))) {
  115. if (AN_METHOD_ECHECK == $order->paymentmethod) {
  116. $refund = get_record('enrol_authorize_refunds', 'transid', $transid);
  117. if ($refund) {
  118. $refund->status = AN_STATUS_CREDIT;
  119. $refund->settletime = $settlementdate;
  120. update_record('enrol_authorize_refunds', $refund);
  121. $updated++;
  122. }
  123. else {
  124. $ignored++;
  125. $ignoredlines .= $reftransid . ": Not our business(Reference Transaction ID)\n";
  126. }
  127. }
  128. }
  129. else {
  130. $ignored++;
  131. $ignoredlines .= $reftransid . ": Not our business(Transaction ID)\n";
  132. }
  133. continue;
  134. }
  135. if (! ($transstatus == 'Settled Successfully' && $transtype == 'Authorization w/ Auto Capture')) {
  136. $ignored++;
  137. $ignoredlines .= $transid . ": Not settled\n";
  138. continue;
  139. }
  140. // TransactionId must match
  141. $order = get_record('enrol_authorize', 'transid', $transid);
  142. if (!$order) {
  143. $ignored++;
  144. $ignoredlines .= $transid . ": Not our business\n";
  145. continue;
  146. }
  147. // Authorized/Captured and Settled
  148. $order->status = AN_STATUS_AUTHCAPTURE;
  149. $order->settletime = $settlementdate;
  150. update_record('enrol_authorize', $order);
  151. $updated++; // Updated order status and settlement date
  152. if ($order->paymentmethod != AN_METHOD_ECHECK) {
  153. $ignored++;
  154. $ignoredlines .= $transid . ": The method must be echeck\n";
  155. continue;
  156. }
  157. // Get course and context
  158. $course = get_record('course', 'id', $order->courseid);
  159. if (!$course) {
  160. $ignored++;
  161. $ignoredlines .= $transid . ": Could not find this course: " . $order->courseid . "\n";
  162. continue;
  163. }
  164. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  165. if (!$coursecontext) {
  166. $ignored++;
  167. $ignoredlines .= $transid . ": Could not find course context: " . $order->courseid . "\n";
  168. continue;
  169. }
  170. // Get user
  171. $user = get_record('user', 'id', $order->userid);
  172. if (!$user) {
  173. $ignored++;
  174. $ignoredlines .= $transid . ": Could not find this user: " . $order->userid . "\n";
  175. continue;
  176. }
  177. // If user wasn't enrolled, enrol now. Ignore otherwise. Because admin user might submit this file again.
  178. if (($role = get_default_course_role($course))) {
  179. if (! user_has_role_assignment($user->id, $role->id, $coursecontext->id)) {
  180. $timestart = $timeend = 0;
  181. if ($course->enrolperiod) {
  182. $timestart = time();
  183. $timeend = $timestart + $course->enrolperiod;
  184. }
  185. if (role_assign($role->id, $user->id, 0, $coursecontext->id, $timestart, $timeend, 0, 'authorize')) {
  186. $imported++;
  187. if (!empty($CFG->enrol_mailstudents)) {
  188. $sendem[] = $order->id;
  189. }
  190. }
  191. else {
  192. $ignoredlines .= $transid . ": Error while trying to enrol " . fullname($user) . " in '$course->fullname' \n";
  193. }
  194. }
  195. }
  196. }
  197. fclose($handle);
  198. /// Send email to admin
  199. if (!empty($ignoredlines)) {
  200. $admin = get_admin();
  201. email_to_user($admin, $admin, "$SITE->fullname: Authorize.net CSV ERROR LOG", $ignoredlines);
  202. }
  203. /// Send welcome messages to users
  204. if (!empty($sendem)) {
  205. send_welcome_messages($sendem);
  206. }
  207. /// Show result
  208. notice("<b>Done...</b><br />Imported: $imported<br />Updated: $updated<br />Ignored: $ignored");
  209. }
  210. ?>