MDL-22788 upgrade enrol/paypal plugin to use enrolments
[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 *
22 * @package enrol_paypal
23 * @copyright 2010 Eugene Venter
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/**
28 * Paypal enrolment plugin implementation.
29 * @author Eugene Venter - based on code by Martin Dougiamas and others
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32
33class enrol_paypal_plugin extends enrol_plugin {
34
35 /**
36 * Add new instance of enrol plugin with default settings.
37 * @param object $course
38 * @return int id of new instance
39 */
40 public function add_default_instance($course) {
41 global $DB;
42
43 $exists = $DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal'));
44
45 $fields = array('enrolperiod'=>$this->get_config('enrolperiod', 0),
46 'roleid'=>$this->get_config('roleid', 0),
47 'cost'=>$this->get_config('cost', 0),
48 'currency'=>$this->get_config('currency', 0)
49 );
50
51 $fields['status'] = $exists ? ENROL_INSTANCE_DISABLED : $this->get_config('status');
52
53 return $this->add_instance($course, $fields);
54 }
55
56 /**
57 * Returns link to page which may be used to add new instance of enrolment plugin in course.
58 * @param int $courseid
59 * @return moodle_url page url
60 */
61 public function get_candidate_link($courseid) {
62 if (!has_capability('moodle/course:enrolconfig', get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST))) {
63 return NULL;
64 }
65 // multiple instances supported - different roles with different password
66 return new moodle_url('/enrol/paypal/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
67 }
68
69 /**
70 * Adds enrol instance UI to course edit form
71 *
72 * @param object $instance enrol instance or null if does not exist yet
73 * @param MoodleQuickForm $mform
74 * @param object $data
75 * @param object $context context of existing course or parent category if course does not exist
76 * @return void
77 */
78 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
79
80 $i = isset($instance->id) ? $instance->id : 0;
81 $plugin = enrol_get_plugin('paypal');
82 $header = $plugin->get_instance_name($instance);
83 $config = has_capability('moodle/course:enrolconfig', $context);
84
85 $mform->addElement('header', 'enrol_paypal_header_'.$i, $header);
86
87
88 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
89 ENROL_INSTANCE_DISABLED => get_string('no'));
90 $mform->addElement('select', 'enrol_paypal_status_'.$i, get_string('status', 'enrol_paypal'), $options);
91 $mform->setDefault('enrol_paypal_status_'.$i, $this->get_config('status'));
92 $mform->setAdvanced('enrol_paypal_status_'.$i, $this->get_config('status_adv'));
93 if (!$config) {
94 $mform->hardFreeze('enrol_paypal_status_'.$i);
95 }
96
97 $mform->addElement('text', 'enrol_paypal_cost_'.$i, get_string('cost', 'enrol_paypal'), array('size'=>4));
98 $mform->setDefault('enrol_paypal_cost_'.$i, $this->get_config('cost'));
99 if (!$config) {
100 $mform->hardFreeze('enrol_paypal_cost_'.$i);
101 } else {
102 $mform->disabledIf('enrol_paypal_cost_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
103 }
104
105 $paypalcurrencies = array( 'USD' => 'US Dollars',
106 'EUR' => 'Euros',
107 'JPY' => 'Japanese Yen',
108 'GBP' => 'British Pounds',
109 'CAD' => 'Canadian Dollars',
110 'AUD' => 'Australian Dollars'
111 );
112 $mform->addElement('select', 'enrol_paypal_currency_'.$i, get_string('currency', 'enrol_paypal'), $paypalcurrencies);
113 $mform->setDefault('enrol_paypal_currency_'.$i, $this->get_config('currency'));
114 if (!$config) {
115 $mform->hardFreeze('enrol_paypal_currency_'.$i);
116 } else {
117 $mform->disabledIf('enrol_paypal_currency_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
118 }
119
120
121 if ($instance) {
122 $roles = get_default_enrol_roles($context, $instance->roleid);
123 } else {
124 $roles = get_default_enrol_roles($context, $this->get_config('roleid'));
125 }
126 $mform->addElement('select', 'enrol_paypal_roleid_'.$i, get_string('assignrole', 'enrol_paypal'), $roles);
127 $mform->setDefault('enrol_paypal_roleid_'.$i, $this->get_config('roleid'));
128 $mform->setAdvanced('enrol_paypal_roleid_'.$i, $this->get_config('roleid_adv'));
129 if (!$config) {
130 $mform->hardFreeze('enrol_paypal_roleid_'.$i);
131 } else {
132 $mform->disabledIf('enrol_paypal_roleid_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
133 }
134
135
136 $mform->addElement('duration', 'enrol_paypal_enrolperiod_'.$i, get_string('enrolperiod', 'enrol_paypal'), array('optional' => true, 'defaultunit' => 86400));
137 $mform->setDefault('enrol_paypal_enrolperiod_'.$i, $this->get_config('enrolperiod'));
138 $mform->setAdvanced('enrol_paypal_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
139 if (!$config) {
140 $mform->hardFreeze('enrol_paypal_enrolperiod_'.$i);
141 } else {
142 $mform->disabledIf('enrol_paypal_enrolperiod_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
143 }
144
145
146 $mform->addElement('date_selector', 'enrol_paypal_enrolstartdate_'.$i, get_string('enrolstartdate', 'enrol_paypal'), array('optional' => true));
147 $mform->setDefault('enrol_paypal_enrolstartdate_'.$i, 0);
148 $mform->setAdvanced('enrol_paypal_enrolstartdate_'.$i, 1);
149 if (!$config) {
150 $mform->hardFreeze('enrol_paypal_enrolstartdate_'.$i);
151 } else {
152 $mform->disabledIf('enrol_paypal_enrolstartdate_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
153 }
154
155
156 $mform->addElement('date_selector', 'enrol_paypal_enrolenddate_'.$i, get_string('enrolenddate', 'enrol_paypal'), array('optional' => true));
157 $mform->setDefault('enrol_paypal_enrolenddate_'.$i, 0);
158 $mform->setAdvanced('enrol_paypal_enrolenddate_'.$i, 1);
159 if (!$config) {
160 $mform->hardFreeze('enrol_paypal_enrolenddate_'.$i);
161 } else {
162 $mform->disabledIf('enrol_paypal_enrolenddate_'.$i, 'enrol_paypal_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
163 }
164
165
166 // now add all values from enrol table
167 if ($instance) {
168 foreach($instance as $key=>$val) {
169 $data->{'enrol_paypal_'.$key.'_'.$i} = $val;
170 }
171 }
172 }
173
174 /**
175 * Validates course edit form data
176 *
177 * @param object $instance enrol instance or null if does not exist yet
178 * @param array $data
179 * @param object $context context of existing course or parent category if course does not exist
180 * @return array errors array
181 */
182 public function course_edit_validation($instance, array $data, $context) {
183 $errors = array();
184
185 if (!has_capability('moodle/course:enrolconfig', $context)) {
186 // we are going to ignore the data later anyway, they would not be able to fix the form anyway
187 return $errors;
188 }
189
190 $i = isset($instance->id) ? $instance->id : 0;
191
192 if ($data['enrol_paypal_status_'.$i] == ENROL_INSTANCE_ENABLED) {
193 if (!empty($data['enrol_paypal_enrolenddate_'.$i]) and $data['enrol_paypal_enrolenddate_'.$i] < $data['enrol_paypal_enrolstartdate_'.$i]) {
194 $errors['enrol_paypal_enrolenddate_'.$i] = get_string('enrolenddaterror', 'enrol_paypal');
195 }
196
197 if (!is_numeric($data['enrol_paypal_cost_'.$i])) {
198 $errors['enrol_paypal_cost_'.$i] = get_string('costerror', 'enrol_paypal');
199
200 }
201 }
202
203 return $errors;
204 }
205
206
207 /**
208 * Called after updating/inserting course.
209 *
210 * @param bool $inserted true if course just inserted
211 * @param object $course
212 * @param object $data form data
213 * @return void
214 */
215 public function course_updated($inserted, $course, $data) {
216 global $DB;
217
218 $context = get_context_instance(CONTEXT_COURSE, $course->id);
219
220 if (has_capability('moodle/course:enrolconfig', $context)) {
221 if ($inserted) {
222 if (isset($data->enrol_paypal_status_0)) {
223 $fields = array('status'=>$data->enrol_paypal_status_0);
224 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
225 $fields['cost'] = $data->enrol_paypal_cost_0;
226 $fields['currency'] = $data->enrol_paypal_currency_0;
227 $fields['roleid'] = $data->enrol_paypal_roleid_0;
228 $fields['enrolperiod'] = $data->enrol_paypal_enrolperiod_0;
229 $fields['enrolstartdate'] = $data->enrol_paypal_enrolstartdate_0;
230 $fields['enrolenddate'] = $data->enrol_paypal_enrolenddate_0;
231 } else {
232 $fields['roleid'] = $this->get_config('roleid');
233 $fields['cost'] = $this->get_config('cost');
234 $fields['currency'] = $this->get_config('currency');
235 $fields['enrolperiod'] = $this->get_config('enrolperiod');
236 $fields['enrolstartdate'] = 0;
237 $fields['enrolenddate'] = 0;
238 }
239 $this->add_instance($course, $fields);
240 }
241
242 } else {
243 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal'));
244 foreach ($instances as $instance) {
245 $i = $instance->id;
246
247 if (isset($data->{'enrol_paypal_status_'.$i})) {
248 $instance->status = $data->{'enrol_paypal_status_'.$i};
249 $instance->timemodified = time();
250 if ($instance->status == ENROL_INSTANCE_ENABLED) {
251 $instance->roleid = $data->{'enrol_paypal_roleid_'.$i};
252 $instance->cost = $data->{'enrol_paypal_cost_'.$i};
253 $instance->currency = $data->{'enrol_paypal_currency_'.$i};
254 $instance->enrolperiod = $data->{'enrol_paypal_enrolperiod_'.$i};
255 $instance->enrolstartdate = $data->{'enrol_paypal_enrolstartdate_'.$i};
256 $instance->enrolenddate = $data->{'enrol_paypal_enrolenddate_'.$i};
257 }
258 $DB->update_record('enrol', $instance);
259 }
260 }
261 }
262
263 } else {
264 if ($inserted) {
265 if ($this->get_config('defaultenrol')) {
266 $this->add_default_instance($course);
267 }
268 } else {
269 // bad luck, user can not change anything
270 }
271 }
272 }
273
274
275 /**
276 * Creates course enrol form, checks if form submitted
277 * and enrols user if necessary. It can also redirect.
278 *
279 * @param stdClass $instance
280 * @return string html text, usually a form in a text box
281 */
282 function enrol_page_hook(stdClass $instance) {
283 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
284
285 ob_start();
286
287 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
288 return ob_get_clean();
289 }
290
291 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
292 return ob_get_clean();
293 }
294
295 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
296 return ob_get_clean();
297 }
298
299 $course = $DB->get_record('course', array('id'=>$instance->courseid));
300
301 $strloginto = get_string("loginto", "", $course->shortname);
302 $strcourses = get_string("courses");
303
304 $context = get_context_instance(CONTEXT_COURSE, $course->id);
305 // Pass $view=true to filter hidden caps if the user cannot see them
306 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
307 '', '', '', '', false, true)) {
308 $users = sort_by_roleassignment_authority($users, $context);
309 $teacher = array_shift($users);
310 } else {
311 $teacher = false;
312 }
313
314 if ( (float) $instance->cost <= 0 ) {
315 $cost = (float) $this->get_config('cost');
316 } else {
317 $cost = (float) $instance->cost;
318 }
319
320 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
321 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
322 } else {
323
324 if ($USER->username == 'guest') { // force login only for guest user, not real users with guest role
325 if (empty($CFG->loginhttps)) {
326 $wwwroot = $CFG->wwwroot;
327 } else {
328 // This actually is not so secure ;-), 'cause we're
329 // in unencrypted connection...
330 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
331 }
332 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
333 echo '<p><b>'.get_string('cost').": $CFG->enrol_currency $cost".'</b></p>';
334 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
335 echo '</div>';
336 } else {
337 //Sanitise some fields before building the PayPal form
338 $coursefullname = $course->fullname;
339 $courseshortname = $course->shortname;
340 $userfullname = fullname($USER);
341 $userfirstname = $USER->firstname;
342 $userlastname = $USER->lastname;
343 $useraddress = $USER->address;
344 $usercity = $USER->city;
345
346 include($CFG->dirroot.'/enrol/paypal/enrol.html');
347 }
348
349 }
350
351 return $OUTPUT->box(ob_get_clean());
352 } // enrol_page_hook
353
354} // class