-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
643 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* wallet enrol plugin external classes including. | ||
* | ||
* @package enrol_wallet | ||
* @copyright 2023 Mo Farouk <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
global $CFG; | ||
|
||
if ($CFG->version >= 2023042400) { | ||
class_alias(\core_external\external_api::class, 'external_api'); | ||
class_alias(\core_external\restricted_context_exception::class, 'restricted_context_exception'); | ||
class_alias(\core_external\external_description::class, 'external_description'); | ||
class_alias(\core_external\external_value::class, 'external_value'); | ||
class_alias(\core_external\external_format_value::class, 'external_format_value'); | ||
class_alias(\core_external\external_single_structure::class, 'external_single_structure'); | ||
class_alias(\core_external\external_multiple_structure::class, 'external_multiple_structure'); | ||
class_alias(\core_external\external_function_parameters::class, 'external_function_parameters'); | ||
class_alias(\core_external\util::class, 'external_util'); | ||
class_alias(\core_external\external_files::class, 'external_files'); | ||
class_alias(\core_external\external_warnings::class, 'external_warnings'); | ||
class_alias(\core_external\external_settings::class, 'external_settings'); | ||
} else { | ||
require_once("$CFG->libdir/externallib.php"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Enrol wallet action after submit the coupon code. | ||
* | ||
* @package enrol_wallet | ||
* @copyright 2023 Mohammad Farouk <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
require_once('../../../config.php'); | ||
require_once(__DIR__.'/../lib.php'); | ||
global $DB, $USER; | ||
|
||
require_login(); | ||
$mform = new enrol_wallet\form\applycoupon_form(); | ||
$data = $mform->get_data(); | ||
|
||
$cancel = optional_param('cancel', '', PARAM_TEXT); | ||
$url = optional_param('url', '', PARAM_URL); | ||
$redirecturl = !empty($url) ? new moodle_url('/'.$url) : new moodle_url('/'); | ||
|
||
if ($cancel) { | ||
// Important to unset the session coupon. | ||
if (isset($_SESSION['coupon'])) { | ||
$_SESSION['coupon'] = ''; | ||
unset($_SESSION['coupon']); | ||
} | ||
redirect($redirecturl); | ||
} | ||
|
||
$userid = $USER->id; | ||
|
||
$coupon = required_param('coupon', PARAM_TEXT); | ||
$instanceid = optional_param('instanceid' , '', PARAM_INT); | ||
$courseid = optional_param('courseid', 0, PARAM_INT); | ||
$cmid = optional_param('cmid', 0, PARAM_INT); | ||
$sectionid = optional_param('sectionid', 0, PARAM_INT); | ||
|
||
$couponsetting = get_config('enrol_wallet', 'coupons'); | ||
|
||
if (!confirm_sesskey()) { | ||
throw new moodle_exception('invalidsesskey'); | ||
} | ||
|
||
// Get the coupon data. | ||
$coupondata = enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, false, $cmid, $sectionid); | ||
if (empty($coupondata) || is_string($coupondata)) { | ||
$msg = get_string('coupon_applyerror', 'enrol_wallet', $coupondata); | ||
$msgtype = 'error'; | ||
// This mean that the function return error. | ||
} else { | ||
$wallet = new enrol_wallet_plugin; | ||
|
||
$value = $coupondata['value'] ?? 0; | ||
$type = $coupondata['type']; | ||
|
||
// Check the type to determine what to do. | ||
if ($type == 'fixed') { | ||
|
||
// Apply the coupon code to add its value to the user's wallet and enrol if value is enough. | ||
enrol_wallet\transactions::apply_coupon($coupondata, $userid, $instanceid); | ||
$currency = get_config('enrol_wallet', 'currency'); | ||
$a = [ | ||
'value' => $value, | ||
'currency' => $currency, | ||
]; | ||
$msg = get_string('coupon_applyfixed', 'enrol_wallet', $a); | ||
$msgtype = 'success'; | ||
|
||
} else if ($type == 'percent' && | ||
($couponsetting == $wallet::WALLET_COUPONSDISCOUNT | ||
|| $couponsetting == $wallet::WALLET_COUPONSALL) | ||
&& !empty($instanceid)) { | ||
// Percentage discount coupons applied in enrolment. | ||
$id = $DB->get_field('enrol', 'courseid', ['id' => $instanceid, 'enrol' => 'wallet'], IGNORE_MISSING); | ||
|
||
if ($id) { | ||
|
||
$redirecturl = new moodle_url('/enrol/index.php', ['id' => $id, 'coupon' => $coupon]); | ||
$msg = get_string('coupon_applydiscount', 'enrol_wallet', $value); | ||
$msgtype = 'success'; | ||
|
||
} else { | ||
|
||
$msg = get_string('coupon_applynocourse', 'enrol_wallet'); | ||
$msgtype = 'error'; | ||
|
||
} | ||
|
||
} else if ($type == 'percent' && | ||
($couponsetting == $wallet::WALLET_COUPONSDISCOUNT | ||
|| $couponsetting == $wallet::WALLET_COUPONSALL) | ||
&& (!empty($cmid) || !empty($sectionid))) { | ||
|
||
// This is the case when the coupon applied by availability wallet. | ||
$_SESSION['coupon'] = $coupon; | ||
|
||
$redirecturl = new moodle_url('/'.$url, ['coupon' => $coupon]); | ||
$msg = get_string('coupon_applydiscount', 'enrol_wallet', $value); | ||
$msgtype = 'success'; | ||
|
||
} else if ($type == 'category' && !empty($instanceid) && !empty($coupondata['category'])) { | ||
// This type of coupons is restricted to be used in certain categories. | ||
$course = $wallet->get_course_by_instance_id($instanceid); | ||
$ok = false; | ||
if ($coupondata['category'] == $course->category) { | ||
$ok = true; | ||
} else { | ||
$parents = core_course_category::get($course->category)->get_parents(); | ||
if (in_array($coupondata['category'], $parents)) { | ||
$ok = true; | ||
} | ||
} | ||
|
||
$redirecturl = new moodle_url('/enrol/index.php', ['id' => $course->id]); | ||
|
||
if ($ok) { | ||
enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, true); | ||
$msg = get_string('coupon_categoryapplied', 'enrol_wallet'); | ||
$msgtype = 'success'; | ||
} else { | ||
$categoryname = core_course_category::get($coupondata['category'])->get_nested_name(false); | ||
$msg = get_string('coupon_categoryfail', 'enrol_wallet', $categoryname); | ||
$msgtype = 'error'; | ||
} | ||
|
||
} else if ($type == 'enrol' && !empty($instanceid) && !empty($coupondata['courses'])) { | ||
// This type has no value, it used to enrol the user direct to the course. | ||
$courseid = $DB->get_field('enrol', 'courseid', ['id' => $instanceid, 'enrol' => 'wallet'], IGNORE_MISSING); | ||
|
||
if (in_array($courseid, $coupondata['courses'])) { | ||
// Apply the coupon and enrol the user. | ||
enrol_wallet\transactions::get_coupon_value($coupon, $userid, $instanceid, true); | ||
|
||
$msg = get_string('coupon_enrolapplied', 'enrol_wallet'); | ||
$msgtype = 'success'; | ||
} else { | ||
$available = ''; | ||
foreach ($coupondata['courses'] as $courseid) { | ||
$coursename = get_course($courseid)->fullname; | ||
$available .= '- ' . $coursename . '<br>'; | ||
} | ||
|
||
$msg = get_string('coupon_enrolerror', 'enrol_wallet', $available); | ||
$msgtype = 'error'; | ||
} | ||
|
||
} else if (($type == 'percent' || $type == 'course' || $type == 'category') && empty($instanceid)) { | ||
|
||
$msg = get_string('coupon_applynothere', 'enrol_wallet'); | ||
$msgtype = 'error'; | ||
|
||
} else { | ||
|
||
$msg = get_string('invalidcoupon_operation', 'enrol_wallet'); | ||
$msgtype = 'error'; | ||
} | ||
} | ||
|
||
redirect($redirecturl, $msg, null, $msgtype); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Action page to generate coupons. | ||
* | ||
* @package enrol_wallet | ||
* @copyright 2023 Mo Farouk <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
require_once('../../../config.php'); | ||
require_once($CFG->dirroot.'/enrol/wallet/locallib.php'); | ||
require_login(); | ||
require_capability('enrol/wallet:createcoupon', context_system::instance()); | ||
$method = required_param('method', PARAM_TEXT); | ||
|
||
if ($method == 'single') { | ||
$code = required_param('code', PARAM_TEXT); | ||
$number = 1; | ||
$length = ''; | ||
$characters = []; | ||
} else if ($method == 'random') { | ||
$code = ''; | ||
$number = required_param('number', PARAM_INT); | ||
$length = required_param('length', PARAM_INT); | ||
$characters = required_param_array('characters', PARAM_BOOL); | ||
} | ||
|
||
$value = optional_param('value', 0, PARAM_FLOAT); | ||
$type = required_param('type', PARAM_TEXT); | ||
$maxusage = required_param('maxusage', PARAM_INT); | ||
$maxperuser = optional_param('maxperuser', 0, PARAM_INT); | ||
$validto = optional_param_array('validto', [], PARAM_INT); | ||
$validfrom = optional_param_array('validfrom', [], PARAM_INT); | ||
$catid = optional_param('category', '', PARAM_INT); | ||
$courses = optional_param_array('courses', '', PARAM_INT); | ||
|
||
$redirecturl = new moodle_url('/enrol/wallet/extra/coupontable.php'); | ||
|
||
|
||
if (!empty($validto)) { | ||
$to = mktime( | ||
$validto['hour'], | ||
$validto['minute'], | ||
0, | ||
$validto['month'], | ||
$validto['day'], | ||
$validto['year'], | ||
); | ||
} else { | ||
$to = 0; | ||
} | ||
|
||
if (!empty($validfrom)) { | ||
$from = mktime( | ||
$validfrom['hour'], | ||
$validfrom['minute'], | ||
0, | ||
$validfrom['month'], | ||
$validfrom['day'], | ||
$validfrom['year'], | ||
); | ||
} else { | ||
$from = 0; | ||
} | ||
|
||
$options = new stdClass; | ||
|
||
if (!empty($characters)) { | ||
$options->lower = isset($characters['lower']) ? $characters['lower'] : false; | ||
$options->upper = isset($characters['upper']) ? $characters['upper'] : false; | ||
$options->digits = isset($characters['digits']) ? $characters['digits'] : false; | ||
} | ||
|
||
$options->number = $number; | ||
$options->length = $length; | ||
$options->maxusage = $maxusage; | ||
$options->maxperuser = $maxperuser; | ||
$options->from = $from; | ||
$options->to = $to; | ||
$options->type = $type; | ||
$options->value = $value; | ||
$options->code = $code; | ||
$options->category = $catid; | ||
$options->courses = !empty($courses) ? implode(',', $courses) : ''; | ||
|
||
// Generate coupons with the options specified. | ||
if (confirm_sesskey()) { | ||
$ids = enrol_wallet_generate_coupons($options); | ||
|
||
if (is_string($ids)) { | ||
$msg = $ids; | ||
} else { | ||
$count = count($ids); | ||
$msg = get_string('coupons_generation_success', 'enrol_wallet', $count); | ||
} | ||
|
||
redirect($redirecturl, $msg); | ||
} |
Oops, something went wrong.