MDL-69166 enrol_fee: use template for payment btn & properly format cost
This commit is contained in:
@@ -197,25 +197,21 @@ class enrol_fee_plugin extends enrol_plugin {
|
||||
if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used.
|
||||
echo '<p>'.get_string('nocost', 'enrol_fee').'</p>';
|
||||
} else {
|
||||
$localisedcost = format_float($cost, -1, true);
|
||||
|
||||
if (isguestuser()) { // Force login only for guest user, not real users with guest role.
|
||||
$wwwroot = $CFG->wwwroot;
|
||||
echo '<div class="mdl-align"><p>'.get_string('paymentrequired').'</p>';
|
||||
echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>';
|
||||
echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
$coursefullname = format_string($course->fullname, true, ['context' => $context]);
|
||||
\core_payment\helper::gateways_modal_requirejs();
|
||||
$attributes = core_payment\helper::gateways_modal_link_params($cost, $instance->currency, 'enrol_fee',
|
||||
$instance->id, get_string('purchasedescription', 'enrol_fee', $coursefullname));
|
||||
|
||||
echo '<div align="center">' .
|
||||
html_writer::tag('button', get_string("sendpaymentbutton", "enrol_paypal"), $attributes) .
|
||||
'</div>';
|
||||
}
|
||||
$locale = get_string('localecldr', 'langconfig');
|
||||
$fmt = NumberFormatter::create($locale, NumberFormatter::CURRENCY);
|
||||
$localisedcost = numfmt_format_currency($fmt, $cost, $instance->currency);
|
||||
|
||||
$data = [
|
||||
'isguestuser' => isguestuser(),
|
||||
'cost' => $localisedcost,
|
||||
'currency' => $instance->currency,
|
||||
'amount' => $cost,
|
||||
'instanceid' => $instance->id,
|
||||
'description' => get_string('purchasedescription', 'enrol_fee',
|
||||
format_string($course->fullname, true, ['context' => $context])),
|
||||
];
|
||||
echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data);
|
||||
}
|
||||
|
||||
return $OUTPUT->box(ob_get_clean());
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
{{!
|
||||
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-amount
|
||||
* data-currency
|
||||
* data-component
|
||||
* data-componentid
|
||||
* data-description
|
||||
|
||||
Context variables required for this template:
|
||||
* cost - Human readable cost string including amount and currency
|
||||
* amount - amount of the cost
|
||||
* currency - currency of the cost
|
||||
* instanceid - Id of the enrolment instance
|
||||
* description - The description for this purchase
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"cost": "$108.50",
|
||||
"amount": 108.50,
|
||||
"currency": "AUD",
|
||||
"instanceid": 11,
|
||||
"description": "Enrolment in course Introduction to algorithms",
|
||||
"isguestuser": false
|
||||
}
|
||||
|
||||
}}
|
||||
<div class="enrol_fee_payment_region text-center">
|
||||
{{#isguestuser}}
|
||||
<div class="mdl-align">
|
||||
<p>{{# str }} paymentrequired {{/ str}}</p>
|
||||
<p><b>{{cost}}</b></p>
|
||||
<p><a href="{{config.wwwroot}}/login/">{{# str }} loginsite {{/ str }}</a></p>
|
||||
</div>
|
||||
{{/isguestuser}}
|
||||
{{^isguestuser}}
|
||||
<p>{{# str }} paymentrequired {{/ str}}</p>
|
||||
<p><b>{{cost}}</b></p>
|
||||
<button
|
||||
class="btn btn-secondary"
|
||||
type="button"
|
||||
id="gateways-modal-trigger-{{ uniqid }}"
|
||||
data-amount="{{amount}}"
|
||||
data-currency="{{currency}}"
|
||||
data-component="enrol_fee"
|
||||
data-componentid="{{instanceid}}"
|
||||
data-description={{# quote }}{{description}}{{/ quote }}
|
||||
>
|
||||
{{# str }} sendpaymentbutton, enrol_fee {{/ str }}
|
||||
</button>
|
||||
{{/isguestuser}}
|
||||
</div>
|
||||
{{#js}}
|
||||
require(['core_payment/gateways_modal'], function(modal) {
|
||||
modal.registerEventListeners(document.querySelector('#gateways-modal-trigger-{{ uniqid }}'));
|
||||
});
|
||||
{{/js}}
|
||||
@@ -32,6 +32,7 @@ $string['iso6392'] = 'eng';
|
||||
$string['labelsep'] = ': ';
|
||||
$string['listsep'] = ',';
|
||||
$string['locale'] = 'en_AU.UTF-8';
|
||||
$string['localecldr'] = 'en-AU';
|
||||
$string['localewin'] = 'English_Australia.1252';
|
||||
$string['localewincharset'] = '';
|
||||
$string['oldcharset'] = 'ISO-8859-1';
|
||||
|
||||
@@ -1126,6 +1126,7 @@ $string['ip_address'] = 'IP address';
|
||||
$string['jump'] = 'Jump';
|
||||
$string['jumpto'] = 'Jump to...';
|
||||
$string['keep'] = 'Keep';
|
||||
$string['labelvalue'] = '{$a->label}: {$a->value}';
|
||||
$string['langltr'] = 'Language direction left-to-right';
|
||||
$string['langrtl'] = 'Language direction right-to-left';
|
||||
$string['language'] = 'Language';
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core/toast","core/notification"],function(a,b,c,d,e,f,g,h,i){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.registerEventListeners=void 0;b=j(b);c=j(c);f=j(f);g=j(g);i=j(i);var m="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function j(a){return a&&a.__esModule?a:{default:a}}function k(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function l(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){k(h,d,e,f,g,"next",a)}function g(a){k(h,d,e,f,g,"throw",a)}f(void 0)})}}a.registerEventListeners=function registerEventListeners(a){var b=document.querySelector(a);b.addEventListener("click",function(a){a.preventDefault();n(b,{focusOnClose:a.target})})};var n=function(){var a=l(regeneratorRuntime.mark(function a(j){var k,l,m,n,p,q,r,s,t,u,v=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:k=1<v.length&&v[1]!==void 0?v[1]:{},l=k.focusOnClose,m=void 0===l?null:l;a.t0=b.default;a.t1=b.default.types.SAVE_CANCEL;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:n=a.sent;(0,h.addToastRegion)(n.getRoot()[0]);n.show();n.getRoot().on(g.default.hidden,function(){n.destroy();try{m.focus()}catch(a){}});n.getRoot().on(g.default.save,function(a){var b=n.getRoot()[0],c=(b.querySelector(f.default.values.gateway)||{value:""}).value;if(c){o(c,j.dataset.amount,j.dataset.currency,j.dataset.component,j.dataset.componentid,j.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;n.hide();if(b){i.default.addNotification({message:d,type:"success"});location.reload()}else{i.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,h.add)(a)})}a.preventDefault()});p=j.dataset.currency;a.next=20;return(0,e.getGatewaysSupportingCurrency)(p);case 20:q=a.sent;r={gateways:q};a.next=24;return c.default.renderForPromise("core_payment/gateways",r);case 24:s=a.sent;t=s.html;u=s.js;c.default.replaceNodeContents(n.getRoot().find(f.default.regions.gatewaysContainer),t,u);case 28:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),o=function(){var a=l(regeneratorRuntime.mark(function a(b,c,d,e,f,g,h){var i;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof m.define&&m.define.amd?new Promise(function(a,c){m.require(["pg_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&m.require&&"component"===m.require.loader?Promise.resolve(require(("pg_".concat(b,"/gateways_modal")))):Promise.resolve(m["pg_".concat(b,"/gateways_modal")]);case 2:i=a.sent;i.process(c,d,e,f,g,h);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}()});
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core/toast","core/notification"],function(a,b,c,d,e,f,g,h,i){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.registerEventListeners=a.registerEventListenersBySelector=void 0;b=j(b);c=j(c);f=j(f);g=j(g);i=j(i);var m="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function j(a){return a&&a.__esModule?a:{default:a}}function k(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function l(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){k(h,d,e,f,g,"next",a)}function g(a){k(h,d,e,f,g,"throw",a)}f(void 0)})}}a.registerEventListenersBySelector=function registerEventListenersBySelector(a){document.querySelectorAll(a).forEach(function(a){n(a)})};var n=function(a){a.addEventListener("click",function(b){b.preventDefault();o(a,{focusOnClose:b.target})})};a.registerEventListeners=n;var o=function(){var a=l(regeneratorRuntime.mark(function a(j){var k,l,m,n,o,q,r,s,t,u,v=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:k=1<v.length&&v[1]!==void 0?v[1]:{},l=k.focusOnClose,m=void 0===l?null:l;a.t0=b.default;a.t1=b.default.types.SAVE_CANCEL;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:n=a.sent;(0,h.addToastRegion)(n.getRoot()[0]);n.show();n.getRoot().on(g.default.hidden,function(){n.destroy();try{m.focus()}catch(a){}});n.getRoot().on(g.default.save,function(a){var b=n.getRoot()[0],c=(b.querySelector(f.default.values.gateway)||{value:""}).value;if(c){p(c,j.dataset.amount,j.dataset.currency,j.dataset.component,j.dataset.componentid,j.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;n.hide();if(b){i.default.addNotification({message:d,type:"success"});location.reload()}else{i.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,h.add)(a)})}a.preventDefault()});o=j.dataset.currency;a.next=20;return(0,e.getGatewaysSupportingCurrency)(o);case 20:q=a.sent;r={gateways:q};a.next=24;return c.default.renderForPromise("core_payment/gateways",r);case 24:s=a.sent;t=s.html;u=s.js;c.default.replaceNodeContents(n.getRoot().find(f.default.regions.gatewaysContainer),t,u);case 28:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),p=function(){var a=l(regeneratorRuntime.mark(function a(b,c,d,e,f,g,h){var i;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof m.define&&m.define.amd?new Promise(function(a,c){m.require(["pg_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&m.require&&"component"===m.require.loader?Promise.resolve(require(("pg_".concat(b,"/gateways_modal")))):Promise.resolve(m["pg_".concat(b,"/gateways_modal")]);case 2:i=a.sent;i.process(c,d,e,f,g,h);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}()});
|
||||
//# sourceMappingURL=gateways_modal.min.js.map
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -36,9 +36,18 @@ import Notification from 'core/notification';
|
||||
*
|
||||
* @param {string} nodeSelector The root to listen to.
|
||||
*/
|
||||
export const registerEventListeners = (nodeSelector) => {
|
||||
const rootNode = document.querySelector(nodeSelector);
|
||||
export const registerEventListenersBySelector = (nodeSelector) => {
|
||||
document.querySelectorAll(nodeSelector).forEach((element) => {
|
||||
registerEventListeners(element);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Register event listeners for the module.
|
||||
*
|
||||
* @param {HTMLElement} rootNode The root to listen to.
|
||||
*/
|
||||
export const registerEventListeners = (rootNode) => {
|
||||
rootNode.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
show(rootNode, {focusOnClose: e.target});
|
||||
|
||||
@@ -76,21 +76,6 @@ class helper {
|
||||
return $gateways;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the JS libraries for the pay button.
|
||||
*/
|
||||
public static function gateways_modal_requirejs(): void {
|
||||
global $PAGE;
|
||||
|
||||
static $done = false;
|
||||
if ($done) {
|
||||
return;
|
||||
}
|
||||
|
||||
$PAGE->requires->js_call_amd('core_payment/gateways_modal', 'registerEventListeners', ['#gateways-modal-trigger']);
|
||||
$done = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attributes to place on a pay button.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user