diff --git a/classes/form/applycoupon_form.php b/classes/form/applycoupon_form.php index 2dbdbae4..1bb4c9dc 100644 --- a/classes/form/applycoupon_form.php +++ b/classes/form/applycoupon_form.php @@ -30,14 +30,35 @@ * */ class applycoupon_form extends \moodleform { + /** + * Overriding this function to get unique form id for multiple wallet enrollments, + * or multiple wallet activity restriction. + * + * @return string form identifier + */ + protected function get_form_identifier() { + $data = (object)$this->_customdata; + if (!empty($data->id)) { + $formid = $data->id.'_'.get_class($this); + + } else if (!empty($data->cmid)) { + $formid = $data->cmid.'_'.get_class($this); + + } else if (!empty($data->sectionid)) { + $formid = $data->sectionid.'_'.get_class($this); + } else { + $formid = parent::get_form_identifier(); + } + + return $formid; + } /** * Form definition. Abstract method - always override! * @return void */ public function definition() { - global $USER; $mform = $this->_form; $instance = $this->_customdata->instance; $url = new \moodle_url('course/view.php', ['id' => $instance->courseid]); @@ -55,16 +76,31 @@ public function definition() { $mform->setType('coupon', PARAM_TEXT); $coupongroup[] = $mform->createElement('submit', 'submitcoupon', get_string('applycoupon', 'enrol_wallet')); } - $mform->addGroup($coupongroup, 'coupons', get_string('applycoupon', 'enrol_wallet'), null, false); - $mform->addHelpButton('coupons', 'applycoupon', 'enrol_wallet'); - $mform->addElement('hidden', 'userid'); - $mform->setType('userid', PARAM_INT); - $mform->setDefault('userid', $USER->id); + if (empty($instance->cmid) && empty($instance->sectionid)) { + $mform->addGroup($coupongroup, 'coupons', get_string('applycoupon', 'enrol_wallet'), null, false); + $mform->addHelpButton('coupons', 'applycoupon', 'enrol_wallet'); + } else { + $mform->addGroup($coupongroup, 'coupons', null, null, false); + } + + if (!empty($instance->id)) { + $mform->addElement('hidden', 'instanceid'); + $mform->setType('instanceid', PARAM_INT); + $mform->setDefault('instanceid', $instance->id); + } - $mform->addElement('hidden', 'instanceid'); - $mform->setType('instanceid', PARAM_INT); - $mform->setDefault('instanceid', $instance->id); + if (!empty($instance->cmid)) { + $mform->addElement('hidden', 'cmid'); + $mform->setType('cmid', PARAM_INT); + $mform->setDefault('cmid', $instance->cmid); + } + + if (!empty($instance->sectionid)) { + $mform->addElement('hidden', 'sectionid'); + $mform->setType('sectionid', PARAM_INT); + $mform->setDefault('sectionid', $instance->sectionid); + } $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); @@ -78,5 +114,4 @@ public function definition() { $mform->setType('sesskey', PARAM_TEXT); $mform->setDefault('sesskey', sesskey()); } - } diff --git a/classes/form/enrol_form.php b/classes/form/enrol_form.php index 6b3ed0a9..81fa89c0 100644 --- a/classes/form/enrol_form.php +++ b/classes/form/enrol_form.php @@ -73,6 +73,7 @@ public function definition() { $heading = $plugin->get_instance_name($instance); $mform->addElement('header', 'walletheader', $heading); + // Display cost and balance. if ($costafter == $costbefore) { $mform->addElement('html', get_string('checkout', 'enrol_wallet', ['credit_cost' => $costbefore, 'user_balance' => $balance])); @@ -81,6 +82,7 @@ public function definition() { ['credit_cost' => $costbefore, 'user_balance' => $balance, 'after_discount' => $costafter])); } + // Display refund policy if enabled. $refund = get_config('enrol_wallet', 'unenrolrefund'); $policy = get_config('enrol_wallet', 'unenrolrefundpolicy'); if (!empty($refund) && !empty($policy)) { diff --git a/classes/form/insuf_form.php b/classes/form/insuf_form.php index 63387efb..7a85c312 100644 --- a/classes/form/insuf_form.php +++ b/classes/form/insuf_form.php @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** This is realy just a display for user that he has insufficient wallet ballance to enrol. +/** This is really just a display for user that he has insufficient wallet ballance to enrol. * @package enrol_wallet * @copyright 2023 Mo Farouk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later diff --git a/classes/form/topup_form.php b/classes/form/topup_form.php index a5270c2a..74d43365 100644 --- a/classes/form/topup_form.php +++ b/classes/form/topup_form.php @@ -63,7 +63,6 @@ public function definition() { $mform->setType('sesskey', PARAM_TEXT); $mform->setDefault('sesskey', sesskey()); - $this->add_action_buttons(false, 'Apply'); + $this->add_action_buttons(false, get_string('topup', 'enrol_wallet')); } - } diff --git a/classes/task/turn_non_refundable.php b/classes/task/turn_non_refundable.php index 5c7b7b28..71c934a1 100644 --- a/classes/task/turn_non_refundable.php +++ b/classes/task/turn_non_refundable.php @@ -133,7 +133,7 @@ public function apply_transformation($userid, $transform) { $refundenabled = get_config('enrol_wallet', 'enablerefund'); if (empty($refundenabled)) { $transform = $balance; - $trace = 'Refunding is disabled in this website, all of user\'s balance will transform...'."\n"; + $trace .= 'Refunding is disabled in this website, all of user\'s balance will transform...'."\n"; } $recorddata = [ diff --git a/classes/transactions.php b/classes/transactions.php index 36446d73..c48e903a 100644 --- a/classes/transactions.php +++ b/classes/transactions.php @@ -128,6 +128,7 @@ public static function payment_topup($amount, $userid, $description = '', $charg * @return mixed */ public static function debit($userid, float $amount, $coursename = '', $charger = '') { + global $DB; if (empty($charger)) { $charger = $userid; @@ -157,13 +158,13 @@ public static function debit($userid, float $amount, $coursename = '', $charger $response = 'done'; } + // No debit occurs. if ($newbalance >= $before) { return false; } - // Inserting a record in the transaction table. - global $DB; + // Inserting a record in the transaction table. $a = (object)[ 'amount' => $amount, 'charger' => $charger, @@ -206,6 +207,7 @@ public static function debit($userid, float $amount, $coursename = '', $charger * @return float|false|string */ public static function get_user_balance($userid) { + global $DB; $source = get_config('enrol_wallet', 'walletsource'); @@ -222,7 +224,6 @@ public static function get_user_balance($userid) { } else if ($source == self::SOURCE_MOODLE) { - global $DB; // Get the balance from the last transaction. $record = $DB->get_records('enrol_wallet_transactions', ['userid' => $userid], 'id DESC', 'balance', 0, 1); @@ -394,6 +395,8 @@ public static function get_coupon_value($coupon, $userid, $instanceid = 0, $appl * @return void */ public static function mark_coupon_used($coupon, $userid, $instanceid) { + global $DB; + // Unset the session coupon to make sure not used again. if (isset($_SESSION['coupon'])) { $_SESSION['coupon'] = ''; @@ -407,7 +410,6 @@ public static function mark_coupon_used($coupon, $userid, $instanceid) { $couponrecord = (object)self::get_coupon_value($coupon, $userid, $instanceid, true); } else { - global $DB; $couponrecord = $DB->get_record('enrol_wallet_coupons', ['code' => $coupon]); $usage = $couponrecord->usetimes + 1; @@ -439,7 +441,7 @@ public static function mark_coupon_used($coupon, $userid, $instanceid) { ] ]; - if (!empty($instanceid) && $instanceid != 0) { + if (!empty($instanceid)) { $instance = $DB->get_record('enrol', ['enrol' => 'wallet', 'id' => $instanceid], '*', MUST_EXIST); $eventdata['courseid'] = $instance->courseid; diff --git a/extra/action.php b/extra/action.php index 043991ef..b016ad5b 100644 --- a/extra/action.php +++ b/extra/action.php @@ -33,23 +33,31 @@ $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 = required_param('userid', PARAM_INT); +$userid = $USER->id; $coupon = required_param('coupon', PARAM_TEXT); $instanceid = optional_param('instanceid' , '', PARAM_INT); $courseid = optional_param('courseid', 0, PARAM_INT); $couponsetting = get_config('enrol_wallet', 'coupons'); +$cmid = optional_param('cmid', 0, PARAM_INT); +$sectionid = optional_param('sectionid', 0, PARAM_INT); if (confirm_sesskey()) { // Get the coupon data. $coupondata = enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, false); if (empty($coupondata) || is_string($coupondata)) { - $errormessage = get_string('coupon_applyerror', 'enrol_wallet', $coupondata); + $msg = get_string('coupon_applyerror', 'enrol_wallet', $coupondata); + $msgtype = 'error'; // This mean that the function return error. - redirect($redirecturl, $errormessage); } else { + $value = $coupondata['value']; $type = $coupondata['type']; // Check the type to determine what to do. @@ -57,7 +65,7 @@ ($couponsetting == enrol_wallet_plugin::WALLET_COUPONSFIXED || $couponsetting == enrol_wallet_plugin::WALLET_COUPONSALL)) { - // Apply the coupon code to add his value to the user's wallet. + // Apply the coupon code to add its value to the user's wallet. enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, true); $currency = get_config('enrol_wallet', 'currency'); $a = [ @@ -65,31 +73,50 @@ 'currency' => $currency, ]; $msg = get_string('coupon_applyfixed', 'enrol_wallet', $a); + $msgtype = 'success'; - redirect($redirecturl, $msg, null, 'success'); } else if ($type == 'percent' && ($couponsetting == enrol_wallet_plugin::WALLET_COUPONSDISCOUNT || $couponsetting == enrol_wallet_plugin::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); - redirect($redirecturl, $msg, null, 'success'); + $msgtype = 'success'; + } else { + $msg = get_string('coupon_applynocourse', 'enrol_wallet'); - redirect($redirecturl, $msg); + $msgtype = 'error'; + } + } else if ($type == 'percent' && + ($couponsetting == enrol_wallet_plugin::WALLET_COUPONSDISCOUNT + || $couponsetting == enrol_wallet_plugin::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 == 'percent' && empty($instanceid)) { + $msg = get_string('coupon_applynothere', 'enrol_wallet'); - redirect($redirecturl, $msg); + $msgtype = 'error'; } else { - $msg = 'Invalid Action.'; - redirect($redirecturl, $msg); + + $msg = get_string('invalidcoupon_operation', 'enrol_wallet'); + $msgtype = 'error'; } } + + redirect($redirecturl, $msg, null, $msgtype); } diff --git a/extra/bulkedit_action.php b/extra/bulkedit_action.php index e389a5bd..c1875d39 100644 --- a/extra/bulkedit_action.php +++ b/extra/bulkedit_action.php @@ -58,6 +58,7 @@ $status = optional_param('status', -1, PARAM_INT); $plugins = optional_param_array('plugins', [], PARAM_TEXT); + foreach ($plugins as $plugin) { $$plugin = enrol_get_plugin($plugin); } diff --git a/extra/bulkinstances_action.php b/extra/bulkinstances_action.php index d2be077d..689734b3 100644 --- a/extra/bulkinstances_action.php +++ b/extra/bulkinstances_action.php @@ -171,7 +171,7 @@ foreach ($courses as $courseid) { $context = context_course::instance($courseid); - + // Check the capability for each course. if (!has_capability('enrol/wallet:manage', $context)) { continue; } @@ -189,7 +189,7 @@ $i++; $wallet->update_instance($instance, $data); } - + // No wallet instances exists in the course? Add one. if ($count < 1) { $data->timecreated = time(); $course = get_course($courseid); diff --git a/extra/charger.php b/extra/charger.php index c206d597..c6e23fb3 100644 --- a/extra/charger.php +++ b/extra/charger.php @@ -23,8 +23,6 @@ */ require_once(__DIR__.'/../../../config.php'); -global $USER, $DB, $CFG; - require_once($CFG->dirroot.'/enrol/wallet/locallib.php'); $context = context_system::instance(); @@ -32,32 +30,31 @@ require_login(); require_capability('enrol/wallet:creditdebit', $context); -$op = optional_param('op', 'none', PARAM_TEXT); +global $USER, $DB; +$op = optional_param('op', '', PARAM_TEXT); -if ($op != 'none' && $op != 'result' && confirm_sesskey()) { +if (!empty($op) && $op != 'result' && confirm_sesskey()) { $value = optional_param('value', '', PARAM_NUMBER); $userid = required_param("userlist", PARAM_INT); $err = ''; $charger = $USER->id; + // No value. if (empty($value) && ($op !== 'balance')) { $err = get_string('charger_novalue', 'enrol_wallet'); - $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', [ - 'error' => $err, - 'op' => 'result' - ]); - redirect($redirecturl, $err); + $params = ['error' => $err, 'op' => 'result']; + $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', $params); + redirect($redirecturl, $err, null, 'error'); } - + // No user. if (empty($userid)) { $err = get_string('charger_nouser', 'enrol_wallet'); - $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', [ - 'error' => $err, - 'op' => 'result' - ]); - redirect($redirecturl, $err); + $params = ['error' => $err, 'op' => 'result']; + $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', $params); + redirect($redirecturl, $err, null, 'error'); } + $transactions = new enrol_wallet\transactions; $before = $transactions->get_user_balance($userid); if ($op === 'credit') { @@ -70,10 +67,11 @@ } else if ($op === 'debit') { if ($value > $before) { + // Cannot deduct more than the user's balance. $a = ['value' => $value, 'before' => $before]; $err = get_string('charger_debit_err', 'enrol_wallet', $a); - $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', array('error' => $err, - 'op' => 'result')); + $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', ['error' => $err, + 'op' => 'result']); redirect($redirecturl); } else { // Process the payment. @@ -89,14 +87,15 @@ $result = get_string('charger_invalid_operation', 'enrol_wallet'); } + $params = [ + 'result' => $result, + 'before' => $before, + 'after' => ($op == 'balance') ? $before : $after, + 'userid' => $userid, + 'op' => 'result' + ]; // Redirect to same page to show results. - $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', [ - 'result' => $result, - 'before' => $before, - 'after' => ($op == 'balance') ? $before : $after, - 'userid' => $userid, - 'op' => 'result' - ]); + $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', $params); redirect($redirecturl); diff --git a/lang/en/enrol_wallet.php b/lang/en/enrol_wallet.php index 02b7e469..2bcb1de9 100644 --- a/lang/en/enrol_wallet.php +++ b/lang/en/enrol_wallet.php @@ -227,6 +227,7 @@ $string['insufficientbalance'] = 'Sorry, you have insufficient balance for this operation. You need {$a->amount} while you have only {$a->balance}'; $string['inyourwallet'] = 'in your wallet.'; $string['invalidpercentcoupon'] = 'Invalid value for percentage coupon, cannot exceed 100.'; +$string['invalidcoupon_operation'] = 'Invalid coupon operation, This coupon type may be disabled is this site or invalid configuration.'; $string['longtimenosee'] = 'Un-enrol inactive after'; $string['longtimenosee_help'] = 'If users haven\'t accessed a course for a long time, then they are automatically unenrolled. This parameter specifies that time limit.'; @@ -317,6 +318,7 @@ $string['transactions'] = 'Wallet Transactions'; +$string['topup'] = 'topup'; $string['topupvalue'] = 'TopUp Value'; $string['topupvalue_help'] = 'Value to topup your wallet by using payment methods'; $string['topupcoupon_desc'] = 'by coupon code {$a}'; diff --git a/lib.php b/lib.php index 998da8d4..d122c455 100644 --- a/lib.php +++ b/lib.php @@ -23,8 +23,8 @@ */ defined('MOODLE_INTERNAL') || die(); - -require_once('extendlib.php'); +global $CFG; +require_once("$CFG->dirroot/enrol/wallet/extendlib.php"); use enrol_wallet\form\enrol_form; use enrol_wallet\form\empty_form; @@ -140,7 +140,7 @@ public function allow_unenrol(stdClass $instance) { /** * Return unenrol link to unenrol user from the current course. - * + * Null if unenrol self is not allowed or the user doesn't has the capability to unernol. * @param stdClass $instance * @return moodle_url|null */ @@ -183,7 +183,7 @@ public function get_unenrolself_link($instance) { * @return void */ public function unenrol_user(stdClass $instance, $userid) { - // Check if refund upon unenrollment is enabled. + // Check if refund upon unenrolment is enabled. $enabled = get_config('enrol_wallet', 'unenrolrefund'); if (empty($enabled)) { return parent::unenrol_user($instance, $userid); @@ -454,7 +454,7 @@ public function is_course_enrolment_restriction($instance) { * @return string html text, usually a form in a text box */ public function enrol_page_hook(stdClass $instance) { - global $OUTPUT, $USER; + global $OUTPUT, $USER, $CFG; $coupon = $this->check_discount_coupon(); $couponsetting = get_config('enrol_wallet', 'coupons'); @@ -551,6 +551,7 @@ public function enrol_page_hook(stdClass $instance) { // If payment is enabled in general, adding topup option. $account = get_config('enrol_wallet', 'paymentaccount'); + require_once("$CFG->dirroot/enrol/wallet/locallib.php"); if (enrol_wallet_is_valid_account($account)) { $topupurl = new moodle_url('/enrol/wallet/extra/topup.php'); $topupform = new topup_form($topupurl, $data); @@ -1577,23 +1578,26 @@ public static function check_discount_coupon() { * @return float the cost after discount. */ public static function get_cost_after_discount($userid, $instance, $coupon = null) { - global $DB; + global $DB, $_SESSION; $couponsetting = get_config('enrol_wallet', 'coupons'); // Check if there is a discount coupon first. if (empty($coupon)) { $coupon = self::check_discount_coupon(); } - // Save coupon in session. - $_SESSION['coupon'] = $coupon; $costaftercoupon = $instance->cost; if (!empty($coupon) && $couponsetting != self::WALLET_NOCOUPONS) { + // Save coupon in session. + $_SESSION['coupon'] = $coupon; + $coupondata = transactions::get_coupon_value($coupon, $userid); $type = (is_array($coupondata)) ? $coupondata['type'] : ''; if ($type == 'percent' && $couponsetting != self::WALLET_COUPONSFIXED && $coupondata['value'] <= 100) { + $costaftercoupon = $instance->cost * (1 - $coupondata['value'] / 100); + } else if ($type == 'fixed' && $couponsetting != self::WALLET_COUPONSDISCOUNT) { // There is no need for this condition as if the type is fixed. // we add the value to the wallet then redirect to enrolment page again. @@ -1642,7 +1646,8 @@ public static function get_cost_after_discount($userid, $instance, $coupon = nul * @return false|string */ public static function show_payment_info(stdClass $instance, $costafter) { - global $USER, $OUTPUT, $DB; + global $USER, $OUTPUT, $DB, $CFG; + require_once("$CFG->dirroot/enrol/wallet/locallib.php"); if (!enrol_wallet_is_valid_account($instance->customint1)) { return ''; } diff --git a/settings.php b/settings.php index fc624fa7..dae6367e 100644 --- a/settings.php +++ b/settings.php @@ -33,7 +33,7 @@ $walletplugin = enrol_get_plugin('wallet'); // General settings. $settings->add(new admin_setting_heading('enrol_wallet_settings', '', - get_string('pluginname_desc', 'enrol_wallet'))); + get_string('pluginname_desc', 'enrol_wallet'))); // Adding choice between using wordpress (woowallet) of internal moodle wallet. $sources = [ enrol_wallet\transactions::SOURCE_WORDPRESS => get_string('sourcewordpress', 'enrol_wallet'),