2 // Implements all the main code for the PayPal plugin
4 require_once("$CFG->dirroot/enrol/enrol.class.php");
7 class enrolment_plugin_paypal
{
10 /// Override the base print_entry() function
11 function print_entry($course) {
15 $strloginto = get_string("loginto", "", $course->shortname
);
16 $strcourses = get_string("courses");
18 $teacher = get_teacher($course->id
);
20 if ( (float) $course->cost
< 0 ) {
21 $cost = (float) $CFG->enrol_cost
;
23 $cost = (float) $course->cost
;
26 if (abs($cost) < 0.01) { // no cost, default to base class entry to course
28 $manual = enrolment_factory
::factory('manual');
29 $manual->print_entry($course);
33 $navlinks[] = array('name' => $strcourses, 'link' => "$CFG->wwwroot/course", 'type' => 'misc');
34 $navlinks[] = array('name' => $strloginto, 'link' => null, 'type' => 'misc');
35 $navigation = build_navigation($navlinks);
37 print_header($strloginto, $course->fullname
, $navigation);
38 print_course($course, "80%");
40 if ($course->password
) { // Presenting two options
41 print_heading(get_string('costorkey', 'enrol_paypal'), 'center');
44 print_simple_box_start("center");
46 if ($USER->username
== 'guest') { // force login only for guest user, not real users with guest role
47 if (empty($CFG->loginhttps
)) {
48 $wwwroot = $CFG->wwwroot
;
50 // This actually is not so secure ;-), 'cause we're
51 // in unencrypted connection...
52 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot
);
54 echo '<div align="center"><p>'.get_string('paymentrequired').'</p>';
55 echo '<p><b>'.get_string('cost').": $CFG->enrol_currency $cost".'</b></p>';
56 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
59 //Sanitise some fields before building the PayPal form
60 $coursefullname = $course->fullname
;
61 $courseshortname = $course->shortname
;
62 $userfullname = fullname($USER);
63 $userfirstname = $USER->firstname
;
64 $userlastname = $USER->lastname
;
65 $useraddress = $USER->address
;
66 $usercity = $USER->city
;
68 include($CFG->dirroot
.'/enrol/paypal/enrol.html');
71 print_simple_box_end();
73 if ($course->password
) { // Second option
75 include($CFG->dirroot
.'/enrol/manual/enrol.html');
81 } // end of function print_entry()
86 /// Override the get_access_icons() function
87 function get_access_icons($course) {
92 if ( (float) $course->cost
< 0) {
93 $cost = (float) $CFG->enrol_cost
;
95 $cost = (float) $course->cost
;
98 if (abs($cost) < 0.01) {
99 $manual = enrolment_factory
::factory('manual');
100 $str = $manual->get_access_icons($course);
104 $strrequirespayment = get_string("requirespayment");
105 $strcost = get_string("cost");
107 if (empty($CFG->enrol_currency
)) {
108 set_config('enrol_currency', 'USD');
111 switch ($CFG->enrol_currency
) {
112 case 'EUR': $currency = '€'; break;
113 case 'CAD': $currency = '$'; break;
114 case 'GBP': $currency = '£'; break;
115 case 'JPY': $currency = '¥'; break;
116 case 'AUD': $currency = '$'; break;
117 default: $currency = '$'; break;
120 $str .= '<div class="cost" title="'.$strrequirespayment.'">'.$strcost.': ';
121 $str .= $currency.format_float($cost,2).'</div>';
129 /// Override the base class config_form() function
130 function config_form($frm) {
133 $paypalcurrencies = array( 'USD' => 'US Dollars',
135 'JPY' => 'Japanese Yen',
136 'GBP' => 'British Pounds',
137 'CAD' => 'Canadian Dollars',
138 'AUD' => 'Australian Dollars'
141 $vars = array('enrol_cost', 'enrol_currency', 'enrol_paypalbusiness',
142 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
143 foreach ($vars as $var) {
144 if (!isset($frm->$var)) {
149 include("$CFG->dirroot/enrol/paypal/config.html");
152 function process_config($config) {
154 if (!isset($config->enrol_cost
)) {
155 $config->enrol_cost
= 0;
157 set_config('enrol_cost', $config->enrol_cost
);
159 if (!isset($config->enrol_currency
)) {
160 $config->enrol_currency
= 'USD';
162 set_config('enrol_currency', $config->enrol_currency
);
164 if (!isset($config->enrol_paypalbusiness
)) {
165 $config->enrol_paypalbusiness
= '';
167 $config->enrol_paypalbusiness
= trim($config->enrol_paypalbusiness
); // remove trailing spaces etc.
168 set_config('enrol_paypalbusiness', $config->enrol_paypalbusiness
);
170 if (!isset($config->enrol_mailstudents
)) {
171 $config->enrol_mailstudents
= '';
173 set_config('enrol_mailstudents', $config->enrol_mailstudents
);
175 if (!isset($config->enrol_mailteachers
)) {
176 $config->enrol_mailteachers
= '';
178 set_config('enrol_mailteachers', $config->enrol_mailteachers
);
180 if (!isset($config->enrol_mailadmins
)) {
181 $config->enrol_mailadmins
= '';
183 set_config('enrol_mailadmins', $config->enrol_mailadmins
);
190 * This function enables internal enrolment when PayPal is primary and course key is set at the same time.
192 * @param form the form data submitted, as an object
193 * @param course the current course, as an object
195 function check_entry($form, $course) {
196 $manual = enrolment_factory
::factory('manual');
197 $manual->check_entry($form, $course);
198 if (isset($manual->errormsg
)) {
199 $this->errormsg
= $manual->errormsg
;
203 } // end of class definition