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', '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) {
63 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
66 public function roles_protected() {
67 // users with role assign cap may tweak the roles later
71 public function allow_unenrol(stdClass
$instance) {
72 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
76 public function allow_manage(stdClass
$instance) {
77 // users with manage cap may tweak period and status - requires enrol/paypal:manage
81 public function show_enrolme_link(stdClass
$instance) {
82 return ($instance->status
== ENROL_INSTANCE_ENABLED
);
86 * Sets up navigation entries.
88 * @param object $instance
91 public function add_course_navigation($instancesnode, stdClass
$instance) {
92 if ($instance->enrol
!== 'paypal') {
93 throw new coding_exception('Invalid enrol instance type!');
96 $context = context_course
::instance($instance->courseid
);
97 if (has_capability('enrol/paypal:config', $context)) {
98 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid
, 'id'=>$instance->id
));
99 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node
::TYPE_SETTING
);
104 * Returns edit icons for the page with list of instances
105 * @param stdClass $instance
108 public function get_action_icons(stdClass
$instance) {
111 if ($instance->enrol
!== 'paypal') {
112 throw new coding_exception('invalid enrol instance!');
114 $context = context_course
::instance($instance->courseid
);
118 if (has_capability('enrol/paypal:config', $context)) {
119 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid
, 'id'=>$instance->id
));
120 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
121 array('class' => 'iconsmall')));
128 * Returns link to page which may be used to add new instance of enrolment plugin in course.
129 * @param int $courseid
130 * @return moodle_url page url
132 public function get_newinstance_link($courseid) {
133 $context = context_course
::instance($courseid, MUST_EXIST
);
135 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
139 // multiple instances supported - different cost for different roles
140 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
144 * Creates course enrol form, checks if form submitted
145 * and enrols user if necessary. It can also redirect.
147 * @param stdClass $instance
148 * @return string html text, usually a form in a text box
150 function enrol_page_hook(stdClass
$instance) {
151 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
155 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id
, 'enrolid'=>$instance->id
))) {
156 return ob_get_clean();
159 if ($instance->enrolstartdate
!= 0 && $instance->enrolstartdate
> time()) {
160 return ob_get_clean();
163 if ($instance->enrolenddate
!= 0 && $instance->enrolenddate
< time()) {
164 return ob_get_clean();
167 $course = $DB->get_record('course', array('id'=>$instance->courseid
));
168 $context = context_course
::instance($course->id
);
170 $shortname = format_string($course->shortname
, true, array('context' => $context));
171 $strloginto = get_string("loginto", "", $shortname);
172 $strcourses = get_string("courses");
174 // Pass $view=true to filter hidden caps if the user cannot see them
175 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
176 '', '', '', '', false, true)) {
177 $users = sort_by_roleassignment_authority($users, $context);
178 $teacher = array_shift($users);
183 if ( (float) $instance->cost
<= 0 ) {
184 $cost = (float) $this->get_config('cost');
186 $cost = (float) $instance->cost
;
189 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
190 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
193 // Calculate localised and "." cost, make sure we send PayPal the same value,
194 // please note PayPal expects amount with 2 decimal places and "." separator.
195 $localisedcost = format_float($cost, 2, true);
196 $cost = format_float($cost, 2, false);
198 if (isguestuser()) { // force login only for guest user, not real users with guest role
199 if (empty($CFG->loginhttps
)) {
200 $wwwroot = $CFG->wwwroot
;
202 // This actually is not so secure ;-), 'cause we're
203 // in unencrypted connection...
204 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot
);
206 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
207 echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>';
208 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
211 //Sanitise some fields before building the PayPal form
212 $coursefullname = format_string($course->fullname
, true, array('context'=>$context));
213 $courseshortname = $shortname;
214 $userfullname = fullname($USER);
215 $userfirstname = $USER->firstname
;
216 $userlastname = $USER->lastname
;
217 $useraddress = $USER->address
;
218 $usercity = $USER->city
;
219 $instancename = $this->get_instance_name($instance);
221 include($CFG->dirroot
.'/enrol/paypal/enrol.html');
226 return $OUTPUT->box(ob_get_clean());
230 * Restore instance and map settings.
232 * @param restore_enrolments_structure_step $step
233 * @param stdClass $data
234 * @param stdClass $course
237 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
239 if ($step->get_task()->get_target() == backup
::TARGET_NEW_COURSE
) {
243 'courseid' => $data->courseid
,
244 'enrol' => $this->get_name(),
245 'roleid' => $data->roleid
,
246 'cost' => $data->cost
,
247 'currency' => $data->currency
,
250 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
251 $instance = reset($instances);
252 $instanceid = $instance->id
;
254 $instanceid = $this->add_instance($course, (array)$data);
256 $step->set_mapping('enrol', $oldid, $instanceid);
260 * Restore user enrolment.
262 * @param restore_enrolments_structure_step $step
263 * @param stdClass $data
264 * @param stdClass $instance
265 * @param int $oldinstancestatus
268 public function restore_user_enrolment(restore_enrolments_structure_step
$step, $data, $instance, $userid, $oldinstancestatus) {
269 $this->enrol_user($instance, $userid, null, $data->timestart
, $data->timeend
, $data->status
);
273 * Gets an array of the user enrolment actions
275 * @param course_enrolment_manager $manager
276 * @param stdClass $ue A user enrolment object
277 * @return array An array of user_enrolment_actions
279 public function get_user_enrolment_actions(course_enrolment_manager
$manager, $ue) {
281 $context = $manager->get_context();
282 $instance = $ue->enrolmentinstance
;
283 $params = $manager->get_moodlepage()->url
->params();
284 $params['ue'] = $ue->id
;
285 if ($this->allow_unenrol($instance) && has_capability("enrol/paypal:unenrol", $context)) {
286 $url = new moodle_url('/enrol/unenroluser.php', $params);
287 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id
));
289 if ($this->allow_manage($instance) && has_capability("enrol/paypal:manage", $context)) {
290 $url = new moodle_url('/enrol/editenrolment.php', $params);
291 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id
));
296 public function cron() {
297 $trace = new text_progress_trace();
298 $this->process_expirations($trace);
302 * Execute synchronisation.
303 * @param progress_trace $trace
304 * @return int exit code, 0 means ok
306 public function sync(progress_trace
$trace) {
307 $this->process_expirations($trace);