Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / enrol / authorize / localfuncs.php
blob2ba4a3973d57e5ac8fa7390fd617006ef56c16b6
1 <?php // $Id$
3 function get_course_cost($course)
5 global $CFG;
7 $cost = (float)0;
8 $currency = (!empty($course->currency))
9 ? $course->currency :( empty($CFG->enrol_currency)
10 ? 'USD' : $CFG->enrol_currency );
12 if (!empty($course->cost)) {
13 $cost = (float)(((float)$course->cost) < 0) ? $CFG->enrol_cost : $course->cost;
16 $cost = format_float($cost, 2);
17 $ret = array(
18 'cost' => $cost,
19 'currency' => $currency
22 return $ret;
25 function zero_cost($course) {
26 $curcost = get_course_cost($course);
27 return (abs($curcost['cost']) < 0.01);
30 function prevent_double_paid($course)
32 global $CFG, $SESSION, $USER;
34 $sql = "SELECT id FROM {$CFG->prefix}enrol_authorize WHERE userid = '$USER->id' AND courseid = '$course->id' ";
36 if (empty($CFG->an_test)) { // Real mode
37 $sql .= 'AND status IN('.AN_STATUS_AUTH.','.AN_STATUS_UNDERREVIEW.','.AN_STATUS_APPROVEDREVIEW.')';
39 else { // Test mode
40 $sql .= 'AND status='.AN_STATUS_NONE;
43 if (($recid = get_field_sql($sql))) {
44 $a = new stdClass;
45 $a->orderid = $recid;
46 $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
47 redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10');
48 return;
50 if (isset($SESSION->ccpaid)) {
51 unset($SESSION->ccpaid);
52 redirect($CFG->wwwroot . '/login/logout.php?sesskey='.sesskey());
53 return;
57 function get_list_of_creditcards($getall = false)
59 global $CFG;
61 $alltypes = array(
62 'mcd' => 'Master Card',
63 'vis' => 'Visa',
64 'amx' => 'American Express',
65 'dsc' => 'Discover',
66 'dnc' => 'Diners Club',
67 'jcb' => 'JCB',
68 'swi' => 'Switch',
69 'dlt' => 'Delta',
70 'enr' => 'EnRoute'
73 if ($getall or empty($CFG->an_acceptccs)) {
74 return $alltypes;
77 $ret = array();
78 $ccs = explode(',', $CFG->an_acceptccs);
79 foreach ($ccs as $key) {
80 $ret[$key] = $alltypes[$key];
82 return $ret;
85 function get_list_of_payment_methods($getall = false)
87 global $CFG;
89 if ($getall || empty($CFG->an_acceptmethods)) {
90 return array(AN_METHOD_CC, AN_METHOD_ECHECK);
92 else {
93 return explode(',', $CFG->an_acceptmethods);
97 function get_list_of_bank_account_types($getall = false)
99 global $CFG;
101 if ($getall || empty($CFG->an_acceptechecktypes)) {
102 return array('CHECKING', 'BUSINESSCHECKING', 'SAVINGS');
104 else {
105 return explode(',', $CFG->an_acceptechecktypes);
109 function email_to_admin($subject, $data)
111 global $SITE;
113 $admin = get_admin();
114 $data = (array)$data;
116 $message = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
117 $message .= print_r($data, true);
118 email_to_user($admin, $admin, "$SITE->fullname: Authorize.net ERROR", $message);
121 function send_welcome_messages($orderdata)
123 global $CFG, $SITE;
125 if (empty($orderdata)) {
126 return;
129 if (is_numeric($orderdata)) {
130 $orderdata = array($orderdata);
133 $sql = "SELECT e.id, e.courseid, e.userid, c.fullname
134 FROM {$CFG->prefix}enrol_authorize e
135 INNER JOIN {$CFG->prefix}course c ON c.id = e.courseid
136 WHERE e.id IN(" . implode(',', $orderdata) . ")
137 ORDER BY e.userid";
139 if (($rs = get_recordset_sql($sql)) && ($ei = rs_fetch_next_record($rs)))
141 if (1 < count($orderdata)) {
142 $sender = get_admin();
144 else {
145 $context = get_context_instance(CONTEXT_COURSE, $ei->courseid);
146 $paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1');
147 $sender = array_shift($paymentmanagers);
152 $usercourses = array();
153 $lastuserid = $ei->userid;
155 while ($ei && $ei->userid == $lastuserid) {
156 $usercourses[] = $ei->fullname;
157 $ei = rs_fetch_next_record($rs);
160 if (($user = get_record('user', 'id', $lastuserid))) {
161 $a = new stdClass;
162 $a->name = $user->firstname;
163 $a->courses = implode("\n", $usercourses);
164 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$lastuserid";
165 $a->paymenturl = "$CFG->wwwroot/enrol/authorize/index.php?user=$lastuserid";
166 $emailmessage = get_string('welcometocoursesemail', 'enrol_authorize', $a);
167 @email_to_user($user, $sender, get_string("enrolmentnew", '', $SITE->shortname), $emailmessage);
170 while ($ei);
172 rs_close($rs);
176 function check_openssl_loaded()
178 return extension_loaded('openssl');