MDL-84142 enrol_fee: use new template for the self enrolment widget

This commit is contained in:
Marina Glancy
2025-03-13 13:06:44 +00:00
parent 58a1667f92
commit 129ab8007e
5 changed files with 82 additions and 138 deletions
+48 -53
View File
@@ -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 '<p>'.get_string('nocost', 'enrol_fee').'</p>';
$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());
}
/**
+30
View File
@@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
@template enrol_fee/enrol_page
Contents of the enrolment widget on the course enrolment page
Example context (json):
{
"cost": "10.00",
"currency": "USD"
}
}}
<span>
{{#str}} labelvalue, core, {"label": {{#quote}}{{#str}} cost {{/str}}{{/quote}}, "value": {{#quote}}{{cost}}{{/quote}} }{{/str}}
</span>
@@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
@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
}
}}
<div class="enrol_fee_payment_region text-center">
{{#isguestuser}}
<div class="mdl-align">
<p>{{{name}}}</p>
<p><b>{{cost}}</b></p>
<p><a href="{{config.wwwroot}}/login/">{{# str }} loginsite {{/ str }}</a></p>
</div>
{{/isguestuser}}
{{^isguestuser}}
<p>{{{name}}}</p>
<p><b>{{cost}}</b></p>
<button
class="btn btn-secondary"
type="button"
id="gateways-modal-trigger-{{ uniqid }}"
data-action="core_payment/triggerPayment"
data-component="enrol_fee"
data-paymentarea="fee"
data-itemid="{{instanceid}}"
data-cost="{{cost}}"
data-successurl="{{successurl}}"
data-description="{{description}}"
>
{{# str }} sendpaymentbutton, enrol_fee {{/ str }}
</button>
{{/isguestuser}}
</div>
{{#js}}
require(['core_payment/gateways_modal'], function(modal) {
modal.init();
});
{{/js}}
+3 -3
View File
@@ -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"
+1 -1
View File
@@ -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'] = '(<small><b><u>{$a}</u></b> pending</small>)';
$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:<br />"{$a}"';