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.
24 * @copyright 2010 Eugene Venter
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * Paypal enrolment plugin implementation.
32 * @author Eugene Venter - based on code by Martin Dougiamas and others
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class enrol_paypal_plugin
extends enrol_plugin
{
37 public function get_currencies() {
38 // See https://www.paypal.com/cgi-bin/webscr?cmd=p/sell/mc/mc_intro-outside,
39 // 3-character ISO-4217: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_currency_codes
41 'AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY',
42 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'USD');
43 $currencies = array();
44 foreach ($codes as $c) {
45 $currencies[$c] = new lang_string($c, 'core_currencies');
52 * Returns optional enrolment information icons.
54 * This is used in course list for quick overview of enrolment options.
56 * We are not using single instance parameter because sometimes
57 * we might want to prevent icon repetition when multiple instances
58 * of one type exist. One instance may also produce several icons.
60 * @param array $instances all enrol instances of this type in one course
61 * @return array of pix_icon
63 public function get_info_icons(array $instances) {
64 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
67 public function roles_protected() {
68 // users with role assign cap may tweak the roles later
72 public function allow_unenrol(stdClass
$instance) {
73 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
77 public function allow_manage(stdClass
$instance) {
78 // users with manage cap may tweak period and status - requires enrol/paypal:manage
82 public function show_enrolme_link(stdClass
$instance) {
83 return ($instance->status
== ENROL_INSTANCE_ENABLED
);
87 * Sets up navigation entries.
89 * @param object $instance
92 public function add_course_navigation($instancesnode, stdClass
$instance) {
93 if ($instance->enrol
!== 'paypal') {
94 throw new coding_exception('Invalid enrol instance type!');
97 $context = context_course
::instance($instance->courseid
);
98 if (has_capability('enrol/paypal:config', $context)) {
99 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid
, 'id'=>$instance->id
));
100 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node
::TYPE_SETTING
);
105 * Returns edit icons for the page with list of instances
106 * @param stdClass $instance
109 public function get_action_icons(stdClass
$instance) {
112 if ($instance->enrol
!== 'paypal') {
113 throw new coding_exception('invalid enrol instance!');
115 $context = context_course
::instance($instance->courseid
);
119 if (has_capability('enrol/paypal:config', $context)) {
120 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid
, 'id'=>$instance->id
));
121 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
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 if (isguestuser()) { // force login only for guest user, not real users with guest role
194 if (empty($CFG->loginhttps
)) {
195 $wwwroot = $CFG->wwwroot
;
197 // This actually is not so secure ;-), 'cause we're
198 // in unencrypted connection...
199 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot
);
201 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
202 echo '<p><b>'.get_string('cost').": $instance->currency $cost".'</b></p>';
203 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
206 //Sanitise some fields before building the PayPal form
207 $coursefullname = format_string($course->fullname
, true, array('context'=>$context));
208 $courseshortname = $shortname;
209 $userfullname = fullname($USER);
210 $userfirstname = $USER->firstname
;
211 $userlastname = $USER->lastname
;
212 $useraddress = $USER->address
;
213 $usercity = $USER->city
;
214 $instancename = $this->get_instance_name($instance);
216 include($CFG->dirroot
.'/enrol/paypal/enrol.html');
221 return $OUTPUT->box(ob_get_clean());
225 * Restore instance and map settings.
227 * @param restore_enrolments_structure_step $step
228 * @param stdClass $data
229 * @param stdClass $course
232 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
234 if ($step->get_task()->get_target() == backup
::TARGET_NEW_COURSE
) {
238 'courseid' => $data->courseid
,
239 'enrol' => $this->get_name(),
240 'roleid' => $data->roleid
,
241 'cost' => $data->cost
,
242 'currency' => $data->currency
,
245 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
246 $instance = reset($instances);
247 $instanceid = $instance->id
;
249 $instanceid = $this->add_instance($course, (array)$data);
251 $step->set_mapping('enrol', $oldid, $instanceid);
255 * Restore user enrolment.
257 * @param restore_enrolments_structure_step $step
258 * @param stdClass $data
259 * @param stdClass $instance
260 * @param int $oldinstancestatus
263 public function restore_user_enrolment(restore_enrolments_structure_step
$step, $data, $instance, $userid, $oldinstancestatus) {
264 $this->enrol_user($instance, $userid, null, $data->timestart
, $data->timeend
, $data->status
);
268 * Gets an array of the user enrolment actions
270 * @param course_enrolment_manager $manager
271 * @param stdClass $ue A user enrolment object
272 * @return array An array of user_enrolment_actions
274 public function get_user_enrolment_actions(course_enrolment_manager
$manager, $ue) {
276 $context = $manager->get_context();
277 $instance = $ue->enrolmentinstance
;
278 $params = $manager->get_moodlepage()->url
->params();
279 $params['ue'] = $ue->id
;
280 if ($this->allow_unenrol($instance) && has_capability("enrol/paypal:unenrol", $context)) {
281 $url = new moodle_url('/enrol/unenroluser.php', $params);
282 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id
));
284 if ($this->allow_manage($instance) && has_capability("enrol/paypal:manage", $context)) {
285 $url = new moodle_url('/enrol/editenrolment.php', $params);
286 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id
));
291 public function cron() {
292 $trace = new text_progress_trace();
293 $this->process_expirations($trace);
297 * Execute synchronisation.
298 * @param progress_trace $trace
299 * @return int exit code, 0 means ok
301 public function sync(progress_trace
$trace) {
302 $this->process_expirations($trace);