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_fee
22
 * @package    enrol_fee
23
 * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
23
 * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
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
 
-
 
27
use core\output\single_button;
-
 
28
use core_enrol\output\enrol_page;
26
 
29
 
27
/**
30
/**
28
 * Fee enrolment plugin implementation.
31
 * Fee enrolment plugin implementation.
29
 *
32
 *
30
 * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
33
 * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
35
 */
Línea 33... Línea 36...
33
class enrol_fee_plugin extends enrol_plugin {
36
class enrol_fee_plugin extends enrol_plugin {
-
 
37
 
-
 
38
    /**
-
 
39
     * How this enrolment method should be displayed on the "Enrolment methods" page
-
 
40
     *
-
 
41
     * @param stdClass $instance
-
 
42
     * @return string
-
 
43
     */
-
 
44
    public function get_instance_name_for_management_page(stdClass $instance): string {
-
 
45
        $result = $this->get_instance_name($instance);
-
 
46
        if (strlen((string)$instance->customchar1)) {
-
 
47
            $context = context_course::instance($instance->courseid);
-
 
48
            $result .= html_writer::empty_tag('br') .
-
 
49
                html_writer::tag('em', format_string($instance->customchar1, true, ['context' => $context]));
-
 
50
        }
-
 
51
        return $result;
-
 
52
    }
34
 
53
 
35
    /**
54
    /**
36
     * Returns the list of currencies that the payment subsystem supports and therefore we can work with.
55
     * Returns the list of currencies that the payment subsystem supports and therefore we can work with.
37
     *
56
     *
38
     * @return array[currencycode => currencyname]
57
     * @return array[currencycode => currencyname]
Línea 134... Línea 153...
134
     * Add new instance of enrol plugin.
153
     * Add new instance of enrol plugin.
135
     * @param object $course
154
     * @param object $course
136
     * @param array $fields instance fields
155
     * @param array $fields instance fields
137
     * @return int id of new instance, null if can not be created
156
     * @return int id of new instance, null if can not be created
138
     */
157
     */
139
    public function add_instance($course, array $fields = null) {
158
    public function add_instance($course, ?array $fields = null) {
140
        if ($fields && !empty($fields['cost'])) {
159
        if ($fields && !empty($fields['cost'])) {
141
            $fields['cost'] = unformat_float($fields['cost']);
160
            $fields['cost'] = unformat_float($fields['cost']);
142
        }
161
        }
143
        return parent::add_instance($course, $fields);
162
        return parent::add_instance($course, $fields);
144
    }
163
    }
Línea 154... Línea 173...
154
            $data->cost = unformat_float($data->cost);
173
            $data->cost = unformat_float($data->cost);
155
        }
174
        }
156
        return parent::update_instance($instance, $data);
175
        return parent::update_instance($instance, $data);
157
    }
176
    }
Línea 158... Línea -...
158
 
-
 
159
    /**
-
 
160
     * Creates course enrol form, checks if form submitted
-
 
161
     * and enrols user if necessary. It can also redirect.
-
 
162
     *
-
 
163
     * @param stdClass $instance
-
 
164
     * @return string html text, usually a form in a text box
177
 
165
     */
178
    #[\Override]
166
    public function enrol_page_hook(stdClass $instance) {
-
 
167
        return $this->show_payment_info($instance);
-
 
168
    }
-
 
169
 
-
 
170
    /**
-
 
171
     * Returns optional enrolment instance description text.
-
 
172
     *
-
 
173
     * This is used in detailed course information.
-
 
174
     *
-
 
175
     *
-
 
176
     * @param object $instance
-
 
177
     * @return string short html text
-
 
178
     */
-
 
179
    public function get_description_text($instance) {
-
 
180
        return $this->show_payment_info($instance);
-
 
181
    }
-
 
182
 
-
 
183
    /**
-
 
184
     * Generates payment information to display on enrol/info page.
-
 
185
     *
-
 
186
     * @param stdClass $instance
-
 
187
     * @return false|string
-
 
188
     * @throws coding_exception
-
 
189
     * @throws dml_exception
-
 
190
     */
-
 
191
    private function show_payment_info(stdClass $instance) {
179
    public function enrol_page_hook(stdClass $instance) {
192
        global $USER, $OUTPUT, $DB;
-
 
193
 
-
 
Línea 194... Línea 180...
194
        ob_start();
180
        global $USER, $OUTPUT, $DB, $PAGE;
195
 
181
 
196
        if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
182
        if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
Línea 197... Línea 183...
197
            return ob_get_clean();
183
            return '';
198
        }
184
        }
199
 
185
 
Línea 200... Línea 186...
200
        if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
186
        if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
201
            return ob_get_clean();
187
            return '';
202
        }
188
        }
Línea 203... Línea 189...
203
 
189
 
204
        if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
190
        if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
Línea 212... Línea 198...
212
            $cost = (float) $this->get_config('cost');
198
            $cost = (float) $this->get_config('cost');
