MDL-69166 enrol_fee: use template for payment btn & properly format cost
authorShamim Rezaie <shamim@moodle.com>
Mon, 13 Jan 2020 17:37:01 +0000 (04:37 +1100)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 04:44:59 +0000 (15:44 +1100)
enrol/fee/classes/plugin.php
enrol/fee/templates/payment_region.mustache [new file with mode: 0644]
lang/en/langconfig.php
lang/en/moodle.php
payment/amd/build/gateways_modal.min.js
payment/amd/build/gateways_modal.min.js.map
payment/amd/src/gateways_modal.js
payment/classes/helper.php

index 0e01c4e..1844de0 100644 (file)
@@ -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());
diff --git a/enrol/fee/templates/payment_region.mustache b/enrol/fee/templates/payment_region.mustache
new file mode 100644 (file)
index 0000000..0939cf5
--- /dev/null
@@ -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}}
index 7ec6c45..8d7be16 100644 (file)
@@ -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';
index 3fe5a85..cbd3140 100644 (file)
@@ -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';
index c32988c..60b3213 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js and b/payment/amd/build/gateways_modal.min.js differ
index c75715d..35d4384 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js.map and b/payment/amd/build/gateways_modal.min.js.map differ
index 9d65b6e..1d29915 100644 (file)
@@ -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});
index e3b4f1a..58c8925 100644 (file)
@@ -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.
      *