Skip to content

Commit

Permalink
Check the existence of payment classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Oct 24, 2023
1 parent 32b3561 commit 2da59c3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 24 deletions.
47 changes: 34 additions & 13 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
use core_payment\helper as payment_helper;

/**
* Privacy Subsystem for enrol_wallet implementing null_provider.
Expand Down Expand Up @@ -136,6 +133,9 @@ public static function export_user_data(approved_contextlist $contextlist) {
$walletplugins = $DB->get_records('enrol', ['courseid' => $context->instanceid, 'enrol' => 'wallet']);

foreach ($walletplugins as $walletplugin) {
if (!class_exists('\core_payment\privacy\provider')) {
break;
}
\core_payment\privacy\provider::export_payment_data_for_user_in_context(
$context,
$subcontext,
Expand All @@ -148,6 +148,9 @@ public static function export_user_data(approved_contextlist $contextlist) {
}

if (in_array(SYSCONTEXTID, $contextlist->get_contextids())) {
if (!$DB->table_exists('payments')) {
return;
}
// Orphaned payments for deleted enrollments.
$sql = "SELECT p.*
FROM {payments} p
Expand All @@ -160,6 +163,9 @@ public static function export_user_data(approved_contextlist $contextlist) {

$orphanedpayments = $DB->get_recordset_sql($sql, $params);
foreach ($orphanedpayments as $payment) {
if (!class_exists('\core_payment\privacy\provider')) {
break;
}
\core_payment\privacy\provider::export_payment_data_for_user_in_context(
\context_system::instance(),
$subcontext,
Expand All @@ -180,6 +186,9 @@ public static function export_user_data(approved_contextlist $contextlist) {
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if (!$DB->table_exists('payments')) {
return;
}
if ($context instanceof \context_course) {
$sql = "SELECT p.id
FROM {payments} p
Expand All @@ -189,8 +198,10 @@ public static function delete_data_for_all_users_in_context(\context $context) {
'component' => 'enrol_wallet',
'courseid' => $context->instanceid,
];
if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}

\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
} else if ($context instanceof \context_system) {
// If context is system, then the enrolment belongs to a deleted enrolment.
$sql = "SELECT p.id
Expand All @@ -200,8 +211,9 @@ public static function delete_data_for_all_users_in_context(\context $context) {
$params = [
'component' => 'enrol_wallet',
];

\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}
// Also there if fake items for topping up the wallet.
$sql = "SELECT p.userid
FROM {payments} p
Expand All @@ -211,7 +223,10 @@ public static function delete_data_for_all_users_in_context(\context $context) {
'component' => 'enrol_wallet',
'paymentarea' => 'wallettopup',
];
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);

if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}
// Delete fake items.
$ids = $DB->get_records('payments', ['component' => 'enrol_wallet', 'paymentarea' => 'wallettopup']);
foreach ($ids as $payment) {
Expand All @@ -231,7 +246,9 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
if (empty($contextlist->count())) {
return;
}

if (!$DB->table_exists('payments')) {
return;
}
$contexts = $contextlist->get_contexts();

$courseids = [];
Expand All @@ -251,8 +268,9 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
'component' => 'enrol_wallet',
'userid' => $contextlist->get_user()->id,
];

\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}

if (in_array(SYSCONTEXTID, $contextlist->get_contextids())) {
// Orphaned payments.
Expand All @@ -265,8 +283,9 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
'component' => 'enrol_wallet',
'userid' => $contextlist->get_user()->id,
];

\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}
// Also check for wallet topup.
$sql = "SELECT p.id
FROM {payments} p
Expand All @@ -277,7 +296,9 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
'userid' => $contextlist->get_user()->id,
'paymentarea' => 'wallettopup',
];
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
if (class_exists('\core_payment\privacy\provider')) {
\core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
}
// Delete fake items.
$ids = $DB->get_records('payments', [
'component' => 'enrol_wallet',
Expand Down
23 changes: 23 additions & 0 deletions extendlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function enrol_wallet_extend_navigation_frontpage(navigation_node $parentnode, s
$capbulkedit = has_capability('enrol/wallet:bulkedit', $context);
$capcouponview = has_capability('enrol/wallet:viewcoupon', $context);
$capcouponcreate = has_capability('enrol/wallet:createcoupon', $context);
$capcouponedit = has_capability('enrol/wallet:editcoupon', $context);
$hassiteconfig = has_capability('moodle/site:config', $context);

$any = ($captransactions || $capcredit || $capbulkedit || $capcouponview || $capcouponcreate);
Expand Down Expand Up @@ -188,6 +189,18 @@ function enrol_wallet_extend_navigation_frontpage(navigation_node $parentnode, s
$parentnode->add_node($node);
}

if ($capcouponcreate && $capcouponedit && $ismoodle) {
// Adding page to generate coupons.
$node = navigation_node::create(
get_string('upload_coupons', 'enrol_wallet'),
new moodle_url('/enrol/wallet/extra/couponupload.php'),
navigation_node::TYPE_CUSTOM,
'enrol_wallet_upload_coupons',
'enrol_wallet_upload_coupons'
);
$parentnode->add_node($node);
}

if ($capcouponview && $ismoodle) {
// Adding page to view coupons.
$node = navigation_node::create(
Expand All @@ -198,6 +211,16 @@ function enrol_wallet_extend_navigation_frontpage(navigation_node $parentnode, s
'enrol_wallet_coupontable'
);
$parentnode->add_node($node);

// Adding page to view coupons.
$node = navigation_node::create(
get_string('coupon_usage', 'enrol_wallet'),
new moodle_url('/enrol/wallet/extra/couponusage.php'),
navigation_node::TYPE_CUSTOM,
'enrol_wallet_coupon_usage',
'enrol_wallet_coupon_usage'
);
$parentnode->add_node($node);
}

if ($capbulkedit) {
Expand Down
7 changes: 6 additions & 1 deletion extra/bulkinstances.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
$mform->setType('cost', PARAM_INT);
$mform->addHelpButton('cost', 'credit_cost', 'enrol_wallet');

$accounts = \core_payment\helper::get_payment_accounts_menu($systemcontext);
if (class_exists('\core_payment\helper')) {
$accounts = \core_payment\helper::get_payment_accounts_menu($systemcontext);
} else {
$accounts = false;
}

if ($accounts) {
$accounts = ((count($accounts) > 1) ? ['' => ''] : []) + $accounts;
$mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'), $accounts, ['optional' => true]);
Expand Down
1 change: 1 addition & 0 deletions extra/couponusage.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
$username = html_writer::link($userurl, $username);
}

$coursename = '';
if (!empty($record->instanceid)) {
try {
$course = $wallet->get_course_by_instance_id($record->instanceid);
Expand Down
20 changes: 19 additions & 1 deletion extrasettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$capbulkedit = has_capability('enrol/wallet:bulkedit', $context);
$capcouponview = has_capability('enrol/wallet:viewcoupon', $context);
$capcouponcreate = has_capability('enrol/wallet:createcoupon', $context);
$capcouponedit = has_capability('enrol/wallet:editcoupon', $context);

$ismoodle = (get_config('enrol_wallet', 'walletsource') == enrol_wallet\transactions::SOURCE_MOODLE);
// Adding these pages for only users with required capability.
Expand All @@ -39,7 +40,7 @@
// Working on solution.
if ($captransactions || $capbulkedit || $capcouponview || $capcouponcreate || $capcredit) {
// Adding new admin category.
$ADMIN->add('root', new admin_category('enrol_wallet_settings',
$ADMIN->add('modules', new admin_category('enrol_wallet_settings',
get_string('bulkfolder', 'enrol_wallet'), false));
}

Expand All @@ -53,6 +54,16 @@
$context));
}

if ($capcouponcreate && $capcouponedit && $ismoodle) {
// Adding page to upload coupons.
$ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_uploadcoupons',
get_string('upload_coupons', 'enrol_wallet'),
new moodle_url('/enrol/wallet/extra/couponupload.php'),
'enrol/wallet:createcoupon',
false,
$context));
}

if ($capcouponview && $ismoodle) {
// Adding page to view coupons.
$ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_coupontable',
Expand All @@ -61,6 +72,13 @@
'enrol/wallet:viewcoupon',
false,
$context));
// Adding page to view coupons usage.
$ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_couponusage',
get_string('coupon_usage', 'enrol_wallet'),
new moodle_url('/enrol/wallet/extra/couponusage.php'),
'enrol/wallet:viewcoupon',
false,
$context));
}

if ($capcredit) {
Expand Down
12 changes: 10 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,12 @@ public function edit_instance_form($instance, \MoodleQuickForm $mform, $context)
$mform->addRule('cost', get_string('invalidvalue', 'enrol_wallet'), 'numeric', null, 'client');

// Payment account.
$accounts = \core_payment\helper::get_payment_accounts_menu($context);
if (class_exists('\core_payment\account')) {
$accounts = \core_payment\helper::get_payment_accounts_menu($context);
} else {
$accounts = false;
}

if ($accounts) {
$accounts = ((count($accounts) > 1) ? ['' => ''] : []) + $accounts;
$mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'), $accounts);
Expand Down Expand Up @@ -1709,7 +1714,10 @@ public function use_standard_editing_ui() {
* @return array[currencycode => currencyname]
*/
public function get_possible_currencies($account = null) {
$codes = \core_payment\helper::get_supported_currencies();
$codes = [];
if (class_exists('\core_payment\helper')) {
$codes = \core_payment\helper::get_supported_currencies();
}

$currencies = [];
foreach ($codes as $c) {
Expand Down
4 changes: 3 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,9 @@ function enrol_wallet_is_valid_account($accountid) {
if (empty($accountid) || !is_number($accountid) || $accountid < 0) {
return false;
}

if (!class_exists('\core_payment\account')) {
return false;
}
$account = new \core_payment\account($accountid);
if (!$account->is_available() || !$account->is_valid()) {
return false;
Expand Down
24 changes: 18 additions & 6 deletions tests/payment/service_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class service_provider_test extends \advanced_testcase {
public function test_get_payable_walletenrol() {
global $DB;
$this->resetAfterTest();

if (class_exists('\core_payment\helper')) {
return;
}
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$walletplugin = enrol_get_plugin('wallet');
$generator = $this->getDataGenerator();
Expand Down Expand Up @@ -70,7 +72,9 @@ public function test_get_payable_walletenrol() {
public function test_get_payable_wallettopup() {
global $DB;
$this->resetAfterTest();

if (class_exists('\core_payment\helper')) {
return;
}
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$user = $generator->create_user();
Expand All @@ -93,7 +97,9 @@ public function test_get_payable_wallettopup() {
public function test_get_success_url_walletenrol() {
global $CFG, $DB;
$this->resetAfterTest();

if (class_exists('\core_payment\helper')) {
return;
}
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$walletplugin = enrol_get_plugin('wallet');
$generator = $this->getDataGenerator();
Expand Down Expand Up @@ -125,7 +131,9 @@ public function test_get_success_url_walletenrol() {
public function test_get_success_url_wallettopup() {
global $CFG, $DB;
$this->resetAfterTest();

if (class_exists('\core_payment\helper')) {
return;
}
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$user = $generator->create_user();
Expand All @@ -150,7 +158,9 @@ public function test_deliver_order_walletenrol() {
global $DB;
$this->resetAfterTest();
$this->preventResetByRollback();

if (class_exists('\core_payment\helper')) {
return;
}
$this->assertTrue(enrol_is_enabled('wallet'));

$studentrole = $DB->get_record('role', ['shortname' => 'student']);
Expand Down Expand Up @@ -191,7 +201,9 @@ public function test_deliver_order_wallettopup() {
global $DB;
$this->resetAfterTest();
$this->preventResetByRollback();

if (class_exists('\core_payment\helper')) {
return;
}
$this->assertTrue(enrol_is_enabled('wallet'));

$generator = $this->getDataGenerator();
Expand Down

0 comments on commit 2da59c3

Please sign in to comment.