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