weekly release 2.7.10+
[moodle.git] / enrol / paypal / lib.php
bloba714a90d52563688c83815cd8d7a83022bf64ba1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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();
29 /**
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
39 $codes = array(
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');
47 return $currencies;
50 /**
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 $found = false;
64 foreach ($instances as $instance) {
65 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
66 continue;
68 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
69 continue;
71 $found = true;
72 break;
74 if ($found) {
75 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
77 return array();
80 public function roles_protected() {
81 // users with role assign cap may tweak the roles later
82 return false;
85 public function allow_unenrol(stdClass $instance) {
86 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
87 return true;
90 public function allow_manage(stdClass $instance) {
91 // users with manage cap may tweak period and status - requires enrol/paypal:manage
92 return true;
95 public function show_enrolme_link(stdClass $instance) {
96 return ($instance->status == ENROL_INSTANCE_ENABLED);
99 /**
100 * Sets up navigation entries.
102 * @param object $instance
103 * @return void
105 public function add_course_navigation($instancesnode, stdClass $instance) {
106 if ($instance->enrol !== 'paypal') {
107 throw new coding_exception('Invalid enrol instance type!');
110 $context = context_course::instance($instance->courseid);
111 if (has_capability('enrol/paypal:config', $context)) {
112 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id));
113 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
118 * Returns edit icons for the page with list of instances
119 * @param stdClass $instance
120 * @return array
122 public function get_action_icons(stdClass $instance) {
123 global $OUTPUT;
125 if ($instance->enrol !== 'paypal') {
126 throw new coding_exception('invalid enrol instance!');
128 $context = context_course::instance($instance->courseid);
130 $icons = array();
132 if (has_capability('enrol/paypal:config', $context)) {
133 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
134 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
135 array('class' => 'iconsmall')));
138 return $icons;
142 * Returns link to page which may be used to add new instance of enrolment plugin in course.
143 * @param int $courseid
144 * @return moodle_url page url
146 public function get_newinstance_link($courseid) {
147 $context = context_course::instance($courseid, MUST_EXIST);
149 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
150 return NULL;
153 // multiple instances supported - different cost for different roles
154 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
158 * Creates course enrol form, checks if form submitted
159 * and enrols user if necessary. It can also redirect.
161 * @param stdClass $instance
162 * @return string html text, usually a form in a text box
164 function enrol_page_hook(stdClass $instance) {
165 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
167 ob_start();
169 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
170 return ob_get_clean();
173 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
174 return ob_get_clean();
177 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
178 return ob_get_clean();
181 $course = $DB->get_record('course', array('id'=>$instance->courseid));
182 $context = context_course::instance($course->id);
184 $shortname = format_string($course->shortname, true, array('context' => $context));
185 $strloginto = get_string("loginto", "", $shortname);
186 $strcourses = get_string("courses");
188 // Pass $view=true to filter hidden caps if the user cannot see them
189 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
190 '', '', '', '', false, true)) {
191 $users = sort_by_roleassignment_authority($users, $context);
192 $teacher = array_shift($users);
193 } else {
194 $teacher = false;
197 if ( (float) $instance->cost <= 0 ) {
198 $cost = (float) $this->get_config('cost');
199 } else {
200 $cost = (float) $instance->cost;
203 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
204 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
205 } else {
207 // Calculate localised and "." cost, make sure we send PayPal the same value,
208 // please note PayPal expects amount with 2 decimal places and "." separator.
209 $localisedcost = format_float($cost, 2, true);
210 $cost = format_float($cost, 2, false);
212 if (isguestuser()) { // force login only for guest user, not real users with guest role
213 if (empty($CFG->loginhttps)) {
214 $wwwroot = $CFG->wwwroot;
215 } else {
216 // This actually is not so secure ;-), 'cause we're
217 // in unencrypted connection...
218 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
220 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
221 echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>';
222 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
223 echo '</div>';
224 } else {
225 //Sanitise some fields before building the PayPal form
226 $coursefullname = format_string($course->fullname, true, array('context'=>$context));
227 $courseshortname = $shortname;
228 $userfullname = fullname($USER);
229 $userfirstname = $USER->firstname;
230 $userlastname = $USER->lastname;
231 $useraddress = $USER->address;
232 $usercity = $USER->city;
233 $instancename = $this->get_instance_name($instance);
235 include($CFG->dirroot.'/enrol/paypal/enrol.html');
240 return $OUTPUT->box(ob_get_clean());
244 * Restore instance and map settings.
246 * @param restore_enrolments_structure_step $step
247 * @param stdClass $data
248 * @param stdClass $course
249 * @param int $oldid
251 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
252 global $DB;
253 if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) {
254 $merge = false;
255 } else {
256 $merge = array(
257 'courseid' => $data->courseid,
258 'enrol' => $this->get_name(),
259 'roleid' => $data->roleid,
260 'cost' => $data->cost,
261 'currency' => $data->currency,
264 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
265 $instance = reset($instances);
266 $instanceid = $instance->id;
267 } else {
268 $instanceid = $this->add_instance($course, (array)$data);
270 $step->set_mapping('enrol', $oldid, $instanceid);
274 * Restore user enrolment.
276 * @param restore_enrolments_structure_step $step
277 * @param stdClass $data
278 * @param stdClass $instance
279 * @param int $oldinstancestatus
280 * @param int $userid
282 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
283 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status);
287 * Gets an array of the user enrolment actions
289 * @param course_enrolment_manager $manager
290 * @param stdClass $ue A user enrolment object
291 * @return array An array of user_enrolment_actions
293 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
294 $actions = array();
295 $context = $manager->get_context();
296 $instance = $ue->enrolmentinstance;
297 $params = $manager->get_moodlepage()->url->params();
298 $params['ue'] = $ue->id;
299 if ($this->allow_unenrol($instance) && has_capability("enrol/paypal:unenrol", $context)) {
300 $url = new moodle_url('/enrol/unenroluser.php', $params);
301 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
303 if ($this->allow_manage($instance) && has_capability("enrol/paypal:manage", $context)) {
304 $url = new moodle_url('/enrol/editenrolment.php', $params);
305 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id));
307 return $actions;
310 public function cron() {
311 $trace = new text_progress_trace();
312 $this->process_expirations($trace);
316 * Execute synchronisation.
317 * @param progress_trace $trace
318 * @return int exit code, 0 means ok
320 public function sync(progress_trace $trace) {
321 $this->process_expirations($trace);
322 return 0;