Skip to content

Commit

Permalink
- Fix custom currency issue.
Browse files Browse the repository at this point in the history
- Generate coupons is an adhoc task now for more efficient generation of large number of coupons.
-fix The style sheet.
- Skip transformation task to non-refundable balance if the user or category were deleted.
  • Loading branch information
fmido88 committed Aug 23, 2024
1 parent 460a093 commit d5d2599
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 62 deletions.
9 changes: 8 additions & 1 deletion classes/category/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,19 @@ public static function get_all_options_with_discount() {
$options[0] = get_string('site');
continue;
}

if (isset($options[$record->category])) {
continue;
}
$cat = core_course_category::get($record->category);

$cat = core_course_category::get($record->category, IGNORE_MISSING);
if (!$cat) {
continue;
}

$options[$record->category] = $cat->get_nested_name(false);
}

ksort($options, SORT_NUMERIC);
return $options;
}
Expand Down
3 changes: 3 additions & 0 deletions classes/output/wallet_balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public function export_for_template(renderer_base $output) {
$details = $helper->get_balance_details();
// Get the default currency.
$currency = get_config('enrol_wallet', 'currency');

$policy = get_config('enrol_wallet', 'refundpolicy');
// Prepare transaction URL to display.
$params = [];
if (!$this->currentuser) {
$params['userid'] = $this->userid;
}

$transactionsurl = new moodle_url('/enrol/wallet/extra/transaction.php', $params);
$transactions = html_writer::link($transactionsurl, get_string('transactions', 'enrol_wallet'));
if ($this->currentuser && !AJAX_SCRIPT) {
Expand All @@ -121,6 +123,7 @@ public function export_for_template(renderer_base $output) {
} else {
$balancedetails[$id]->name = $category->get_nested_name(false);
}

$balancedetails[$id]->refundable = number_format($obj->refundable, 2) ?? 0;
$balancedetails[$id]->nonrefundable = number_format($obj->nonrefundable, 2) ?? 0;
$total = $obj->balance ?? $balancedetails[$id]->refundable + $balancedetails[$id]->nonrefundable;
Expand Down
46 changes: 46 additions & 0 deletions classes/task/generate_coupons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 enrol_wallet\task;

/**
* Class generate_coupons
*
* @package enrol_wallet
* @copyright 2024 Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generate_coupons extends \core\task\adhoc_task {
/**
* Execute coupons generation task.
* @return void
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot.'/enrol/wallet/locallib.php');

\core_php_time_limit::raise();
raise_memory_limit(MEMORY_HUGE);

$trace = PHPUNIT_TEST ? new \null_progress_trace : new \text_progress_trace;
$options = $this->get_custom_data();
$trace->output('Starting task...');
$trace->output('Data: ' . $this->get_custom_data_as_string());

$ids = enrol_wallet_generate_coupons($options, $trace);
$trace->output('Finished generating coupons with codes: ' . implode(',', $ids));
}
}
27 changes: 24 additions & 3 deletions classes/task/turn_non_refundable.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,29 @@ public function check_transform_validation($data, $trace) {
$amount = $data->amount;
$this->catid = $data->catid ?? 0;

$balancehelper = new balance($userid, $this->catid);
if (!empty($this->catid)) {
$category = \core_course_category::get($this->catid, IGNORE_MISSING, true);
if (!$category) {
$output = 'Category not found...';
$trace->output($output);
if (!PHPUNIT_TEST) {
return false;
}
return $output;
}
}

$user = \core_user::get_user($userid, 'id, deleted');
if (!$user || !empty($user->deleted)) {
$output = 'User not found...';
$trace->output($output);
if (!PHPUNIT_TEST) {
return false;
}
return $output;
}

$balancehelper = new balance($userid, $category ?? $this->catid);

$mainbalance = $balancehelper->get_main_balance();
$mainnorefund = $balancehelper->get_main_nonrefundable();
Expand All @@ -111,9 +133,8 @@ public function check_transform_validation($data, $trace) {
$trace->output($output);
if (!PHPUNIT_TEST) {
return false;
} else {
return $output;
}
return $output;
}

// Get all transactions in this time.
Expand Down
3 changes: 2 additions & 1 deletion classes/util/balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public function __construct($userid = 0, $category = 0) {
global $USER;
$this->userid = $USER->id;
}
$this->catenabled = (bool)get_config('enrol_wallet', 'catbalance') && $this->source == self::MOODLE;

$this->catenabled = (bool)get_config('enrol_wallet', 'catbalance') && ($this->source == self::MOODLE);
if ($this->catenabled) {
if (empty($category)) {
global $COURSE;
Expand Down
28 changes: 18 additions & 10 deletions extra/coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
$systemcontext = context_system::instance();
require_capability('enrol/wallet:createcoupon', $systemcontext);

$pageurl = new moodle_url('/enrol/wallet/extra/coupon.php');
// Setup the page.
$PAGE->set_pagelayout('admin');
$PAGE->set_context($systemcontext);
$PAGE->set_url(new moodle_url('/enrol/wallet/extra/coupon.php'));
$PAGE->set_url($pageurl);
$PAGE->set_title(get_string('coupon_generation_title', 'enrol_wallet'));
$PAGE->set_heading(get_string('coupon_generation_heading', 'enrol_wallet'));

Expand Down Expand Up @@ -100,17 +101,24 @@
if ($options->type == 'enrol') {
$options->value = 0;
}

$options->timecreated = time();

// Generate coupons with the options specified.
$ids = enrol_wallet_generate_coupons($options);
$task = new enrol_wallet\task\generate_coupons();
$task->set_custom_data($options);
$task->set_next_run_time(time() + 1);
\core\task\manager::queue_adhoc_task($task);

if (is_string($ids)) {
core\notification::error($ids);
} else {
$count = count($ids);
$msg = get_string('coupons_generation_success', 'enrol_wallet', $count);
$redirecturl = new moodle_url('/enrol/wallet/extra/coupontable.php', ['ids' => implode(',', $ids)]);
redirect($redirecturl, $msg);
}
$couponsurl = new moodle_url('/enrol/wallet/extra/coupontable.php', [
'createdfrom' => $options->timecreated,
'createdto' => $options->timecreated,
]);
$msg = get_string('coupons_generation_taskcreated', 'enrol_wallet', [
'count' => $options->number,
'link' => html_writer::link($couponsurl, get_string('check')),
]);
redirect($pageurl, $msg);
}

echo $OUTPUT->header();
Expand Down
32 changes: 16 additions & 16 deletions extra/coupontable.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@
$candownload = has_capability('enrol/wallet:downloadcoupon', $systemcontext);

// Parameters.
$ids = optional_param('ids', false, PARAM_RAW);
$code = optional_param('code', '', PARAM_TEXT);
$value = optional_param('value', null, PARAM_TEXT);
$ids = optional_param('ids', false, PARAM_RAW);
$code = optional_param('code', '', PARAM_TEXT);
$value = optional_param('value', null, PARAM_TEXT);
if (!empty($value) || $value === '0') {
$value = clean_param($value, PARAM_FLOAT);
} else {
$value = null;
}

$valuerelation = optional_param('valuerelation', null, PARAM_TEXT);
$type = optional_param('type', null, PARAM_TEXT);
$category = optional_param('category', null, PARAM_INT);
$courses = optional_param_array('courses', null, PARAM_RAW);
$valuerelation = optional_param('valuerelation', null, PARAM_TEXT);
$type = optional_param('type', null, PARAM_TEXT);
$category = optional_param('category', null, PARAM_INT);
$courses = optional_param_array('courses', null, PARAM_RAW);

$checkarrays = ['validto', 'validfrom', 'createdfrom', 'createdto'];
foreach ($checkarrays as $arrayparam) {
$param = $arrayparam.'array';
Expand All @@ -62,26 +63,26 @@
}
}

$maxusage = optional_param('maxusage', null, PARAM_TEXT);
$maxusage = optional_param('maxusage', null, PARAM_TEXT);
if (!empty($maxusage) || $maxusage === '0') {
$maxusage = clean_param($maxusage, PARAM_INT);
} else {
$maxusage = null;
}

$maxrelation = optional_param('maxrelation', null, PARAM_TEXT);
$usetimes = optional_param('usetimes', null, PARAM_TEXT);
$maxrelation = optional_param('maxrelation', null, PARAM_TEXT);
$usetimes = optional_param('usetimes', null, PARAM_TEXT);
if (!empty($usetimes) || $usetimes === '0') {
$usetimes = clean_param($usetimes, PARAM_INT);
} else {
$usetimes = null;
}

$userelation = optional_param('userelation', null, PARAM_TEXT);
$sort = optional_param('tsort', 'id', PARAM_ALPHA);
$download = optional_param('download', '', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);
$limitnum = optional_param('perpage', 50, PARAM_INT);
$userelation = optional_param('userelation', null, PARAM_TEXT);
$sort = optional_param('tsort', 'id', PARAM_ALPHA);
$download = optional_param('download', '', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);
$limitnum = optional_param('perpage', 50, PARAM_INT);

// Sterilize the url parameters and conditions for sql.
$conditions = '1=1';
Expand Down Expand Up @@ -344,7 +345,6 @@
$mform->setDefault('courses', $courses);
}


$usetimesgroup[] = $mform->createElement('select', 'userelation', '', $opt);

$usetimesgroup[] = $mform->createElement('text', 'usetimes', get_string('coupon_t_usage', 'enrol_wallet'));
Expand Down
1 change: 1 addition & 0 deletions lang/en/enrol_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
$string['coupons_delete_selected'] = 'Delete selected coupons';
$string['coupons_discount_error'] = 'Discount value cannot exceed 100%';
$string['coupons_generation_success'] = '{$a} coupon codes successfully generated.';
$string['coupons_generation_taskcreated'] = '{$a->count} coupon generation task processing in the back ground, check this link after a while {$a->link}.';
$string['coupons_ids'] = 'Coupon id(s) separated by (,)';
$string['coupons_length'] = 'Length';
$string['coupons_length_help'] = 'How many characters in a single coupon';
Expand Down
13 changes: 3 additions & 10 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1884,19 +1884,12 @@ public function get_possible_currencies($account = null) {
return strcmp($a, $b);
});

// Adding Wallet Coins currency and empty currency.
if (empty($currencies)) {
$currencies = [
'' => '',
'MCW' => get_string('MWC', 'enrol_wallet'),
];
}
// Adding custom currency in case of there is no available payment gateway or customize the wallet.
if (empty($currencies) || (empty($account))) {
$customcurrency = $this->get_config('customcurrency') ?? '';
$cc = $this->get_config('customcurrencycode') ?? '';
$customcurrency = $this->get_config('customcurrency') ?? get_string('MWC', 'enrol_wallet');
$cc = $this->get_config('customcurrencycode');
// Don't override standard currencies.
if (!array_key_exists($cc, $currencies) || $cc === '' || $cc === 'MCW') {
if (!array_key_exists($cc, $currencies) || $cc === '' || $cc === 'MWC') {
$currencies[$cc] = $customcurrency;
}
}
Expand Down
Loading

0 comments on commit d5d2599

Please sign in to comment.