Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / enrol / authorize / enrol_form.php
blob2549d5aa03dd03c5645434c81ba677ad7516d33c
1 <?php // $Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class enrol_authorize_form extends moodleform
7 function definition()
9 global $CFG, $USER;
11 $paymentmethodsenabled = get_list_of_payment_methods();
12 $paymentmethod = optional_param('paymentmethod', $paymentmethodsenabled[0], PARAM_ALPHA);
13 if (!in_array($paymentmethod, $paymentmethodsenabled)) {
14 error("Invalid payment method: $paymentmethod");
17 $mform =& $this->_form;
18 $course = $this->_customdata['course'];
20 $mform->addElement('header', '', '&nbsp;&nbsp;' . get_string('paymentrequired'), '');
21 if ($othermethodstr = $this->other_method_available($paymentmethod)) {
22 $mform->addElement('static', '', '<div align="right">' . $othermethodstr . '&nbsp;&nbsp;</div>', '');
25 $mform->addElement('hidden', 'id', $course->id);
26 $mform->setType('id', PARAM_INT);
28 $mform->addElement('hidden', 'paymentmethod', $paymentmethod);
29 $mform->setType('paymentmethod', PARAM_ALPHA);
31 $firstlastnamestr = (AN_METHOD_CC == $paymentmethod) ?
32 get_string('nameoncard', 'enrol_authorize') : get_string('echeckfirslasttname', 'enrol_authorize');
33 $firstlastnamegrp = array();
34 $firstlastnamegrp[] = &MoodleQuickForm::createElement('text', 'firstname', '', 'size="8"');
35 $firstlastnamegrp[] = &MoodleQuickForm::createElement('text', 'lastname', '', 'size="8"');
36 $mform->addGroup($firstlastnamegrp, 'firstlastgrp', $firstlastnamestr, '&nbsp;', false);
37 $firstlastnamegrprules = array();
38 $firstlastnamegrprules['firstname'][] = array(get_string('missingfirstname'), 'required', null, 'client');
39 $firstlastnamegrprules['lastname'][] = array(get_string('missinglastname'), 'required', null, 'client');
40 $mform->addGroupRule('firstlastgrp', $firstlastnamegrprules);
41 $mform->setType('firstname', PARAM_ALPHANUM);
42 $mform->setType('lastname', PARAM_ALPHANUM);
43 $mform->setDefault('firstname', $USER->firstname);
44 $mform->setDefault('lastname', $USER->lastname);
46 if (AN_METHOD_CC == $paymentmethod)
48 $mform->addElement('text', 'cc', get_string('ccno', 'enrol_authorize'), 'size="20"');
49 $mform->setType('cc', PARAM_ALPHANUM);
50 $mform->setDefault('cc', '');
51 $mform->addRule('cc', get_string('missingcc', 'enrol_authorize'), 'required', null, 'client');
52 $mform->addRule('cc', get_string('ccinvalid', 'enrol_authorize'), 'numeric', null, 'client');
54 $creditcardsmenu = array('' => get_string('choose')) + get_list_of_creditcards();
55 $mform->addElement('select', 'cctype', get_string('cctype', 'enrol_authorize'), $creditcardsmenu);
56 $mform->setType('cctype', PARAM_ALPHA);
57 $mform->addRule('cctype', get_string('missingcctype', 'enrol_authorize'), 'required', null, 'client');
58 $mform->setDefault('cctype', '');
60 $monthsmenu = array('' => get_string('choose'));
61 for ($i = 1; $i <= 12; $i++) {
62 $monthsmenu[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B");
64 $yearsmenu = array('' => get_string('choose'));
65 $nowdate = getdate();
66 $nowyear = $nowdate["year"] - 1;
67 for ($i = $nowyear; $i <= $nowyear + 11; $i++) {
68 $yearsmenu[$i] = $i;
70 $ccexpiregrp = array();
71 $ccexpiregrp[] = &MoodleQuickForm::createElement('select', 'ccexpiremm', '', $monthsmenu);
72 $ccexpiregrp[] = &MoodleQuickForm::createElement('select', 'ccexpireyyyy', '', $yearsmenu);
73 $mform->addGroup($ccexpiregrp, 'ccexpiregrp', get_string('ccexpire', 'enrol_authorize'), '&nbsp;', false);
74 $ccexpiregrprules = array();
75 $ccexpiregrprules['ccexpiremm'][] = array(get_string('missingccexpire', 'enrol_authorize'), 'required', null, 'client');
76 $ccexpiregrprules['ccexpireyyyy'][] = array(get_string('missingccexpire', 'enrol_authorize'), 'required', null, 'client');
77 $mform->addGroupRule('ccexpiregrp', $ccexpiregrprules);
78 $mform->setType('ccexpiremm', PARAM_INT);
79 $mform->setType('ccexpireyyyy', PARAM_INT);
80 $mform->setDefault('ccexpiremm', '');
81 $mform->setDefault('ccexpireyyyy', '');
83 $mform->addElement('text', 'cvv', get_string('ccvv', 'enrol_authorize'), 'size="4"');
84 $mform->setHelpButton('cvv', array('cvv',get_string('ccvv', 'enrol_authorize'),'enrol/authorize'), true);
85 $mform->setType('cvv', PARAM_ALPHANUM);
86 $mform->setDefault('cvv', '');
87 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'required', null, 'client');
88 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'numeric', null, 'client');
90 if (!empty($CFG->an_authcode)) {
91 $ccauthgrp = array();
92 $ccauthgrp[] = &MoodleQuickForm::createElement('checkbox', 'haveauth', null, get_string('haveauthcode', 'enrol_authorize'));
93 $ccauthgrp[] = &MoodleQuickForm::createElement('static', 'nextline', null, '<br />');
94 $ccauthgrp[] = &MoodleQuickForm::createElement('text', 'ccauthcode', '', 'size="8"');
95 $mform->addGroup($ccauthgrp, 'ccauthgrp', get_string('authcode', 'enrol_authorize'), '&nbsp;', false);
96 $mform->setHelpButton('ccauthgrp', array('authcode',get_string('authcode', 'enrol_authorize'),'enrol/authorize'), true);
97 $ccauthgrprules = array();
98 $ccauthgrprules['ccauthcode'][] = array(get_string('missingccauthcode', 'enrol_authorize'), 'numeric', null, 'client');
99 $mform->addGroupRule('ccauthgrp', $ccauthgrprules);
100 $mform->setDefault('haveauth', '');
101 $mform->setDefault('ccauthcode', '');
104 if (!empty($CFG->an_avs)) {
105 $mform->addElement('header', '', '&nbsp;&nbsp;' . get_string('address'), '');
107 $mform->addElement('text', 'ccaddress', get_string('address'), 'size="30"');
108 $mform->setType('ccaddress', PARAM_ALPHANUM);
109 $mform->setDefault('ccaddress', $USER->address);
110 $mform->addRule('ccaddress', get_string('missingaddress', 'enrol_authorize'), 'required', null, 'client');
112 $citystategrp = array();
113 $citystategrp[] = &MoodleQuickForm::createElement('text', 'cccity', '', 'size="14"');
114 $citystategrp[] = &MoodleQuickForm::createElement('static', 'sep', null, ' - ');
115 $citystategrp[] = &MoodleQuickForm::createElement('text', 'ccstate', '', 'size="8"');
116 $mform->addGroup($citystategrp, 'citystategrp', get_string('city') . ' - ' . get_string('state'), '&nbsp;', false);
117 $citystategrprules = array();
118 $citystategrprules['cccity'][] = array(get_string('missingcity'), 'required', null, 'client');
119 $mform->addGroupRule('citystategrp', $citystategrprules);
120 $mform->setType('cccity', PARAM_ALPHANUM);
121 $mform->setType('ccstate', PARAM_ALPHANUM);
122 $mform->setDefault('cccity', $USER->city);
123 $mform->setDefault('ccstate', '');
125 $mform->addElement('select', 'cccountry', get_string('country'), get_list_of_countries());
126 $mform->addRule('cccountry', get_string('missingcountry'), 'required', null, 'client');
127 $mform->setType('cccountry', PARAM_ALPHA);
128 $mform->setDefault('cccountry', $USER->country);
130 else {
131 $mform->addElement('hidden', 'ccstate', '');
132 $mform->addElement('hidden', 'ccaddress', $USER->address);
133 $mform->addElement('hidden', 'cccity', $USER->city);
134 $mform->addElement('hidden', 'cccountry', $USER->country);
137 elseif (AN_METHOD_ECHECK == $paymentmethod)
139 $mform->addElement('text', 'abacode', get_string('echeckabacode', 'enrol_authorize'), 'size="9" maxlength="9"');
140 $mform->setHelpButton('abacode', array('aba',get_string('echeckabacode', 'enrol_authorize'),'enrol/authorize'), true);
141 $mform->setType('abacode', PARAM_ALPHANUM);
142 $mform->setDefault('abacode', '');
143 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'required', null, 'client');
144 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'numeric', null, 'client');
146 $mform->addElement('text', 'accnum', get_string('echeckaccnum', 'enrol_authorize'), 'size="20" maxlength="20"');
147 $mform->setType('accnum', PARAM_ALPHANUM);
148 $mform->setDefault('accnum', '');
149 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'required', null, 'client');
150 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'numeric', null, 'client');
152 $acctypes = array();
153 $acctypesenabled = get_list_of_bank_account_types();
154 foreach ($acctypesenabled as $key) {
155 $acctypes[$key] = get_string("echeck".strtolower($key), "enrol_authorize");
157 $acctypes = array('' => get_string('choose')) + $acctypes;
158 $mform->addElement('select', 'acctype', get_string('echeckacctype', 'enrol_authorize'), $acctypes);
159 $mform->setType('acctype', PARAM_ALPHA);
160 $mform->addRule('acctype', get_string('invalidacctype', 'enrol_authorize'), 'required', null, 'client');
161 $mform->setDefault('acctype', '');
163 $mform->addElement('text', 'bankname', get_string('echeckbankname', 'enrol_authorize'), 'size="20" maxlength="50"');
164 $mform->setType('bankname', PARAM_ALPHANUM);
165 $mform->setDefault('bankname', '');
166 $mform->addRule('bankname', get_string('missingbankname', 'enrol_authorize'), 'required', null, 'client');
169 $mform->addElement('text', 'cczip', get_string('zipcode', 'enrol_authorize'), 'size="5"');
170 $mform->setType('cczip', PARAM_ALPHANUM);
171 $mform->setDefault('cczip', '');
172 $mform->addRule('cczip', get_string('missingzip', 'enrol_authorize'), 'required', null, 'client');
173 $mform->addRule('cczip', get_string('missingzip', 'enrol_authorize'), 'numeric', null, 'client');
175 $this->add_action_buttons(false, get_string('sendpaymentbutton', 'enrol_authorize'));
178 function validation($data)
180 global $CFG;
182 $errors = array();
184 if (AN_METHOD_CC == $data['paymentmethod'])
186 if (!in_array($data['cctype'], array_keys(get_list_of_creditcards()))) {
187 $errors['cctype'] = get_string('missingcctype', 'enrol_authorize');
190 $expdate = sprintf("%02d", intval($data['ccexpiremm'])) . $data['ccexpireyyyy'];
191 $validcc = $this->validate_cc($data['cc'], $data['cctype'], $expdate);
192 if (!$validcc) {
193 if ($validcc === 0) {
194 $errors['ccexpiregrp'] = get_string('ccexpired', 'enrol_authorize');
196 else {
197 $errors['cc'] = get_string('ccinvalid', 'enrol_authorize');
201 if (!empty($CFG->an_authcode) && !empty($data['haveauth']) && empty($data['ccauthcode'])) {
202 $errors['ccauthgrp'] = get_string('missingccauthcode', 'enrol_authorize');
205 elseif (AN_METHOD_ECHECK == $data['paymentmethod'])
207 if (!$this->validate_aba($data['abacode'])) {
208 $errors['abacode'] = get_string('invalidaba', 'enrol_authorize');
211 if (!in_array($data['acctype'], get_list_of_bank_account_types())) {
212 $errors['acctype'] = get_string('invalidacctype', 'enrol_authorize');
216 return (empty($errors) ? true : $errors);
219 function other_method_available($currentmethod)
221 $course = $this->_customdata['course'];
223 if ($currentmethod == AN_METHOD_CC) {
224 $otheravailable = in_array(AN_METHOD_ECHECK, get_list_of_payment_methods());
225 $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_ECHECK;
226 $stringtofetch = 'usingecheckmethod';
228 else {
229 $otheravailable = in_array(AN_METHOD_CC, get_list_of_payment_methods());
230 $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_CC;
231 $stringtofetch = 'usingccmethod';
233 if ($otheravailable) {
234 $a = new stdClass;
235 $a->url = $url;
236 return get_string($stringtofetch, "enrol_authorize", $a);
238 else {
239 return '';
243 function validate_aba($aba)
245 if (ereg("^[0-9]{9}$", $aba)) {
246 $n = 0;
247 for($i = 0; $i < 9; $i += 3) {
248 $n += (substr($aba, $i, 1) * 3) + (substr($aba, $i + 1, 1) * 7) + (substr($aba, $i + 2, 1));
250 if ($n != 0 and $n % 10 == 0) {
251 return true;
254 return false;
257 function validate_cc($Num, $Name = "n/a", $Exp = "")
259 // Check the expiration date first
260 if (strlen($Exp))
262 $Month = substr($Exp, 0, 2);
263 $Year = substr($Exp, -2);
264 $WorkDate = "$Month/01/$Year";
265 $WorkDate = strtotime($WorkDate);
266 $LastDay = date("t", $WorkDate);
267 $Expires = strtotime("$Month/$LastDay/$Year 11:59:59");
268 if ($Expires < time()) return 0;
271 // Innocent until proven guilty
272 $GoodCard = true;
274 // Get rid of any non-digits
275 $Num = ereg_replace("[^0-9]", "", $Num);
277 // Perform card-specific checks, if applicable
278 switch ($Name)
280 case "mcd" :
281 $GoodCard = ereg("^5[1-5].{14}$", $Num);
282 break;
284 case "vis" :
285 $GoodCard = ereg("^4.{15}$|^4.{12}$", $Num);
286 break;
288 case "amx" :
289 $GoodCard = ereg("^3[47].{13}$", $Num);
290 break;
292 case "dsc" :
293 $GoodCard = ereg("^6011.{12}$", $Num);
294 break;
296 case "dnc" :
297 $GoodCard = ereg("^30[0-5].{11}$|^3[68].{12}$", $Num);
298 break;
300 case "jcb" :
301 $GoodCard = ereg("^3.{15}$|^2131|1800.{11}$", $Num);
302 break;
304 case "dlt" :
305 $GoodCard = ereg("^4.{15}$", $Num);
306 break;
308 case "swi" :
309 $GoodCard = ereg("^[456].{15}$|^[456].{17,18}$", $Num);
310 break;
312 case "enr" :
313 $GoodCard = ereg("^2014.{11}$|^2149.{11}$", $Num);
314 break;
317 // The Luhn formula works right to left, so reverse the number.
318 $Num = strrev($Num);
319 $Total = 0;
321 for ($x=0; $x < strlen($Num); $x++)
323 $digit = substr($Num, $x, 1);
325 // If it's an odd digit, double it
326 if ($x/2 != floor($x/2)) {
327 $digit *= 2;
329 // If the result is two digits, add them
330 if (strlen($digit) == 2)
331 $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
333 // Add the current digit, doubled and added if applicable, to the Total
334 $Total += $digit;
337 // If it passed (or bypassed) the card-specific check and the Total is
338 // evenly divisible by 10, it's cool!
339 return ($GoodCard && $Total % 10 == 0);