Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / enrol / paypal / enrol.php
blob930672be808c5cc6f1796ad5b598cfaa2674ac58
1 <?php // $Id$
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) {
12 global $CFG, $USER;
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;
22 } else {
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);
31 } else {
32 $navlinks = array();
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;
49 } else {
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>';
57 echo '</div>';
58 } else {
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
74 $password = '';
75 include($CFG->dirroot.'/enrol/manual/enrol.html');
78 print_footer();
81 } // end of function print_entry()
86 /// Override the get_access_icons() function
87 function get_access_icons($course) {
88 global $CFG;
90 $str = '';
92 if ( (float) $course->cost < 0) {
93 $cost = (float) $CFG->enrol_cost;
94 } else {
95 $cost = (float) $course->cost;
98 if (abs($cost) < 0.01) {
99 $manual = enrolment_factory::factory('manual');
100 $str = $manual->get_access_icons($course);
102 } else {
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 = '&euro;'; break;
113 case 'CAD': $currency = '$'; break;
114 case 'GBP': $currency = '&pound;'; break;
115 case 'JPY': $currency = '&yen;'; 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>';
125 return $str;
128 /// Override the base class config_form() function
129 function config_form($frm) {
130 global $CFG;
132 $paypalcurrencies = array( 'USD' => 'US Dollars',
133 'EUR' => 'Euros',
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)) {
144 $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);
184 return true;
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