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>';
128 /// Override the base class config_form() function
129 function config_form($frm) {
132 $paypalcurrencies = array( 'USD' => 'US Dollars',
134 'JPY' => 'Japanese Yen',
135 'GBP' => 'British Pounds',
136 'CAD' => 'Canadian Dollars',
137 'AUD' => 'Australian Dollars'
140 $vars = array('enrol_cost', 'enrol_currency', 'enrol_paypalbusiness',
141 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
142 foreach ($vars as $var) {
143 if (!isset($frm->$var)) {
148 include("$CFG->dirroot/enrol/paypal/config.html");
151 function process_config($config) {
153 if (!isset($config->enrol_cost
)) {
154 $config->enrol_cost
= 0;
156 set_config('enrol_cost', $config->enrol_cost
);
158 if (!isset($config->enrol_currency
)) {
159 $config->enrol_currency
= 'USD';
161 set_config('enrol_currency', $config->enrol_currency
);
163 if (!isset($config->enrol_paypalbusiness
)) {
164 $config->enrol_paypalbusiness
= '';
166 $config->enrol_paypalbusiness
= trim($config->enrol_paypalbusiness
); // remove trailing spaces etc.
167 set_config('enrol_paypalbusiness', $config->enrol_paypalbusiness
);
169 if (!isset($config->enrol_mailstudents
)) {
170 $config->enrol_mailstudents
= '';
172 set_config('enrol_mailstudents', $config->enrol_mailstudents
);
174 if (!isset($config->enrol_mailteachers
)) {
175 $config->enrol_mailteachers
= '';
177 set_config('enrol_mailteachers', $config->enrol_mailteachers
);
179 if (!isset($config->enrol_mailadmins
)) {
180 $config->enrol_mailadmins
= '';
182 set_config('enrol_mailadmins', $config->enrol_mailadmins
);
189 * This function enables internal enrolment when PayPal is primary and course key is set at the same time.
191 * @param form the form data submitted, as an object
192 * @param course the current course, as an object
194 function check_entry($form, $course) {
195 $manual = enrolment_factory
::factory('manual');
196 $manual->check_entry($form, $course);
197 if (isset($manual->errormsg
)) {
198 $this->errormsg
= $manual->errormsg
;
203 * Provides method to print the enrolment key form code. This method is called
204 * from /enrol/manual/enrol.html if it's included
205 * @param object a valid course object
207 function print_enrolmentkeyfrom($course) {
208 $manual = enrolment_factory
::factory('manual');
209 $manual->print_enrolmentkeyfrom($course);
213 } // end of class definition