213
        } else {
199
        } else {
214
            $cost = (float) $instance->cost;
200
            $cost = (float) $instance->cost;
215
        }
201
        }
Línea -... Línea 202...
-
 
202
 
-
 
203
        $name = !empty($instance->name) ?
-
 
204
            format_string($instance->name, true, ['context' => $context]) :
-
 
205
            get_string('paymentrequired');
216
 
206
 
217
        if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used.
207
        if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used.
-
 
208
            $notification = new \core\output\notification(get_string('nocost', 'enrol_fee'), 'error', false);
-
 
209
            $notification->set_extra_classes(['mb-0']);
-
 
210
            $enrolpage = new enrol_page(
-
 
211
                instance: $instance,
-
 
212
                header: $name,
-
 
213
                body: $OUTPUT->render($notification));
218
            echo '<p>'.get_string('nocost', 'enrol_fee').'</p>';
214
            return $OUTPUT->render($enrolpage);
-
 
215
        } else {
-
 
216
            if (isguestuser() || !isloggedin()) {
-
 
217
                $button = new single_button(new moodle_url(get_login_url()), get_string('loginsite'),
-
 
218
                     'get', single_button::BUTTON_PRIMARY);
-
 
219
            } else {
-
 
220
                $PAGE->requires->js_call_amd('core_payment/gateways_modal', 'init');
-
 
221
                $button = new single_button(
-
 
222
                    $PAGE->url,
-
 
223
                    get_string('sendpaymentbutton', 'enrol_fee'),
-
 
224
                    'post',
-
 
225
                    single_button::BUTTON_PRIMARY,
-
 
226
                    [
-
 
227
                        'data-action' => 'core_payment/triggerPayment',
-
 
228
                        'data-component' => 'enrol_fee',
-
 
229
                        'data-paymentarea' => 'fee',
-
 
230
                        'data-itemid' => $instance->id,
-
 
231
                        'data-cost' => $cost,
-
 
232
                        'data-successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false),
-
 
233
                        'data-description' => get_string('purchasedescription', 'enrol_fee',
-
 
234
                            format_string($course->fullname, true, ['context' => $context])),
-
 
235
                    ]);
Línea 219... Línea -...
219
        } else {
-
 
220
 
236
            }
221
            $data = [
237
 
-
 
238
            $body = $OUTPUT->render_from_template('enrol_fee/enrol_page', [
222
                'isguestuser' => isguestuser() || !isloggedin(),
239
                'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency),
223
                'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency),
240
            ]);
224
                'instanceid' => $instance->id,
241
            $enrolpage = new enrol_page(
225
                'description' => get_string('purchasedescription', 'enrol_fee',
242
                instance: $instance,
226
                    format_string($course->fullname, true, ['context' => $context])),
243
                header: $name,
227
                'successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false),
244
                body: $body,
228
            ];
245
                buttons: [$button]);
229
            echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data);
-
 
230
        }
-
 
231
 
246
            return $OUTPUT->render($enrolpage);
Línea 232... Línea 247...
232
        return $OUTPUT->box(ob_get_clean());
247
        }
233
    }
248
    }
234
 
249
 
Línea 307... Línea 322...
307
     * Add elements to the edit instance form.
322
     * Add elements to the edit instance form.
308
     *
323
     *
309
     * @param stdClass $instance
324
     * @param stdClass $instance
310
     * @param MoodleQuickForm $mform
325
     * @param MoodleQuickForm $mform
311
     * @param context $context
326
     * @param context $context
312
     * @return bool
-
 
313
     */
327
     */
314
    public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
328
    public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
Línea 315... Línea 329...
315
 
329
 
316
        $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
330
        $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
-
 
331
        $mform->setType('name', PARAM_TEXT);
-
 
332
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
-
 
333
 
-
 
334
        $mform->addElement('text', 'customchar1', get_string('instancedescription', 'enrol_fee'));
-
 
335
        $mform->setType('customchar1', PARAM_TEXT);
-
 
336
        $mform->addHelpButton('customchar1', 'instancedescription', 'enrol_fee');
Línea 317... Línea 337...
317
        $mform->setType('name', PARAM_TEXT);
337
        $mform->addRule('customchar1', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
318
 
338
 
319
        $options = $this->get_status_options();
339
        $options = $this->get_status_options();
Línea 372... Línea 392...
372
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
392
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
373
     * @param object $instance The instance loaded from the DB
393
     * @param object $instance The instance loaded from the DB
374
     * @param context $context The context of the instance we are editing
394
     * @param context $context The context of the instance we are editing
375
     * @return array of "element_name"=>"error_description" if there are errors,
395
     * @return array of "element_name"=>"error_description" if there are errors,
376
     *         or an empty array if everything is OK.
396
     *         or an empty array if everything is OK.
377
     * @return void
-
 
378
     */
397
     */
379
    public function edit_instance_validation($data, $files, $instance, $context) {
398
    public function edit_instance_validation($data, $files, $instance, $context) {
380
        $errors = array();
399
        $errors = array();
Línea 381... Línea 400...
381
 
400