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