Skip to content

Commit

Permalink
solve issue #20
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Feb 26, 2024
1 parent 6b4a8df commit fb9c852
Show file tree
Hide file tree
Showing 33 changed files with 1,225 additions and 883 deletions.
10 changes: 1 addition & 9 deletions amd/build/cdiscount.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/cdiscount.min.js.map

Large diffs are not rendered by default.

285 changes: 143 additions & 142 deletions amd/src/cdiscount.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,171 +23,172 @@
/* eslint-disable camelcase */
import {get_string} from 'core/str';

let form;
let valueInput;
let opInput;
let categoryInput;
let valueAfterInput;
let chargingLabel = '';
let calculateValueHolder;
let rules = [];
export const init = (formid, formType) => {

/**
* For the charger form.
* calculate the actual charge value and display it.
*/
function calculateCharge() {
var value = parseFloat(valueInput.value);
var op = opInput.value;
var cat = parseInt(categoryInput.value);

var maxDiscount = 0;
var calculatedValue = value;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
let form;
let valueInput;
let opInput;
let categoryInput;
let valueAfterInput;
let chargingLabel = '';
let calculateValueHolder;
let rules = [];

/**
* For the charger form.
* calculate the actual charge value and display it.
*/
function calculateCharge() {
var value = parseFloat(valueInput.value);
var op = opInput.value;
var cat = parseInt(categoryInput.value);

var maxDiscount = 0;
var calculatedValue = value;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
}
var discount = rules[i].discount;
var condition = rules[i].condition;
var valueBefore = value + (value * discount / (1 - discount));

if (valueBefore >= condition && discount > maxDiscount) {
maxDiscount = discount;
calculatedValue = valueBefore;
}
}
var discount = rules[i].discount;
var condition = rules[i].condition;
var valueBefore = value + (value * discount / (1 - discount));

if (valueBefore >= condition && discount > maxDiscount) {
maxDiscount = discount;
calculatedValue = valueBefore;
if (op == "credit") {
calculateValueHolder.innerHTML = chargingLabel + Math.round(calculatedValue * 100) / 100;
calculateValueHolder.style.display = '';
} else {
calculateValueHolder.innerHTML = "";
calculateValueHolder.style.display = 'none';
}
}

if (op == "credit") {
calculateValueHolder.innerHTML = chargingLabel + Math.round(calculatedValue * 100) / 100;
calculateValueHolder.style.display = '';
} else {
calculateValueHolder.innerHTML = "";
calculateValueHolder.style.display = 'none';
/**
* Add listeners for the inputs of charger form.
*/
function addListenersChargerForm() {
valueInput.addEventListener('change', () => {
calculateCharge();
});
valueInput.addEventListener('keyup', () => {
calculateCharge();
});
opInput.addEventListener('change', () => {
calculateCharge();
});
categoryInput.addEventListener('change', () => {
calculateCharge();
});
}
}

/**
* Add listeners for the inputs of charger form.
*/
function addListenersChargerForm() {
valueInput.addEventListener('change', () => {
calculateCharge();
});
valueInput.addEventListener('keyup', () => {
calculateCharge();
});
opInput.addEventListener('change', () => {
calculateCharge();
});
categoryInput.addEventListener('change', () => {
calculateCharge();
});
}
/**
* Continue the procedure of the charger form.
*/
function proceedChargerForm() {
calculateValueHolder = form.querySelector("[data-holder=calculated-value]");
opInput = form.querySelector("[name=op]");
addListenersChargerForm();
get_string('charging_value', 'enrol_wallet').done((data) => {
chargingLabel = data;
});
}

/**
* Continue the procedure of the charger form.
*/
function proceedChargerForm() {
calculateValueHolder = form.querySelector("[data-holder=calculated-value]");
opInput = form.querySelector("[name=op]");
addListenersChargerForm();
get_string('charging_value', 'enrol_wallet').done((data) => {
chargingLabel = data;
});
}
/**
* Calculate the value after discount and put it in the discounted input.
*/
function calculateAfter() {
var value = parseFloat(valueInput.value);
var cat = parseInt(categoryInput.value);
var maxDiscount = 0;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
}
var discount = rules[i].discount;
var condition = rules[i].condition;

/**
* Calculate the value after discount and put it in the discounted input.
*/
function calculateAfter() {
var value = parseFloat(valueInput.value);
var cat = parseInt(categoryInput.value);
var maxDiscount = 0;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
if (value >= condition && discount > maxDiscount) {
maxDiscount = discount;
}
}
var discount = rules[i].discount;
var condition = rules[i].condition;

if (value >= condition && discount > maxDiscount) {
maxDiscount = discount;
}
var calculatedValue = value - (value * maxDiscount);
valueAfterInput.value = calculatedValue;
}

