From 673387d1664c4ef1a1cea07cf13c9cf4819b4ad4 Mon Sep 17 00:00:00 2001 From: Mo Farouk <122031508+fmido88@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:49:50 +0300 Subject: [PATCH] V 1.7 --- CHANGELOG.md | 6 + classes/editselectedusers_operation.php | 12 +- classes/event/transactions_triggered.php | 1 + classes/form/applycoupon_form.php | 4 +- classes/form/insuf_form.php | 2 +- classes/notifications.php | 12 +- classes/observer.php | 38 +++-- classes/task/turn_non_refundable.php | 18 +-- classes/transactions.php | 72 ++++----- classes/wordpress.php | 74 +++++---- db/access.php | 104 +++++++------ externallib.php | 14 +- extra/action.php | 4 +- extra/bulkedit_action.php | 20 +-- extra/charger.php | 24 +-- extra/coupondelete.php | 70 +++++++++ extra/couponedit.php | 181 ++++++++++++++++++----- extra/coupontable.php | 73 ++++----- extra/generator.php | 2 +- extra/transaction.php | 74 ++++----- extrasettings.php | 103 +++++++++++++ lang/en/enrol_wallet.php | 24 ++- lib.php | 121 +++++++-------- locallib.php | 19 ++- settings.php | 83 +++-------- unenrolself.php | 12 +- version.php | 8 +- 27 files changed, 747 insertions(+), 428 deletions(-) create mode 100644 extra/coupondelete.php create mode 100644 extrasettings.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d9d807..d102ce99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Wallet Enrollment for Moodle # ========== +## V 1.7.0 ## +- Add ability for editing coupons. +- Hide cheaper instances or non available enrol wallet instance in the same course. +- Fix and test logging in and out to wordpress. +- Bug fixes and enhancement. + ## V 1.6.7 ## - Fix and enhance coupons and transaction pages. diff --git a/classes/editselectedusers_operation.php b/classes/editselectedusers_operation.php index 900fad86..f165aa52 100644 --- a/classes/editselectedusers_operation.php +++ b/classes/editselectedusers_operation.php @@ -63,8 +63,8 @@ public function process(\course_enrolment_manager $manager, array $users, \stdCl } // Get all of the user enrolment id's. - $ueids = array(); - $instances = array(); + $ueids = []; + $instances = []; foreach ($users as $user) { foreach ($user->enrolments as $enrolment) { $ueids[] = $enrolment->id; @@ -88,7 +88,7 @@ public function process(\course_enrolment_manager $manager, array $users, \stdCl list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED); - $updatesql = array(); + $updatesql = []; if ($status == ENROL_USER_ACTIVE || $status == ENROL_USER_SUSPENDED) { $updatesql[] = 'status = :status'; $params['status'] = (int)$status; @@ -126,13 +126,13 @@ public function process(\course_enrolment_manager $manager, array $users, \stdCl $enrolment->enrol = 'wallet'; // Trigger event. $event = \core\event\user_enrolment_updated::create( - array( + [ 'objectid' => $enrolment->id, 'courseid' => $enrolment->courseid, 'context' => \context_course::instance($enrolment->courseid), 'relateduserid' => $user->id, - 'other' => array('enrol' => 'wallet') - ) + 'other' => ['enrol' => 'wallet'] + ] ); $event->trigger(); } diff --git a/classes/event/transactions_triggered.php b/classes/event/transactions_triggered.php index a2b77773..c032d57b 100644 --- a/classes/event/transactions_triggered.php +++ b/classes/event/transactions_triggered.php @@ -69,6 +69,7 @@ public function get_description() { $type = $this->other['type']; if ($type == 'debit') { return get_string('event_transaction_debit_description', 'enrol_wallet', $a); + } else if ($type == 'credit') { $refundable = clean_param($this->other['refundable'], PARAM_BOOL); $a->refundable = $refundable ? 'refundable' : 'not refundable'; diff --git a/classes/form/applycoupon_form.php b/classes/form/applycoupon_form.php index fd1ed739..2dbdbae4 100644 --- a/classes/form/applycoupon_form.php +++ b/classes/form/applycoupon_form.php @@ -41,7 +41,9 @@ public function definition() { $mform = $this->_form; $instance = $this->_customdata->instance; $url = new \moodle_url('course/view.php', ['id' => $instance->courseid]); - $coupon = optional_param('coupon', '', PARAM_TEXT); + + $wallet = enrol_get_plugin('wallet'); + $coupon = $wallet->check_discount_coupon(); $coupongroup = []; if (!empty($coupon)) { diff --git a/classes/form/insuf_form.php b/classes/form/insuf_form.php index afc50ca2..63387efb 100644 --- a/classes/form/insuf_form.php +++ b/classes/form/insuf_form.php @@ -27,7 +27,7 @@ require_once($CFG->libdir.'/formslib.php'); /** - * 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. */ class insuf_form extends \moodleform { diff --git a/classes/notifications.php b/classes/notifications.php index 2abea2ad..beb0d5d1 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -44,12 +44,12 @@ public static function transaction_notify($data) { $time = userdate($data['timecreated']); $a = (object)[ - 'type' => $type, - 'amount' => $amount, - 'before' => $before, + 'type' => $type, + 'amount' => $amount, + 'before' => $before, 'balance' => $balance, - 'desc' => $desc, - 'time' => $time, + 'desc' => $desc, + 'time' => $time, ]; $user = \core_user::get_user($userid); $message = new \core\message\message(); @@ -72,7 +72,7 @@ public static function transaction_notify($data) { $message->smallmessage = $desc; $message->notification = 1; // Because this is a notification generated from Moodle, not a user-to-user message. - $content = array('*' => array('header' => ' Wallet Transaction ', 'footer' => '')); // Extra content for specific processor. + $content = ['*' => ['header' => ' Wallet Transaction ', 'footer' => '']]; // Extra content for specific processor. $message->set_additional_content('email', $content); // Actually send the message. diff --git a/classes/observer.php b/classes/observer.php index 72bcb4cf..8b58bb18 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -99,25 +99,25 @@ public static function wallet_completion_awards(\core\event\course_completed $ev // Insert the record. $data = [ - 'userid' => $userid, + 'userid' => $userid, 'courseid' => $courseid, - 'grade' => $usergrade, + 'grade' => $usergrade, 'maxgrade' => $maxgrade, - 'percent' => $percentage, - 'amount' => $award, + 'percent' => $percentage, + 'amount' => $award, 'timecreated' => time() ]; $id = $DB->insert_record('enrol_wallet_awards', $data); // Trigger award event. $eventdata = [ - 'context' => \context_course::instance($courseid), - 'userid' => $userid, + 'context' => \context_course::instance($courseid), + 'userid' => $userid, 'relateduserid' => $userid, - 'objectid' => $id, - 'courseid' => $courseid, + 'objectid' => $id, + 'courseid' => $courseid, 'other' => [ - 'grade' => number_format($percentage, 2), + 'grade' => number_format($percentage, 2), 'amount' => $award, ], ]; @@ -152,10 +152,10 @@ public static function wallet_gifting_new_user(\core\event\user_created $event) // Trigger gifts event. $eventdata = [ - 'context' => \context_system::instance(), - 'userid' => $userid, + 'context' => \context_system::instance(), + 'userid' => $userid, 'relateduserid' => $userid, - 'objectid' => $id, + 'objectid' => $id, 'other' => [ 'amount' => $giftvalue, ], @@ -225,6 +225,13 @@ public static function login_to_wordpress(\core\event\user_loggedin $event) { return; } + $wordpressurl = get_config('enrol_wallet', 'wordpress_url'); + $wordpressurl = clean_param($wordpressurl, PARAM_URL); + $allowed = get_config('enrol_wallet', 'wordpressloggins'); + if (empty($allowed) || empty($wordpressurl)) { + return; + } + // Clone the old wantsurl. $params = []; if (isset($SESSION->wantsurl)) { @@ -257,6 +264,13 @@ public static function logout_from_wordpress(\core\event\user_loggedout $event) return; } + $wordpressurl = get_config('enrol_wallet', 'wordpress_url'); + $wordpressurl = clean_param($wordpressurl, PARAM_URL); + $allowed = get_config('enrol_wallet', 'wordpressloggins'); + if (empty($allowed) || empty($wordpressurl)) { + return; + } + $params = []; if (!empty($redirect)) { $params['redirect'] = $redirect; diff --git a/classes/task/turn_non_refundable.php b/classes/task/turn_non_refundable.php index 5bff7c2a..5c7b7b28 100644 --- a/classes/task/turn_non_refundable.php +++ b/classes/task/turn_non_refundable.php @@ -89,8 +89,8 @@ public function check_transform_validation($data) { $where = "userid = :userid AND type = :type AND timecreated >= :checktime"; $params = [ - 'userid' => $userid, - 'type' => 'debit', + 'userid' => $userid, + 'type' => 'debit', 'checktime' => time() - $period, ]; $records = $DB->get_records_select('enrol_wallet_transactions', $where, $params, 'id DESC', 'id, amount'); @@ -137,13 +137,13 @@ public function apply_transformation($userid, $transform) { } $recorddata = [ - 'userid' => $userid, - 'amount' => 0, - 'type' => 'credit', - 'balbefore' => $balance, - 'balance' => $balance, - 'norefund' => min($norefund + $transform, $balance), - 'descripe' => 'Transform the transaction to nonrefundable due to expiring of refund period.'."\n", + 'userid' => $userid, + 'amount' => 0, + 'type' => 'credit', + 'balbefore' => $balance, + 'balance' => $balance, + 'norefund' => min($norefund + $transform, $balance), + 'descripe' => get_string('nonrefundable_transform_desc', 'enrol_wallet'), 'timecreated' => time(), ]; $DB->insert_record('enrol_wallet_transactions', $recorddata); diff --git a/classes/transactions.php b/classes/transactions.php index 8a1eaebe..be016e4c 100644 --- a/classes/transactions.php +++ b/classes/transactions.php @@ -93,13 +93,13 @@ public static function payment_topup($amount, $userid, $description = '', $charg $oldnotrefund = self::get_nonrefund_balance($userid); $recorddata = [ - 'userid' => $userid, - 'type' => 'credit', - 'amount' => $amount, - 'balbefore' => $before, - 'balance' => $newbalance, - 'norefund' => $refundable ? $oldnotrefund : $amount + $oldnotrefund, - 'descripe' => $description, + 'userid' => $userid, + 'type' => 'credit', + 'amount' => $amount, + 'balbefore' => $before, + 'balance' => $newbalance, + 'norefund' => $refundable ? $oldnotrefund : $amount + $oldnotrefund, + 'descripe' => $description, 'timecreated' => time() ]; @@ -158,8 +158,8 @@ public static function debit($userid, float $amount, $coursename = '', $charger global $DB; $a = (object)[ - 'amount' => $amount, - 'charger' => $charger, + 'amount' => $amount, + 'charger' => $charger, 'coursename' => $coursename, ]; @@ -172,13 +172,13 @@ public static function debit($userid, float $amount, $coursename = '', $charger $oldnotrefund = self::get_nonrefund_balance($userid); $recorddata = [ - 'userid' => $userid, - 'type' => 'debit', - 'amount' => $amount, - 'balbefore' => $before, - 'balance' => $newbalance, - 'norefund' => ($newbalance >= $oldnotrefund) ? $oldnotrefund : $newbalance, - 'descripe' => $description, + 'userid' => $userid, + 'type' => 'debit', + 'amount' => $amount, + 'balbefore' => $before, + 'balance' => $newbalance, + 'norefund' => ($newbalance >= $oldnotrefund) ? $oldnotrefund : $newbalance, + 'descripe' => $description, 'timecreated' => time() ]; @@ -312,7 +312,7 @@ public static function get_coupon_value($coupon, $userid, $instanceid = 0, $appl // Set the returning coupon data. $coupondata = [ 'value' => $couponrecord->value, - 'type' => $couponrecord->type, + 'type' => $couponrecord->type, ]; } @@ -406,8 +406,8 @@ public static function mark_coupon_used($coupon, $userid, $instanceid) { $couponrecord = $DB->get_record('enrol_wallet_coupons', ['code' => $coupon]); $usage = $couponrecord->usetimes + 1; $data = (object)[ - 'id' => $couponrecord->id, - 'lastuse' => time(), + 'id' => $couponrecord->id, + 'lastuse' => time(), 'usetimes' => $usage, ]; $DB->update_record('enrol_wallet_coupons', $data); @@ -415,22 +415,22 @@ public static function mark_coupon_used($coupon, $userid, $instanceid) { // Logging the usage in the coupon usage table. $logdata = (object)[ - 'code' => $coupon, - 'type' => $couponrecord->type, - 'value' => $couponrecord->value, - 'userid' => $userid, + 'code' => $coupon, + 'type' => $couponrecord->type, + 'value' => $couponrecord->value, + 'userid' => $userid, 'instanceid' => $instanceid, - 'timeused' => time(), + 'timeused' => time(), ]; $id = $DB->insert_record('enrol_wallet_coupons_usage', $logdata); $eventdata = [ - 'userid' => $userid, + 'userid' => $userid, 'relateduserid' => $userid, - 'objectid' => !empty($id) ? $id : null, - 'other' => [ - 'code' => $coupon, - ] + 'objectid' => !empty($id) ? $id : null, + 'other' => [ + 'code' => $coupon, + ] ]; if (!empty($instanceid) && $instanceid != 0) { @@ -465,15 +465,15 @@ private static function triger_transaction_event($amount, $type, $charger, $user $context = \context_system::instance(); $eventarray = [ - 'context' => $context, - 'objectid' => $id, - 'userid' => $charger, + 'context' => $context, + 'objectid' => $id, + 'userid' => $charger, 'relateduserid' => $userid, 'other' => [ - 'type' => $type, - 'amount' => $amount, + 'type' => $type, + 'amount' => $amount, 'refundable' => $refundable, - 'desc' => $desc, + 'desc' => $desc, ], ]; @@ -501,7 +501,7 @@ private static function queue_transaction_transformation($id) { $task = new \enrol_wallet\task\turn_non_refundable; $task->set_custom_data( [ - 'id' => $id, + 'id' => $id, 'userid' => $record->userid, 'amount' => $record->amount, ] diff --git a/classes/wordpress.php b/classes/wordpress.php index efedb451..0bae446d 100644 --- a/classes/wordpress.php +++ b/classes/wordpress.php @@ -43,21 +43,21 @@ class wordpress { public function request($method, $data) { $wordpressurl = get_config('enrol_wallet', 'wordpress_url'); + $wordpressurl = clean_param($wordpressurl, PARAM_URL); if (empty($wordpressurl)) { return; } + $url = $wordpressurl . self::ENDPOINT . $method; $encrypted = $this->encrypt_data($data); - $sendingdata = [ - 'encdata' => $encrypted, - ]; + $sendingdata = ['encdata' => $encrypted]; $curl = curl_init(); $curlsetopt = [ - CURLOPT_URL => $url, + CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, - CURLOPT_FAILONERROR => false, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => $sendingdata, + CURLOPT_FAILONERROR => false, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $sendingdata, ]; curl_setopt_array($curl, $curlsetopt); @@ -82,18 +82,15 @@ public function request($method, $data) { public static function encrypt_data($data) { $key = get_config('enrol_wallet', 'wordpress_secretkey'); $data['sk'] = $key; - $query = http_build_query( $data, 'flags_' ); - $token = $query; - - $encryptmethod = 'AES-128-CTR'; + $token = http_build_query( $data, 'flags_' ); $encryptkey = openssl_digest( $key, 'SHA256', true ); - $encryptiv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($encryptmethod)); - $crypttext = openssl_encrypt($token, $encryptmethod, $encryptkey, 0, $encryptiv) . "::" . bin2hex($encryptiv); + $encryptiv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-128-CTR')); + $crypttext = openssl_encrypt($token, 'AES-128-CTR', $encryptkey, 0, $encryptiv) . "::" . bin2hex($encryptiv); $encdata = base64_encode($crypttext); - $encdata = str_replace(array('+', '/', '='), array('-', '_', ''), $encdata); + $encdata = str_replace(['+', '/', '='], ['-', '_', ''], $encdata); $encrypteddata = trim($encdata); return $encrypteddata; @@ -102,17 +99,17 @@ public static function encrypt_data($data) { * Deduct amount from user's wallet. * @param int $userid * @param float $amount - * @param string $coursename - * @param int $charger \\ id of user did the operation. + * @param string $coursename COURSE name at which the request occurred. + * @param int $charger id of user did the operation. * @return mixed */ public function debit($userid, float $amount, $coursename = '', $charger = '') { - $data = array( + $data = [ 'moodle_user_id' => $userid, - 'amount' => $amount, - 'course' => $coursename, // COURSE name at which the request occurred. - 'charger' => $charger - ); + 'amount' => $amount, + 'course' => $coursename, + 'charger' => $charger + ]; return $this->request('debit', $data); } @@ -128,12 +125,12 @@ public function debit($userid, float $amount, $coursename = '', $charger = '') { * @return array|string */ public function credit($amount, $userid, $description = '', $charger = '') { - $data = array( - 'amount' => $amount, + $data = [ + 'amount' => $amount, 'moodle_user_id' => $userid, - 'description' => $description, - 'charger' => $charger - ); + 'description' => $description, + 'charger' => $charger + ]; $responsedata = $this->request('wallet_topup', $data); @@ -146,6 +143,7 @@ public function credit($amount, $userid, $description = '', $charger = '') { return $responsedata['err']; } } + if ($responsedata['success'] == 'false') { // Response format is incorrect. return $responsedata['err']; @@ -165,12 +163,12 @@ public function credit($amount, $userid, $description = '', $charger = '') { */ public function get_coupon($coupon, $userid, $instanceid, $apply) { $method = 'get_coupon_value'; - $data = array( - 'coupon' => $coupon, + $data = [ + 'coupon' => $coupon, 'moodle_user_id' => $userid, - 'instanceid' => $instanceid, - 'apply' => $apply, - ); + 'instanceid' => $instanceid, + 'apply' => $apply, + ]; $responsedata = $this->request($method, $data); @@ -204,7 +202,7 @@ public function get_coupon($coupon, $userid, $instanceid, $apply) { $coupondata = [ 'value' => $couponvalue, - 'type' => $coupontype, + 'type' => $coupontype, ]; return $coupondata; } @@ -262,12 +260,13 @@ private function create_wordpress_user($userid) { public function login_logout_user_to_wordpress($userid, $method, $redirect) { $allowed = get_config('enrol_wallet', 'wordpressloggins'); $wordpressurl = get_config('enrol_wallet', 'wordpress_url'); + $wordpressurl = clean_param($wordpressurl, PARAM_URL); + $user = \core_user::get_user($userid); if ( empty($allowed) // Check if this option allowed in the settings. || empty($wordpressurl) // If the wp url is not set. - || !filter_var($wordpressurl, FILTER_VALIDATE_URL) // If the wp url is valid url. || !$user // If this is a valid user. || isguestuser($user) // Not guest. ) { @@ -277,14 +276,13 @@ public function login_logout_user_to_wordpress($userid, $method, $redirect) { // The data to send to wordpress. $data = [ 'moodle_user_id' => $userid, - 'method' => $method, - 'url' => $redirect, - 'email' => $user->email, - 'username' => $user->username, // ...username and email used for login only in case user need to be created. + 'method' => $method, + 'url' => $redirect, + 'email' => $user->email, + 'username' => $user->username, // ...username and email used for login only in case user need to be created. ]; $encdata = $this->encrypt_data($data); redirect($wordpressurl . '?encdata=' . $encdata . '&moodleurl=' . (new \moodle_url('/'))->out()); } } - diff --git a/db/access.php b/db/access.php index c3b99115..e30304c9 100644 --- a/db/access.php +++ b/db/access.php @@ -27,108 +27,124 @@ $capabilities = [ /* Add or edit enrol-wallet instance in course. */ - 'enrol/wallet:config' => array( - - 'captype' => 'write', + 'enrol/wallet:config' => [ + 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, - 'archetypes' => array( + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW, - ) - ), + 'manager' => CAP_ALLOW, + ] + ], /* Manage user wallet-enrolments. */ - 'enrol/wallet:manage' => array( - - 'captype' => 'write', + 'enrol/wallet:manage' => [ + 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, - 'archetypes' => array( + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW, - ) - ), + 'manager' => CAP_ALLOW, + ] + ], /* Voluntarily unenrol self from course - watch out for data loss. */ - 'enrol/wallet:unenrolself' => array( - 'captype' => 'write', + 'enrol/wallet:unenrolself' => [ + 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, - 'archetypes' => array( + 'archetypes' => [ 'student' => CAP_ALLOW, - ) - ), + ] + ], /* Unenrol anybody from course (including self) - watch out for data loss. */ - 'enrol/wallet:unenrol' => array( - 'captype' => 'write', + 'enrol/wallet:unenrol' => [ + 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, - 'archetypes' => array( + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW, - ) - ), + 'manager' => CAP_ALLOW, + ] + ], + // View pages for bulk edit. 'enrol/wallet:bulkedit' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW, + 'manager' => CAP_ALLOW, ] ], + // View pages for transaction. 'enrol/wallet:transaction' => [ - 'captype' => 'read', + 'captype' => 'read', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], - // View pages for transaction. + + // View other users balance. 'enrol/wallet:viewotherbalance' => [ - 'captype' => 'read', + 'captype' => 'read', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], + // Creating coupons. 'enrol/wallet:createcoupon' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], + // Deleting coupons. 'enrol/wallet:deletecoupon' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], + + // Edit coupons. + 'enrol/wallet:editcoupon' => [ + 'captype' => 'write', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => [ + 'manager' => CAP_ALLOW, + ] + ], + // Download coupons. 'enrol/wallet:downloadcoupon' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], - // Download coupons. + + // View coupons table. 'enrol/wallet:viewcoupon' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], + // Performing Transactions. 'enrol/wallet:creditdebit' => [ - 'captype' => 'write', + 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [ + 'archetypes' => [ 'manager' => CAP_ALLOW, ] ], + ]; diff --git a/externallib.php b/externallib.php index cfecf883..f8dbae5c 100644 --- a/externallib.php +++ b/externallib.php @@ -61,7 +61,7 @@ public static function get_instance_info($instanceid) { require_once($CFG->libdir . '/enrollib.php'); - $params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid)); + $params = self::validate_parameters(self::get_instance_info_parameters(), ['instanceid' => $instanceid]); // Retrieve wallet enrolment plugin. $enrolplugin = enrol_get_plugin('wallet'); @@ -71,8 +71,8 @@ public static function get_instance_info($instanceid) { self::validate_context(context_system::instance()); - $enrolinstance = $DB->get_record('enrol', array('id' => $params['instanceid']), '*', MUST_EXIST); - $course = $DB->get_record('course', array('id' => $enrolinstance->courseid), '*', MUST_EXIST); + $enrolinstance = $DB->get_record('enrol', ['id' => $params['instanceid']], '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $enrolinstance->courseid], '*', MUST_EXIST); if (!core_course_category::can_view_course_info($course) && !can_access_course($course)) { throw new moodle_exception('coursehidden'); } @@ -130,10 +130,10 @@ public static function enrol_user($courseid, $instanceid = 0) { require_once($CFG->libdir . '/enrollib.php'); $params = self::validate_parameters(self::enrol_user_parameters(), - array( - 'courseid' => $courseid, - 'instanceid' => $instanceid - )); + [ + 'courseid' => $courseid, + 'instanceid' => $instanceid + ]); $warnings = []; diff --git a/extra/action.php b/extra/action.php index 32d04aeb..043991ef 100644 --- a/extra/action.php +++ b/extra/action.php @@ -45,7 +45,7 @@ if (confirm_sesskey()) { // Get the coupon data. $coupondata = enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, false); - if (is_string($coupondata) || $coupondata === false) { + if (empty($coupondata) || is_string($coupondata)) { $errormessage = get_string('coupon_applyerror', 'enrol_wallet', $coupondata); // This mean that the function return error. redirect($redirecturl, $errormessage); @@ -82,9 +82,11 @@ $msg = get_string('coupon_applynocourse', 'enrol_wallet'); redirect($redirecturl, $msg); } + } else if ($type == 'percent' && empty($instanceid)) { $msg = get_string('coupon_applynothere', 'enrol_wallet'); redirect($redirecturl, $msg); + } else { $msg = 'Invalid Action.'; redirect($redirecturl, $msg); diff --git a/extra/bulkedit_action.php b/extra/bulkedit_action.php index adbb6264..e389a5bd 100644 --- a/extra/bulkedit_action.php +++ b/extra/bulkedit_action.php @@ -74,7 +74,7 @@ foreach ($enrolusers as $euser) { $instance = $DB->get_record('enrol', ['id' => $euser->ueenrolid]); - if (!in_array($instance->enrol , $plugins)) { + if (!in_array($instance->enrol, $plugins)) { continue; } @@ -82,13 +82,6 @@ continue; } - if ($euser->uestatus == $status - && $euser->uetimestart == $start - && $euser->uetimeend == $end) { - // No change. - continue; - } - $data = new stdClass; if ($status !== -1) { $data->status = $status; @@ -96,18 +89,25 @@ $data->status = $euser->uestatus; } - if (!empty($start) && $euser->uetimestart > $start) { + if (!empty($start) && $euser->uetimestart > $start && !empty($euser->uetimestart)) { $data->timestart = $start; } else { $data->timestart = $euser->uetimestart; } - if (!empty($end) && $euser->uetimeend < $end && $euser->uetimeend !== 0) { + if (!empty($end) && $euser->uetimeend < $end && !empty($euser->uetimeend)) { $data->timeend = $end; } else { $data->timeend = $euser->uetimeend; } + if ($euser->uestatus == $data->status + && $euser->uetimestart == $data->timestart + && $euser->uetimeend == $data->timeend) { + // No change. + continue; + } + $plugin = $instance->enrol; $$plugin->update_user_enrol($instance, $euser->id, $data->status, $data->timestart, $data->timeend); diff --git a/extra/charger.php b/extra/charger.php index d5d55c51..c206d597 100644 --- a/extra/charger.php +++ b/extra/charger.php @@ -43,15 +43,19 @@ $charger = $USER->id; if (empty($value) && ($op !== 'balance')) { $err = get_string('charger_novalue', 'enrol_wallet'); - $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, $err); } if (empty($userid)) { $err = get_string('charger_nouser', 'enrol_wallet'); - $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, $err); } $transactions = new enrol_wallet\transactions; @@ -86,11 +90,13 @@ } // Redirect to same page to show results. - $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', array('result' => $result, - 'before' => $before, - 'after' => ($op == 'balance') ? $before : $after, - 'userid' => $userid, - 'op' => 'result')); + $redirecturl = new moodle_url('/enrol/wallet/extra/charger.php', [ + 'result' => $result, + 'before' => $before, + 'after' => ($op == 'balance') ? $before : $after, + 'userid' => $userid, + 'op' => 'result' + ]); redirect($redirecturl); diff --git a/extra/coupondelete.php b/extra/coupondelete.php new file mode 100644 index 00000000..b8084eb0 --- /dev/null +++ b/extra/coupondelete.php @@ -0,0 +1,70 @@ +. + +/** + * Delete 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'); + +$select = optional_param_array('select', [], PARAM_INT); +$confirm = optional_param('confirm', 0, PARAM_BOOL); // Delete confirmation. +$ids = ($confirm) ? required_param('ids', PARAM_TEXT) : ''; + +require_login(); +require_capability('enrol/wallet:deletecoupon', context_system::instance()); + +if (confirm_sesskey()) { + if ($confirm) { + $ids = explode(',', $ids); + $n = 0; + foreach ($ids as $id) { + $delete = $DB->delete_records('enrol_wallet_coupons', ['id' => $id]); + $n += (int)$delete; + } + + $url = new moodle_url('/enrol/wallet/extra/coupontable.php'); + redirect($url, get_string('couponsdeleted', 'enrol_wallet', $n)); + + } else { + + $PAGE->set_context(context_system::instance()); + $PAGE->set_pagelayout('standard'); + $PAGE->set_url(new moodle_url('/enrol/wallet/extra/coupondelete.php')); + $PAGE->set_title(new lang_string('confirm')); + + echo $OUTPUT->header(); + + $ids = implode(',', array_keys($select)); + $optionsyes = ['sesskey' => sesskey(), 'confirm' => 1, 'ids' => $ids]; + + $url = new moodle_url('/enrol/wallet/extra/coupondelete.php', $optionsyes); + $buttoncontinue = new single_button($url, get_string('yes'), 'post'); + + $url = new moodle_url('/enrol/wallet/extra/coupontable.php'); + $buttoncancel = new single_button($url, get_string('no')); + + $message = get_string('confirmdeletecoupon', 'enrol_wallet', $ids); + + echo $OUTPUT->confirm($message, $buttoncontinue, $buttoncancel); + echo $OUTPUT->footer(); + } +} diff --git a/extra/couponedit.php b/extra/couponedit.php index e0662bfc..02bbe1f4 100644 --- a/extra/couponedit.php +++ b/extra/couponedit.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Delete coupons. + * Edit coupons. * * @package enrol_wallet * @copyright 2023 Mo Farouk @@ -23,49 +23,162 @@ */ require_once('../../../config.php'); -require_once($CFG->dirroot.'/enrol/wallet/locallib.php'); + require_login(); +require_capability('enrol/wallet:editcoupon', context_system::instance()); -$select = optional_param_array('select', [], PARAM_INT); -$confirm = optional_param('confirm', 0, PARAM_BOOL); -$ids = optional_param('ids', '', PARAM_TEXT); +$edit = optional_param('edit', 0, PARAM_BOOL); +$confirm = optional_param('confirm', 0, PARAM_BOOL); // Edit confirmation. -require_login(); -require_capability('enrol/wallet:deletecoupon', context_system::instance()); -// TODO adding options to edit coupons. -if (confirm_sesskey()) { - if ($confirm) { - $ids = explode(',', $ids); - $n = 0; - foreach ($ids as $id) { - $delete = $DB->delete_records('enrol_wallet_coupons', ['id' => $id]); - $n += (int)$delete; - } - - $url = new moodle_url('/enrol/wallet/extra/coupontable.php'); - redirect($url, get_string('couponsdeleted', 'enrol_wallet', $n)); +if ($edit) { // The edit form. + require_once("$CFG->libdir/formslib.php"); - } else { + $id = required_param('id', PARAM_INT); + $code = required_param('code', PARAM_TEXT); + $type = required_param('type', PARAM_TEXT); + $value = required_param('value', PARAM_NUMBER); + $maxusage = optional_param('maxusage', 0, PARAM_INT); + $usetimes = optional_param('usetimes', 0, PARAM_INT); + $validfrom = optional_param('validfrom', 0, PARAM_INT); + $validto = optional_param('validto', 0, PARAM_INT); + + $validfromedit = false; + if (!empty($validfrom)) { + $validfromedit = true; + } + + $validtoedit = false; + if (!empty($validto)) { + $validtoedit = true; + } + + // Setup the page. + $PAGE->set_context(context_system::instance()); + $PAGE->set_url(new moodle_url('/enrol/wallet/extra/couponedit.php')); + $PAGE->set_title(get_string('coupon_edit_title', 'enrol_wallet')); + $PAGE->set_heading(get_string('coupon_edit_heading', 'enrol_wallet')); + + $mform = new MoodleQuickForm('wallet_coupons', 'post', 'couponedit.php'); + + $mform->addElement('text', 'code', get_string('coupon_code', 'enrol_wallet')); + $mform->setType('code', PARAM_TEXT); + $mform->addHelpButton('code', 'coupon_code', 'enrol_wallet'); + $mform->setDefault('code', $code); + + $mform->addElement('text', 'value', get_string('coupon_value', 'enrol_wallet')); + $mform->setType('value', PARAM_NUMBER); + $mform->addHelpButton('value', 'coupon_value', 'enrol_wallet'); + $mform->setDefault('value', $value); + + $types = [ + 'fixed' => get_string('fixedvaluecoupon', 'enrol_wallet'), + 'percent' => get_string('percentdiscountcoupon', 'enrol_wallet'), + ]; + $mform->addElement('select', 'type', get_string('coupon_type', 'enrol_wallet'), $types); + $mform->addHelpButton('type', 'coupon_type', 'enrol_wallet'); + $mform->setDefault('type', $type); + + $mform->addElement('text', 'maxusage', get_string('coupons_maxusage', 'enrol_wallet')); + $mform->setType('maxusage', PARAM_INT); + $mform->addHelpButton('maxusage', 'coupons_maxusage', 'enrol_wallet'); + $mform->setDefault('maxusage', $maxusage); + + $mform->addElement('static', 'usetimes', get_string('coupon_usetimes', 'enrol_wallet'), $usetimes); - $PAGE->set_context(context_system::instance()); - $PAGE->set_pagelayout('standard'); - $PAGE->set_url(new moodle_url('/enrol/wallet/extra/couponedit.php')); - $PAGE->set_title(new lang_string('confirm')); + $mform->addElement('checkbox', 'usetimesreset', get_string('coupon_resetusetime', 'enrol_wallet')); + $mform->addHelpButton('usetimesreset', 'coupon_resetusetime', 'enrol_wallet'); - echo $OUTPUT->header(); + $mform->addElement('date_time_selector', 'validfrom', get_string('validfrom', 'enrol_wallet'), ['optional' => true]); + $mform->setDefault('validfrom', $validfrom); - $ids = implode(',', array_keys($select)); - $optionsyes = ['sesskey' => sesskey(), 'confirm' => 1, 'ids' => $ids]; + $mform->addElement('date_time_selector', 'validto', get_string('validto', 'enrol_wallet'), ['optional' => true]); + $mform->setDefault('validto', $validto); - $url = new moodle_url('/enrol/wallet/extra/couponedit.php', $optionsyes); - $buttoncontinue = new single_button($url, get_string('yes'), 'post'); + $mform->addElement('submit', 'confirm', get_string('confirm')); + $mform->disabledIf('confirm', 'value', 'eq', 0); + $mform->disabledIf('confirm', 'value', 'eq', ''); - $url = new moodle_url('/enrol/wallet/extra/coupontable.php'); - $buttoncancel = new single_button($url, get_string('no')); + $mform->addElement('hidden', 'sesskey'); + $mform->setType('sesskey', PARAM_TEXT); + $mform->setDefault('sesskey', sesskey()); - $message = get_string('confirmdeletecoupon', 'enrol_wallet', $ids); + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + $mform->setDefault('id', $id); - echo $OUTPUT->confirm($message, $buttoncontinue, $buttoncancel); - echo $OUTPUT->footer(); + echo $OUTPUT->header(); + $mform->display(); + echo $OUTPUT->footer(); + +} else if ($confirm && confirm_sesskey()) { // The edit action. + global $DB; + + $id = required_param('id', PARAM_INT); + $code = required_param('code', PARAM_TEXT); + $type = required_param('type', PARAM_TEXT); + $value = required_param('value', PARAM_NUMBER); + $maxusage = optional_param('maxusage', 0, PARAM_INT); + $validfrom = optional_param_array('validfrom', [], PARAM_INT); + $validto = optional_param_array('validto', [], PARAM_INT); + $usetimesreset = optional_param('usetimesreset', false, PARAM_BOOL); + + $coupondata = [ + 'id' => $id, + 'code' => $code, + 'type' => $type, + 'value' => $value, + 'maxusage' => $maxusage, + ]; + + if (!empty($validfrom)) { + $coupondata['validfrom'] = mktime( + $validfrom['hour'], + $validfrom['minute'], + null, + $validfrom['month'], + $validfrom['day'], + $validfrom['year'], + ); + } else { + $coupondata['validfrom'] = 0; + } + + if (!empty($validto)) { + $coupondata['validto'] = mktime( + $validto['hour'], + $validto['minute'], + null, + $validto['month'], + $validto['day'], + $validto['year'], + ); + } else { + $coupondata['validto'] = 0; + } + + if (!empty($usetimesreset)) { + $coupondata['usetimes'] = 0; } + // Check if there is another code similar to this one. + $params = [ + 'id' => $id, + 'code' => $code, + ]; + $select = 'code = :code AND id != :id'; + $notvalid = $DB->record_exists_select('enrol_wallet_coupons', $select, $params); + + if ($notvalid) { + $msg = get_string('couponexist', 'enrol_wallet'); + $notify = 'warning'; + } else { + $done = $DB->update_record('enrol_wallet_coupons', (object)$coupondata); + $msg = ($done) ? get_string('coupon_update_success', 'enrol_wallet') : get_string('coupon_update_failed', 'enrol_wallet'); + $notify = ($done) ? 'success' : 'error'; + } + + $url = new moodle_url('coupontable.php'); + redirect($url, $msg, null, $notify); +} else { + $url = new moodle_url('coupontable.php'); + redirect($url); } diff --git a/extra/coupontable.php b/extra/coupontable.php index a1f7f0a2..a8c0eb85 100644 --- a/extra/coupontable.php +++ b/extra/coupontable.php @@ -35,6 +35,7 @@ require_capability('enrol/wallet:viewcoupon', $systemcontext); $candelete = has_capability('enrol/wallet:deletecoupon', $systemcontext); +$canedit = has_capability('enrol/wallet:editcoupon', $systemcontext); $candownload = has_capability('enrol/wallet:downloadcoupon', $systemcontext); // Parameters. @@ -69,9 +70,9 @@ $arraydates = [ 'createdfrom' => $createdfromarray, - 'createdto' => $createdtoarray, - 'validfrom' => $validfromarray, - 'validto' => $validtoarray, + 'createdto' => $createdtoarray, + 'validfrom' => $validfromarray, + 'validto' => $validtoarray, ]; // ...mktime all dates. foreach ($arraydates as $key => $date) { @@ -160,7 +161,7 @@ if (!$table->is_downloading($download, 'walletcoupons')) { if ($candelete) { - echo '
'; + echo ''; } echo $OUTPUT->header(); @@ -169,26 +170,31 @@ // Set up the coupons table. $columns = [ - 'checkbox' => null, - 'id' => 'id', - 'code' => 'Code', - 'value' => 'Value', - 'type' => 'Type', - 'maxusage' => 'Maximum usage', - 'usetimes' => 'Usage', - 'validfrom' => 'Valid from', - 'validto' => 'Valid to', - 'lastuse' => 'lastuse', - 'timecreated' => 'timecreated', + 'checkbox' => null, + 'id' => 'id', + 'code' => get_string('coupon_t_code', 'enrol_wallet'), + 'value' => get_string('coupon_t_value', 'enrol_wallet'), + 'type' => get_string('coupon_t_type', 'enrol_wallet'), + 'maxusage' => get_string('coupons_maxusage', 'enrol_wallet'), + 'usetimes' => get_string('coupon_t_usage', 'enrol_wallet'), + 'validfrom' => get_string('validfrom', 'enrol_wallet'), + 'validto' => get_string('validto', 'enrol_wallet'), + 'lastuse' => get_string('coupon_t_lastuse', 'enrol_wallet'), + 'timecreated' => get_string('coupon_t_timecreated', 'enrol_wallet'), + 'edit' => null, ]; -if ($table->is_downloading()) { +if ($table->is_downloading() || !$candelete) { unset($columns['checkbox']); } +if ($table->is_downloading() || !$canedit) { + unset($columns['edit']); +} + $table->define_columns(array_keys($columns)); $table->define_headers(array_values($columns)); -$table->set_attribute('class', 'generaltable generalbox'); +$table->set_attribute('class', 'generaltable generalbox wallet-couponstable'); // Setup the sorting properties. $table->sortable(true); @@ -269,24 +275,25 @@ } foreach ($records as $record) { - if ($candelete) { - $chkbox = ''; - } else { - $chkbox = ''; - } + $editparams = array_merge(['edit' => true], (array)$record); + $editurl = new moodle_url('couponedit.php', $editparams); + $editbutton = ($canedit) ? $OUTPUT->single_button($editurl, get_string('edit'), 'post') : ''; + + $chkbox = ($candelete) ? '' : ''; $row = [ - 'checkbox' => $chkbox, - 'id' => $record->id, - 'code' => $record->code, - 'value' => number_format($record->value, 2), - 'type' => $record->type, - 'maxusage' => $record->maxusage, - 'usetimes' => $record->usetimes, - 'validfrom' => !empty($record->validfrom) ? userdate($record->validfrom) : '', - 'validto' => !empty($record->validto) ? userdate($record->validto) : '', - 'lastuse' => !empty($record->lastuse) ? userdate($record->lastuse) : '', + 'checkbox' => $chkbox, + 'id' => $record->id, + 'code' => $record->code, + 'value' => number_format($record->value, 2), + 'type' => $record->type, + 'maxusage' => $record->maxusage, + 'usetimes' => $record->usetimes, + 'validfrom' => !empty($record->validfrom) ? userdate($record->validfrom) : '', + 'validto' => !empty($record->validto) ? userdate($record->validto) : '', + 'lastuse' => !empty($record->lastuse) ? userdate($record->lastuse) : '', 'timecreated' => !empty($record->timecreated) ? userdate($record->timecreated) : '', + 'edit' => $editbutton, ]; $table->add_data_keyed($row); @@ -310,8 +317,6 @@ echo $OUTPUT->box($pageslinks); } - - if (!$table->is_downloading()) { echo $OUTPUT->footer(); } diff --git a/extra/generator.php b/extra/generator.php index 37abb767..b26ef8f4 100644 --- a/extra/generator.php +++ b/extra/generator.php @@ -33,7 +33,7 @@ $number = 1; $length = ''; $characters = []; -} else { +} else if ($method == 'random') { $code = ''; $number = required_param('number', PARAM_INT); $length = required_param('length', PARAM_INT); diff --git a/extra/transaction.php b/extra/transaction.php index fde4f82b..b5f985d3 100644 --- a/extra/transaction.php +++ b/extra/transaction.php @@ -34,8 +34,8 @@ $systemcontext = context_system::instance(); $viewall = has_capability('enrol/wallet:transaction', $systemcontext); -$sort = optional_param('tsort', 'timecreated', PARAM_ALPHA); -$userid = optional_param('user', '', PARAM_INT); +$sort = optional_param('tsort', '', PARAM_ALPHA); +$userid = (!$viewall) ? $USER->id : optional_param('user', '', PARAM_INT); $datefrom = optional_param_array('datefrom', [], PARAM_INT); $dateto = optional_param_array('dateto', [], PARAM_INT); $ttype = optional_param('ttype', '', PARAM_TEXT); @@ -45,12 +45,12 @@ // Page parameters. $urlparams = [ - 'tsort' => $sort, + 'tsort' => $sort, 'perpage' => $limitnum, - 'page' => $limitfrom, - 'user' => $userid, - 'ttype' => $ttype, - 'value' => $value, + 'page' => $limitfrom, + 'user' => $userid, + 'ttype' => $ttype, + 'value' => $value, ]; if (!empty($datefrom)) { foreach ($datafrom as $key => $value) { @@ -64,11 +64,14 @@ } } -if (!$viewall) { - $userid = $USER->id; +// Unset empty params. +foreach ($urlparams as $key => $val) { + if (empty($val)) { + unset($urlparams[$key]); + } } + // Setup the page. -$PAGE->set_pagelayout('admin'); $PAGE->set_context($systemcontext); $PAGE->set_title("Wallet Transactions"); $PAGE->set_heading('Wallet Transactions'); @@ -85,15 +88,15 @@ if ($viewall) { // Borrow potential users selectors from enrol_manual. - $options = array( - 'ajax' => 'enrol_manual/form-potential-user-selector', - 'multiple' => false, - 'courseid' => SITEID, - 'enrolid' => 0, - 'perpage' => $CFG->maxusersperpage, - 'userfields' => implode(',', \core_user\fields::get_identity_fields($systemcontext, true)), + $options = [ + 'ajax' => 'enrol_manual/form-potential-user-selector', + 'multiple' => false, + 'courseid' => SITEID, + 'enrolid' => 0, + 'perpage' => $CFG->maxusersperpage, + 'userfields' => implode(',', \core_user\fields::get_identity_fields($systemcontext, true)), 'noselectionstring' => get_string('allusers', 'enrol_wallet'), - ); + ]; $mform->addElement('autocomplete', 'user', get_string('selectusers', 'enrol_manual'), array(), $options); } @@ -126,18 +129,20 @@ // Setup the transactions table. $columns = [ - 'user' => 'User', - 'timecreated' => 'Time', - 'amount' => 'Amount', - 'type' => 'Type of transaction', - 'balbefore' => 'balance before', - 'balance' => 'balance after', - 'norefund' => 'Non refundable', - 'descripe' => 'description', + 'user' => get_string('user'), + 'timecreated' => get_string('time'), + 'amount' => get_string('amount', 'enrol_wallet'), + 'type' => get_string('transaction_type', 'enrol_wallet'), + 'balbefore' => get_string('balance_before', 'enrol_wallet'), + 'balance' => get_string('balance_after', 'enrol_wallet'), + 'norefund' => get_string('nonrefundable', 'enrol_wallet'), + 'descripe' => get_string('description'), ]; $table = new flexible_table('wallet_transactions'); -$table->define_baseurl($thisurl->out(true)); + +$baseurl = new moodle_url('/enrol/wallet/extra/transaction.php'); +$table->define_baseurl($baseurl->out(true)); $table->define_columns(array_keys($columns)); $table->define_headers(array_values($columns)); @@ -250,14 +255,14 @@ $desc = $record->descripe; $row = [ - 'user' => $userfullname, + 'user' => $userfullname, 'timecreated' => $time, - 'amount' => $amount, - 'type' => $record->type, - 'balbefore' => $before, - 'balance' => $after, - 'norefund' => $norefund, - 'descripe' => $desc, + 'amount' => $amount, + 'type' => $record->type, + 'balbefore' => $before, + 'balance' => $after, + 'norefund' => $norefund, + 'descripe' => $desc, ]; $table->add_data_keyed($row); @@ -276,6 +281,7 @@ // Display the table. $table->finish_output(); +// Page links again. echo $pageslinks; echo $OUTPUT->box(ob_get_clean()); diff --git a/extrasettings.php b/extrasettings.php new file mode 100644 index 00000000..ef619524 --- /dev/null +++ b/extrasettings.php @@ -0,0 +1,103 @@ +. + +/** + * wallet enrolment plugin extra pages. + * + * @package enrol_wallet + * @copyright 2023 Mo Farouk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$context = context_system::instance(); + +$captransactions = has_capability('enrol/wallet:transaction', $context); +$capcredit = has_capability('enrol/wallet:creditdebit', $context); +$capbulkedit = has_capability('enrol/wallet:bulkedit', $context); +$capcouponview = has_capability('enrol/wallet:viewcoupon', $context); +$capcouponcreate = has_capability('enrol/wallet:createcoupon', $context); + +$ismoodle = (get_config('enrol_wallet', 'walletsource') === enrol_wallet\transactions::SOURCE_MOODLE); +// Adding these pages for only users with required capability. +// These aren't appear to user's with capabilities, Only admins!. +// That is because enrolment plugins not loading the settings unless the user has the capability moodle/site:config. +// Working on solution. +if ($captransactions || $capbulkedit || $capcouponview || $capcouponcreate || $capcredit) { + // Adding new admin category. + $ADMIN->add('root', new admin_category('enrol_wallet_settings', + get_string('bulkfolder', 'enrol_wallet'), false)); +} + +if ($capcouponcreate && $ismoodle) { + // Adding page to generate coupons. + $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_coupongenerate', + get_string('coupon_generation', 'enrol_wallet'), + new moodle_url('/enrol/wallet/extra/coupon.php'), + 'enrol/wallet:createcoupon', + false, + $context)); +} + +if ($capcouponview && $ismoodle) { + // Adding page to view coupons. + $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_coupontable', + get_string('coupon_table', 'enrol_wallet'), + new moodle_url('/enrol/wallet/extra/coupontable.php'), + 'enrol/wallet:viewcoupon', + false, + $context)); +} + +if ($capcredit) { + // Adding page to charge wallets of other users. + $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_charging', + get_string('chargingoptions', 'enrol_wallet'), + new moodle_url('/enrol/wallet/extra/charger.php'), + 'enrol/wallet:creditdebit', + false, + $context)); +} + +if ($captransactions) { + // Adding page to show user's transactions. + $url = new moodle_url('/enrol/wallet/extra/transaction.php'); + $pagename = get_string('transactions', 'enrol_wallet'); + $page = new admin_externalpage('wallettransactions', $pagename, $url, 'enrol/wallet:transaction', false, $context); + $ADMIN->add('enrol_wallet_settings', $page); +} + +if ($capbulkedit) { + // Adding new page to bulk edit all user enrolments. + $bulkeditor = get_string('bulkeditor', 'enrol_wallet'); + + $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_bulkedit', + $bulkeditor, + new moodle_url('/enrol/wallet/extra/bulkedit.php'), + "enrol/wallet:bulkedit", + false, + $context)); + + // Adding page to bulk edit all instances. + $walletbulk = get_string('walletbulk', 'enrol_wallet'); + $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_bulkedit', + $walletbulk, + new moodle_url('/enrol/wallet/extra/bulkinstances.php'), + "enrol/wallet:bulkedit", + false, + $context)); +} diff --git a/lang/en/enrol_wallet.php b/lang/en/enrol_wallet.php index 7bb12523..78249edd 100644 --- a/lang/en/enrol_wallet.php +++ b/lang/en/enrol_wallet.php @@ -29,6 +29,7 @@ $string['applycoupon_help'] = 'Apply Coupon code to get discount or have a fixed value to charge your wallet.
If the coupon is a fixed value and it is greater than the fee of the course you will get enrolled.'; +$string['amount'] = 'Amount'; $string['awards'] = 'awards program'; $string['awards_help'] = 'enable or disable the awards program in this course'; $string['awardcreteria'] = 'condition for getting awarded'; @@ -39,6 +40,8 @@ $string['awardsalter'] = 'Alter awards'; $string['awardsalter_help'] = 'Alter the status of awards program'; +$string['balance_after'] = 'Balance after'; +$string['balance_before'] = 'Balance before'; $string['bulkfolder'] = 'Extra by wallet enrollments'; $string['bulkeditor'] = 'Bulk edit for enrollments'; $string['bulkeditor_head'] = 'Bulk Enrollment Edit (for all users in selected courses)'; @@ -90,10 +93,12 @@ $string['coupon_applyfixed'] = 'Coupon code applied successfully with value of {$a->value} {$a->currency}.'; $string['coupon_applynocourse'] = 'Error during applying coupon, course not found.'; $string['coupon_applynothere'] = 'Cannot apply discount coupon here.'; -$string['coupon_exceedusage'] = 'This coupon exceeds the maximum usage'; -$string['coupon_expired'] = 'This coupon is expired'; $string['coupon_code'] = 'Coupon code'; $string['coupon_code_help'] = 'Enter the coupon code you want.'; +$string['coupon_edit_title'] = 'coupon edit'; +$string['coupon_edit_heading'] = 'Edit Coupon'; +$string['coupon_exceedusage'] = 'This coupon exceeds the maximum usage'; +$string['coupon_expired'] = 'This coupon is expired'; $string['coupon_generation'] = 'Create coupons'; $string['coupon_generation_title'] = 'generate coupons'; $string['coupon_generation_heading'] = 'Add new coupons'; @@ -111,14 +116,23 @@ $string['coupons_number_help'] = 'Please don\'t set a large number to not overload the database.'; $string['coupons_maxusage'] = 'Maximum Usage'; $string['coupons_maxusage_help'] = 'How many times the coupon could be used. (0 means unlimited)'; -$string['coupon_operation'] = 'What to you need to do?'; -$string['coupon_operation_help'] = 'If you want to delete some coupons or to download the table. Please select the operation you want and click apply.'; $string['coupon_perpage'] = 'Coupons per page'; +$string['coupon_resetusetime'] = 'Reset used'; +$string['coupon_resetusetime_help'] = 'Reset the usage of the coupon to zero.'; +$string['coupon_t_code'] = 'Code'; +$string['coupon_t_value'] = 'Value'; +$string['coupon_t_type'] = 'Type'; +$string['coupon_t_usage'] = 'Usage'; +$string['coupon_t_lastuse'] = 'Last use'; +$string['coupon_t_timecreated'] = 'Time Created'; $string['coupon_table'] = 'View Wallet Coupons'; $string['coupon_type'] = 'Type of coupons'; $string['coupon_type_help'] = 'choose the type of coupons to generate.'; $string['coupon_value'] = 'Coupon value'; $string['coupon_value_help'] = 'Value of the coupon, fixed or percentage discounted value.'; +$string['coupon_usetimes'] = 'Used times'; +$string['coupon_update_failed'] = 'Failed to update the coupon.'; +$string['coupon_update_success'] = 'The coupon updated successfully.'; $string['couponsall'] = 'Allow all types'; $string['couponsdeleted'] = '{$a} Coupons are deleted successfully'; $string['couponsdiscount'] = 'Discount coupons only'; @@ -249,6 +263,8 @@ $string['newusergift_enable_help'] = 'If enabled, new users will have the gift you decided in their wallet.'; $string['not_set'] = 'Not set'; $string['notrefund'] = ' but this amount is nonrefundable: '; +$string['nonrefundable'] = 'Nonrefundable'; +$string['nonrefundable_transform_desc'] = "Transform the transaction to nonrefundable due to expiring of refund period.\n"; $string['nochange'] = 'No change'; $string['nocost'] = 'this course has invalid cost'; $string['nocoupons'] = 'Disable coupons'; diff --git a/lib.php b/lib.php index 0591df01..8dc180fb 100644 --- a/lib.php +++ b/lib.php @@ -248,7 +248,7 @@ public function can_add_instance($courseid) { return false; } // Check the number of allowed instances. - $count = $DB->count_records('enrol', array('courseid' => $courseid, 'enrol' => 'wallet')); + $count = $DB->count_records('enrol', ['courseid' => $courseid, 'enrol' => 'wallet']); if ($multiple = get_config('enrol_wallet', 'allowmultipleinstances')) { if (empty($multiple)) { return true; @@ -317,14 +317,14 @@ public function enrol_self(stdClass $instance, \stdClass $user = null) { $id = transactions::payment_topup($value, $user->id, $desc, $user->id, false); // Trigger cashback event. $eventdata = [ - 'context' => \context_course::instance($instance->courseid), - 'courseid' => $instance->courseid, - 'objectid' => $id, - 'userid' => $user->id, + 'context' => \context_course::instance($instance->courseid), + 'courseid' => $instance->courseid, + 'objectid' => $id, + 'userid' => $user->id, 'relateduserid' => $user->id, - 'other' => [ - 'amount' => $value, - 'original' => $costafter, + 'other' => [ + 'amount' => $value, + 'original' => $costafter, ], ]; $event = \enrol_wallet\event\cashback_applied::create($eventdata); @@ -346,7 +346,6 @@ public function enrol_self(stdClass $instance, \stdClass $user = null) { * @return bool */ public function hide_due_cheaper_instance($thisinstance) { - // TODO create a PHPUnit test for this function. global $DB, $USER; $coupon = $this->check_discount_coupon(); $courseid = $thisinstance->courseid; @@ -510,7 +509,7 @@ public function enrol_page_hook(stdClass $instance) { $data->info = get_string('insufficient_balance', 'enrol_wallet', [ - 'cost_before' => $costbefore, + 'cost_before' => $costbefore, 'user_balance' => $balance ]); @@ -519,8 +518,8 @@ public function enrol_page_hook(stdClass $instance) { $data->info = get_string('insufficient_balance_discount', 'enrol_wallet', [ - 'cost_before' => $costbefore, - 'cost_after' => $costafter, + 'cost_before' => $costbefore, + 'cost_after' => $costafter, 'user_balance' => $balance ]); @@ -878,9 +877,9 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla } else { $merge = [ 'courseid' => $data->courseid, - 'enrol' => $this->get_name(), - 'status' => $data->status, - 'roleid' => $data->roleid, + 'enrol' => $this->get_name(), + 'status' => $data->status, + 'roleid' => $data->roleid, ]; } if ($merge && $instances = $DB->get_records('enrol', $merge, 'id')) { @@ -1090,7 +1089,7 @@ protected function get_cohorts_options($instance, $context) { */ public function get_bulk_operations(course_enrolment_manager $manager) { $context = $manager->get_context(); - $bulkoperations = array(); + $bulkoperations = []; if (has_capability("enrol/wallet:manage", $context)) { $bulkoperations['editselectedusers'] = new \enrol_wallet\editselectedusers_operation($manager, $this); } @@ -1209,7 +1208,7 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $context) $mform->setType('customchar3', PARAM_TEXT); $params = [ - 'id' => 'wallet_courserestriction', + 'id' => 'wallet_courserestriction', 'onChange' => 'restrictByCourse()' ]; $restrictionlable = get_string('coursesrestriction', 'enrol_wallet'); @@ -1310,25 +1309,25 @@ public function edit_instance_validation($data, $files, $instance, $context) { $cohorts = $this->get_cohorts_options($instance, $context); $validcohorts = array_keys($cohorts); $validcurrencies = array_keys($this->get_possible_currencies()); - $tovalidate = array( + $tovalidate = [ 'enrolstartdate' => PARAM_INT, - 'enrolenddate' => PARAM_INT, - 'name' => PARAM_TEXT, - 'currency' => $validcurrencies, - 'cost' => PARAM_NUMBER, - 'customint2' => $validlongtimenosee, - 'customint3' => PARAM_INT, - 'customint4' => $validswep, - 'customint6' => $validnewenrols, - 'customint7' => PARAM_INT, - 'customint8' => PARAM_BOOL, - 'customdec1' => PARAM_NUMBER, - 'customdec2' => PARAM_NUMBER, - 'status' => $validstatus, - 'enrolperiod' => PARAM_INT, - 'expirynotify' => $validexpirynotify, - 'roleid' => $validroles - ); + 'enrolenddate' => PARAM_INT, + 'name' => PARAM_TEXT, + 'currency' => $validcurrencies, + 'cost' => PARAM_NUMBER, + 'customint2' => $validlongtimenosee, + 'customint3' => PARAM_INT, + 'customint4' => $validswep, + 'customint6' => $validnewenrols, + 'customint7' => PARAM_INT, + 'customint8' => PARAM_BOOL, + 'customdec1' => PARAM_NUMBER, + 'customdec2' => PARAM_NUMBER, + 'status' => $validstatus, + 'enrolperiod' => PARAM_INT, + 'expirynotify' => $validexpirynotify, + 'roleid' => $validroles + ]; if (count($cohorts) > 0) { $tovalidate['customint5'] = $validcohorts; @@ -1359,21 +1358,23 @@ public function get_instance_defaults() { $notifyall = 0; } - $fields = []; - $fields['status'] = $this->get_config('status'); - $fields['roleid'] = $this->get_config('roleid'); - $fields['enrolperiod'] = $this->get_config('enrolperiod'); - $fields['expirynotify'] = $expirynotify; - $fields['notifyall'] = $notifyall; - $fields['expirythreshold'] = $this->get_config('expirythreshold'); - $fields['currency'] = $this->get_config('currency'); - $fields['customint1'] = $this->get_config('paymentaccount'); - $fields['customint2'] = $this->get_config('longtimenosee'); - $fields['customint3'] = $this->get_config('maxenrolled'); - $fields['customint4'] = $this->get_config('sendcoursewelcomemessage'); - $fields['customint5'] = 0; - $fields['customint6'] = $this->get_config('newenrols'); - $fields['customint7'] = 0; + $fields = [ + 'status' => $this->get_config('status'), + 'roleid' => $this->get_config('roleid'), + 'enrolperiod' => $this->get_config('enrolperiod'), + 'expirynotify' => $expirynotify, + 'notifyall' => $notifyall, + 'expirythreshold' => $this->get_config('expirythreshold'), + 'currency' => $this->get_config('currency'), + 'customint1' => $this->get_config('paymentaccount'), + 'customint2' => $this->get_config('longtimenosee'), + 'customint3' => $this->get_config('maxenrolled'), + 'customint4' => $this->get_config('sendcoursewelcomemessage'), + 'customint5' => 0, + 'customint6' => $this->get_config('newenrols'), + 'customint7' => 0, + ]; + $awards = $this->get_config('awards'); $fields['customint8'] = !empty($awards) ? $awards : 0; if (!empty($awards)) { @@ -1412,9 +1413,11 @@ public function get_possible_currencies(): array { uasort($currencies, function($a, $b) { return strcmp($a, $b); }); + if (empty($currencies)) { $currencies = ['' => '']; } + return $currencies; } @@ -1504,7 +1507,7 @@ public function extend_assignable_roles($context, $defaultrole) { $roles = get_assignable_roles($context, ROLENAME_BOTH); if (!isset($roles[$defaultrole])) { - if ($role = $DB->get_record('role', array('id' => $defaultrole))) { + if ($role = $DB->get_record('role', ['id' => $defaultrole])) { $roles[$defaultrole] = role_get_name($role, $context, ROLENAME_BOTH); } } @@ -1525,7 +1528,7 @@ public function get_welcome_email_contact($sendoption, $context) { $contact = null; // Send as the first user assigned as the course contact. if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) { - $rusers = array(); + $rusers = []; if (!empty($CFG->coursecontact)) { $croles = explode(',', $CFG->coursecontact); list($sort, $sortparams) = users_order_by_sql('u'); @@ -1645,21 +1648,23 @@ public static function show_payment_info(stdClass $instance, $costafter) { if ($balance >= $fee) { return ''; } + $cost = $fee - $balance; $course = $DB->get_record('course', ['id' => $instance->courseid], '*', MUST_EXIST); $context = context_course::instance($course->id); - if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used. - echo '

'.get_string('nocost', 'enrol_wallet').'

'; + if ($cost < 0.01) { // No cost. + return '

'.get_string('nocost', 'enrol_wallet').'

'; + } else { require_once(__DIR__.'/classes/payment/service_provider.php'); $data = [ 'isguestuser' => isguestuser() || !isloggedin(), - 'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency), - 'instanceid' => $instance->id, + 'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency), + 'instanceid' => $instance->id, 'description' => get_string('purchasedescription', 'enrol_wallet', - format_string($course->fullname, true, ['context' => $context])), - 'successurl' => \enrol_wallet\payment\service_provider::get_success_url('wallet', $instance->id)->out(false), + format_string($course->fullname, true, ['context' => $context])), + 'successurl' => \enrol_wallet\payment\service_provider::get_success_url('wallet', $instance->id)->out(false), ]; $balance != 0 ? $data['balance'] = \core_payment\helper::get_cost_as_string($balance, $instance->currency) : $data['balance'] = false; diff --git a/locallib.php b/locallib.php index 72723c62..8f000958 100644 --- a/locallib.php +++ b/locallib.php @@ -190,15 +190,15 @@ function enrol_wallet_display_charger_form() { $mform->hideIf('value', 'op', 'eq', 'balance'); $context = context_system::instance(); - $options = array( - 'ajax' => 'enrol_manual/form-potential-user-selector', - 'multiple' => false, - 'courseid' => SITEID, - 'enrolid' => 0, - 'perpage' => $CFG->maxusersperpage, + $options = [ + 'ajax' => 'enrol_manual/form-potential-user-selector', + 'multiple' => false, + 'courseid' => SITEID, + 'enrolid' => 0, + 'perpage' => $CFG->maxusersperpage, 'userfields' => implode(',', \core_user\fields::get_identity_fields($context, true)) - ); - $mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options); + ]; + $mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), [], $options); $mform->addRule('userlist', 'select user', 'required'); // Empty div used by js to display the calculated final value. @@ -251,6 +251,9 @@ function calculateCharge() { * @return string */ function enrol_wallet_display_coupon_urls() { + if (get_config('enrol_wallet', 'walletsource') !== enrol_wallet\transactions::SOURCE_MOODLE) { + return ''; + } $context = context_system::instance(); $canviewcoupons = has_capability('enrol/wallet:viewcoupon', $context); $cangeneratecoupon = has_capability('enrol/wallet:createcoupon', $context); diff --git a/settings.php b/settings.php index 82c0df1f..60f820c1 100644 --- a/settings.php +++ b/settings.php @@ -24,60 +24,12 @@ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->dirroot.'/enrol/wallet/lib.php'); $context = context_system::instance(); -$captransactions = has_capability('enrol/wallet:transaction', $context); -$capbulkedit = has_capability('enrol/wallet:bulkedit', $context); - -// Adding these pages for only users with required capability. -// Don't know why these aren't appear to user's with capabilities? Only admins! -if ($hassiteconfig || $captransactions || $capbulkedit) { - // Adding new admin category. - $ADMIN->add('root', new admin_category('enrol_wallet_settings', - get_string('bulkfolder', 'enrol_wallet'))); - - // Adding page to generate coupons. - $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_coupongenerate', - get_string('coupon_generation', 'enrol_wallet'), - new moodle_url('/enrol/wallet/extra/coupon.php'), - 'enrol/wallet:createcoupon')); - - // Adding page to generate coupons. - $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_coupontable', - get_string('coupon_table', 'enrol_wallet'), - new moodle_url('/enrol/wallet/extra/coupontable.php'), - 'enrol/wallet:viewcoupon')); - - // Adding page to generate coupons. - $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_charging', - get_string('chargingoptions', 'enrol_wallet'), - new moodle_url('/enrol/wallet/extra/charger.php'), - 'enrol/wallet:creditdebit')); - - // Adding page to show user's transactions. - $url = new moodle_url('/enrol/wallet/extra/transaction.php'); - $pagename = get_string('transactions', 'enrol_wallet'); - $page = new admin_externalpage('wallettransactions', $pagename, $url, 'enrol/wallet:transaction'); - $ADMIN->add('enrol_wallet_settings', $page); - - // Adding new page to bulk edit all user enrolments. - $bulkeditor = get_string('bulkeditor', 'enrol_wallet'); - - $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_bulkedit', - $bulkeditor, - new moodle_url('/enrol/wallet/extra/bulkedit.php'), - "enrol/wallet:bulkedit")); - - // Adding page to bulk edit all instances. - $walletbulk = get_string('walletbulk', 'enrol_wallet'); - $ADMIN->add('enrol_wallet_settings', new admin_externalpage('enrol_wallet_bulkedit', - $walletbulk, - new moodle_url('/enrol/wallet/extra/bulkinstances.php'), - "enrol/wallet:bulkedit")); -} if ($ADMIN->fulltree) { global $DB; + + require_once($CFG->dirroot.'/enrol/wallet/lib.php'); $walletplugin = enrol_get_plugin('wallet'); // General settings. $settings->add(new admin_setting_heading('enrol_wallet_settings', '', @@ -85,7 +37,7 @@ // Adding choice between using wordpress (woowallet) of internal moodle wallet. $sources = [ enrol_wallet\transactions::SOURCE_WORDPRESS => get_string('sourcewordpress', 'enrol_wallet'), - enrol_wallet\transactions::SOURCE_MOODLE => get_string('sourcemoodle', 'enrol_wallet'), + enrol_wallet\transactions::SOURCE_MOODLE => get_string('sourcemoodle', 'enrol_wallet'), ]; $settings->add(new admin_setting_configselect('enrol_wallet/walletsource', get_string('walletsource', 'enrol_wallet'), @@ -110,16 +62,16 @@ )); // Note: let's reuse the ext sync constants and strings here, internally it is very similar, // it describes what should happened when users are not supposed to be enrolled any more. - $options = array( - ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'), + $options = [ + ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'), ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'), - ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'), - ); + ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'), + ]; $settings->add(new admin_setting_configselect('enrol_wallet/expiredaction', get_string('expiredaction', 'enrol_wallet'), get_string('expiredaction_help', 'enrol_wallet'), ENROL_EXT_REMOVED_KEEP, $options)); // Expiry notifications. - $options = array(); + $options = []; for ($i = 0; $i < 24; $i++) { $options[$i] = $i; } @@ -164,10 +116,10 @@ // Adding options to enable and disable coupons. $choices = [ - enrol_wallet_plugin::WALLET_NOCOUPONS => get_string('nocoupons', 'enrol_wallet'), - enrol_wallet_plugin::WALLET_COUPONSFIXED => get_string('couponsfixed', 'enrol_wallet'), + enrol_wallet_plugin::WALLET_NOCOUPONS => get_string('nocoupons', 'enrol_wallet'), + enrol_wallet_plugin::WALLET_COUPONSFIXED => get_string('couponsfixed', 'enrol_wallet'), enrol_wallet_plugin::WALLET_COUPONSDISCOUNT => get_string('couponsdiscount', 'enrol_wallet'), - enrol_wallet_plugin::WALLET_COUPONSALL => get_string('couponsall', 'enrol_wallet'), + enrol_wallet_plugin::WALLET_COUPONSALL => get_string('couponsall', 'enrol_wallet'), ]; $settings->add(new admin_setting_configselect('enrol_wallet/coupons', get_string('couponstype', 'enrol_wallet'), @@ -241,13 +193,13 @@ $settings->add(new admin_setting_configselect('enrol_wallet/currency', get_string('currency', 'enrol_wallet'), get_string('currency_help', 'enrol_wallet'), '', $supportedcurrencies)); // Is instance enabled. - $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), - ENROL_INSTANCE_DISABLED => get_string('no')); + $options = [ENROL_INSTANCE_ENABLED => get_string('yes'), + ENROL_INSTANCE_DISABLED => get_string('no')]; $settings->add(new admin_setting_configselect('enrol_wallet/status', get_string('status', 'enrol_wallet'), get_string('status_desc', 'enrol_wallet'), ENROL_INSTANCE_ENABLED, $options)); // Allow users to enrol into new courses by default. - $options = array(1 => get_string('yes'), 0 => get_string('no')); + $options = [1 => get_string('yes'), 0 => get_string('no')]; $settings->add(new admin_setting_configselect('enrol_wallet/newenrols', get_string('newenrols', 'enrol_wallet'), get_string('newenrols_desc', 'enrol_wallet'), 1, $options)); @@ -264,8 +216,8 @@ $settings->add(new admin_setting_configduration('enrol_wallet/enrolperiod', get_string('enrolperiod', 'enrol_wallet'), get_string('enrolperiod_desc', 'enrol_wallet'), 0)); // Expiry notification. - $options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => - get_string('expirynotifyall', 'core_enrol')); + $options = [0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => + get_string('expirynotifyall', 'core_enrol')]; $settings->add(new admin_setting_configselect('enrol_wallet/expirynotify', get_string('expirynotify', 'core_enrol'), get_string('expirynotify_help', 'core_enrol'), 0, $options)); // Expiry threshold. @@ -306,5 +258,6 @@ // Award value. $settings->add(new admin_setting_configtext('enrol_wallet/awardvalue', get_string('awardvalue', 'enrol_wallet'), get_string('awardvalue_help', 'enrol_wallet'), 0, PARAM_NUMBER)); - } +// Include extra pages. +require_once('extrasettings.php'); diff --git a/unenrolself.php b/unenrolself.php index 7ce3fed1..498118db 100644 --- a/unenrolself.php +++ b/unenrolself.php @@ -28,8 +28,8 @@ $enrolid = required_param('enrolid', PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_BOOL); -$instance = $DB->get_record('enrol', array('id' => $enrolid, 'enrol' => 'wallet'), '*', MUST_EXIST); -$course = $DB->get_record('course', array('id' => $instance->courseid), '*', MUST_EXIST); +$instance = $DB->get_record('enrol', ['id' => $enrolid, 'enrol' => 'wallet'], '*', MUST_EXIST); +$course = $DB->get_record('course', ['id' => $instance->courseid], '*', MUST_EXIST); $context = context_course::instance($course->id, MUST_EXIST); require_login(); @@ -42,10 +42,10 @@ // Security defined inside following function. if (!$plugin->get_unenrolself_link($instance)) { - redirect(new moodle_url('/course/view.php', array('id' => $course->id))); + redirect(new moodle_url('/course/view.php', ['id' => $course->id])); } -$PAGE->set_url('/enrol/wallet/unenrolself.php', array('enrolid' => $instance->id)); +$PAGE->set_url('/enrol/wallet/unenrolself.php', ['enrolid' => $instance->id]); $PAGE->set_title($plugin->get_instance_name($instance)); if ($confirm && confirm_sesskey()) { @@ -55,8 +55,8 @@ } echo $OUTPUT->header(); -$yesurl = new moodle_url($PAGE->url, array('confirm' => 1, 'sesskey' => sesskey())); -$nourl = new moodle_url('/course/view.php', array('id' => $course->id)); +$yesurl = new moodle_url($PAGE->url, ['confirm' => 1, 'sesskey' => sesskey()]); +$nourl = new moodle_url('/course/view.php', ['id' => $course->id]); $message = get_string('unenrolselfconfirm', 'enrol_wallet', format_string($course->fullname)); echo $OUTPUT->confirm($message, $yesurl, $nourl); echo $OUTPUT->footer(); diff --git a/version.php b/version.php index 2539e5f9..c2362a24 100644 --- a/version.php +++ b/version.php @@ -24,11 +24,11 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023062505; +$plugin->version = 2023062705; $plugin->requires = 2020110911; $plugin->component = 'enrol_wallet'; -$plugin->release = '1.6.7'; +$plugin->release = '1.7.0'; $plugin->maturity = MATURITY_STABLE; -$plugin->dependencies = array( +$plugin->dependencies = [ 'enrol_manual' => ANY_VERSION -); +];