Skip to content

Commit

Permalink
MDL-69166 enrol_fee: use template for payment btn & properly format cost
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Oct 27, 2020
1 parent ad6df31 commit 9d773ee
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 37 deletions.
32 changes: 14 additions & 18 deletions enrol/fee/classes/plugin.php
Expand Up @@ -197,25 +197,21 @@ public function enrol_page_hook(stdClass $instance) {
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());
Expand Down
79 changes: 79 additions & 0 deletions enrol/fee/templates/payment_region.mustache
@@ -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}}
1 change: 1 addition & 0 deletions lang/en/langconfig.php
Expand Up @@ -32,6 +32,7 @@
$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';
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Expand Up @@ -1126,6 +1126,7 @@
$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';
Expand Down
2 changes: 1 addition & 1 deletion payment/amd/build/gateways_modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d773ee

Please sign in to comment.