Skip to content

Commit

Permalink
v4
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Nov 13, 2023
1 parent 2da59c3 commit 3bcb978
Show file tree
Hide file tree
Showing 39 changed files with 1,039 additions and 354 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Wallet Enrollment for Moodle #
==========
## V 4.0.0 ##
- Many fixes.
- Improve the validation of forms.
- Add new types of coupons.
- Add coupons usage page.
- Add repurchase option.
- Improve filtrations of transaction table and coupons tables.
- After creating coupons, redirect to coupons table with only the newly created coupons displayed.

## V 3.1.0 ##
- Add Referral program.
- Fix some bugs.
Expand Down
4 changes: 2 additions & 2 deletions classes/deleteselectedusers_operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function get_title() {
* @return \enrol_wallet\deleteselectedusers_form
*/
public function get_form($defaultaction = null, $defaultcustomdata = null) {
if (!array($defaultcustomdata)) {
$defaultcustomdata = array();
if (!is_array($defaultcustomdata)) {
$defaultcustomdata = [];
}
$defaultcustomdata['title'] = $this->get_title();
$defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_wallet');
Expand Down
2 changes: 1 addition & 1 deletion classes/editselectedusers_operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function process(\course_enrolment_manager $manager, array $users, \stdCl
'courseid' => $enrolment->courseid,
'context' => \context_course::instance($enrolment->courseid),
'relateduserid' => $user->id,
'other' => ['enrol' => 'wallet']
'other' => ['enrol' => 'wallet'],
]
);
$event->trigger();
Expand Down
65 changes: 41 additions & 24 deletions classes/form/charger_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ public function definition() {
}
}

if (file_exists($CFG->dirroot.'/blocks/vc/lib.php')
&& !empty($this->_customdata['vc'])
&& function_exists('block_vc_extend_credit_form')) {

require_once($CFG->dirroot.'/blocks/vc/lib.php');
block_vc_extend_credit_form($mform, $this->get_data());
}

$mform->addElement('header', 'main', get_string('chargingoptions', 'enrol_wallet'));

$operations = [
'credit' => 'credit',
'debit' => 'debit',
'balance' => 'balance'
'balance' => 'balance',
];
$oplabel = get_string('chargingoperation', 'enrol_wallet');
$attr = !empty($i) ? ['id' => 'charge-operation', 'onchange' => 'calculateCharge()'] : [];
Expand Down Expand Up @@ -104,10 +112,9 @@ public function definition() {
'courseid' => $courseid,
'enrolid' => 0,
'perpage' => $CFG->maxusersperpage,
'userfields' => implode(',', \core_user\fields::get_identity_fields($context, true))
'userfields' => implode(',', \core_user\fields::get_identity_fields($context, true)),
];
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), [], $options);
$mform->addRule('userlist', 'select user', 'required', null, 'client');

$mform->addElement('submit', 'submit', get_string('submit'));

