2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Paypal enrolment plugin.
20 * This plugin allows you to set up paid courses.
22 * @package enrol_paypal
23 * @copyright 2010 Eugene Venter
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Paypal enrolment plugin implementation.
29 * @author Eugene Venter - based on code by Martin Dougiamas and others
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class enrol_paypal_plugin extends enrol_plugin {
36 * Add new instance of enrol plugin with default settings.
37 * @param object $course
38 * @return int id of new instance
40 public function add_default_instance($course) {
43 $exists = $DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal'));
45 $fields = array('enrolperiod'=>$this->get_config('enrolperiod', 0),
46 'roleid'=>$this->get_config('roleid', 0),
47 'cost'=>$this->get_config('cost', 0),
48 'currency'=>$this->get_config('currency', 0)
51 $fields['status'] = $exists ? ENROL_INSTANCE_DISABLED : $this->get_config('status');
53 return $this->add_instance($course, $fields);
57 * Returns link to page which may be used to add new instance of enrolment plugin in course.
58 * @param int $courseid
59 * @return moodle_url page url
61 public function get_candidate_link($courseid) {
62 if (!has_capability('moodle/course:enrolconfig', get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST))) {
65 // multiple instances supported - different roles with different password
66 return new moodle_url('/enrol/paypal/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
70 * Adds enrol instance UI to course edit form
72 * @param object $instance enrol instance or null if does not exist yet
73 * @param MoodleQuickForm $mform
75 * @param object $context context of existing course or parent category if course does not exist
78 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
80 $i = isset($instance->id) ? $instance->id : 0;
81 $plugin = enrol_get_plugin('paypal');
82 $header = $plugin->get_instance_name($instance);
83 $config = has_capability('moodle/course:enrolconfig', $context);
85 $mform->addElement('header', 'enrol_paypal_header_'.$i, $header);
88 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
89 ENROL_INSTANCE_DISABLED => get_string('no'));
90 $mform->addElement('select', 'enrol_paypal_status_'.$i, get_string('status', 'enrol_paypal'), $options);
91 $mform->setDefault('enrol_paypal_status_'.$i, $this->get_config('status'));
92 $mform->setAdvanced('enrol_paypal_status_'.$i, $this->get_config('status_adv'));
94 $mform->hardFreeze('enrol_paypal_status_'.$i);
97 $mform->addElement('text', 'enrol_paypal_cost_'.$i, get_string('cost', 'enrol_paypal'), array('size'=>4));
98 $mform->setDefault('enrol_paypal_cost_'.$i, $this->get_config('cost'));
100 $mform->hardFreeze('enrol_paypal_cost_'.$i);
102 $mform->disabledIf('enrol_paypal_cost_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
105 $paypalcurrencies = array( 'USD' => 'US Dollars',
107 'JPY' => 'Japanese Yen',
108 'GBP' => 'British Pounds',
109 'CAD' => 'Canadian Dollars',
110 'AUD' => 'Australian Dollars'
112 $mform->addElement('select', 'enrol_paypal_currency_'.$i, get_string('currency', 'enrol_paypal'), $paypalcurrencies);
113 $mform->setDefault('enrol_paypal_currency_'.$i, $this->get_config('currency'));
115 $mform->hardFreeze('enrol_paypal_currency_'.$i);
117 $mform->disabledIf('enrol_paypal_currency_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
122 $roles = get_default_enrol_roles($context, $instance->roleid);
124 $roles = get_default_enrol_roles($context, $this->get_config('roleid'));
126 $mform->addElement('select', 'enrol_paypal_roleid_'.$i, get_string('assignrole', 'enrol_paypal'), $roles);
127 $mform->setDefault('enrol_paypal_roleid_'.$i, $this->get_config('roleid'));
128 $mform->setAdvanced('enrol_paypal_roleid_'.$i, $this->get_config('roleid_adv'));
130 $mform->hardFreeze('enrol_paypal_roleid_'.$i);
132 $mform->disabledIf('enrol_paypal_roleid_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
136 $mform->addElement('duration', 'enrol_paypal_enrolperiod_'.$i, get_string('enrolperiod', 'enrol_paypal'), array('optional' => true, 'defaultunit' => 86400));
137 $mform->setDefault('enrol_paypal_enrolperiod_'.$i, $this->get_config('enrolperiod'));
138 $mform->setAdvanced('enrol_paypal_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
140 $mform->hardFreeze('enrol_paypal_enrolperiod_'.$i);
142 $mform->disabledIf('enrol_paypal_enrolperiod_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
146 $mform->addElement('date_selector', 'enrol_paypal_enrolstartdate_'.$i, get_string('enrolstartdate', 'enrol_paypal'), array('optional' => true));
147 $mform->setDefault('enrol_paypal_enrolstartdate_'.$i, 0);
148 $mform->setAdvanced('enrol_paypal_enrolstartdate_'.$i, 1);
150 $mform->hardFreeze('enrol_paypal_enrolstartdate_'.$i);
152 $mform->disabledIf('enrol_paypal_enrolstartdate_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
156 $mform->addElement('date_selector', 'enrol_paypal_enrolenddate_'.$i, get_string('enrolenddate', 'enrol_paypal'), array('optional' => true));
157 $mform->setDefault('enrol_paypal_enrolenddate_'.$i, 0);
158 $mform->setAdvanced('enrol_paypal_enrolenddate_'.$i, 1);
160 $mform->hardFreeze('enrol_paypal_enrolenddate_'.$i);
162 $mform->disabledIf('enrol_paypal_enrolenddate_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
166 // now add all values from enrol table
168 foreach($instance as $key=>$val) {
169 $data->{'enrol_paypal_'.$key.'_'.$i} = $val;
175 * Validates course edit form data
177 * @param object $instance enrol instance or null if does not exist yet
179 * @param object $context context of existing course or parent category if course does not exist
180 * @return array errors array
182 public function course_edit_validation($instance, array $data, $context) {
185 if (!has_capability('moodle/course:enrolconfig', $context)) {
186 // we are going to ignore the data later anyway, they would not be able to fix the form anyway
190 $i = isset($instance->id) ? $instance->id : 0;
192 if ($data['enrol_paypal_status_'.$i] == ENROL_INSTANCE_ENABLED) {
193 if (!empty($data['enrol_paypal_enrolenddate_'.$i]) and $data['enrol_paypal_enrolenddate_'.$i] < $data['enrol_paypal_enrolstartdate_'.$i]) {
194 $errors['enrol_paypal_enrolenddate_'.$i] = get_string('enrolenddaterror', 'enrol_paypal');
197 if (!is_numeric($data['enrol_paypal_cost_'.$i])) {
198 $errors['enrol_paypal_cost_'.$i] = get_string('costerror', 'enrol_paypal');
208 * Called after updating/inserting course.
210 * @param bool $inserted true if course just inserted
211 * @param object $course
212 * @param object $data form data
215 public function course_updated($inserted, $course, $data) {
218 $context = get_context_instance(CONTEXT_COURSE, $course->id);
220 if (has_capability('moodle/course:enrolconfig', $context)) {
222 if (isset($data->enrol_paypal_status_0)) {
223 $fields = array('status'=>$data->enrol_paypal_status_0);
224 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
225 $fields['cost'] = $data->enrol_paypal_cost_0;
226 $fields['currency'] = $data->enrol_paypal_currency_0;
227 $fields['roleid'] = $data->enrol_paypal_roleid_0;
228 $fields['enrolperiod'] = $data->enrol_paypal_enrolperiod_0;
229 $fields['enrolstartdate'] = $data->enrol_paypal_enrolstartdate_0;
230 $fields['enrolenddate'] = $data->enrol_paypal_enrolenddate_0;
232 $fields['roleid'] = $this->get_config('roleid');
233 $fields['cost'] = $this->get_config('cost');
234 $fields['currency'] = $this->get_config('currency');
235 $fields['enrolperiod'] = $this->get_config('enrolperiod');
236 $fields['enrolstartdate'] = 0;
237 $fields['enrolenddate'] = 0;
239 $this->add_instance($course, $fields);
243 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal'));
244 foreach ($instances as $instance) {
247 if (isset($data->{'enrol_paypal_status_'.$i})) {
248 $instance->status = $data->{'enrol_paypal_status_'.$i};
249 $instance->timemodified = time();
250 if ($instance->status == ENROL_INSTANCE_ENABLED) {
251 $instance->roleid = $data->{'enrol_paypal_roleid_'.$i};
252 $instance->cost = $data->{'enrol_paypal_cost_'.$i};
253 $instance->currency = $data->{'enrol_paypal_currency_'.$i};
254 $instance->enrolperiod = $data->{'enrol_paypal_enrolperiod_'.$i};
255 $instance->enrolstartdate = $data->{'enrol_paypal_enrolstartdate_'.$i};
256 $instance->enrolenddate = $data->{'enrol_paypal_enrolenddate_'.$i};
258 $DB->update_record('enrol', $instance);
265 if ($this->get_config('defaultenrol')) {
266 $this->add_default_instance($course);
269 // bad luck, user can not change anything
276 * Creates course enrol form, checks if form submitted
277 * and enrols user if necessary. It can also redirect.
279 * @param stdClass $instance
280 * @return string html text, usually a form in a text box
282 function enrol_page_hook(stdClass $instance) {
283 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
287 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
288 return ob_get_clean();
291 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
292 return ob_get_clean();
295 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
296 return ob_get_clean();
299 $course = $DB->get_record('course', array('id'=>$instance->courseid));
301 $strloginto = get_string("loginto", "", $course->shortname);
302 $strcourses = get_string("courses");
304 $context = get_context_instance(CONTEXT_COURSE, $course->id);
305 // Pass $view=true to filter hidden caps if the user cannot see them
306 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
307 '', '', '', '', false, true)) {
308 $users = sort_by_roleassignment_authority($users, $context);
309 $teacher = array_shift($users);
314 if ( (float) $instance->cost <= 0 ) {
315 $cost = (float) $this->get_config('cost');
317 $cost = (float) $instance->cost;
320 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
321 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
324 if ($USER->username == 'guest') { // force login only for guest user, not real users with guest role
325 if (empty($CFG->loginhttps)) {
326 $wwwroot = $CFG->wwwroot;
328 // This actually is not so secure ;-), 'cause we're
329 // in unencrypted connection...
330 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
332 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
333 echo '<p><b>'.get_string('cost').": $CFG->enrol_currency $cost".'</b></p>';
334 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
337 //Sanitise some fields before building the PayPal form
338 $coursefullname = $course->fullname;
339 $courseshortname = $course->shortname;
340 $userfullname = fullname($USER);
341 $userfirstname = $USER->firstname;
342 $userlastname = $USER->lastname;
343 $useraddress = $USER->address;
344 $usercity = $USER->city;
346 include($CFG->dirroot.'/enrol/paypal/enrol.html');
351 return $OUTPUT->box(ob_get_clean());