Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 22... Línea 22...
22
 * @package    enrol_paypal
22
 * @package    enrol_paypal
23
 * @copyright  2010 Eugene Venter
23
 * @copyright  2010 Eugene Venter
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
25
 */
Línea -... Línea 26...
-
 
26
 
26
 
27
use core\output\single_button;
Línea 27... Línea 28...
27
defined('MOODLE_INTERNAL') || die();
28
use core_enrol\output\enrol_page;
28
 
29
 
29
/**
30
/**
30
 * Paypal enrolment plugin implementation.
31
 * Paypal enrolment plugin implementation.
Línea 125... Línea 126...
125
     * Add new instance of enrol plugin.
126
     * Add new instance of enrol plugin.
126
     * @param object $course
127
     * @param object $course
127
     * @param array $fields instance fields
128
     * @param array $fields instance fields
128
     * @return int id of new instance, null if can not be created
129
     * @return int id of new instance, null if can not be created
129
     */
130
     */
130
    public function add_instance($course, array $fields = null) {
131
    public function add_instance($course, ?array $fields = null) {
131
        if ($fields && !empty($fields['cost'])) {
132
        if ($fields && !empty($fields['cost'])) {
132
            $fields['cost'] = unformat_float($fields['cost']);
133
            $fields['cost'] = unformat_float($fields['cost']);
133
        }
134
        }
134
        return parent::add_instance($course, $fields);
135
        return parent::add_instance($course, $fields);
135
    }
136
    }
Línea 145... Línea 146...
145
            $data->cost = unformat_float($data->cost);
146
            $data->cost = unformat_float($data->cost);
146
        }
147
        }
147
        return parent::update_instance($instance, $data);
148
        return parent::update_instance($instance, $data);
148
    }
149
    }
Línea 149... Línea -...
149
 
-
 
150
    /**
-
 
151
     * Creates course enrol form, checks if form submitted
-
 
152
     * and enrols user if necessary. It can also redirect.
-
 
153
     *
-
 
154
     * @param stdClass $instance
-
 
155
     * @return string html text, usually a form in a text box
150
 
156
     */
151
    #[\Override]
157
    function enrol_page_hook(stdClass $instance) {
152
    public function enrol_page_hook(stdClass $instance) {
Línea 158... Línea -...
158
        global $CFG, $USER, $OUTPUT, $PAGE, $DB;
-
 
159
 
-
 
160
        ob_start();
153
        global $CFG, $USER, $OUTPUT, $PAGE, $DB;
161
 
154
 
162
        if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
155
        if ($DB->record_exists('user_enrolments', ['userid' => $USER->id, 'enrolid' => $instance->id])) {
Línea 163... Línea 156...
163
            return ob_get_clean();
156
            return '';
164
        }
157
        }
165
 
158
 
Línea 166... Línea 159...
166
        if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
159
        if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
167
            return ob_get_clean();
160
            return '';
168
        }
161
        }
Línea 169... Línea 162...
169
 
162
 
170
        if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
163
        if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
Línea 171... Línea -...
171
            return ob_get_clean();
-
 
172
        }
-
 
173
 
-
 
174
        $course = $DB->get_record('course', array('id'=>$instance->courseid));
-
 
175
        $context = context_course::instance($course->id);
-
 
176
 
-
 
177
        $shortname = format_string($course->shortname, true, array('context' => $context));
-
 
178
        $strloginto = get_string("loginto", "", $shortname);
-
 
179
        $strcourses = get_string("courses");
-
 
180
 
-
 
181
        // Pass $view=true to filter hidden caps if the user cannot see them
-
 
182
        if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
-
 
183
                                             '', '', '', '', false, true)) {
-
 
184
            $users = sort_by_roleassignment_authority($users, $context);
164
            return '';
185
            $teacher = array_shift($users);
165
        }
186
        } else {
166
 
187
            $teacher = false;
167
        $course = $DB->get_record('course', ['id' => $instance->courseid]);
188
        }
168
        $context = context_course::instance($course->id);
Línea 189... Línea -...
189
 
-
 