Expand Down Expand Up @@ -168,30 +175,40 @@ public function validation($data, $files) {

global $DB;
$errors = parent::validation($data, $files);
$op = $data['op'];
if (!in_array($op, ['credit', 'debit', 'balance'])) {
$errors['op'] = get_string('charger_invalid_operation', 'enrol_wallet');
return $errors;
}
if (!empty($data['submit'])) {
if (empty($data['userlist'])) {
$errors['userlist'] = get_string('selectuser', 'enrol_wallet');
}

$value = $data['value'] ?? '';
$userid = $data['userlist'];
// No value.
if (empty($value) && ($op !== 'balance')) {
$errors['value'] = get_string('charger_novalue', 'enrol_wallet');
}
$op = $data['op'];
if (!in_array($op, ['credit', 'debit', 'balance'])) {
$errors['op'] = get_string('charger_invalid_operation', 'enrol_wallet');
return $errors;
}

// No user.
if (empty($userid) || !$DB->record_exists('user', ['id' => $userid])) {
$errors['userlist'] = get_string('charger_nouser', 'enrol_wallet');
}
$value = $data['value'] ?? '';
$userid = $data['userlist'];
// No value.
if (empty($value) && ($op !== 'balance')) {
$errors['value'] = get_string('charger_novalue', 'enrol_wallet');
}

// No user.
if (empty($userid) || !$DB->record_exists('user', ['id' => $userid])) {
$errors['userlist'] = get_string('charger_nouser', 'enrol_wallet');
}

$transactions = new \enrol_wallet\transactions;
$before = $transactions->get_user_balance($userid);
if ($op == 'debit' && $value > $before) {
// Cannot deduct more than the user's balance.
$a = ['value' => $value, 'before' => $before];
$errors['value'] = get_string('charger_debit_err', 'enrol_wallet', $a);
$transactions = new \enrol_wallet\transactions;
$before = $transactions->get_user_balance($userid);
if ($op == 'debit' && $value > $before) {
// Cannot deduct more than the user's balance.
$a = ['value' => $value, 'before' => $before];
$errors['value'] = get_string('charger_debit_err', 'enrol_wallet', $a);
}

} else if (!empty($data['submitvc'])) {
// TODO add validation function to vc block.
return $errors;
}

return $errors;
Expand Down
4 changes: 2 additions & 2 deletions classes/form/coupons_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public function definition() {
$mform->addHelpButton('maxperuser', 'coupons_maxperuser', 'enrol_wallet');
$mform->setDefault('maxperuser', 0);

$mform->addElement('date_time_selector', 'validfrom', get_string('validfrom', 'enrol_wallet'), array('optional' => true));
$mform->addElement('date_time_selector', 'validto', get_string('validto', 'enrol_wallet'), array('optional' => true));
$mform->addElement('date_time_selector', 'validfrom', get_string('validfrom', 'enrol_wallet'), ['optional' => true]);
$mform->addElement('date_time_selector', 'validto', get_string('validto', 'enrol_wallet'), ['optional' => true]);

$group = [];
$group[] = $mform->createElement('checkbox', 'upper', get_string('upperletters', 'enrol_wallet'));
Expand Down
2 changes: 1 addition & 1 deletion classes/form/enrol_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function definition() {
'credit_cost' => $costbefore,
'user_balance' => $balance,
'after_discount' => $costafter,
'currency' => $currency
'currency' => $currency,
];
// Display cost and balance.
if ($balance >= $costafter) {
Expand Down
2 changes: 1 addition & 1 deletion classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function wallet_completion_awards(\core\event\course_completed $ev
'maxgrade' => $maxgrade,
'percent' => $percentage,
'amount' => $award,
'timecreated' => time()
'timecreated' => time(),
];
$id = $DB->insert_record('enrol_wallet_awards', $data);

Expand Down
2 changes: 1 addition & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
$ids = $DB->get_records('payments', [
'component' => 'enrol_wallet',
'paymentarea' => 'wallettopup',
'userid' => $contextlist->get_user()->id
'userid' => $contextlist->get_user()->id,
]);
foreach ($ids as $payment) {
$DB->delete_records('enrol_wallet_items', ['id' => $payment->itemid, 'userid' => $payment->userid]);
Expand Down
23 changes: 11 additions & 12 deletions classes/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function payment_topup($amount, $userid, $description = '', $charg
'balance' => $newbalance,
'norefund' => $refundable ? $oldnotrefund : $amount + $oldnotrefund,
'descripe' => $description,
'timecreated' => time()
'timecreated' => time(),
];

$id = $DB->insert_record('enrol_wallet_transactions', $recorddata);
Expand Down Expand Up @@ -208,7 +208,7 @@ public static function debit(
'balance' => $newbalance,
'norefund' => ($newbalance >= $oldnotrefund) ? $oldnotrefund : $newbalance,
'descripe' => $description,
'timecreated' => time()
'timecreated' => time(),
];

$id = $DB->insert_record('enrol_wallet_transactions', $recorddata);
Expand Down Expand Up @@ -532,7 +532,8 @@ public static function validate_coupon($coupondata, $area = []) {
}

// Make sure that the coupon didn't exceed the max usage (0 mean unlimited).
if (!empty($couponrecord->maxusage) && $couponrecord->maxusage <= $couponrecord->usetimes) {
$olduse = $DB->count_records('enrol_wallet_coupons_usage', ['code' => $coupondata['code']]);
if (!empty($couponrecord->maxusage) && $couponrecord->maxusage <= max($couponrecord->usetimes, $olduse)) {
return get_string('coupon_exceedusage', 'enrol_wallet');
}

Expand All @@ -550,7 +551,7 @@ public static function validate_coupon($coupondata, $area = []) {
if (!empty($couponrecord->maxperuser)) {
$countperuser = $DB->count_records('enrol_wallet_coupons_usage', [
'code' => $coupondata['code'],
'userid' => $USER->id
'userid' => $USER->id,
]);
if ($countperuser >= $couponrecord->maxperuser) {
return get_string('coupon_exceedusage', 'enrol_wallet');
Expand Down Expand Up @@ -590,13 +591,11 @@ public static function mark_coupon_used($coupon, $userid, $instanceid = 0, $type
} else {

$couponrecord = $DB->get_record('enrol_wallet_coupons', ['code' => $coupon]);
$usage = $couponrecord->usetimes + 1;
$data = (object)[
'id' => $couponrecord->id,
'lastuse' => time(),
'usetimes' => $usage,
];
$DB->update_record('enrol_wallet_coupons', $data);
$olduse = $DB->count_records('enrol_wallet_coupons_usage', ['code' => $coupon]);
$usage = max($couponrecord->usetimes, $olduse) + 1;
$couponrecord->lastuse = time();
$couponrecord->usetimes = $usage;
$DB->update_record('enrol_wallet_coupons', $couponrecord);
}

// Logging the usage in the coupon usage table.
Expand All @@ -615,7 +614,7 @@ public static function mark_coupon_used($coupon, $userid, $instanceid = 0, $type
'userid' => $userid,
'relateduserid' => $userid,
'objectid' => !empty($id) ? $id : null,
'other' => $logdata
'other' => $logdata,
];

if (!empty($instanceid)) {
Expand Down
8 changes: 4 additions & 4 deletions classes/uploadcoupon/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class processor {
protected $cir;

/** @var array CSV columns. */
protected $columns = array();
protected $columns = [];

/** @var int line number. */
protected $linenb = 0;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function execute($tracker = null) {
'maxperuser' => get_string('coupons_maxperuser', 'enrol_wallet'),
'validfrom' => get_string('validfrom', 'enrol_wallet'),
'validto' => get_string('validto', 'enrol_wallet'),
'result' => get_string('upload_result', 'enrol_wallet')
'result' => get_string('upload_result', 'enrol_wallet'),
];
$tracker->start($reportheadings, true);

Expand Down Expand Up @@ -334,7 +334,7 @@ public function execute($tracker = null) {
get_string('coupons_uploadtotal', 'enrol_wallet', $total),
get_string('coupons_uploadcreated', 'enrol_wallet', $created),
get_string('coupons_uploadupdated', 'enrol_wallet', $updated),
get_string('coupons_uploaderrors', 'enrol_wallet', $errors)
get_string('coupons_uploaderrors', 'enrol_wallet', $errors),
];

$tracker->finish();
Expand Down Expand Up @@ -370,7 +370,7 @@ public function reset() {
$this->processstarted = false;
$this->linenb = 0;
$this->cir->init();
$this->errors = array();
$this->errors = [];
}

/**
Expand Down
18 changes: 9 additions & 9 deletions classes/uploadcoupon/tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class tracker {
/**
* @var array columns to display.
*/
protected $columns = array();
protected $columns = [];

/**
* @var bool display visual outcome column.
Expand Down Expand Up @@ -118,17 +118,17 @@ public function start(array $reportheadings, bool $outcomecol = false) {
} else if ($this->outputmode == self::OUTPUT_HTML) {
// Print HTML table.
$ci = 0;
echo \html_writer::start_tag('table', array('class' => 'generaltable boxaligncenter flexible-wrap'));
echo \html_writer::start_tag('table', ['class' => 'generaltable boxaligncenter flexible-wrap']);
echo \html_writer::start_tag('thead');
echo \html_writer::start_tag('tr', array('class' => 'heading r' . $this->rownb));
echo \html_writer::start_tag('tr', ['class' => 'heading r' . $this->rownb]);
if ($this->outcomecol) {
echo \html_writer::tag('th', '', array('class' => 'c' . $ci++, 'scope' => 'col'));
echo \html_writer::tag('th', '', ['class' => 'c' . $ci++, 'scope' => 'col']);
}
// Print the headings in array order, and keep track of the columns and order for printing body rows.
$ci = 0;
foreach ($reportheadings as $hkey => $label) {
echo \html_writer::tag('th', $label,
array('class' => 'c' . $ci++, 'scope' => 'col'));
['class' => 'c' . $ci++, 'scope' => 'col']);
}
echo \html_writer::end_tag('tr');
echo \html_writer::end_tag('thead');
Expand Down Expand Up @@ -170,23 +170,23 @@ public function output(array $rowdata, bool $outcome = false) {
$this->rownb++; // Use to mark odd and even rows for visual striping.

// Print a row of output.
echo \html_writer::start_tag('tr', array('class' => 'r' . $this->rownb % 2));
echo \html_writer::start_tag('tr', ['class' => 'r' . $this->rownb % 2]);
// Print a visual success indicator column (green tickbox or red x) for the outcome.
if ($this->outcomecol) {
if ($outcome) {
$outcome = $OUTPUT->pix_icon('i/valid', '');
} else {
$outcome = $OUTPUT->pix_icon('i/invalid', '');
}
echo \html_writer::tag('td', $outcome, array('class' => 'c' . $ci++));
echo \html_writer::tag('td', $outcome, ['class' => 'c' . $ci++]);
}

// Print a column for each heading.
foreach ($this->columns as $key => $value) {
if (isset($rowdata[$key])) {
echo \html_writer::tag('td', $rowdata[$key], array('class' => 'c' . $ci++));
echo \html_writer::tag('td', $rowdata[$key], ['class' => 'c' . $ci++]);
} else {
echo \html_writer::tag('td', '', array('class' => 'c' . $ci++));
echo \html_writer::tag('td', '', ['class' => 'c' . $ci++]);
}
}
echo \html_writer::end_tag('tr');
Expand Down
4 changes: 2 additions & 2 deletions classes/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function debit($userid, float $amount, $coursename = '', $charger = '') {
'moodle_user_id' => $userid,
'amount' => $amount,
'course' => $coursename,
'charger' => $charger
'charger' => $charger,
];

return $this->request('debit', $data);
Expand All @@ -129,7 +129,7 @@ public function credit($amount, $userid, $description = '', $charger = '') {
'amount' => $amount,
'moodle_user_id' => $userid,
'description' => $description,
'charger' => $charger
'charger' => $charger,
];

$responsedata = $this->request('wallet_topup', $data);
Expand Down
4 changes: 2 additions & 2 deletions cli/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
require_once("$CFG->libdir/clilib.php");

// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('verbose' => false, 'help' => false),
array('v' => 'verbose', 'h' => 'help'));
list($options, $unrecognized) = cli_get_params(['verbose' => false, 'help' => false],
['v' => 'verbose', 'h' => 'help']);

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
Expand Down
Loading

0 comments on commit 3bcb978

Please sign in to comment.