diff --git a/classes/category/options.php b/classes/category/options.php index 92760b01..d70522e9 100644 --- a/classes/category/options.php +++ b/classes/category/options.php @@ -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; } diff --git a/classes/output/wallet_balance.php b/classes/output/wallet_balance.php index ae1f3cb0..7e1474f6 100644 --- a/classes/output/wallet_balance.php +++ b/classes/output/wallet_balance.php @@ -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) { @@ -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; diff --git a/classes/task/generate_coupons.php b/classes/task/generate_coupons.php new file mode 100644 index 00000000..f5b60ce6 --- /dev/null +++ b/classes/task/generate_coupons.php @@ -0,0 +1,46 @@ +. + +namespace enrol_wallet\task; + +/** + * Class generate_coupons + * + * @package enrol_wallet + * @copyright 2024 Mohammad Farouk + * @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)); + } +} diff --git a/classes/task/turn_non_refundable.php b/classes/task/turn_non_refundable.php index e8dee968..c6686286 100644 --- a/classes/task/turn_non_refundable.php +++ b/classes/task/turn_non_refundable.php @@ -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(); @@ -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. diff --git a/classes/util/balance.php b/classes/util/balance.php index cc2291eb..702deed6 100644 --- a/classes/util/balance.php +++ b/classes/util/balance.php @@ -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; diff --git a/extra/coupon.php b/extra/coupon.php index 2ec428fa..18184d62 100644 --- a/extra/coupon.php +++ b/extra/coupon.php @@ -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')); @@ -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(); diff --git a/extra/coupontable.php b/extra/coupontable.php index db382adf..64876d1f 100644 --- a/extra/coupontable.php +++ b/extra/coupontable.php @@ -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'; @@ -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'; @@ -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')); diff --git a/lang/en/enrol_wallet.php b/lang/en/enrol_wallet.php index 55016dee..1493e878 100644 --- a/lang/en/enrol_wallet.php +++ b/lang/en/enrol_wallet.php @@ -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'; diff --git a/lib.php b/lib.php index 6625d42f..dfca9fad 100644 --- a/lib.php +++ b/lib.php @@ -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; } } diff --git a/locallib.php b/locallib.php index eeb01fcc..fb5f38da 100644 --- a/locallib.php +++ b/locallib.php @@ -92,11 +92,16 @@ function enrol_wallet_get_random_coupon($length, $options) { * Generating coupons. * * @param object $options the options from coupon form. + * @param progress_trace $trace * @return array|string array of coupon, or string of error. */ -function enrol_wallet_generate_coupons($options) { +function enrol_wallet_generate_coupons($options, ?progress_trace $trace = null) { global $DB; + if (empty($trace)) { + $trace = new null_progress_trace; + } + $number = $options->number; $maxusage = $options->maxusage; $maxperuser = $options->maxperuser; @@ -115,7 +120,7 @@ function enrol_wallet_generate_coupons($options) { 'maxperuser' => $maxperuser, 'validfrom' => $from, 'validto' => $to, - 'timecreated' => time(), + 'timecreated' => $options->timecreated ?? time(), ]; $ids = []; @@ -123,6 +128,7 @@ function enrol_wallet_generate_coupons($options) { $recorddata->code = $code; $ids[] = $DB->insert_record('enrol_wallet_coupons', $recorddata); + $trace->output('Single coupon created...'); } else { $length = $options->length; @@ -130,7 +136,14 @@ function enrol_wallet_generate_coupons($options) { $upper = $options->upper; $digits = $options->digits; + $lastprogress = 0; for ($i = 0; $i < $number; $i++) { + $progress = round($i / $number * 100); + if ($progress > $lastprogress + 5) { + $trace->output('Generating coupons... ' . $progress . '%'); + $lastprogress = $progress; + } + $gopt = [ 'lower' => $lower, 'upper' => $upper, @@ -409,6 +422,11 @@ function enrol_wallet_display_coupon_urls() { if (get_config('enrol_wallet', 'walletsource') != balance::MOODLE) { return ''; } + + if (!isloggedin() || isguestuser()) { + return ''; + } + $context = context_system::instance(); $canviewcoupons = has_capability('enrol/wallet:viewcoupon', $context); $cangeneratecoupon = has_capability('enrol/wallet:createcoupon', $context); @@ -443,6 +461,10 @@ function enrol_wallet_display_coupon_urls() { */ function enrol_wallet_display_current_user_balance($userid = 0) { global $USER, $OUTPUT, $CFG, $PAGE; + if (!isloggedin() || isguestuser()) { + return ''; + } + $renderable = new \enrol_wallet\output\wallet_balance($userid); $renderer = $PAGE->get_renderer('enrol_wallet'); return $renderer->render($renderable); @@ -458,10 +480,6 @@ function enrol_wallet_display_topup_options() { require_once($CFG->dirroot.'/enrol/wallet/classes/form/topup_form.php'); require_once(__DIR__.'/lib.php'); - if (!isloggedin() || isguestuser()) { - return ''; - } - $username = optional_param('s', '', PARAM_USERNAME); if (!empty($username)) { $user = get_complete_user_data('username', $username); @@ -470,6 +488,10 @@ function enrol_wallet_display_topup_options() { $user = $USER; } + if (empty($user) || empty($user->id) || isguestuser($user)) { + return ''; + } + // Get the default currency. $currency = get_config('enrol_wallet', 'currency'); // Get the default payment account. @@ -538,7 +560,14 @@ function enrol_wallet_display_topup_options() { $tm .= $OUTPUT->heading(get_string('tellermen_display_guide', 'enrol_wallet'), 6); $tm .= html_writer::start_tag('ul'); foreach ($chargerids as $tellerid) { + if (empty($tellerid)) { + continue; + } $teller = core_user::get_user($tellerid); + if (!$teller || isguestuser($teller) || !core_user::is_real_user($tellerid)) { + continue; + } + $tellername = fullname($teller); if (user_can_view_profile($teller)) { $tellername = html_writer::link(new moodle_url('/user/view.php', ['id' => $tellerid]), $tellername); diff --git a/styles.css b/styles.css index 3d7721d5..8fa7ec53 100644 --- a/styles.css +++ b/styles.css @@ -132,22 +132,21 @@ } @media (min-width: 768px) { - #page-enrol-wallet-confirm #region-main { - .generalbox:first-of-type { - width: 33.33%; - float: left; - } - .generalbox:not(:first-of-type) { - float: right; - width: calc(66.66% - 1rem); - padding: 1rem; - margin-top: 1rem; - margin-left: 1rem; - border-radius: .5rem; - -webkit-box-shadow: 0 0 35px 0 rgba(154, 161, 171, .15); - box-shadow: 0 0 35px 0 rgba(154, 161, 171, .15); - } + #page-enrol-wallet-confirm #region-main .generalbox:first-of-type { + width: 33.33%; + float: left; + } + + #page-enrol-wallet-confirm #region-main .generalbox:not(:first-of-type) { + float: right; + width: calc(66.66% - 1rem); + padding: 1rem; + margin-top: 1rem; + margin-left: 1rem; + border-radius: .5rem; + -webkit-box-shadow: 0 0 35px 0 rgba(154, 161, 171, .15); + box-shadow: 0 0 35px 0 rgba(154, 161, 171, .15); } }