Skip to content

Commit

Permalink
MDL-69166 core_payment: archiving accounts, triggering events, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy authored and rezaies committed Oct 27, 2020
1 parent 73527fa commit 409857a
Show file tree
Hide file tree
Showing 18 changed files with 893 additions and 21 deletions.
6 changes: 6 additions & 0 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@
$ADMIN->add('modules', new admin_category('paymentgateways', new lang_string('type_pg_plural', 'plugin')));
$temp = new admin_settingpage('managepaymentgateways', new lang_string('type_pgmanage', 'plugin'));
$temp->add(new \core_admin\local\settings\manage_payment_gateway_plugins());
$temp->add(new admin_setting_description(
'managepaymentgatewayspostfix',
'',
new lang_string('gotopaymentaccounts', 'payment',
html_writer::link(new moodle_url('/payment/accounts.php'), get_string('paymentaccounts', 'payment')))
));
$ADMIN->add('paymentgateways', $temp);

$plugins = core_plugin_manager::instance()->get_plugins_of_type('pg');
Expand Down
19 changes: 16 additions & 3 deletions enrol/fee/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,16 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $context)
$mform->addElement('select', 'status', get_string('status', 'enrol_fee'), $options);
$mform->setDefault('status', $this->get_config('status'));

$mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'),
['' => ''] + \core_payment\helper::get_payment_accounts_menu($context));
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');
$accounts = \core_payment\helper::get_payment_accounts_menu($context);
if ($accounts) {
$accounts = ((count($accounts) > 1) ? ['' => ''] : []) + $accounts;
$mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'), $accounts);
} else {
$mform->addElement('static', 'customint1_text', get_string('paymentaccount', 'payment'),
html_writer::span(get_string('noaccountsavilable', 'payment'), 'alert alert-danger'));
$mform->addElement('hidden', 'customint1');
$mform->setType('customint1', PARAM_INT);
}

$mform->addElement('text', 'cost', get_string('cost', 'enrol_fee'), array('size' => 4));
$mform->setType('cost', PARAM_RAW);
Expand Down Expand Up @@ -379,6 +386,12 @@ public function edit_instance_validation($data, $files, $instance, $context) {
$typeerrors = $this->validate_param_types($data, $tovalidate);
$errors = array_merge($errors, $typeerrors);

if ($data['status'] == ENROL_INSTANCE_ENABLED &&
(!$data['customint1']
|| !array_key_exists($data['customint1'], \core_payment\helper::get_payment_accounts_menu($context)))) {
$errors['status'] = 'Enrolments can not be enabled without specifying the payment account';
}

return $errors;
}

Expand Down
46 changes: 46 additions & 0 deletions enrol/fee/tests/behat/fee.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@enrol @enrol_fee
Feature: Signing up for a course with a fee enrolment method

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| manager1 | Manager | 1 | manager1@example.com |
And the following "courses" exist:
| fullname | shortname | format | summary |
| Course 1 | C1 | topics | |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| manager1 | C1 | manager |
And payment plugin "paypal" is enabled
And the following "core_payment > payment accounts" exist:
| name | gateways |
| Account1 | paypal |
And I log in as "admin"
And I navigate to "Plugins > Enrolments > Manage enrol plugins" in site administration
And I click on "Enable" "link" in the "Fee" "table_row"
And I log out

@javascript
Scenario: Student can see the payment prompt on the course enrolment page
When I log in as "manager1"
And I am on "Course 1" course homepage
And I navigate to "Users > Enrolment methods" in current page administration
And I select "Fee" from the "Add method" singleselect
And I set the following fields to these values:
| Payment account | Account1 |
| Enrol cost | 10 |
| Currency | Euro |
And I press "Add method"
And I log out
And I log in as "student1"
And I am on course index
And I follow "Course 1"
And I should see "This course requires a payment for entry."
#And I should see "Cost: EUR 10.00" # TODO for some reason behat does not "see" this text.
And I press "Pay enrolment fee"
And I should see "PayPal" in the "Select Payment Type" "dialogue"
And I click on "Cancel" "button" in the "Select Payment Type" "dialogue"
And I log out
12 changes: 12 additions & 0 deletions lang/en/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,34 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['accountarchived'] = 'Archived';
$string['accountdeleteconfirm'] = 'If this account has previous payments, they will be archived, otherwise all other data will be permanently deleted. Are you sure you want to continue?';
$string['accountname'] = 'Account name';
$string['accountnotavailable'] = 'Not available';
$string['callbacknotimplemented'] = 'The callback is not implemented for component {$a}.';
$string['createaccount'] = 'Create payment account';
$string['deleteorarchive'] = 'Delete or archive';
$string['eventaccountcreated'] = 'Payment account created';
$string['eventaccountdeleted'] = 'Payment account deleted';
$string['eventaccountupdated'] = 'Payment account updated';
$string['feeincludesurcharge'] = '{$a->fee} (includes {$a->surcharge}% surcharge for using this payment type)';
$string['gatewaycannotbeenabled'] = 'The payment gateway cannot be enabled because the configuration is incomplete.';
$string['gatewaydisabled'] = 'Disabled';
$string['gatewayenabled'] = 'Enabled';
$string['gatewaynotfound'] = 'Gateway not found';
$string['gotomanageplugins'] = 'Enable and disable payment gateways and set surcharges via {$a}.';
$string['gotopaymentaccounts'] = 'You can create multiple payment accounts using any of these gateways on the {$a} page';
$string['hidearchived'] = 'Hide archived';
$string['noaccountsavilable'] = 'No payment accounts are available.';
$string['nocurrencysupported'] = 'No payment in any currency is supported. Please make sure that at least one payment gateway is enabled.';
$string['nogateway'] = 'There is no payment gateway that can be used.';
$string['nogatewayselected'] = 'You first need to select a payment gateway.';
$string['payments'] = 'Payments';
$string['paymentaccount'] = 'Payment account';
$string['paymentaccounts'] = 'Payment accounts';
$string['restoreaccount'] = 'Restore';
$string['selectpaymenttype'] = 'Select payment type';
$string['showarchived'] = 'Show archived';
$string['supportedcurrencies'] = 'Supported currencies';
$string['surcharge'] = 'Surcharge (percentage)';
$string['surcharge_desc'] = 'The surcharge is an additional percentage charged to users who choose to pay using this payment gateway.';
1 change: 1 addition & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4295,6 +4295,7 @@
<FIELD NAME="idnumber" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="archived" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
Expand Down
15 changes: 15 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2959,5 +2959,20 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021052500.33);
}

