From c964da667da9c24f3375e73c66dcdbcb0b928421 Mon Sep 17 00:00:00 2001 From: Mohammad Farouk Date: Sun, 1 Sep 2024 16:09:00 +0300 Subject: [PATCH] small fixes --- classes/hooks_callbacks.php | 10 +- classes/output/wallet_balance.php | 9 +- classes/util/offers.php | 2 +- extra/action.php | 175 ------------------------------ extra/generator.php | 113 ------------------- 5 files changed, 16 insertions(+), 293 deletions(-) delete mode 100644 extra/action.php delete mode 100644 extra/generator.php diff --git a/classes/hooks_callbacks.php b/classes/hooks_callbacks.php index 1c954c06..e9dd1a80 100644 --- a/classes/hooks_callbacks.php +++ b/classes/hooks_callbacks.php @@ -35,6 +35,10 @@ class hooks_callbacks { * @return void */ public static function show_price(before_footer_html_generation $hook) { + if (during_initial_install()) { + return; + } + $showprice = (bool)get_config('enrol_wallet', 'showprice'); if ($showprice) { $page = $hook->renderer->get_page(); @@ -50,7 +54,7 @@ public static function show_price(before_footer_html_generation $hook) { */ public static function low_balance_warning(before_standard_top_of_body_html_generation $hook) { // Don't display notice for guests or logged out. - if (!isloggedin() || isguestuser()) { + if (!isloggedin() || isguestuser() || during_initial_install()) { return; } @@ -78,6 +82,10 @@ public static function low_balance_warning(before_standard_top_of_body_html_gene * @return void */ public static function add_offers(primary_extend $hook) { + if (during_initial_install()) { + return; + } + $enabled = (bool)get_config('enrol_wallet', 'offers_nav'); if (empty($enabled)) { return; diff --git a/classes/output/wallet_balance.php b/classes/output/wallet_balance.php index 7e1474f6..317b89ec 100644 --- a/classes/output/wallet_balance.php +++ b/classes/output/wallet_balance.php @@ -124,11 +124,12 @@ public function export_for_template(renderer_base $output) { $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; + $balancedetails[$id]->refundable = number_format($obj->refundable ?? 0, 2); + $balancedetails[$id]->nonrefundable = number_format($obj->nonrefundable ?? 0, 2); + $total = $obj->balance ?? (float)(($obj->refundable ?? 0) + ($obj->nonrefundable ?? 0)); $balancedetails[$id]->total = number_format($total, 2); } + $balancedetails = !(empty($balancedetails)) ? array_values($balancedetails) : false; $tempctx = new stdClass; @@ -138,6 +139,7 @@ public function export_for_template(renderer_base $output) { $tempctx->hasdetails = !empty($balancedetails); $tempctx->catdetails = $balancedetails; $tempctx->currency = $currency; + if (!AJAX_SCRIPT) { $tempctx->transactions = $transactions; $tempctx->transfer = !empty($transferenabled) ? $transfer : false; @@ -145,6 +147,7 @@ public function export_for_template(renderer_base $output) { $tempctx->policy = !empty($policy) ? $policy : false; $tempctx->walleturl = (new moodle_url('/enrol/wallet/wallet.php#linkbalance'))->out(); } + $tempctx->currentuser = $this->currentuser; return $tempctx; } diff --git a/classes/util/offers.php b/classes/util/offers.php index da344424..cc0f184c 100644 --- a/classes/util/offers.php +++ b/classes/util/offers.php @@ -883,7 +883,7 @@ public static function validate($data) { break; case self::PROFILE_FIELD: - if (empty($offer->field)) { + if (empty($offer->cf) || empty($offer->sf)) { $errors[self::fname($type, '', $i)] = get_string('offers_error_pfselect', 'enrol_wallet'); } else if (!in_array($offer->op, [self::PFOP_IS_EMPTY, self::PFOP_IS_NOT_EMPTY])) { if (empty($offer->value)) { diff --git a/extra/action.php b/extra/action.php deleted file mode 100644 index 8ee7ec0b..00000000 --- a/extra/action.php +++ /dev/null @@ -1,175 +0,0 @@ -. - -/** - * Enrol wallet action after submit the coupon code. - * - * @package enrol_wallet - * @copyright 2023 Mohammad Farouk - * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once('../../../config.php'); -require_once(__DIR__.'/../lib.php'); -global $DB, $USER; - -require_login(); -$mform = new enrol_wallet\form\applycoupon_form(); -$data = $mform->get_data(); - -$cancel = optional_param('cancel', '', PARAM_TEXT); -$url = optional_param('url', '', PARAM_URL); -$redirecturl = !empty($url) ? new moodle_url('/'.$url) : new moodle_url('/'); - -if ($cancel) { - // Important to unset the session coupon. - if (isset($_SESSION['coupon'])) { - $_SESSION['coupon'] = ''; - unset($_SESSION['coupon']); - } - redirect($redirecturl); -} - -$userid = $USER->id; - -$coupon = required_param('coupon', PARAM_TEXT); -$instanceid = optional_param('instanceid' , '', PARAM_INT); -$courseid = optional_param('courseid', 0, PARAM_INT); -$cmid = optional_param('cmid', 0, PARAM_INT); -$sectionid = optional_param('sectionid', 0, PARAM_INT); - -$couponsetting = get_config('enrol_wallet', 'coupons'); - -if (!confirm_sesskey()) { - throw new moodle_exception('invalidsesskey'); -} - -// Get the coupon data. -$coupondata = enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, false, $cmid, $sectionid); -if (empty($coupondata) || is_string($coupondata)) { - $msg = get_string('coupon_applyerror', 'enrol_wallet', $coupondata); - $msgtype = 'error'; - // This mean that the function return error. -} else { - $wallet = new enrol_wallet_plugin; - - $value = $coupondata['value'] ?? 0; - $type = $coupondata['type']; - - // Check the type to determine what to do. - if ($type == 'fixed') { - - // Apply the coupon code to add its value to the user's wallet and enrol if value is enough. - enrol_wallet\transactions::apply_coupon($coupondata, $userid, $instanceid); - $currency = get_config('enrol_wallet', 'currency'); - $a = [ - 'value' => $value, - 'currency' => $currency, - ]; - $msg = get_string('coupon_applyfixed', 'enrol_wallet', $a); - $msgtype = 'success'; - - } else if ($type == 'percent' && - ($couponsetting == $wallet::WALLET_COUPONSDISCOUNT - || $couponsetting == $wallet::WALLET_COUPONSALL) - && !empty($instanceid)) { - // Percentage discount coupons applied in enrolment. - $id = $DB->get_field('enrol', 'courseid', ['id' => $instanceid, 'enrol' => 'wallet'], IGNORE_MISSING); - - if ($id) { - - $redirecturl = new moodle_url('/enrol/index.php', ['id' => $id, 'coupon' => $coupon]); - $msg = get_string('coupon_applydiscount', 'enrol_wallet', $value); - $msgtype = 'success'; - - } else { - - $msg = get_string('coupon_applynocourse', 'enrol_wallet'); - $msgtype = 'error'; - - } - - } else if ($type == 'percent' && - ($couponsetting == $wallet::WALLET_COUPONSDISCOUNT - || $couponsetting == $wallet::WALLET_COUPONSALL) - && (!empty($cmid) || !empty($sectionid))) { - - // This is the case when the coupon applied by availability wallet. - $_SESSION['coupon'] = $coupon; - - $redirecturl = new moodle_url('/'.$url, ['coupon' => $coupon]); - $msg = get_string('coupon_applydiscount', 'enrol_wallet', $value); - $msgtype = 'success'; - - } else if ($type == 'category' && !empty($instanceid) && !empty($coupondata['category'])) { - // This type of coupons is restricted to be used in certain categories. - $course = $wallet->get_course_by_instance_id($instanceid); - $ok = false; - if ($coupondata['category'] == $course->category) { - $ok = true; - } else { - $parents = core_course_category::get($course->category)->get_parents(); - if (in_array($coupondata['category'], $parents)) { - $ok = true; - } - } - - $redirecturl = new moodle_url('/enrol/index.php', ['id' => $course->id]); - - if ($ok) { - enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, true); - $msg = get_string('coupon_categoryapplied', 'enrol_wallet'); - $msgtype = 'success'; - } else { - $categoryname = core_course_category::get($coupondata['category'])->get_nested_name(false); - $msg = get_string('coupon_categoryfail', 'enrol_wallet', $categoryname); - $msgtype = 'error'; - } - - } else if ($type == 'enrol' && !empty($instanceid) && !empty($coupondata['courses'])) { - // This type has no value, it used to enrol the user direct to the course. - $courseid = $DB->get_field('enrol', 'courseid', ['id' => $instanceid, 'enrol' => 'wallet'], IGNORE_MISSING); - - if (in_array($courseid, $coupondata['courses'])) { - // Apply the coupon and enrol the user. - enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, true); - - $msg = get_string('coupon_enrolapplied', 'enrol_wallet'); - $msgtype = 'success'; - } else { - $available = ''; - foreach ($coupondata['courses'] as $courseid) { - $coursename = get_course($courseid)->fullname; - $available .= '- ' . $coursename . '
'; - } - - $msg = get_string('coupon_enrolerror', 'enrol_wallet', $available); - $msgtype = 'error'; - } - - } else if (($type == 'percent' || $type == 'course' || $type == 'category') && empty($instanceid)) { - - $msg = get_string('coupon_applynothere', 'enrol_wallet'); - $msgtype = 'error'; - - } else { - - $msg = get_string('invalidcoupon_operation', 'enrol_wallet'); - $msgtype = 'error'; - } -} - -redirect($redirecturl, $msg, null, $msgtype); diff --git a/extra/generator.php b/extra/generator.php deleted file mode 100644 index 7aaa1a8f..00000000 --- a/extra/generator.php +++ /dev/null @@ -1,113 +0,0 @@ -. - -/** - * Action page to generate coupons. - * - * @package enrol_wallet - * @copyright 2023 Mo Farouk - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once('../../../config.php'); -require_once($CFG->dirroot.'/enrol/wallet/locallib.php'); -require_login(); -require_capability('enrol/wallet:createcoupon', context_system::instance()); -$method = required_param('method', PARAM_TEXT); - -if ($method == 'single') { - $code = required_param('code', PARAM_TEXT); - $number = 1; - $length = ''; - $characters = []; -} else if ($method == 'random') { - $code = ''; - $number = required_param('number', PARAM_INT); - $length = required_param('length', PARAM_INT); - $characters = required_param_array('characters', PARAM_BOOL); -} - -$value = optional_param('value', 0, PARAM_FLOAT); -$type = required_param('type', PARAM_TEXT); -$maxusage = required_param('maxusage', PARAM_INT); -$maxperuser = optional_param('maxperuser', 0, PARAM_INT); -$validto = optional_param_array('validto', [], PARAM_INT); -$validfrom = optional_param_array('validfrom', [], PARAM_INT); -$catid = optional_param('category', '', PARAM_INT); -$courses = optional_param_array('courses', '', PARAM_INT); - -$redirecturl = new moodle_url('/enrol/wallet/extra/coupontable.php'); - - -if (!empty($validto)) { - $to = mktime( - $validto['hour'], - $validto['minute'], - 0, - $validto['month'], - $validto['day'], - $validto['year'], - ); -} else { - $to = 0; -} - -if (!empty($validfrom)) { - $from = mktime( - $validfrom['hour'], - $validfrom['minute'], - 0, - $validfrom['month'], - $validfrom['day'], - $validfrom['year'], - ); -} else { - $from = 0; -} - -$options = new stdClass; - -if (!empty($characters)) { - $options->lower = isset($characters['lower']) ? $characters['lower'] : false; - $options->upper = isset($characters['upper']) ? $characters['upper'] : false; - $options->digits = isset($characters['digits']) ? $characters['digits'] : false; -} - -$options->number = $number; -$options->length = $length; -$options->maxusage = $maxusage; -$options->maxperuser = $maxperuser; -$options->from = $from; -$options->to = $to; -$options->type = $type; -$options->value = $value; -$options->code = $code; -$options->category = $catid; -$options->courses = !empty($courses) ? implode(',', $courses) : ''; - -// Generate coupons with the options specified. -if (confirm_sesskey()) { - $ids = enrol_wallet_generate_coupons($options); - - if (is_string($ids)) { - $msg = $ids; - } else { - $count = count($ids); - $msg = get_string('coupons_generation_success', 'enrol_wallet', $count); - } - - redirect($redirecturl, $msg); -}