From 129ab8007ec818e8ecaa85c9beeca9efd46a1752 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 18 Feb 2025 13:34:26 +0000 Subject: [PATCH] MDL-84142 enrol_fee: use new template for the self enrolment widget --- enrol/fee/classes/plugin.php | 101 ++++++++++---------- enrol/fee/templates/enrol_page.mustache | 30 ++++++ enrol/fee/templates/payment_region.mustache | 81 ---------------- enrol/fee/tests/behat/fee.feature | 6 +- lang/en/moodle.php | 2 +- 5 files changed, 82 insertions(+), 138 deletions(-) create mode 100644 enrol/fee/templates/enrol_page.mustache delete mode 100644 enrol/fee/templates/payment_region.mustache diff --git a/enrol/fee/classes/plugin.php b/enrol/fee/classes/plugin.php index 3ca18f0b699..c8603a4fa43 100644 --- a/enrol/fee/classes/plugin.php +++ b/enrol/fee/classes/plugin.php @@ -24,6 +24,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core\output\single_button; +use core_enrol\output\enrol_page; + /** * Fee enrolment plugin implementation. * @@ -172,53 +175,20 @@ class enrol_fee_plugin extends enrol_plugin { return parent::update_instance($instance, $data); } - /** - * Creates course enrol form, checks if form submitted - * and enrols user if necessary. It can also redirect. - * - * @param stdClass $instance - * @return string html text, usually a form in a text box - */ + #[\Override] public function enrol_page_hook(stdClass $instance) { - return $this->show_payment_info($instance); - } - - /** - * Returns optional enrolment instance description text. - * - * This is used in detailed course information. - * - * - * @param object $instance - * @return string short html text - */ - public function get_description_text($instance) { - return $this->show_payment_info($instance); - } - - /** - * Generates payment information to display on enrol/info page. - * - * @param stdClass $instance - * @return false|string - * @throws coding_exception - * @throws dml_exception - */ - private function show_payment_info(stdClass $instance) { - global $USER, $OUTPUT, $DB; - - ob_start(); + global $USER, $OUTPUT, $DB, $PAGE; if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) { - return ob_get_clean(); + return ''; } if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { - return ob_get_clean(); + return ''; } if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { - return ob_get_clean(); + return ''; } $course = $DB->get_record('course', array('id' => $instance->courseid)); @@ -230,26 +200,51 @@ class enrol_fee_plugin extends enrol_plugin { $cost = (float) $instance->cost; } + $name = !empty($instance->name) ? + format_string($instance->name, true, ['context' => $context]) : + get_string('paymentrequired'); + if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used. - echo '

'.get_string('nocost', 'enrol_fee').'