if ($oldversion < 2021052500.26) {

// Define field archived to be added to payment_accounts.
$table = new xmldb_table('payment_accounts');
$field = new xmldb_field('archived', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'enabled');

// Conditionally launch add field archived.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.26);
}

return true;
}
32 changes: 28 additions & 4 deletions payment/accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
require_once(__DIR__ . '/../config.php');
require_once($CFG->libdir . '/adminlib.php');

$showarchived = optional_param('showarchived', false, PARAM_BOOL);

admin_externalpage_setup('paymentaccounts');
$PAGE->set_heading(get_string('paymentaccounts', 'payment'));

$enabledplugins = \core\plugininfo\pg::get_enabled_plugins();

echo $OUTPUT->header();

$accounts = \core_payment\helper::get_payment_accounts_to_manage(context_system::instance());
$accounts = \core_payment\helper::get_payment_accounts_to_manage(context_system::instance(), $showarchived);
$table = new html_table();
$table->head = [get_string('accountname', 'payment'), get_string('type_pg', 'plugin'), ''];
$table->colclasses = ['', '', 'mdl-right'];
Expand All @@ -50,22 +52,44 @@
if (!$account->is_available()) {
$name .= ' ' . html_writer::span(get_string('accountnotavailable', 'payment'), 'badge badge-warning');
}
if ($account->get('archived')) {
$name .= ' ' . html_writer::span(get_string('accountarchived', 'payment'), 'badge badge-secondary');
}

$menu = new action_menu();
$menu->set_alignment(action_menu::TL, action_menu::BL);
$menu->set_menu_trigger(get_string('edit'));
if ($canmanage) {
$menu->add(new action_menu_link_secondary($account->get_edit_url(), null, get_string('edit')));
$deleteurl = $account->get_edit_url(['delete' => 1, 'sesskey' => sesskey()]);
$deleteaction = new confirm_action(get_string('deleteconfirm', 'tool_recyclebin'));
$menu->add(new action_menu_link_secondary($deleteurl, null, get_string('delete')));
if (!$account->get('archived')) {
$deleteurl = $account->get_edit_url(['delete' => 1, 'sesskey' => sesskey()]);
$menu->add(new action_menu_link_secondary($deleteurl, null, get_string('deleteorarchive', 'payment'),
['data-action' => 'delete']));
} else {
$restoreurl = $account->get_edit_url(['restore' => 1, 'sesskey' => sesskey()]);
$menu->add(new action_menu_link_secondary($restoreurl, null, get_string('restoreaccount', 'payment')));
}
}

$table->data[] = [$name, join(', ', $gateways), $OUTPUT->render($menu)];
}

echo html_writer::table($table);

$PAGE->requires->event_handler('[data-action=delete]', 'click', 'M.util.show_confirm_dialog',
array('message' => get_string('accountdeleteconfirm', 'payment')));

echo html_writer::div(html_writer::link(new moodle_url($PAGE->url, ['showarchived' => !$showarchived]),
$showarchived ? get_string('hidearchived', 'payment') : get_string('showarchived', 'payment')), 'mdl-right');

echo $OUTPUT->single_button(new moodle_url('/payment/manage_account.php'), get_string('createaccount', 'payment'), 'get');

if (has_capability('moodle/site:config', context_system::instance())) {
// For administrators add a link to "Manage payment gateways" page.
$link = html_writer::link(new moodle_url('/admin/settings.php', ['section' => 'managepaymentgateways']),
get_string('type_pgmanage', 'plugin'));
$text = get_string('gotomanageplugins', 'payment', $link);
echo html_writer::div($text, 'pt-3');
}

echo $OUTPUT->footer();
4 changes: 4 additions & 0 deletions payment/classes/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ protected static function define_properties() : array {
'type' => PARAM_BOOL,
'default' => true
],
'archived' => [
'type' => PARAM_BOOL,
'default' => false
],
);
}

Expand Down
94 changes: 94 additions & 0 deletions payment/classes/event/account_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
// 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/>.

namespace core_payment\event;

use core\event\base;
use core_payment\account;

/**
* Class account_created
*
* @package core_payment
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


/**
* Class account_created
*
* @package core_payment
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class account_created extends base {

/**
* Initialise event parameters.
*/
protected function init() {
$this->data['objecttable'] = 'payment_accounts';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}

/**
* Create an instance of the event and add a record snapshot
*
* @param account $account
* @return base
* @throws \coding_exception
*/
public static function create_from_account(account $account) {
$eventparams = [
'objectid' => $account->get('id'),
'context' => $account->get_context(),
'other' => ['name' => $account->get('name')]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $account->to_record());
return $event;
}

/**
* Returns localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventaccountcreated', 'payment');
}

/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
$name = s($this->other['name']);
return "The user with id '$this->userid' created payment account with id '$this->objectid' and the name '{$name}'.";
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/payment/accounts.php');
}
}
Loading

0 comments on commit 409857a

Please sign in to comment.