var calculatedValue = value - (value * maxDiscount);
valueAfterInput.value = calculatedValue;
}
/**
* Calculate the value before the discount and put it in the value input.
*/
function calculateBefore() {
var value = parseFloat(valueAfterInput.value);
var cat = parseInt(categoryInput.value);
var maxDiscount = 0;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
}
var discount = rules[i].discount;
var condition = rules[i].condition;

/**
* Calculate the value before the discount and put it in the value input.
*/
function calculateBefore() {
var value = parseFloat(valueAfterInput.value);
var cat = parseInt(categoryInput.value);
var maxDiscount = 0;
for (var i = 0; i < rules.length; i++) {
var category = rules[i].category;
if (category !== cat) {
continue;
var valueBefore = value / (1 - discount);
if (valueBefore >= condition && discount > maxDiscount) {
maxDiscount = discount;
}
}
var discount = rules[i].discount;
var condition = rules[i].condition;

var valueBefore = value / (1 - discount);
if (valueBefore >= condition && discount > maxDiscount) {
maxDiscount = discount;
}
var realValueBefore = value / (1 - maxDiscount);
valueInput.value = realValueBefore;
}

var realValueBefore = value / (1 - maxDiscount);
valueInput.value = realValueBefore;
}
/**
* Hide discount rule for non-selected category.
*/
function hideElse() {
let container = document.getElementsByClassName('enrol-wallet-discounts-container');

let cat = parseInt(categoryInput.value);
for (let i = 0; i < container.length; i++) {
let children = container[i].children;

for (let x = 0; x < children.length; x++) {
let catid = children[x].getAttribute('data-catid');
if (catid == cat) {
children[x].style.display = '';
} else {
children[x].style.display = 'none';
/**
* Hide discount rule for non-selected category.
*/
function hideElse() {
let container = form.getElementsByClassName('enrol-wallet-discounts-container');

let cat = parseInt(categoryInput.value);
for (let i = 0; i < container.length; i++) {
let children = container[i].children;

for (let x = 0; x < children.length; x++) {
let catid = children[x].getAttribute('data-catid');
if (catid == cat) {
children[x].style.display = '';
} else {
children[x].style.display = 'none';
}
}
}
}
}
/**
* Adding event listeners to the top up form.
*/
function addListenersTopUpForm() {
valueInput.onchange = calculateAfter;
valueInput.onkeyup = calculateAfter;
valueAfterInput.onchange = calculateBefore;
valueAfterInput.onkeyup = calculateBefore;
categoryInput.addEventListener('change', () => {
calculateAfter();
hideElse();
});
}
/**
* Adding event listeners to the top up form.
*/
function addListenersTopUpForm() {
valueInput.onchange = calculateAfter;
valueInput.onkeyup = calculateAfter;
valueAfterInput.onchange = calculateBefore;
valueAfterInput.onkeyup = calculateBefore;
categoryInput.addEventListener('change', () => {
calculateAfter();
hideElse();
});
}

/**
* Continue the procedure for the top up form.
*/
function proceedTopUpForm() {
valueAfterInput = form.querySelector('[name=value-after]');
addListenersTopUpForm();
}
/**
* Continue the procedure for the top up form.
*/
function proceedTopUpForm() {
valueAfterInput = form.querySelector('[name=value-after]');
addListenersTopUpForm();
}

export const init = (formid, formType) => {
form = document.getElementById(formid);
valueInput = form.querySelector("[name=value]");
categoryInput = form.querySelector("[name=category]");
Expand Down
8 changes: 4 additions & 4 deletions classes/category/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get_parents_ids() {
* @return array[string]
*/
public function get_parents_options() {
$catoptions = [0 => get_string('any')];
$catoptions = [0 => get_string('site')];
foreach ($this->parents as $catid) {
$catname = core_course_category::get($catid)->get_formatted_name();
$catoptions[$catid] = $catname;
Expand All @@ -114,7 +114,7 @@ public static function get_all_categories_options() {
$catoptions[$catid] = $cat->get_nested_name(false);
}
asort($catoptions, SORT_STRING | SORT_FLAG_CASE);
$catoptions = [0 => get_string('any')] + $catoptions;
$catoptions = [0 => get_string('site')] + $catoptions;
return $catoptions;
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public function get_local_options_with_discounts() {
if (isset($options[0])) {
continue;
}
$options[0] = get_string('any');
$options[0] = get_string('site');
continue;
}
if (isset($options[$record->category])) {
Expand Down Expand Up @@ -207,7 +207,7 @@ public static function get_all_options_with_discount() {
if (isset($options[0])) {
continue;
}
$options[0] = get_string('any');
$options[0] = get_string('site');
continue;
}
if (isset($options[$record->category])) {
Expand Down
Loading

0 comments on commit fb9c852

Please sign in to comment.