'; + $notification = new \core\output\notification(get_string('nocost', 'enrol_fee'), 'error', false); + $notification->set_extra_classes(['mb-0']); + $enrolpage = new enrol_page( + instance: $instance, + header: $name, + body: $OUTPUT->render($notification)); + return $OUTPUT->render($enrolpage); } else { + if (isguestuser() || !isloggedin()) { + $button = new single_button(new moodle_url(get_login_url()), get_string('loginsite'), + 'get', single_button::BUTTON_PRIMARY); + } else { + $PAGE->requires->js_call_amd('core_payment/gateways_modal', 'init'); + $button = new single_button( + $PAGE->url, + get_string('sendpaymentbutton', 'enrol_fee'), + 'post', + single_button::BUTTON_PRIMARY, + [ + 'data-action' => 'core_payment/triggerPayment', + 'data-component' => 'enrol_fee', + 'data-paymentarea' => 'fee', + 'data-itemid' => $instance->id, + 'data-cost' => $cost, + 'data-successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false), + 'data-description' => get_string('purchasedescription', 'enrol_fee', + format_string($course->fullname, true, ['context' => $context])), + ]); + } - $name = !empty($instance->name) ? - format_string($instance->name, true, ['context' => $context]) : - get_string('paymentrequired'); - $data = [ - 'name' => $name, - 'isguestuser' => isguestuser() || !isloggedin(), + $body = $OUTPUT->render_from_template('enrol_fee/enrol_page', [ 'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency), - 'instanceid' => $instance->id, - 'description' => get_string('purchasedescription', 'enrol_fee', - format_string($course->fullname, true, ['context' => $context])), - 'successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false), - ]; - echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data); + ]); + $enrolpage = new enrol_page( + instance: $instance, + header: $name, + body: $body, + buttons: [$button]); + return $OUTPUT->render($enrolpage); } - - return $OUTPUT->box(ob_get_clean()); } /** diff --git a/enrol/fee/templates/enrol_page.mustache b/enrol/fee/templates/enrol_page.mustache new file mode 100644 index 00000000000..fbb0275dfc4 --- /dev/null +++ b/enrol/fee/templates/enrol_page.mustache @@ -0,0 +1,30 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template enrol_fee/enrol_page + + Contents of the enrolment widget on the course enrolment page + + Example context (json): + { + "cost": "10.00", + "currency": "USD" + } +}} + +{{#str}} labelvalue, core, {"label": {{#quote}}{{#str}} cost {{/str}}{{/quote}}, "value": {{#quote}}{{cost}}{{/quote}} }{{/str}} + diff --git a/enrol/fee/templates/payment_region.mustache b/enrol/fee/templates/payment_region.mustache deleted file mode 100644 index 9ad10514bd7..00000000000 --- a/enrol/fee/templates/payment_region.mustache +++ /dev/null @@ -1,81 +0,0 @@ -{{! - This file is part of Moodle - http://moodle.org/ - - Moodle is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Moodle is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Moodle. If not, see . -}} -{{! - @template enrol_fee/payment_region - - This template will render information about course fee along with a button for payment. - - Classes required for JS: - * none - - Data attributes required for JS: - * data-component - * data-paymentarea - * data-itemid - * data-cost - * data-description - * data-successurl - - Context variables required for this template: - * cost - Human readable cost string including amount and currency - * instanceid - Id of the enrolment instance - * description - The description for this purchase - * successurl - The URL of the course - - Example context (json): - { - "cost": "$108.50", - "name": "This course requires a payment for entry.", - "instanceid": 11, - "description": "Enrolment in course Introduction to algorithms", - "successurl": "https://moodlesite/course/view.php?id=2", - "isguestuser": false - } - -}} -
- {{#isguestuser}} -
-

{{{name}}}

-

{{cost}}

-

{{# str }} loginsite {{/ str }}

-
- {{/isguestuser}} - {{^isguestuser}} -

{{{name}}}

-

{{cost}}

- - {{/isguestuser}} -
-{{#js}} - require(['core_payment/gateways_modal'], function(modal) { - modal.init(); - }); -{{/js}} diff --git a/enrol/fee/tests/behat/fee.feature b/enrol/fee/tests/behat/fee.feature index 94cf3ad7039..8896b716cea 100644 --- a/enrol/fee/tests/behat/fee.feature +++ b/enrol/fee/tests/behat/fee.feature @@ -36,7 +36,7 @@ Feature: Signing up for a course with a fee enrolment method When I log in as "student1" And I am on course index And I follow "Course 1" - Then I should see "This course requires a payment for entry." + Then I should see "This course requires a payment for entry" And I should see "123.45" And I press "Select payment type" And I should see "PayPal" in the "Select payment type" "dialogue" @@ -46,7 +46,7 @@ Feature: Signing up for a course with a fee enrolment method When I log in as "guest" And I am on course index And I follow "Course 1" - Then I should see "This course requires a payment for entry." + Then I should see "This course requires a payment for entry" And I should see "123.45" And I should see "Log in to the site" @@ -66,6 +66,6 @@ Feature: Signing up for a course with a fee enrolment method When I log in as "student1" And I am on course index And I follow "Course 1" - Then I should not see "This course requires a payment for entry." + Then I should not see "This course requires a payment for entry" Then I should see "Lifetime access" Then I should not see "Only for teachers" diff --git a/lang/en/moodle.php b/lang/en/moodle.php index de2625673e2..8aed1cfa9a6 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1658,7 +1658,7 @@ $string['pathnotexists'] = 'Path doesn\'t exist in your server!'; $string['pathslasherror'] = 'Path can\'t end with a slash!!'; $string['paymentinstant'] = 'Use the button below to pay and be enrolled within minutes!'; $string['paymentpending'] = '({$a} pending)'; -$string['paymentrequired'] = 'This course requires a payment for entry.'; +$string['paymentrequired'] = 'This course requires a payment for entry'; $string['payments'] = 'Payments'; $string['paymentsorry'] = 'Thank you for your payment! Unfortunately your payment has not yet been fully processed, and you are not yet registered to enter the course "{$a->fullname}". Please try continuing to the course in a few seconds, but if you continue to have trouble then please alert the {$a->teacher} or the site administrator'; $string['paymentthanks'] = 'Thank you for your payment! You are now enrolled in your course:
"{$a}"';