190
        if ( (float) $instance->cost <= 0 ) {
169
 
191
            $cost = (float) $this->get_config('cost');
-
 
Línea -... Línea 170...
-
 
170
        if ( (float) $instance->cost <= 0 ) {
-
 
171
            $cost = (float) $this->get_config('cost');
-
 
172
        } else {
-
 
173
            $cost = (float) $instance->cost;
-
 
174
        }
-
 
175
 
-
 
176
        $name = $this->get_instance_name($instance);
-
 
177
 
-
 
178
        if (abs($cost) < 0.01) {
-
 
179
            // No cost, other enrolment methods (instances) should be used.
192
        } else {
180
            $notification = new \core\output\notification(get_string('nocost', 'enrol_paypal'), 'error', false);
193
            $cost = (float) $instance->cost;
181
            $notification->set_extra_classes(['mb-0']);
194
        }
182
            $enrolpage = new enrol_page(
195
 
183
                instance: $instance,
Línea 196... Línea 184...
196
        if (abs($cost) < 0.01) { // no cost, other enrolment methods (instances) should be used
184
                header: $name,
197
            echo '<p>'.get_string('nocost', 'enrol_paypal').'</p>';
-
 
198
        } else {
185
                body: $OUTPUT->render($notification));
199
 
186
            return $OUTPUT->render($enrolpage);
200
            // Calculate localised and "." cost, make sure we send PayPal the same value,
187
        } else {
201
            // please note PayPal expects amount with 2 decimal places and "." separator.
188
            // Calculate localised and "." cost, make sure we send PayPal the same value,
202
            $localisedcost = format_float($cost, 2, true);
189
            // please note PayPal expects amount with 2 decimal places and "." separator.
203
            $cost = format_float($cost, 2, false);
190
            $localisedcost = format_float($cost, 2, true);
204
 
191
            $cost = format_float($cost, 2, false);
205
            if (isguestuser()) { // force login only for guest user, not real users with guest role
192
 
206
                $wwwroot = $CFG->wwwroot;
193
            $body = $OUTPUT->render_from_template('enrol_paypal/enrol_page',
207
                echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
194
                ['currency' => $instance->currency, 'cost' => $localisedcost]);
208
                echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>';
195
            if (isguestuser() || !isloggedin()) {
209
                echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
196
                $button = new single_button(new moodle_url(get_login_url()), get_string('loginsite'), 'get',
210
                echo '</div>';
197
                    single_button::BUTTON_PRIMARY);
-
 
198
            } else {
-
 
199
                // Sanitise some fields before building the PayPal form.
-
 
200
                $coursefullname  = format_string($course->fullname, true, ['context' => $context]);
-
 
201
                $courseshortname = format_string($course->shortname, true, ['context' => $context]);
-
 
202
                $userfullname    = fullname($USER);
-
 
203
                $userfirstname   = $USER->firstname;
211
            } else {
204
                $userlastname    = $USER->lastname;
212
                //Sanitise some fields before building the PayPal form
-
 
-
 
205
                $useraddress     = $USER->address;
-
 
206
                $usercity        = $USER->city;
-
 
207
                $buttonurl = new moodle_url(empty($CFG->usepaypalsandbox) ?
-
 
208
                        'https://www.paypal.com/cgi-bin/webscr' :
-
 
209
                        'https://www.sandbox.paypal.com/cgi-bin/webscr',
-
 
210
                    [
-
 
211
                        'cmd' => '_xclick',
-
 
212
                        'charset' => 'utf-8',
-
 
213
                        'business' => $this->get_config('paypalbusiness'),
-
 
214
                        'item_name' => $coursefullname,
-
 
215
                        'item_number' => $courseshortname,
213
                $coursefullname  = format_string($course->fullname, true, array('context'=>$context));
216
                        'quantity' => 1,
-
 
217
                        'on0' => get_string("user"),
-
 
218
                        'os0' => $userfullname,
-
 
219
                        'custom' => "{$USER->id}-{$course->id}-{$instance->id}",
-
 
220
                        'currency_code' => $instance->currency,
-
 
221
                        'amount' => $cost,
-
 
222
                        'for_auction' => 'false',
-
 
223
                        'no_note' => 1,
-
 
224
                        'no_shipping' => 1,
-
 
225
                        'notify_url' => "$CFG->wwwroot/enrol/paypal/ipn.php",
-
 
226
                        'return' => "$CFG->wwwroot/enrol/paypal/return.php?id={$course->id}",
-
 
227
                        'cancel_return' => $CFG->wwwroot,
-
 
228
                        'rm' => 2,
-
 
229
                        'cbt' => get_string("continuetocourse"),
214
                $courseshortname = $shortname;
230
                        'first_name' => $userfirstname,
Línea -... Línea 231...
-
 
231
                        'last_name' => $userlastname,
-
 
232
                        'address' => $useraddress,
-
 
233
                        'city' => $usercity,
-
 
234
                        'email' => $USER->email,
-
 
235
                        'country' => $USER->country,
-
 
236
                    ]);
215
                $userfullname    = fullname($USER);
237
                $button = new single_button($buttonurl, get_string("sendpaymentbutton", "enrol_paypal"),
216
                $userfirstname   = $USER->firstname;
-
 
217
                $userlastname    = $USER->lastname;
-
 
218
                $useraddress     = $USER->address;
238
                    'get', single_button::BUTTON_PRIMARY);
Línea 219... Línea 239...
219
                $usercity        = $USER->city;
239
            }
220
                $instancename    = $this->get_instance_name($instance);
240
 
221
 
241
            $enrolpage = new enrol_page(