Merge branch 'w24_MDL-33635_m23_sort' of git://github.com/skodak/moodle
[moodle.git] / enrol / paypal / lib.php
blobd9f9d498d9b7b2bace748177f2c944f52c901689
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
23 * @subpackage paypal
24 * @copyright 2010 Eugene Venter
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
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 /**
38 * Returns optional enrolment information icons.
40 * This is used in course list for quick overview of enrolment options.
42 * We are not using single instance parameter because sometimes
43 * we might want to prevent icon repetition when multiple instances
44 * of one type exist. One instance may also produce several icons.
46 * @param array $instances all enrol instances of this type in one course
47 * @return array of pix_icon
49 public function get_info_icons(array $instances) {
50 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
53 public function roles_protected() {
54 // users with role assign cap may tweak the roles later
55 return false;
58 public function allow_unenrol(stdClass $instance) {
59 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
60 return true;
63 public function allow_manage(stdClass $instance) {
64 // users with manage cap may tweak period and status - requires enrol/paypal:manage
65 return true;
68 public function show_enrolme_link(stdClass $instance) {
69 return ($instance->status == ENROL_INSTANCE_ENABLED);
72 /**
73 * Sets up navigation entries.
75 * @param object $instance
76 * @return void
78 public function add_course_navigation($instancesnode, stdClass $instance) {
79 if ($instance->enrol !== 'paypal') {
80 throw new coding_exception('Invalid enrol instance type!');
83 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
84 if (has_capability('enrol/paypal:config', $context)) {
85 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id));
86 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
90 /**
91 * Returns edit icons for the page with list of instances
92 * @param stdClass $instance
93 * @return array
95 public function get_action_icons(stdClass $instance) {
96 global $OUTPUT;
98 if ($instance->enrol !== 'paypal') {
99 throw new coding_exception('invalid enrol instance!');
101 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
103 $icons = array();
105 if (has_capability('enrol/paypal:config', $context)) {
106 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
107 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
110 return $icons;
114 * Returns link to page which may be used to add new instance of enrolment plugin in course.
115 * @param int $courseid
116 * @return moodle_url page url
118 public function get_newinstance_link($courseid) {
119 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
121 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
122 return NULL;
125 // multiple instances supported - different cost for different roles
126 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
130 * Creates course enrol form, checks if form submitted
131 * and enrols user if necessary. It can also redirect.
133 * @param stdClass $instance
134 * @return string html text, usually a form in a text box
136 function enrol_page_hook(stdClass $instance) {
137 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
139 ob_start();
141 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
142 return ob_get_clean();
145 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
146 return ob_get_clean();
149 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
150 return ob_get_clean();
153 $course = $DB->get_record('course', array('id'=>$instance->courseid));
154 $context = get_context_instance(CONTEXT_COURSE, $course->id);
156 $shortname = format_string($course->shortname, true, array('context' => $context));
157 $strloginto = get_string("loginto", "", $shortname);
158 $strcourses = get_string("courses");
160 // Pass $view=true to filter hidden caps if the user cannot see them
161 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
162 '', '', '', '', false, true)) {
163 $users = sort_by_roleassignment_authority($users, $context);
164 $teacher = array_shift($users);
165 } else {
166 $teacher = false;
169 if ( (float) $instance->cost <= 0 ) {
170 $cost = (float) $this->get_config('cost');
171 } else {
172 $cost = (float) $instance->cost;
175 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
176 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
177 } else {
179 if (isguestuser()) { // force login only for guest user, not real users with guest role
180 if (empty($CFG->loginhttps)) {
181 $wwwroot = $CFG->wwwroot;
182 } else {
183 // This actually is not so secure ;-), 'cause we're
184 // in unencrypted connection...
185 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
187 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
188 echo '<p><b>'.get_string('cost').": $instance->currency $cost".'</b></p>';
189 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
190 echo '</div>';
191 } else {
192 //Sanitise some fields before building the PayPal form
193 $coursefullname = format_string($course->fullname, true, array('context'=>$context));
194 $courseshortname = $shortname;
195 $userfullname = fullname($USER);
196 $userfirstname = $USER->firstname;
197 $userlastname = $USER->lastname;
198 $useraddress = $USER->address;
199 $usercity = $USER->city;
200 $instancename = $this->get_instance_name($instance);
202 include($CFG->dirroot.'/enrol/paypal/enrol.html');
207 return $OUTPUT->box(ob_get_clean());