MDL-23767 PayPal instance name printed on the enrol page
[moodle.git] / enrol / paypal / lib.php
CommitLineData
5ea88553
AB
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.
13//
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/>.
16
17/**
18 * Paypal enrolment plugin.
19 *
20 * This plugin allows you to set up paid courses.
21 *
24a62572
PS
22 * @package enrol
23 * @subpackage paypal
42761bc6
PS
24 * @copyright 2010 Eugene Venter
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5ea88553
AB
26 */
27
97795859 28defined('MOODLE_INTERNAL') || die();
24a62572 29
5ea88553
AB
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
34 */
5ea88553
AB
35class enrol_paypal_plugin extends enrol_plugin {
36
5ea88553 37
42761bc6
PS
38 public function roles_protected() {
39 // users wit htole assign cap may tweak the roles later
40 return false;
41 }
5ea88553 42
42761bc6
PS
43 public function allow_unenrol(stdClass $instance) {
44 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
45 return true;
46 }
5ea88553 47
42761bc6
PS
48 public function allow_manage(stdClass $instance) {
49 // users with manage cap may tweak period and status - requires enrol/paypal:manage
50 return true;
5ea88553
AB
51 }
52
42761bc6
PS
53 public function show_enrolme_link(stdClass $instance) {
54 return ($instance->status == ENROL_INSTANCE_ENABLED);
5ea88553
AB
55 }
56
57 /**
42761bc6 58 * Sets up navigation entries.
5ea88553 59 *
42761bc6 60 * @param object $instance
5ea88553
AB
61 * @return void
62 */
42761bc6
PS
63 public function add_course_navigation($instancesnode, stdClass $instance) {
64 if ($instance->enrol !== 'paypal') {
65 throw new coding_exception('Invalid enrol instance type!');
5ea88553
AB
66 }
67
42761bc6
PS
68 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
69 if (has_capability('enrol/paypal:config', $context)) {
70 $managelink = new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id));
71 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
5ea88553
AB
72 }
73 }
74
75 /**
42761bc6
PS
76 * Returns edit icons for the page with list of instances
77 * @param stdClass $instance
78 * @return array
5ea88553 79 */
42761bc6
PS
80 public function get_action_icons(stdClass $instance) {
81 global $OUTPUT;
5ea88553 82
42761bc6
PS
83 if ($instance->enrol !== 'paypal') {
84 throw new coding_exception('invalid enrol instance!');
5ea88553 85 }
42761bc6 86 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
5ea88553 87
42761bc6 88 $icons = array();
5ea88553 89
42761bc6
PS
90 if (has_capability('enrol/paypal:config', $context)) {
91 $editlink = new moodle_url("/enrol/paypal/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
92 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
5ea88553
AB
93 }
94
42761bc6 95 return $icons;
5ea88553
AB
96 }
97
5ea88553 98 /**
42761bc6
PS
99 * Returns link to page which may be used to add new instance of enrolment plugin in course.
100 * @param int $courseid
101 * @return moodle_url page url
5ea88553 102 */
e25f2466 103 public function get_newinstance_link($courseid) {
42761bc6 104 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
5ea88553 105
42761bc6
PS
106 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
107 return NULL;
5ea88553 108 }
5ea88553 109
42761bc6
PS
110 // multiple instances supported - different cost for different roles
111 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
112 }
5ea88553
AB
113
114 /**
115 * Creates course enrol form, checks if form submitted
116 * and enrols user if necessary. It can also redirect.
117 *
118 * @param stdClass $instance
119 * @return string html text, usually a form in a text box
120 */
121 function enrol_page_hook(stdClass $instance) {
122 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
123
124 ob_start();
125
126 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
127 return ob_get_clean();
128 }
129
130 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
131 return ob_get_clean();
132 }
133
134 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
135 return ob_get_clean();
136 }
137
138 $course = $DB->get_record('course', array('id'=>$instance->courseid));
139
140 $strloginto = get_string("loginto", "", $course->shortname);
141 $strcourses = get_string("courses");
142
143 $context = get_context_instance(CONTEXT_COURSE, $course->id);
144 // Pass $view=true to filter hidden caps if the user cannot see them
145 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
146 '', '', '', '', false, true)) {
147 $users = sort_by_roleassignment_authority($users, $context);
148 $teacher = array_shift($users);
149 } else {
150 $teacher = false;
151 }
152
153 if ( (float) $instance->cost <= 0 ) {
154 $cost = (float) $this->get_config('cost');
155 } else {
156 $cost = (float) $instance->cost;
157 }
158
159 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
160 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
161 } else {
162
163 if ($USER->username == 'guest') { // force login only for guest user, not real users with guest role
164 if (empty($CFG->loginhttps)) {
165 $wwwroot = $CFG->wwwroot;
166 } else {
167 // This actually is not so secure ;-), 'cause we're
168 // in unencrypted connection...
169 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
170 }
171 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
172 echo '<p><b>'.get_string('cost').": $CFG->enrol_currency $cost".'</b></p>';
173 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
174 echo '</div>';
175 } else {
176 //Sanitise some fields before building the PayPal form
177 $coursefullname = $course->fullname;
178 $courseshortname = $course->shortname;
179 $userfullname = fullname($USER);
180 $userfirstname = $USER->firstname;
181 $userlastname = $USER->lastname;
182 $useraddress = $USER->address;
183 $usercity = $USER->city;
2c3e42fd 184 $instancename = $this->get_instance_name($instance);
5ea88553
AB
185
186 include($CFG->dirroot.'/enrol/paypal/enrol.html');
187 }
188
189 }
190
191 return $OUTPUT->box(ob_get_clean());
42761bc6 192 }
5ea88553 193
42761bc6 194}