MDL-22795 new paypal icons - credit goes to Rossiani Wijaya
[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
5bd2219b
PS
37 /**
38 * Returns optional enrolment information icons.
39 *
40 * This is used in course list for quick overview of enrolment options.
41 *
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.
45 *
46 * @param array $instances all enrol instances of this type in one course
47 * @return array of pix_icon
48 */
49 public function get_info_icons(array $instances) {
50 return array(new pix_icon('icon', get_string('pluginname', 'enrol_paypal'), 'enrol_paypal'));
51 }
5ea88553 52
42761bc6 53 public function roles_protected() {
1d6ba3f5 54 // users with role assign cap may tweak the roles later
42761bc6
PS
55 return false;
56 }
5ea88553 57
42761bc6
PS
58 public function allow_unenrol(stdClass $instance) {
59 // users with unenrol cap may unenrol other users manually - requires enrol/paypal:unenrol
60 return true;
61 }
5ea88553 62
42761bc6
PS
63 public function allow_manage(stdClass $instance) {
64 // users with manage cap may tweak period and status - requires enrol/paypal:manage
65 return true;
5ea88553
AB
66 }
67
42761bc6
PS
68 public function show_enrolme_link(stdClass $instance) {
69 return ($instance->status == ENROL_INSTANCE_ENABLED);
5ea88553
AB
70 }
71
72 /**
42761bc6 73 * Sets up navigation entries.
5ea88553 74 *
42761bc6 75 * @param object $instance
5ea88553
AB
76 * @return void
77 */
42761bc6
PS
78 public function add_course_navigation($instancesnode, stdClass $instance) {
79 if ($instance->enrol !== 'paypal') {
80 throw new coding_exception('Invalid enrol instance type!');
5ea88553
AB
81 }
82
42761bc6
PS
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);
5ea88553
AB
87 }
88 }
89
90 /**
42761bc6
PS
91 * Returns edit icons for the page with list of instances
92 * @param stdClass $instance
93 * @return array
5ea88553 94 */
42761bc6
PS
95 public function get_action_icons(stdClass $instance) {
96 global $OUTPUT;
5ea88553 97
42761bc6
PS
98 if ($instance->enrol !== 'paypal') {
99 throw new coding_exception('invalid enrol instance!');
5ea88553 100 }
42761bc6 101 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
5ea88553 102
42761bc6 103 $icons = array();
5ea88553 104
42761bc6
PS
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')));
5ea88553
AB
108 }
109
42761bc6 110 return $icons;
5ea88553
AB
111 }
112
5ea88553 113 /**
42761bc6
PS
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
5ea88553 117 */
e25f2466 118 public function get_newinstance_link($courseid) {
42761bc6 119 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
5ea88553 120
42761bc6
PS
121 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/paypal:config', $context)) {
122 return NULL;
5ea88553 123 }
5ea88553 124
42761bc6
PS
125 // multiple instances supported - different cost for different roles
126 return new moodle_url('/enrol/paypal/edit.php', array('courseid'=>$courseid));
127 }
5ea88553
AB
128
129 /**
130 * Creates course enrol form, checks if form submitted
131 * and enrols user if necessary. It can also redirect.
132 *
133 * @param stdClass $instance
134 * @return string html text, usually a form in a text box
135 */
136 function enrol_page_hook(stdClass $instance) {
137 global $CFG, $USER, $OUTPUT, $PAGE, $DB;
138
139 ob_start();
140
141 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
142 return ob_get_clean();
143 }
144
145 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
146 return ob_get_clean();
147 }
148
149 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
150 return ob_get_clean();
151 }
152
153 $course = $DB->get_record('course', array('id'=>$instance->courseid));
154
155 $strloginto = get_string("loginto", "", $course->shortname);
156 $strcourses = get_string("courses");
157
158 $context = get_context_instance(CONTEXT_COURSE, $course->id);
159 // Pass $view=true to filter hidden caps if the user cannot see them
160 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
161 '', '', '', '', false, true)) {
162 $users = sort_by_roleassignment_authority($users, $context);
163 $teacher = array_shift($users);
164 } else {
165 $teacher = false;
166 }
167
168 if ( (float) $instance->cost <= 0 ) {
169 $cost = (float) $this->get_config('cost');
170 } else {
171 $cost = (float) $instance->cost;
172 }
173
174 if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
175 echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
176 } else {
177
b3df1764 178 if (isguestuser()) { // force login only for guest user, not real users with guest role
5ea88553
AB
179 if (empty($CFG->loginhttps)) {
180 $wwwroot = $CFG->wwwroot;
181 } else {
182 // This actually is not so secure ;-), 'cause we're
183 // in unencrypted connection...
184 $wwwroot = str_replace("http://", "https://", $CFG->wwwroot);
185 }
186 echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
c99a4ea2 187 echo '<p><b>'.get_string('cost').": $instance->currency $cost".'</b></p>';
5ea88553
AB
188 echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
189 echo '</div>';
190 } else {
191 //Sanitise some fields before building the PayPal form
32d66135 192 $coursefullname = format_string($course->fullname, true, array('context'=>$context));
5ea88553
AB
193 $courseshortname = $course->shortname;
194 $userfullname = fullname($USER);
195 $userfirstname = $USER->firstname;
196 $userlastname = $USER->lastname;
197 $useraddress = $USER->address;
198 $usercity = $USER->city;
2c3e42fd 199 $instancename = $this->get_instance_name($instance);
5ea88553
AB
200
201 include($CFG->dirroot.'/enrol/paypal/enrol.html');
202 }
203
204 }
205
206 return $OUTPUT->box(ob_get_clean());
42761bc6 207 }
5ea88553 208
42761bc6 209}