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
27 defined('MOODLE_INTERNAL') ||
die();
30 * Paypal enrolment plugin implementation.
31 * @author Eugene Venter - based on code by Martin Dougiamas and others
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class enrol_paypal_plugin
extends enrol_plugin
{
36 public function get_currencies() {
37 // See https://www.paypal.com/cgi-bin/webscr?cmd=p/sell/mc/mc_intro-outside,
38 // 3-character ISO-4217: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_currency_codes
40 'AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'INR', 'JPY',
41 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'USD');
42 $currencies = array();
43 foreach ($codes as $c) {
44 $currencies[$c] = new lang_string($c, 'core_currencies');
51 * Returns optional enrolment information icons.
53 * This is used in course list for quick overview of enrolment options.
55 * We are not using single instance parameter because sometimes
56 * we might want to prevent icon repetition when multiple instances
57 * of one type exist. One instance may also produce several icons.
59 * @param array $instances all enrol instances of this type in one course
60 * @return array of pix_icon
62 public function get_info_icons(array $instances) {
64 foreach ($instances as $instance) {
65 if ($instance->enrolstartdate
!= 0 && $instance->enrolstartdate
> time()) {
68 if ($instance->enrolenddate
!= 0 && $instance->enrolenddate
< time()) {
75 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
80 public function roles_protected() {
81 // users with role assign cap may tweak the roles later
85 public function allow_unenrol(stdClass
$instance) {
86 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
90 public function allow_manage(stdClass
$instance) {
91 // users with manage cap may tweak period and status - requires enrol/paypal:manage
95 public function show_enrolme_link(stdClass
$instance) {
96 return ($instance->status
== ENROL_INSTANCE_ENABLED
);
100 * Returns true if the user can add a new instance in this course.
101 * @param int $courseid
104 public function can_add_instance($courseid) {
105 $context = context_course
::instance($courseid, MUST_EXIST
);
107 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
111 // multiple instances supported - different cost for different roles
116 * We are a good plugin and don't invent our own UI/validation code path.
120 public function use_standard_editing_ui() {
125 * Add new instance of enrol plugin.
126 * @param object $course
127 * @param array $fields instance fields
128 * @return int id of new instance, null if can not be created
130 public function add_instance($course, array $fields = null) {
131 if ($fields && !empty($fields['cost'])) {
132 $fields['cost'] = unformat_float($fields['cost']);
134 return parent
::add_instance($course, $fields);
138 * Update instance of enrol plugin.
139 * @param stdClass $instance
140 * @param stdClass $data modified instance fields
143 public function update_instance($instance, $data) {
145 $data->cost
= unformat_float($data->cost
);
147 return parent
::update_instance($instance, $data);
151 * Creates course enrol form, checks if form submitted
152 * and enrols user if necessary. It can also redirect.
154 * @param stdClass $instance
155 * @return string html text, usually a form in a text box
157 function enrol_page_hook(stdClass
$instance) {
158 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
162 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id
, 'enrolid'=>$instance->id
))) {
163 return ob_get_clean();
166 if ($instance->enrolstartdate
!= 0 && $instance->enrolstartdate
> time()) {
167 return ob_get_clean();
170 if ($instance->enrolenddate
!= 0 && $instance->enrolenddate
< time()) {
171 return ob_get_clean();
174 $course = $DB->get_record('course', array('id'=>$instance->courseid
));
175 $context = context_course
::instance($course->id
);
177 $shortname = format_string($course->shortname
, true, array('context' => $context));
178 $strloginto = get_string("loginto", "", $shortname);
179 $strcourses = get_string("courses");
181 // Pass $view=true to filter hidden caps if the user cannot see them
182 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
183 '', '', '', '', false, true)) {
184 $users = sort_by_roleassignment_authority($users, $context);
185 $teacher = array_shift($users);
190 if ( (float) $instance->cost
<= 0 ) {
191 $cost = (float) $this->get_config('cost');
193 $cost = (float) $instance->cost
;
196 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
197 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
200 // Calculate localised and "." cost, make sure we send PayPal the same value,
201 // please note PayPal expects amount with 2 decimal places and "." separator.
202 $localisedcost = format_float($cost, 2, true);
203 $cost = format_float($cost, 2, false);
205 if (isguestuser()) { // force login only for guest user, not real users with guest role
206 $wwwroot = $CFG->wwwroot
;
207 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
208 echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>';
209 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
212 //Sanitise some fields before building the PayPal form
213 $coursefullname = format_string($course->fullname
, true, array('context'=>$context));
214 $courseshortname = $shortname;
215 $userfullname = fullname($USER);
216 $userfirstname = $USER->firstname
;
217 $userlastname = $USER->lastname
;
218 $useraddress = $USER->address
;
219 $usercity = $USER->city
;
220 $instancename = $this->get_instance_name($instance);
222 include($CFG->dirroot
.'/enrol/paypal/enrol.html');
227 return $OUTPUT->box(ob_get_clean());
231 * Restore instance and map settings.
233 * @param restore_enrolments_structure_step $step
234 * @param stdClass $data
235 * @param stdClass $course
238 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
240 if ($step->get_task()->get_target() == backup
::TARGET_NEW_COURSE
) {
244 'courseid' => $data->courseid
,
245 'enrol' => $this->get_name(),
246 'roleid' => $data->roleid
,
247 'cost' => $data->cost
,
248 'currency' => $data->currency
,
251 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
252 $instance = reset($instances);
253 $instanceid = $instance->id
;
255 $instanceid = $this->add_instance($course, (array)$data);
257 $step->set_mapping('enrol', $oldid, $instanceid);
261 * Restore user enrolment.
263 * @param restore_enrolments_structure_step $step
264 * @param stdClass $data
265 * @param stdClass $instance
266 * @param int $oldinstancestatus
269 public function restore_user_enrolment(restore_enrolments_structure_step
$step, $data, $instance, $userid, $oldinstancestatus) {
270 $this->enrol_user($instance, $userid, null, $data->timestart
, $data->timeend
, $data->status
);
274 * Return an array of valid options for the status.
278 protected function get_status_options() {
279 $options = array(ENROL_INSTANCE_ENABLED
=> get_string('yes'),
280 ENROL_INSTANCE_DISABLED
=> get_string('no'));
285 * Return an array of valid options for the roleid.
287 * @param stdClass $instance
288 * @param context $context
291 protected function get_roleid_options($instance, $context) {
293 $roles = get_default_enrol_roles($context, $instance->roleid
);
295 $roles = get_default_enrol_roles($context, $this->get_config('roleid'));
302 * Add elements to the edit instance form.
304 * @param stdClass $instance
305 * @param MoodleQuickForm $mform
306 * @param context $context
309 public function edit_instance_form($instance, MoodleQuickForm
$mform, $context) {
311 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
312 $mform->setType('name', PARAM_TEXT
);
314 $options = $this->get_status_options();
315 $mform->addElement('select', 'status', get_string('status', 'enrol_paypal'), $options);
316 $mform->setDefault('status', $this->get_config('status'));
318 $mform->addElement('text', 'cost', get_string('cost', 'enrol_paypal'), array('size' => 4));
319 $mform->setType('cost', PARAM_RAW
);
320 $mform->setDefault('cost', format_float($this->get_config('cost'), 2, true));
322 $paypalcurrencies = $this->get_currencies();
323 $mform->addElement('select', 'currency', get_string('currency', 'enrol_paypal'), $paypalcurrencies);
324 $mform->setDefault('currency', $this->get_config('currency'));
326 $roles = $this->get_roleid_options($instance, $context);
327 $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_paypal'), $roles);
328 $mform->setDefault('roleid', $this->get_config('roleid'));
330 $options = array('optional' => true, 'defaultunit' => 86400);
331 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_paypal'), $options);
332 $mform->setDefault('enrolperiod', $this->get_config('enrolperiod'));
333 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_paypal');
335 $options = array('optional' => true);
336 $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_paypal'), $options);
337 $mform->setDefault('enrolstartdate', 0);
338 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_paypal');
340 $options = array('optional' => true);
341 $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_paypal'), $options);
342 $mform->setDefault('enrolenddate', 0);
343 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_paypal');
345 if (enrol_accessing_via_instance($instance)) {
346 $warningtext = get_string('instanceeditselfwarningtext', 'core_enrol');
347 $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warningtext);
352 * Perform custom validation of the data used to edit the instance.
354 * @param array $data array of ("fieldname"=>value) of submitted data
355 * @param array $files array of uploaded files "element_name"=>tmp_file_path
356 * @param object $instance The instance loaded from the DB
357 * @param context $context The context of the instance we are editing
358 * @return array of "element_name"=>"error_description" if there are errors,
359 * or an empty array if everything is OK.
362 public function edit_instance_validation($data, $files, $instance, $context) {
365 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
366 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_paypal');
369 $cost = str_replace(get_string('decsep', 'langconfig'), '.', $data['cost']);
370 if (!is_numeric($cost)) {
371 $errors['cost'] = get_string('costerror', 'enrol_paypal');
374 $validstatus = array_keys($this->get_status_options());
375 $validcurrency = array_keys($this->get_currencies());
376 $validroles = array_keys($this->get_roleid_options($instance, $context));
378 'name' => PARAM_TEXT
,
379 'status' => $validstatus,
380 'currency' => $validcurrency,
381 'roleid' => $validroles,
382 'enrolperiod' => PARAM_INT
,
383 'enrolstartdate' => PARAM_INT
,
384 'enrolenddate' => PARAM_INT
387 $typeerrors = $this->validate_param_types($data, $tovalidate);
388 $errors = array_merge($errors, $typeerrors);
394 * Execute synchronisation.
395 * @param progress_trace $trace
396 * @return int exit code, 0 means ok
398 public function sync(progress_trace
$trace) {
399 $this->process_expirations($trace);
404 * Is it possible to delete enrol instance via standard UI?
406 * @param stdClass $instance
409 public function can_delete_instance($instance) {
410 $context = context_course
::instance($instance->courseid
);
411 return has_capability('enrol/paypal:config', $context);
415 * Is it possible to hide/show enrol instance via standard UI?
417 * @param stdClass $instance
420 public function can_hide_show_instance($instance) {
421 $context = context_course
::instance($instance->courseid
);
422 return has_capability('enrol/paypal:config', $context);