Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Apr 8, 2024
1 parent 3da00ac commit c3ce9ca
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 60 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Wallet Enrollment for Moodle #
==========
## V 5.3.2 ##
- Adding main page for wallet (when the user click my wallet).
- Add bundles for quick top up with conditional discounts
- Fix conditional discount precision.
- Bug fixes.

## V 5.2.0 ##
- Add a page to display offers.
- Display conditional discouts available to users in the top up form.
- Display conditional discounts available to users in the top up form.
- Bug fixes.

## V 5.1.0 ##
Expand Down
1 change: 0 additions & 1 deletion classes/api/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static function get_cost_parameters() {
* @return array
*/
public static function get_cost($instanceid, $userid) {
global $CFG, $PAGE;
$params = ['instanceid' => $instanceid, 'userid' => $userid];
$params = self::validate_parameters(self::get_cost_parameters(), $params);
$userid = $params['userid'];
Expand Down
54 changes: 32 additions & 22 deletions classes/coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,20 @@ private function validate_enrol_coupon() {
* @param int $areaid the area at which the coupon applied (instanceid, cmid, sectionid)
* @return bool|string
*/
public function validate_coupon($area = self::AREA_TOPUP, $areaid = 0) {
public function validate_coupon($area = null, $areaid = 0) {
if (!empty($this->error)) {
return $this->error;
}

$this->valid = true;
if (is_string($area)) {
if (is_string($area) && !is_number($area)) {
$area = self::AREAS[$area];
}
$this->set_area($area, $areaid);

if (!is_null($area) && in_array($area, self::AREAS)) {
$this->set_area($area, $areaid);
}

$this->validate_area();

if (!empty($this->error)) {
Expand Down Expand Up @@ -659,27 +663,33 @@ public function apply_coupon($area = self::AREA_TOPUP, $areaid = 0) {
$user = \core_user::get_user($this->userid);
$fee = (float)$util->get_cost_after_discount();
$plugin = new wallet();
if ($this->type == self::FIXED) {
// Check if the coupon value is grater than or equal the fee.
// Enrol the user in the course.
if ($balance >= $fee) {
$plugin->enrol_self($instance, $user);
$used = true;
}

} else if ($this->type == self::ENROL) {

$plugin->enrol_self($instance, $user, false);
$used = true;
if ($this->type == self::ENROL
|| (
($this->type == self::CATEGORY || $this->type == self::FIXED)
&& $balance >= $fee
)
) {

} else if ($this->type == self::CATEGORY) {
if ($balance >= $fee) {
$plugin->enrol_self($instance, $user);
$used = true;
} else {
$error = get_string('coupon_cat_notsufficient', 'enrol_wallet');
\core\notification::error($error);
$used = true;
// Check if the coupon value is grater than or equal the fee.
// Enrol the user in the course.
$context = \context_course::instance($instance->courseid);
if (!is_enrolled($context, $user, '', true)) {
if ($this->type == self::ENROL) {
$charge = false;
} else {
$charge = true;
}
$plugin->enrol_self($instance, $user, $charge);
} else if ($this->type == self::ENROL) {
$used = false;
}

} else if ($this->type == self::CATEGORY && $balance < $fee) {
$error = get_string('coupon_cat_notsufficient', 'enrol_wallet');
\core\notification::error($error);
}
}

Expand Down Expand Up @@ -738,7 +748,7 @@ public function mark_coupon_used() {
// Unset the session coupon to make sure not used again.
self::unset_session_coupon();

if ($this->area == self::ENROL) {
if ($this->area == self::AREA_ENROL) {
$instanceid = $this->areaid;
} else {
$instanceid = 0;
Expand Down Expand Up @@ -855,7 +865,7 @@ public static function get_coupon_value($code, $userid = 0, $apply = false, $ins
}

if ($apply) {
$coupon->apply_coupon();
$coupon->apply_coupon($area, $areaid);
}

return $coupon->get_data();
Expand Down
12 changes: 11 additions & 1 deletion classes/form/transactions_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ public function definition() {
];
foreach ($_GET as $key => $value) {
if ($mform->elementExists($key) && isset($types[$key])) {
$mform->setDefault($key, clean_param($value, $types[$key]));
if (is_array($value)) {
$array = clean_param_array($value, $types[$key]);
$time = make_timestamp($array['year'],
$array['month'],
$array['day'] ?? 1,
$array['hour'] ?? 0,
$array['minute'] ?? 0);
$mform->setDefault($key, $time);
} else {
$mform->setDefault($key, clean_param($value, $types[$key]));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion classes/output/wallet_balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function export_for_template(renderer_base $output) {
// Prepare transaction URL to display.
$params = [];
if (!$this->currentuser) {
$params['user'] = $this->userid;
$params['userid'] = $this->userid;
}
$transactionsurl = new moodle_url('/enrol/wallet/extra/transaction.php', $params);
$transactions = html_writer::link($transactionsurl, get_string('transactions', 'enrol_wallet'));
Expand Down
2 changes: 2 additions & 0 deletions extra/coupon_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

require_once('../../../config.php');
require_once($CFG->dirroot.'/enrol/wallet/locallib.php');
// Any error displaying causing page to not redirect and charging operation may be proceeded twice.
set_debugging(DEBUG_NONE, false);
require_login(null, false);

$data = [
Expand Down
29 changes: 13 additions & 16 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

// Disable all callbacks during upgrade.
$version = get_config('enrol_wallet', 'version');
if ($version >= 2024021018) {
if ($version >= 2024030900) {
global $CFG;
require_once("$CFG->dirroot/enrol/wallet/extendlib.php");
}
Expand Down Expand Up @@ -599,6 +599,18 @@ public function enrol_page_hook(stdClass $instance) {
$enrolstatus = $this->can_self_enrol($instance);

$output = '';
if (coupons::is_enabled()) {
$formdata = new stdClass();
$formdata->header = $this->get_instance_name($instance);
$formdata->instance = $instance;
$formdata->url = (new \moodle_url('/enrol/index.php', ['id' => $instance->courseid]))->out();
$couponaction = new \moodle_url('/enrol/wallet/extra/coupon_action.php');
$couponform = new applycoupon_form($couponaction, $formdata);
if ($submitteddata = $couponform->get_data()) {
enrol_wallet_process_coupon_data($submitteddata);
}
}

if (true === $enrolstatus) {

$confirmpage = new moodle_url('/enrol/wallet/confirm.php');
Expand All @@ -621,15 +633,6 @@ public function enrol_page_hook(stdClass $instance) {
// Now prepare the coupon form.
// Check the coupons settings first.
if (coupons::is_enabled() && $costafter != 0) {
$data = new stdClass();
$data->header = $this->get_instance_name($instance);
$data->instance = $instance;
$data->url = (new \moodle_url('/enrol/index.php', ['id' => $instance->courseid]))->out();
$couponaction = new \moodle_url('/enrol/wallet/extra/coupon_action.php');
$couponform = new applycoupon_form($couponaction, $data);
if ($submitteddata = $couponform->get_data()) {
enrol_wallet_process_coupon_data($submitteddata);
}
ob_start();
$couponform->display();
$output .= ob_get_clean();
Expand Down Expand Up @@ -665,12 +668,6 @@ public function enrol_page_hook(stdClass $instance) {

// Now prepare the coupon form.
if (coupons::is_enabled()) {
$data->url = (new \moodle_url('/enrol/index.php', ['id' => $instance->courseid]))->out();
$couponaction = new \moodle_url('/enrol/wallet/extra/coupon_action.php');
$couponform = new applycoupon_form($couponaction, $data);
if ($submitteddata = $couponform->get_data()) {
enrol_wallet_process_coupon_data($submitteddata);
}
ob_start();
$couponform->display();
$output .= ob_get_clean();
Expand Down
5 changes: 3 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ function enrol_wallet_process_coupon_data($data) {
default:
}

$couponutil->apply_coupon();
$couponutil->apply_coupon($area, $areaid);
// Check the type to determine what to do.
if ($type == $couponutil::FIXED) {
// Apply the coupon code to add its value to the user's wallet and enrol if value is enough.
Expand Down Expand Up @@ -523,7 +523,8 @@ function enrol_wallet_display_topup_options() {
&& (bool)get_config('block_vc', 'enablecredit')) {

require_once("$CFG->dirroot/blocks/vc/classes/form/vc_credit_form.php");
$vcform = new \block_vc\form\vc_credit_form($CFG->wwwroot.'/blocks/vc/credit.php');
$action = new moodle_url('/blocks/vc/credit.php');
$vcform = new \block_vc\form\vc_credit_form($action);

$render .= enrol_wallet_topup_option($vcform->render(), get_string('topupbyvc', 'enrol_wallet'));
}
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
$settings->add(new admin_setting_configselect('enrol_wallet/discount_field',
get_string('profile_field_map', 'enrol_wallet'),
get_string('profile_field_map_help', 'enrol_wallet'),
null,
0,
$menu));

// Adding options to enable and disable coupons.
Expand Down
2 changes: 1 addition & 1 deletion tests/coupons_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @copyright 2024 2024, Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class coupons_test extends \advanced_testcase {
final class coupons_test extends \advanced_testcase {

/**
* Categories.
Expand Down
2 changes: 1 addition & 1 deletion tests/enrol_wallet_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_wallet_test extends \advanced_testcase {
final class enrol_wallet_test extends \advanced_testcase {
/**
* Basic test for enrol wallet plugin
* @covers \enrol_wallet_plugin
Expand Down
2 changes: 1 addition & 1 deletion tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @runTestsInSeparateProcesses
*/
class externallib_test extends externallib_advanced_testcase {
final class externallib_test extends externallib_advanced_testcase {

/**
* Test get_instance_info
Expand Down
2 changes: 1 addition & 1 deletion tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class locallib_test extends \advanced_testcase {
final class locallib_test extends \advanced_testcase {

/**
* test_enrol_wallet_is_borrow_eligible
Expand Down
2 changes: 1 addition & 1 deletion tests/notifications_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notifications_test extends \advanced_testcase {
final class notifications_test extends \advanced_testcase {
/**
* Test transaction_notifications
* @covers ::transaction_notify()
Expand Down
2 changes: 1 addition & 1 deletion tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class observer_test extends \advanced_testcase {
final class observer_test extends \advanced_testcase {
/**
* Test event observer completion awards.
* @covers ::wallet_completion_awards
Expand Down
2 changes: 1 addition & 1 deletion tests/payment/service_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Unit tests for the enrol_wallet's payment subsystem callback implementation.
*
*/
class service_provider_test extends \advanced_testcase {
final class service_provider_test extends \advanced_testcase {

/**
* Test for service_provider::get_payable().
Expand Down
4 changes: 1 addition & 3 deletions tests/transactions_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
namespace enrol_wallet;

use enrol_wallet\transactions;

defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/enrol/wallet/lib.php');
Expand All @@ -36,7 +34,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class transactions_test extends \advanced_testcase {
final class transactions_test extends \advanced_testcase {

/**
* The transactions class.
Expand Down
2 changes: 1 addition & 1 deletion tests/turn_non_refundable_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class turn_non_refundable_test extends \advanced_testcase {
final class turn_non_refundable_test extends \advanced_testcase {
/**
* Test adhoc task turn_non_refundable.
* @covers \turn_non_refundable
Expand Down
2 changes: 1 addition & 1 deletion tests/util/balance_op_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @copyright 2024 Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class balance_op_test extends \advanced_testcase {
final class balance_op_test extends \advanced_testcase {
/**
* Test conditional discounts.
* @covers ::conditional_discount_charging()
Expand Down
2 changes: 1 addition & 1 deletion tests/util/instance_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @copyright 2024 2024, Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class instance_test extends \advanced_testcase {
final class instance_test extends \advanced_testcase {
/**
* Testing get cost after discount.
*
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024022619;
$plugin->version = 2024030900;
$plugin->requires = 2020061500;
$plugin->component = 'enrol_wallet';
$plugin->release = '5.2.0';
$plugin->release = '5.3.2';
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [
'enrol_manual' => ANY_VERSION,
Expand Down

0 comments on commit c3ce9ca

Please sign in to comment.