Skip to content

Commit

Permalink
compatibility with availability_wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Jun 29, 2023
1 parent 36ef212 commit 9a1326b
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 66 deletions.
55 changes: 45 additions & 10 deletions classes/form/applycoupon_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
Expand All @@ -78,5 +114,4 @@ public function definition() {
$mform->setType('sesskey', PARAM_TEXT);
$mform->setDefault('sesskey', sesskey());
}

}
2 changes: 2 additions & 0 deletions classes/form/enrol_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion classes/form/insuf_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/** 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 <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down
3 changes: 1 addition & 2 deletions classes/form/topup_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}
2 changes: 1 addition & 1 deletion classes/task/turn_non_refundable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
12 changes: 7 additions & 5 deletions classes/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');

Expand All @@ -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);

Expand Down Expand Up @@ -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'] = '';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 38 additions & 11 deletions extra/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,63 +33,90 @@
$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.
if ($type == 'fixed' &&
($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 = [
'value' => $value,
'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);
}
1 change: 1 addition & 0 deletions extra/bulkedit_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions extra/bulkinstances_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 9a1326b

Please sign in to comment.