diff --git a/amd/build/cdiscount.min.js b/amd/build/cdiscount.min.js index 8108aafd..bd6ddf71 100644 --- a/amd/build/cdiscount.min.js +++ b/amd/build/cdiscount.min.js @@ -1,11 +1,3 @@ -define("enrol_wallet/cdiscount",["exports","core/str"],(function(_exports,_str){ -/** - * Handling and calculate the values before and after discount in top up form and charger form. - * - * @module enrol_wallet/cdiscount - * @copyright 2024 Mohammad Farouk - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -let form,valueInput,opInput,categoryInput,valueAfterInput;Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0;let calculateValueHolder,chargingLabel="",rules=[];function calculateCharge(){for(var value=parseFloat(valueInput.value),op=opInput.value,cat=parseInt(categoryInput.value),maxDiscount=0,calculatedValue=value,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount,calculatedValue=valueBefore)}}"credit"==op?(calculateValueHolder.innerHTML=chargingLabel+Math.round(100*calculatedValue)/100,calculateValueHolder.style.display=""):(calculateValueHolder.innerHTML="",calculateValueHolder.style.display="none")}function proceedChargerForm(){calculateValueHolder=form.querySelector("[data-holder=calculated-value]"),opInput=form.querySelector("[name=op]"),valueInput.addEventListener("change",(()=>{calculateCharge()})),valueInput.addEventListener("keyup",(()=>{calculateCharge()})),opInput.addEventListener("change",(()=>{calculateCharge()})),categoryInput.addEventListener("change",(()=>{calculateCharge()})),(0,_str.get_string)("charging_value","enrol_wallet").done((data=>{chargingLabel=data}))}function calculateAfter(){for(var value=parseFloat(valueInput.value),cat=parseInt(categoryInput.value),maxDiscount=0,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount)}}var calculatedValue=value-value*maxDiscount;valueAfterInput.value=calculatedValue}function calculateBefore(){for(var value=parseFloat(valueAfterInput.value),cat=parseInt(categoryInput.value),maxDiscount=0,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount)}}var realValueBefore=value/(1-maxDiscount);valueInput.value=realValueBefore}function hideElse(){let container=document.getElementsByClassName("enrol-wallet-discounts-container"),cat=parseInt(categoryInput.value);for(let i=0;i{calculateAfter(),hideElse()}))}_exports.init=(formid,formType)=>{form=document.getElementById(formid),valueInput=form.querySelector("[name=value]"),categoryInput=form.querySelector("[name=category]");for(let i=1;;i++){let element=form.querySelector("[name=discount_rule_"+i+"]");if(!element)break;var object=JSON.parse(element.value);object.condition=parseFloat(object.condition),object.discount=parseFloat(object.discount),object.category=parseInt(object.category),rules.push(object)}hideElse(),"charge"==formType?proceedChargerForm():proceedTopUpForm()}})); +define("enrol_wallet/cdiscount",["exports","core/str"],(function(_exports,_str){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0;_exports.init=(formid,formType)=>{let form,valueInput,opInput,categoryInput,valueAfterInput,calculateValueHolder,chargingLabel="",rules=[];function calculateCharge(){for(var value=parseFloat(valueInput.value),op=opInput.value,cat=parseInt(categoryInput.value),maxDiscount=0,calculatedValue=value,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount,calculatedValue=valueBefore)}}"credit"==op?(calculateValueHolder.innerHTML=chargingLabel+Math.round(100*calculatedValue)/100,calculateValueHolder.style.display=""):(calculateValueHolder.innerHTML="",calculateValueHolder.style.display="none")}function calculateAfter(){for(var value=parseFloat(valueInput.value),cat=parseInt(categoryInput.value),maxDiscount=0,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount)}}var calculatedValue=value-value*maxDiscount;valueAfterInput.value=calculatedValue}function calculateBefore(){for(var value=parseFloat(valueAfterInput.value),cat=parseInt(categoryInput.value),maxDiscount=0,i=0;i=rules[i].condition&&discount>maxDiscount&&(maxDiscount=discount)}}var realValueBefore=value/(1-maxDiscount);valueInput.value=realValueBefore}function hideElse(){let container=form.getElementsByClassName("enrol-wallet-discounts-container"),cat=parseInt(categoryInput.value);for(let i=0;i{calculateCharge()})),valueInput.addEventListener("keyup",(()=>{calculateCharge()})),opInput.addEventListener("change",(()=>{calculateCharge()})),categoryInput.addEventListener("change",(()=>{calculateCharge()})),(0,_str.get_string)("charging_value","enrol_wallet").done((data=>{chargingLabel=data}))):(valueAfterInput=form.querySelector("[name=value-after]"),valueInput.onchange=calculateAfter,valueInput.onkeyup=calculateAfter,valueAfterInput.onchange=calculateBefore,valueAfterInput.onkeyup=calculateBefore,categoryInput.addEventListener("change",(()=>{calculateAfter(),hideElse()})))}})); //# sourceMappingURL=cdiscount.min.js.map \ No newline at end of file diff --git a/amd/build/cdiscount.min.js.map b/amd/build/cdiscount.min.js.map index ab16daaf..81296c70 100644 --- a/amd/build/cdiscount.min.js.map +++ b/amd/build/cdiscount.min.js.map @@ -1 +1 @@ -{"version":3,"file":"cdiscount.min.js","sources":["../src/cdiscount.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Handling and calculate the values before and after discount in top up form and charger form.\n *\n * @module enrol_wallet/cdiscount\n * @copyright 2024 Mohammad Farouk \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n/* eslint-disable camelcase */\nimport {get_string} from 'core/str';\n\nlet form;\nlet valueInput;\nlet opInput;\nlet categoryInput;\nlet valueAfterInput;\nlet chargingLabel = '';\nlet calculateValueHolder;\nlet rules = [];\n\n/**\n * For the charger form.\n * calculate the actual charge value and display it.\n */\nfunction calculateCharge() {\n var value = parseFloat(valueInput.value);\n var op = opInput.value;\n var cat = parseInt(categoryInput.value);\n\n var maxDiscount = 0;\n var calculatedValue = value;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n var valueBefore = value + (value * discount / (1 - discount));\n\n if (valueBefore >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n calculatedValue = valueBefore;\n }\n }\n\n if (op == \"credit\") {\n calculateValueHolder.innerHTML = chargingLabel + Math.round(calculatedValue * 100) / 100;\n calculateValueHolder.style.display = '';\n } else {\n calculateValueHolder.innerHTML = \"\";\n calculateValueHolder.style.display = 'none';\n }\n}\n\n/**\n * Add listeners for the inputs of charger form.\n */\nfunction addListenersChargerForm() {\n valueInput.addEventListener('change', () => {\n calculateCharge();\n });\n valueInput.addEventListener('keyup', () => {\n calculateCharge();\n });\n opInput.addEventListener('change', () => {\n calculateCharge();\n });\n categoryInput.addEventListener('change', () => {\n calculateCharge();\n });\n}\n\n/**\n * Continue the procedure of the charger form.\n */\nfunction proceedChargerForm() {\n calculateValueHolder = form.querySelector(\"[data-holder=calculated-value]\");\n opInput = form.querySelector(\"[name=op]\");\n addListenersChargerForm();\n get_string('charging_value', 'enrol_wallet').done((data) => {\n chargingLabel = data;\n });\n}\n\n/**\n * Calculate the value after discount and put it in the discounted input.\n */\nfunction calculateAfter() {\n var value = parseFloat(valueInput.value);\n var cat = parseInt(categoryInput.value);\n var maxDiscount = 0;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n\n if (value >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n }\n }\n\n var calculatedValue = value - (value * maxDiscount);\n valueAfterInput.value = calculatedValue;\n}\n\n/**\n * Calculate the value before the discount and put it in the value input.\n */\nfunction calculateBefore() {\n var value = parseFloat(valueAfterInput.value);\n var cat = parseInt(categoryInput.value);\n var maxDiscount = 0;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n\n var valueBefore = value / (1 - discount);\n if (valueBefore >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n }\n }\n\n var realValueBefore = value / (1 - maxDiscount);\n valueInput.value = realValueBefore;\n}\n/**\n * Hide discount rule for non-selected category.\n */\nfunction hideElse() {\n let container = document.getElementsByClassName('enrol-wallet-discounts-container');\n\n let cat = parseInt(categoryInput.value);\n for (let i = 0; i < container.length; i++) {\n let children = container[i].children;\n\n for (let x = 0; x < children.length; x++) {\n let catid = children[x].getAttribute('data-catid');\n if (catid == cat) {\n children[x].style.display = '';\n } else {\n children[x].style.display = 'none';\n }\n }\n }\n}\n/**\n * Adding event listeners to the top up form.\n */\nfunction addListenersTopUpForm() {\n valueInput.onchange = calculateAfter;\n valueInput.onkeyup = calculateAfter;\n valueAfterInput.onchange = calculateBefore;\n valueAfterInput.onkeyup = calculateBefore;\n categoryInput.addEventListener('change', () => {\n calculateAfter();\n hideElse();\n });\n}\n\n/**\n * Continue the procedure for the top up form.\n */\nfunction proceedTopUpForm() {\n valueAfterInput = form.querySelector('[name=value-after]');\n addListenersTopUpForm();\n}\n\nexport const init = (formid, formType) => {\n form = document.getElementById(formid);\n valueInput = form.querySelector(\"[name=value]\");\n categoryInput = form.querySelector(\"[name=category]\");\n\n for (let i = 1; ; i++) {\n let element = form.querySelector(\"[name=discount_rule_\" + i + \"]\");\n if (!element) {\n break;\n }\n var object = JSON.parse(element.value);\n object.condition = parseFloat(object.condition);\n object.discount = parseFloat(object.discount);\n object.category = parseInt(object.category);\n rules.push(object);\n }\n hideElse();\n if (formType == 'charge') {\n proceedChargerForm();\n } else {\n proceedTopUpForm();\n }\n};"],"names":["form","valueInput","opInput","categoryInput","valueAfterInput","calculateValueHolder","chargingLabel","rules","calculateCharge","value","parseFloat","op","cat","parseInt","maxDiscount","calculatedValue","i","length","category","discount","valueBefore","condition","innerHTML","Math","round","style","display","proceedChargerForm","querySelector","addEventListener","done","data","calculateAfter","calculateBefore","realValueBefore","hideElse","container","document","getElementsByClassName","children","x","catid","getAttribute","proceedTopUpForm","onchange","onkeyup","formid","formType","getElementById","element","object","JSON","parse","push"],"mappings":";;;;;;;;IAyBIA,KACAC,WACAC,QACAC,cACAC,iGAEAC,qBADAC,cAAgB,GAEhBC,MAAQ,YAMHC,0BACDC,MAAQC,WAAWT,WAAWQ,OAC9BE,GAAKT,QAAQO,MACbG,IAAMC,SAASV,cAAcM,OAE7BK,YAAc,EACdC,gBAAkBN,MACbO,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAEpBC,YAAcX,MAASA,MAAQU,UAAY,EAAIA,UAE/CC,aAHYb,MAAMS,GAAGK,WAGOF,SAAWL,cACvCA,YAAcK,SACdJ,gBAAkBK,cAIhB,UAANT,IACAN,qBAAqBiB,UAAYhB,cAAgBiB,KAAKC,MAAwB,IAAlBT,iBAAyB,IACrFV,qBAAqBoB,MAAMC,QAAU,KAErCrB,qBAAqBiB,UAAY,GACjCjB,qBAAqBoB,MAAMC,QAAU,iBAyBpCC,qBACLtB,qBAAuBL,KAAK4B,cAAc,kCAC1C1B,QAAUF,KAAK4B,cAAc,aAnB7B3B,WAAW4B,iBAAiB,UAAU,KAClCrB,qBAEJP,WAAW4B,iBAAiB,SAAS,KACjCrB,qBAEJN,QAAQ2B,iBAAiB,UAAU,KAC/BrB,qBAEJL,cAAc0B,iBAAiB,UAAU,KACrCrB,yCAWO,iBAAkB,gBAAgBsB,MAAMC,OAC/CzB,cAAgByB,iBAOfC,yBACDvB,MAAQC,WAAWT,WAAWQ,OAC9BG,IAAMC,SAASV,cAAcM,OAC7BK,YAAc,EACTE,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAGpBV,OAFYF,MAAMS,GAAGK,WAECF,SAAWL,cACjCA,YAAcK,eAIlBJ,gBAAkBN,MAASA,MAAQK,YACvCV,gBAAgBK,MAAQM,yBAMnBkB,0BACDxB,MAAQC,WAAWN,gBAAgBK,OACnCG,IAAMC,SAASV,cAAcM,OAC7BK,YAAc,EACTE,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAGNV,OAAS,EAAIU,WAFfZ,MAAMS,GAAGK,WAGOF,SAAWL,cACvCA,YAAcK,eAIlBe,gBAAkBzB,OAAS,EAAIK,aACnCb,WAAWQ,MAAQyB,yBAKdC,eACDC,UAAYC,SAASC,uBAAuB,oCAE5C1B,IAAMC,SAASV,cAAcM,WAC5B,IAAIO,EAAI,EAAGA,EAAIoB,UAAUnB,OAAQD,IAAK,KACnCuB,SAAWH,UAAUpB,GAAGuB,aAEvB,IAAIC,EAAI,EAAGA,EAAID,SAAStB,OAAQuB,IAAK,KAClCC,MAAQF,SAASC,GAAGE,aAAa,cAEjCH,SAASC,GAAGf,MAAMC,QADlBe,OAAS7B,IACmB,GAEA,kBAsBnC+B,mBACLvC,gBAAkBJ,KAAK4B,cAAc,sBAdrC3B,WAAW2C,SAAWZ,eACtB/B,WAAW4C,QAAUb,eACrB5B,gBAAgBwC,SAAWX,gBAC3B7B,gBAAgByC,QAAUZ,gBAC1B9B,cAAc0B,iBAAiB,UAAU,KACrCG,iBACAG,4BAYY,CAACW,OAAQC,YACzB/C,KAAOqC,SAASW,eAAeF,QAC/B7C,WAAaD,KAAK4B,cAAc,gBAChCzB,cAAgBH,KAAK4B,cAAc,uBAE9B,IAAIZ,EAAI,GAAKA,IAAK,KACfiC,QAAUjD,KAAK4B,cAAc,uBAAyBZ,EAAI,SACzDiC,kBAGDC,OAASC,KAAKC,MAAMH,QAAQxC,OAChCyC,OAAO7B,UAAYX,WAAWwC,OAAO7B,WACrC6B,OAAO/B,SAAWT,WAAWwC,OAAO/B,UACpC+B,OAAOhC,SAAWL,SAASqC,OAAOhC,UAClCX,MAAM8C,KAAKH,QAEff,WACgB,UAAZY,SACApB,qBAEAgB"} \ No newline at end of file +{"version":3,"file":"cdiscount.min.js","sources":["../src/cdiscount.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Handling and calculate the values before and after discount in top up form and charger form.\n *\n * @module enrol_wallet/cdiscount\n * @copyright 2024 Mohammad Farouk \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n/* eslint-disable camelcase */\nimport {get_string} from 'core/str';\n\nexport const init = (formid, formType) => {\n\n let form;\n let valueInput;\n let opInput;\n let categoryInput;\n let valueAfterInput;\n let chargingLabel = '';\n let calculateValueHolder;\n let rules = [];\n\n /**\n * For the charger form.\n * calculate the actual charge value and display it.\n */\n function calculateCharge() {\n var value = parseFloat(valueInput.value);\n var op = opInput.value;\n var cat = parseInt(categoryInput.value);\n\n var maxDiscount = 0;\n var calculatedValue = value;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n var valueBefore = value + (value * discount / (1 - discount));\n\n if (valueBefore >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n calculatedValue = valueBefore;\n }\n }\n\n if (op == \"credit\") {\n calculateValueHolder.innerHTML = chargingLabel + Math.round(calculatedValue * 100) / 100;\n calculateValueHolder.style.display = '';\n } else {\n calculateValueHolder.innerHTML = \"\";\n calculateValueHolder.style.display = 'none';\n }\n }\n\n /**\n * Add listeners for the inputs of charger form.\n */\n function addListenersChargerForm() {\n valueInput.addEventListener('change', () => {\n calculateCharge();\n });\n valueInput.addEventListener('keyup', () => {\n calculateCharge();\n });\n opInput.addEventListener('change', () => {\n calculateCharge();\n });\n categoryInput.addEventListener('change', () => {\n calculateCharge();\n });\n }\n\n /**\n * Continue the procedure of the charger form.\n */\n function proceedChargerForm() {\n calculateValueHolder = form.querySelector(\"[data-holder=calculated-value]\");\n opInput = form.querySelector(\"[name=op]\");\n addListenersChargerForm();\n get_string('charging_value', 'enrol_wallet').done((data) => {\n chargingLabel = data;\n });\n }\n\n /**\n * Calculate the value after discount and put it in the discounted input.\n */\n function calculateAfter() {\n var value = parseFloat(valueInput.value);\n var cat = parseInt(categoryInput.value);\n var maxDiscount = 0;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n\n if (value >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n }\n }\n\n var calculatedValue = value - (value * maxDiscount);\n valueAfterInput.value = calculatedValue;\n }\n\n /**\n * Calculate the value before the discount and put it in the value input.\n */\n function calculateBefore() {\n var value = parseFloat(valueAfterInput.value);\n var cat = parseInt(categoryInput.value);\n var maxDiscount = 0;\n for (var i = 0; i < rules.length; i++) {\n var category = rules[i].category;\n if (category !== cat) {\n continue;\n }\n var discount = rules[i].discount;\n var condition = rules[i].condition;\n\n var valueBefore = value / (1 - discount);\n if (valueBefore >= condition && discount > maxDiscount) {\n maxDiscount = discount;\n }\n }\n\n var realValueBefore = value / (1 - maxDiscount);\n valueInput.value = realValueBefore;\n }\n /**\n * Hide discount rule for non-selected category.\n */\n function hideElse() {\n let container = form.getElementsByClassName('enrol-wallet-discounts-container');\n\n let cat = parseInt(categoryInput.value);\n for (let i = 0; i < container.length; i++) {\n let children = container[i].children;\n\n for (let x = 0; x < children.length; x++) {\n let catid = children[x].getAttribute('data-catid');\n if (catid == cat) {\n children[x].style.display = '';\n } else {\n children[x].style.display = 'none';\n }\n }\n }\n }\n /**\n * Adding event listeners to the top up form.\n */\n function addListenersTopUpForm() {\n valueInput.onchange = calculateAfter;\n valueInput.onkeyup = calculateAfter;\n valueAfterInput.onchange = calculateBefore;\n valueAfterInput.onkeyup = calculateBefore;\n categoryInput.addEventListener('change', () => {\n calculateAfter();\n hideElse();\n });\n }\n\n /**\n * Continue the procedure for the top up form.\n */\n function proceedTopUpForm() {\n valueAfterInput = form.querySelector('[name=value-after]');\n addListenersTopUpForm();\n }\n\n form = document.getElementById(formid);\n valueInput = form.querySelector(\"[name=value]\");\n categoryInput = form.querySelector(\"[name=category]\");\n\n for (let i = 1; ; i++) {\n let element = form.querySelector(\"[name=discount_rule_\" + i + \"]\");\n if (!element) {\n break;\n }\n var object = JSON.parse(element.value);\n object.condition = parseFloat(object.condition);\n object.discount = parseFloat(object.discount);\n object.category = parseInt(object.category);\n rules.push(object);\n }\n hideElse();\n if (formType == 'charge') {\n proceedChargerForm();\n } else {\n proceedTopUpForm();\n }\n};"],"names":["formid","formType","form","valueInput","opInput","categoryInput","valueAfterInput","calculateValueHolder","chargingLabel","rules","calculateCharge","value","parseFloat","op","cat","parseInt","maxDiscount","calculatedValue","i","length","category","discount","valueBefore","condition","innerHTML","Math","round","style","display","calculateAfter","calculateBefore","realValueBefore","hideElse","container","getElementsByClassName","children","x","catid","getAttribute","document","getElementById","querySelector","element","object","JSON","parse","push","addEventListener","done","data","onchange","onkeyup"],"mappings":"2KAyBoB,CAACA,OAAQC,gBAErBC,KACAC,WACAC,QACAC,cACAC,gBAEAC,qBADAC,cAAgB,GAEhBC,MAAQ,YAMHC,0BACDC,MAAQC,WAAWT,WAAWQ,OAC9BE,GAAKT,QAAQO,MACbG,IAAMC,SAASV,cAAcM,OAE7BK,YAAc,EACdC,gBAAkBN,MACbO,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAEpBC,YAAcX,MAASA,MAAQU,UAAY,EAAIA,UAE/CC,aAHYb,MAAMS,GAAGK,WAGOF,SAAWL,cACvCA,YAAcK,SACdJ,gBAAkBK,cAIhB,UAANT,IACAN,qBAAqBiB,UAAYhB,cAAgBiB,KAAKC,MAAwB,IAAlBT,iBAAyB,IACrFV,qBAAqBoB,MAAMC,QAAU,KAErCrB,qBAAqBiB,UAAY,GACjCjB,qBAAqBoB,MAAMC,QAAU,iBAqCpCC,yBACDlB,MAAQC,WAAWT,WAAWQ,OAC9BG,IAAMC,SAASV,cAAcM,OAC7BK,YAAc,EACTE,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAGpBV,OAFYF,MAAMS,GAAGK,WAECF,SAAWL,cACjCA,YAAcK,eAIlBJ,gBAAkBN,MAASA,MAAQK,YACvCV,gBAAgBK,MAAQM,yBAMnBa,0BACDnB,MAAQC,WAAWN,gBAAgBK,OACnCG,IAAMC,SAASV,cAAcM,OAC7BK,YAAc,EACTE,EAAI,EAAGA,EAAIT,MAAMU,OAAQD,IAAK,IACpBT,MAAMS,GAAGE,WACPN,SAGbO,SAAWZ,MAAMS,GAAGG,SAGNV,OAAS,EAAIU,WAFfZ,MAAMS,GAAGK,WAGOF,SAAWL,cACvCA,YAAcK,eAIlBU,gBAAkBpB,OAAS,EAAIK,aACnCb,WAAWQ,MAAQoB,yBAKdC,eACDC,UAAY/B,KAAKgC,uBAAuB,oCAExCpB,IAAMC,SAASV,cAAcM,WAC5B,IAAIO,EAAI,EAAGA,EAAIe,UAAUd,OAAQD,IAAK,KACnCiB,SAAWF,UAAUf,GAAGiB,aAEvB,IAAIC,EAAI,EAAGA,EAAID,SAAShB,OAAQiB,IAAK,KAClCC,MAAQF,SAASC,GAAGE,aAAa,cAEjCH,SAASC,GAAGT,MAAMC,QADlBS,OAASvB,IACmB,GAEA,SA2B5CZ,KAAOqC,SAASC,eAAexC,QAC/BG,WAAaD,KAAKuC,cAAc,gBAChCpC,cAAgBH,KAAKuC,cAAc,uBAE9B,IAAIvB,EAAI,GAAKA,IAAK,KACfwB,QAAUxC,KAAKuC,cAAc,uBAAyBvB,EAAI,SACzDwB,kBAGDC,OAASC,KAAKC,MAAMH,QAAQ/B,OAChCgC,OAAOpB,UAAYX,WAAW+B,OAAOpB,WACrCoB,OAAOtB,SAAWT,WAAW+B,OAAOtB,UACpCsB,OAAOvB,SAAWL,SAAS4B,OAAOvB,UAClCX,MAAMqC,KAAKH,QAEfX,WACgB,UAAZ/B,UAlHAM,qBAAuBL,KAAKuC,cAAc,kCAC1CrC,QAAUF,KAAKuC,cAAc,aAnB7BtC,WAAW4C,iBAAiB,UAAU,KAClCrC,qBAEJP,WAAW4C,iBAAiB,SAAS,KACjCrC,qBAEJN,QAAQ2C,iBAAiB,UAAU,KAC/BrC,qBAEJL,cAAc0C,iBAAiB,UAAU,KACrCrC,yCAWO,iBAAkB,gBAAgBsC,MAAMC,OAC/CzC,cAAgByC,UA0FpB3C,gBAAkBJ,KAAKuC,cAAc,sBAdrCtC,WAAW+C,SAAWrB,eACtB1B,WAAWgD,QAAUtB,eACrBvB,gBAAgB4C,SAAWpB,gBAC3BxB,gBAAgB6C,QAAUrB,gBAC1BzB,cAAc0C,iBAAiB,UAAU,KACrClB,iBACAG"} \ No newline at end of file diff --git a/amd/src/cdiscount.js b/amd/src/cdiscount.js index dd215f72..89cd6685 100644 --- a/amd/src/cdiscount.js +++ b/amd/src/cdiscount.js @@ -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]"); diff --git a/classes/category/options.php b/classes/category/options.php index 630e9ea2..92760b01 100644 --- a/classes/category/options.php +++ b/classes/category/options.php @@ -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; @@ -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; } @@ -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])) { @@ -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])) { diff --git a/classes/form/conditional_discount.php b/classes/form/conditional_discount.php new file mode 100644 index 00000000..203f06cf --- /dev/null +++ b/classes/form/conditional_discount.php @@ -0,0 +1,112 @@ +. + +namespace enrol_wallet\form; + +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->libdir.'/formslib.php'); + +use moodleform; +use enrol_wallet\category\options; +/** + * Class conditional_discount + * + * @package enrol_wallet + * @copyright 2024 2024, Mohammad Farouk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class conditional_discount extends moodleform { + /** + * definition + * @return void + */ + public function definition() { + $mform = $this->_form; + $data = $this->_customdata; + + $mform->addElement('header', 'conditionaldiscount', get_string('conditionaldiscount', 'enrol_wallet')); + + $options = options::get_all_categories_options(); + $mform->addElement('select', 'category', get_string('category'), $options); + + $mform->addElement('text', 'cond', get_string('conditionaldiscount_condition', 'enrol_wallet')); + $mform->setType('cond', PARAM_FLOAT); + $mform->addHelpButton('cond', 'conditionaldiscount_condition', 'enrol_wallet'); + + $mform->addElement('text', 'percent', get_string('conditionaldiscount_percent', 'enrol_wallet')); + $mform->setType('percent', PARAM_FLOAT); + $mform->addHelpButton('percent', 'conditionaldiscount_percent', 'enrol_wallet'); + + $mform->addElement('date_time_selector', 'timefrom', + get_string('conditionaldiscount_timefrom', 'enrol_wallet'), ['optional' => true]); + $mform->addHelpButton('timefrom', 'conditionaldiscount_timefrom', 'enrol_wallet'); + + $mform->addElement('date_time_selector', 'timeto', + get_string('conditionaldiscount_timeto', 'enrol_wallet'), ['optional' => true]); + $mform->addHelpButton('timefrom', 'conditionaldiscount_timeto', 'enrol_wallet'); + + $mform->addElement('checkbox', 'have_bundle', get_string('addbundle', 'enrol_wallet')); + $mform->addHelpButton('have_bundle', 'addbundle', 'enrol_wallet'); + + $mform->addElement('float', 'bundle_value', get_string('bundle_value', 'enrol_wallet')); + $mform->addHelpButton('bundle_value', 'bundle_value', 'enrol_wallet'); + $mform->hideIf('bundle_value', 'have_bundle'); + + $options = [ + 'enable_filemanagement' => false, + ]; + $mform->addElement('editor', 'bundle_desc', get_string('bundle_desc', 'enrol_wallet'), '', $options); + $mform->addHelpButton('bundle_desc', 'bundle_desc', 'enrol_wallet'); + $mform->setType('bundle_desc', PARAM_CLEANHTML); + $mform->hideIf('bundle_desc', 'have_bundle'); + + if (!empty($data['id'])) { + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + } + + if ($data['edit']) { + $mform->addElement('hidden', 'edit'); + $mform->setType('edit', PARAM_BOOL); + + $mform->setDefaults($_GET); + + $mform->addElement('submit', 'confirmedit', get_string('confirmedit', 'enrol_wallet')); + } else { + $mform->addElement('submit', 'add', get_string('add')); + } + } + + /** + * Validation + * @param array $data + * @param array $files + * @return array + */ + public function validation($data, $files) { + $errors = []; + if ($data['percent'] > 100 || $data['percent'] < 0) { + $errors['percent'] = get_string('percent_error', 'enrol_wallet'); + } + if (!empty($data['have_bundle'])) { + if (empty($data['bundle_value']) || $data['bundle_value'] < $data['cond']) { + $errors['bundle_value'] = get_string('bundle_value_error', 'enrol_wallet'); + } + } + return $errors; + } +} diff --git a/classes/form/topup_form.php b/classes/form/topup_form.php index eed1e9e4..099933b0 100644 --- a/classes/form/topup_form.php +++ b/classes/form/topup_form.php @@ -125,6 +125,14 @@ public function definition() { $catoptions = $helper->get_local_options_with_discounts(); } } + + if (count($catoptions) === 1) { + $rules = discount_rules::get_the_discount_line(reset($catoptions)); + } else { + $rules = discount_rules::get_the_discount_line(-1); + } + $mform->addElement('html', $rules); + if (count($catoptions) > 1) { $mform->addElement('select', 'category', $categorytitle, $catoptions); } else { diff --git a/classes/form/transfer_form.php b/classes/form/transfer_form.php index 0ce80b3c..af7216d8 100644 --- a/classes/form/transfer_form.php +++ b/classes/form/transfer_form.php @@ -90,7 +90,7 @@ protected function definition() { if ($this->balance->catenabled) { $main = $this->balance->get_main_balance(); $mainbalance = format_string(format_float($main, 2) . ' ' . $currency); - $mform->addElement('static', 'mainbalance', get_string('mainbalance', 'enrol_wallet'), $displaybalance); + $mform->addElement('static', 'mainbalance', get_string('mainbalance', 'enrol_wallet'), $mainbalance); $details = $this->balance->get_balance_details(); if (!empty($details['catbalance'])) { diff --git a/classes/pages.php b/classes/pages.php new file mode 100644 index 00000000..6d4421d9 --- /dev/null +++ b/classes/pages.php @@ -0,0 +1,344 @@ +. + +namespace enrol_wallet; + +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->libdir.'/tablelib.php'); +require_once($CFG->libdir.'/formslib.php'); + +use moodle_url; +use html_table; +use html_writer; +use MoodleQuickForm; +use DOMDocument; +use core_user; +use single_button; +use core_course_category; +use enrol_wallet\util\balance; +use enrol_wallet\util\balance_op; +use enrol_wallet\util\discount_rules; +use enrol_wallet\util\offers; + +/** + * Class pages + * + * @package enrol_wallet + * @copyright 2024 2024, Mohammad Farouk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class pages { + /** + * Output the content of referral page + * @param int $userid + */ + public static function process_referral_page($userid = 0) { + global $DB, $USER, $CFG, $OUTPUT; + if (!(bool)get_config( 'enrol_wallet', 'referral_enabled')) { + echo 'Referrals not enabled'; + return; + } + + if (empty($userid)) { + $user = $USER; + } else { + $user = core_user::get_user($userid); + } + + $isparent = false; + if (file_exists("$CFG->dirroot/auth/parent/auth.php")) { + require_once("$CFG->dirroot/auth/parent/auth.php"); + $authparent = new \auth_plugin_parent; + $isparent = $authparent->is_parent($user); + } + + if ($isparent) { + echo 'Parents not allow to access referral program.'; + return; + } + + $amount = get_config('enrol_wallet', 'referral_amount'); + $maxref = get_config('enrol_wallet', 'referral_max'); + + $exist = $DB->get_record('enrol_wallet_referral', ['userid' => $user->id]); + if (!$exist) { + $data = (object)[ + 'userid' => $user->id, + 'code' => random_string(15) . $user->id, + ]; + $DB->insert_record('enrol_wallet_referral', $data); + $exist = $DB->get_record('enrol_wallet_referral', ['userid' => $user->id]); + } + + $holdgift = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $user->username]); + + $refusers = $DB->get_records('enrol_wallet_hold_gift', ['referrer' => $user->id]); + + $output = ''; + if (!empty($holdgift)) { + $referrer = core_user::get_user($holdgift->referrer); + $a = [ + 'name' => fullname($referrer), + 'amount' => format_float($holdgift->amount, 2), + ]; + $message = get_string('referral_holdgift', 'enrol_wallet', ); + $output .= $OUTPUT->notification($message); + } + + if (!empty($refusers)) { + $table = new html_table; + $headers = [ + get_string('user'), + get_string('status'), + get_string('referral_amount', 'enrol_wallet'), + get_string('referral_timecreated', 'enrol_wallet'), + get_string('referral_timereleased' , 'enrol_wallet'), + ]; + $table->data[] = $headers; + foreach ($refusers as $data) { + $referred = core_user::get_user_by_username($data->referred); + $status = empty($data->released) ? get_string('referral_hold', 'enrol_wallet') + : get_string('referral_done', 'enrol_wallet'); + $table->data[] = [ + $referred->firstname . ' ' . $referred->lastname, + $status, + format_float($data->amount, 2), + userdate($data->timecreated), + !empty($data->timemodified) ? userdate($data->timemodified) : '', + ]; + } + $output .= html_writer::table($table); + } else { + $message = get_string('noreferraldata', 'enrol_wallet'); + $output .= $OUTPUT->notification($message); + } + + $mform = new MoodleQuickForm('referral_info', 'get', null); + + $signup = new moodle_url('/login/signup.php', ['refcode' => $exist->code]); + $mform->addElement('static', 'refurl', get_string('referral_url', 'enrol_wallet'), $signup->out(false)); + $mform->addHelpButton('refurl', 'referral_url', 'enrol_wallet'); + + $mform->addElement('static', 'refcode', get_string('referral_code', 'enrol_wallet'), $exist->code); + $mform->addHelpButton('refcode', 'referral_code', 'enrol_wallet'); + + $mform->addElement('text', 'refamount', get_string('referral_amount', 'enrol_wallet')); + $mform->addHelpButton('refamount', 'referral_amount', 'enrol_wallet'); + $mform->setType('refamount', PARAM_FLOAT); + $mform->setConstant('refamount', $amount); + + $mform->addElement('hidden', 'disable'); + $mform->setType('disable', PARAM_INT); + $mform->setConstant('disable', 0); + + $mform->disabledIf('refamount', 'disable', 'neq', 1); + + if (!empty($maxref)) { + $mform->addElement('text', 'refremain', get_string('referral_remain', 'enrol_wallet')); + $mform->setType('refremain', PARAM_INT); + $mform->addHelpButton('refremain', 'referral_remain', 'enrol_wallet'); + $mform->setConstant('refremain', $maxref - $exist->usetimes); + $mform->disabledIf('refremain', 'disable', 'neq', 1); + } + + echo $OUTPUT->heading(get_string('referral_past', 'enrol_wallet')); + + echo $output; + + echo $OUTPUT->heading(get_string('referral_data', 'enrol_wallet')); + + $mform->display(); + } + + /** + * Return a confirmation message for charging another user wallet. + * @param array|stdClass $data the submitted data. + * @param moodle_url $returnurl + * @param moodle_url $pageurl + * @return string + */ + public static function get_charger_confirm($data, $returnurl, $pageurl = null) { + global $OUTPUT, $PAGE; + if (!has_capability('enrol/wallet:creditdebit', \context_system::instance())) { + return ''; + } + + if (empty($pageurl)) { + $pageurl = $PAGE->url; + } + + $confirmurl = new moodle_url($pageurl, $data); + $confirmurl->param('confirm', true); + $confirmurl->param('return', $returnurl->out_as_local_url()); + $confirmbutton = new single_button($confirmurl, get_string('confirm'), 'post'); + $cancelbutton = new single_button($returnurl, get_string('cancel'), 'post'); + + if (!empty($data['category'])) { + $category = core_course_category::get($data['category'], IGNORE_MISSING); + } else { + $category = null; + } + + $balance = new balance($data['userlist'], $category); + $userbalance = $balance->get_valid_balance(); + + $user = core_user::get_user($data['userlist']); + $name = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id]), fullname($user), ['target' => '_blank']); + + $a = [ + 'name' => $name, + 'amount' => $data['value'], + 'balance' => $userbalance, + ]; + $a['category'] = !(empty($category)) ? $category->get_nested_name(false) : get_string('site'); + + $negativewarn = false; + switch ($data['op']) { + case 'debit': + $a['after'] = ($userbalance - $data['value']); + if ($a['after'] < 0) { + $negativewarn = true; + } + $msg = get_string('confirm_debit', 'enrol_wallet', $a); + break; + case 'credit': + $msg = get_string('confirm_credit', 'enrol_wallet', $a); + list($extra, $condition) = discount_rules::get_the_rest($data['value'], $data['category']); + if (!empty($extra)) { + $msg .= '
'.get_string('confirm_additional_credit', 'enrol_wallet', $extra); + } + break; + default: + $msg = ''; + } + if ($negativewarn) { + $warning = get_string('confirm_negative', 'enrol_wallet'); + $msg .= $OUTPUT->notification($warning, 'error', false); + } + return $OUTPUT->confirm($msg, $confirmbutton, $cancelbutton); + } + + /** + * Out the content of transfer page. + * @param moodle_url|string $url + * @return void + */ + public static function process_transfer_page($url) { + $mform = new \enrol_wallet\form\transfer_form(); + + if ($data = $mform->get_data()) { + + $catid = $data->category; + $op = new balance_op(0, $catid); + + $msg = $op->transfer_to_other($data, $mform); + if (stristr($msg, 'error')) { + $type = 'error'; + } else { + $type = 'success'; + } + // All done. + redirect($url, $msg, null, $type); + + } else { + $mform->display(); + } + } + + /** + * Return the content of offers page. + * @return string + */ + public static function get_offers_content() { + global $DB, $PAGE, $OUTPUT; + $renderer = $PAGE->get_renderer('core', 'course'); + + $courses = offers::get_courses_with_offers(); + + $free = ''; + $withoffers = ''; + + $dom = new DOMDocument(); + $injected = new DOMDocument(); + libxml_use_internal_errors(true); + + foreach ($courses as $course) { + $coursebox = mb_convert_encoding($renderer->course_info_box($course), 'HTML-ENTITIES', "UTF-8"); + $dom->loadHTML($coursebox); + + $fragment = $dom->createDocumentFragment(); + foreach ($course->offers as $offer) { + $injected->loadHTML(html_writer::div($offer, 'card-body')); + $injectednode = $dom->importNode($injected->documentElement, true); + $fragment->appendChild($injectednode); + } + + $innercoursenodes = $dom->getElementsByTagName('div'); + $innercoursenode = $innercoursenodes->item(1); + + $innercoursenode->appendChild($fragment); + + if ($course->free) { + $free .= $dom->saveHTML($innercoursenode); + } else { + $withoffers .= $dom->saveHTML($innercoursenode); + } + } + + $out = ''; + + $rules = discount_rules::get_the_discount_line(-1); + if (!empty($rules)) { + $out .= $OUTPUT->heading(get_string('topupoffers', 'enrol_wallet')); + $out .= $OUTPUT->box(get_string('topupoffers_desc', 'enrol_wallet')); + $out .= $rules; + $out .= "
"; + } + + $config = get_config('enrol_wallet'); + if (!empty($config->cashback)) { + $cashbackvalue = (float)$config->cashbackpercent; + if ($cashbackvalue > 0 && $cashbackvalue <= 100) { + $out .= $OUTPUT->heading(get_string('cashback', 'enrol_wallet')); + $out .= $OUTPUT->box(get_string('cashback_desc', 'enrol_wallet', $cashbackvalue)); + $out .= "
"; + } + } + + if (!empty($config->referral_enabled) && (float)$config->referral_amount > 0) { + $out .= $OUTPUT->heading(get_string('referral_program', 'enrol_wallet')); + $url = new moodle_url('/enrol/wallet/extra/referral.php'); + $text = get_string('clickhere'); + $link = html_writer::link($url, $text); + $out .= $OUTPUT->box(get_string('referral_site_desc', 'enrol_wallet') . $link); + $out .= "
"; + } + + if (!empty($free)) { + $out .= $OUTPUT->heading(get_string('freecourses', 'enrol_wallet')); + $out .= html_writer::div($free, 'courses courses-flex-cards'); + $out .= "
"; + } + + if (!empty($withoffers)) { + $out .= $OUTPUT->heading(get_string('courseswithdiscounts', 'enrol_wallet')); + $out .= html_writer::div($withoffers, 'courses courses-flex-cards'); + $out .= "
"; + } + return $out; + } +} diff --git a/classes/table/transactions.php b/classes/table/transactions.php index cd1b3e8a..c1d171a9 100644 --- a/classes/table/transactions.php +++ b/classes/table/transactions.php @@ -23,6 +23,11 @@ */ namespace enrol_wallet\table; + +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->libdir.'/tablelib.php'); + use moodle_url; use table_sql; use core_user; @@ -56,9 +61,9 @@ class transactions extends table_sql { * Creates a transaction table. * * @param int $uniqueid all tables have to have a unique id. - * @param stdClass $filterdata The data passed from the filtration form. + * @param stdClass|null $filterdata The data passed from the filtration form. */ - public function __construct($uniqueid, $filterdata) { + public function __construct($uniqueid, $filterdata = null) { parent::__construct($uniqueid); $columnsandheaders = [ 'user' => get_string('user'), diff --git a/classes/util/discount_rules.php b/classes/util/discount_rules.php index d23daaaa..68937937 100644 --- a/classes/util/discount_rules.php +++ b/classes/util/discount_rules.php @@ -168,12 +168,41 @@ public static function get_the_rest($amount, $catid = 0) { /** * Get the value before applying the conditional discount. - * + * (i.e. the min value) + * @param float $amount + * @param int $catid + * @param float $discount + * @return float + */ + public static function get_the_before($amount, $catid = 0, $discount = 0) { + if (empty($discount)) { + $discount = self::get_applied_discount($amount, $catid); + } + return (float)($amount * (1 - $discount / 100)); + } + + /** + * Get the value that will be added to the wallet balance after applying the discount + * rule. + * (i.e. the max value) + * @param float $amount + * @param int $catid + * @param float $discount + */ + public static function get_the_after($amount, $catid = 0, $discount = 0) { + if (empty($discount)) { + $discount = self::get_applied_discount($amount, $catid); + } + return (float)($amount / (1 - $discount / 100)); + } + + /** + * Get the discount that could be applied for a given amount and category. * @param float $amount * @param int $catid * @return float */ - public static function get_the_before($amount, $catid = 0) { + private static function get_applied_discount($amount, $catid) { $discount = 0; $records = self::get_current_discount_rules($catid); foreach ($records as $record) { @@ -181,10 +210,8 @@ public static function get_the_before($amount, $catid = 0) { $discount = $record->percent; } } - - return (float)($amount * (1 - $discount / 100)); + return $discount; } - /** * Render the discount rules as a form of line divided by the rules. * @param int $catid -1 for all rules, 0 site rules only or category id for the rules @@ -212,14 +239,13 @@ public static function get_the_discount_line($catid = 0) { $currency = get_config('enrol_wallet', 'currency'); $data = new \stdClass; $data->data = []; - $maxcondition = 0; + $maxconditions = []; foreach ($records as $record) { - if ($record->cond > $maxcondition) { - $maxcondition = $record->cond; + if ($record->cond > ($maxconditions[$record->category ?? 0] ?? 0)) { + $maxconditions[$record->category] = $record->cond; } } - $maxcondition = $maxcondition * 1.2; $discounts = []; $catid = -1; foreach ($records as $id => $record) { @@ -231,6 +257,7 @@ public static function get_the_discount_line($catid = 0) { $catid = $record->category; $data->data[$catid] = new \stdClass; $data->data[$catid]->catid = $catid; + $data->data[$catid]->count = 0; if (empty($catid)) { $name = get_string('site'); @@ -239,17 +266,96 @@ public static function get_the_discount_line($catid = 0) { $name = $category->get_nested_name(false); } $data->data[$catid]->heading = $OUTPUT->heading($name, 4); + $prevwidth = 0; } - + $maxcondition = $maxconditions[$catid] * 1.2; + $data->data[$catid]->count++; $discounts[$id] = new \stdClass; - $discounts[$id]->percent = $record->cond / $maxcondition * 100; + $discounts[$id]->percent = (100 - ($record->cond / $maxcondition * 100)) - $prevwidth; $discounts[$id]->order = (int)round((float)$record->cond / $maxcondition * 10); $discounts[$id]->color = (int)round((1 - ((float)$record->cond / $maxcondition)) * 255); - $discounts[$id]->condition = '>' . format_float($record->cond, 2) . " $currency"; + $discounts[$id]->condition = '> ' . format_float($record->cond, 2) . " $currency"; $discounts[$id]->discount = format_float($record->percent, 2) . '%'; + $prevwidth = $discounts[$id]->percent; } $data->data[$catid]->discounts = array_values($discounts); $data->data = array_values($data->data); return $OUTPUT->render_from_template('enrol_wallet/discount-line', $data); } + + /** + * Get the conditional discount records with bundles. + * @return array + */ + private static function get_bundles_records() { + global $DB;$now = time(); + $params = [ + 'time1' => $now, + 'time2' => $now, + ]; + $select = '(timefrom <= :time1 OR timefrom = 0) AND (timeto >= :time2 OR timeto = 0)'; + $select .= ' AND bundle IS NOT NULL'; + return $DB->get_records_select('enrol_wallet_cond_discount', $select, $params); + } + + /** + * Return a single bundle top up button. + * @param \stdClass $record + * @return string + */ + private static function format_single_bundle_button($record) { + global $OUTPUT; + + $before = $record->bundle; + $discount = self::get_applied_discount($before, $record->category ?? 0); + $after = self::get_the_before($before, $record->category ?? 0, $discount); + $desc = !empty($record->bundledesc) ? format_text($record->bundledesc, $record->descformat) : ''; + $data = new \stdClass; + $data->category = $record->category ?? 0; + $data->value = $before; + $data->instanceid = 0; + $data->courseid = SITEID; + $data->account = get_config('enrol_wallet', 'account'); + $data->currency = get_config('enrol_wallet', 'currency'); + $topupurl = new \moodle_url('/enrol/wallet/extra/topup.php', (array)$data); + + $context = [ + 'discount' => $discount, + 'after' => $after, + 'before' => $before, + 'description' => $desc, + 'currency' => $data->currency, + ]; + if (empty($data->category)) { + $context['category'] = get_string('site'); + } else { + $category = core_course_category::get($data->category, IGNORE_MISSING); + if (!$category) { + return ''; + } + $context['category'] = $category->get_nested_name(); + } + $button = new \single_button($topupurl, ''); + $buttoncontext = (array)$button->export_for_template($OUTPUT); + $context = $context + $buttoncontext; + return $OUTPUT->render_from_template('enrol_wallet/single-bundle', $context); + } + + /** + * Render all bundle buttons available for topping up. + * @return string + */ + public static function bundles_buttons() { + $out = ''; + $records = self::get_bundles_records(); + if (empty($records)) { + return $out; + } + $out .= \html_writer::start_div('enrol-wallet-bundles'); + foreach ($records as $record) { + $out .= self::format_single_bundle_button($record); + } + $out .= \html_writer::end_div(); + return $out; + } } diff --git a/db/install.xml b/db/install.xml index d4401cd2..b2e96c5e 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -93,6 +93,9 @@ + + + diff --git a/db/upgrade.php b/db/upgrade.php index 5e5be0fc..754e54f9 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -321,5 +321,32 @@ function xmldb_enrol_wallet_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2024020300, 'enrol', 'wallet'); } + if ($oldversion < 2024022500) { + + // Define field bundle to be added to enrol_wallet_cond_discount. + $table = new xmldb_table('enrol_wallet_cond_discount'); + $field = new xmldb_field('bundle', XMLDB_TYPE_NUMBER, '10, 2', null, null, null, null, 'timeto'); + + // Conditionally launch add field bundle. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('bundledesc', XMLDB_TYPE_TEXT, null, null, null, null, null, 'bundle'); + + // Conditionally launch add field bundledesc. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('descformat', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'bundledesc'); + + // Conditionally launch add field descformat. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + // Wallet savepoint reached. + upgrade_plugin_savepoint(true, 2024022500, 'enrol', 'wallet'); + } return true; } diff --git a/extra/action.php b/extra/action.php deleted file mode 100644 index 8ee7ec0b..00000000 --- a/extra/action.php +++ /dev/null @@ -1,175 +0,0 @@ -. - -/** - * Enrol wallet action after submit the coupon code. - * - * @package enrol_wallet - * @copyright 2023 Mohammad Farouk - * @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 . '
'; - } - - $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); diff --git a/extra/charger.php b/extra/charger.php index 5b4389dd..4a4cb7dc 100644 --- a/extra/charger.php +++ b/extra/charger.php @@ -78,57 +78,12 @@ redirect($returnurl); exit; } else { - $confirmurl = new moodle_url($pageurl, $data); - $confirmurl->param('confirm', true); - $confirmurl->param('return', $returnurl->out_as_local_url()); - $confirmbutton = new single_button($confirmurl, get_string('confirm'), 'post'); - $cancelbutton = new single_button($returnurl, get_string('cancel'), 'post'); - - if (!empty($data['category'])) { - $category = core_course_category::get($data['category'], IGNORE_MISSING); - } else { - $category = 0; - } - - $balance = new enrol_wallet\util\balance($data['userlist'], $category); - $userbalance = $balance->get_valid_balance(); - - $user = core_user::get_user($data['userlist']); - $name = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id]), fullname($user), ['target' => '_blank']); - - $a = [ - 'name' => $name, - 'amount' => $data['value'], - 'balance' => $userbalance, - ]; - $a['category'] = !(empty($category)) ? $category->get_nested_name(false) : get_string('site'); - - $negativewarn = false; - switch ($data['op']) { - case 'debit': - $a['after'] = ($userbalance - $data['value']); - if ($a['after'] < 0) { - $negativewarn = true; - } - $msg = get_string('confirm_debit', 'enrol_wallet', $a); - break; - case 'credit': - $msg = get_string('confirm_credit', 'enrol_wallet', $a); - list($extra, $condition) = enrol_wallet\util\discount_rules::get_the_rest($data['value'], $data['category']); - if (!empty($extra)) { - $msg .= '
'.get_string('confirm_additional_credit', 'enrol_wallet', $extra); - } - break; - default: - $msg = ''; - } + $confirm = enrol_wallet\pages::get_charger_confirm($data, $returnurl, $pageurl); echo $OUTPUT->header(); - if ($negativewarn) { - $warning = get_string('confirm_negative', 'enrol_wallet'); - $msg .= $OUTPUT->notification($warning, 'error', false); - } - echo $OUTPUT->confirm($msg, $confirmbutton, $cancelbutton); + + echo $confirm; + echo $OUTPUT->footer(); exit; } @@ -142,6 +97,7 @@ // Display the charger form. $form = enrol_wallet_display_charger_form(); + echo $OUTPUT->box($form); echo $OUTPUT->footer(); diff --git a/extra/conditionaldiscount.php b/extra/conditionaldiscount.php index 7bf6a062..6631db1e 100644 --- a/extra/conditionaldiscount.php +++ b/extra/conditionaldiscount.php @@ -25,7 +25,7 @@ require_once('../../../config.php'); require_once($CFG->dirroot.'/enrol/wallet/locallib.php'); require_once($CFG->libdir.'/tablelib.php'); -require_once($CFG->libdir.'/formslib.php'); + global $OUTPUT, $PAGE, $DB; // Adding some security. @@ -38,78 +38,6 @@ $url = new moodle_url('/enrol/wallet/extra/conditionaldiscount.php'); -// Parameters. -$id = optional_param('id', 0, PARAM_INT); -$condition = optional_param('cond', '', PARAM_FLOAT); -$catid = optional_param('category', 0, PARAM_INT); -$percentage = optional_param('percent', '', PARAM_FLOAT); -if (isset($_GET['timeto']) && is_array($_GET['timeto'])) { - $timetoarray = optional_param_array('timeto', [], PARAM_INT); -} else { - $timeto = optional_param('timeto', null, PARAM_INT); -} -if (isset($_GET['timefrom']) && is_array($_GET['timefrom'])) { - $timefromarray = optional_param_array('timefrom', [], PARAM_INT); -} else { - $timefrom = optional_param('timefrom', null, PARAM_INT); -} -$delete = optional_param('delete', false, PARAM_BOOL); -$add = optional_param('add', false, PARAM_BOOL); -$edit = optional_param('edit', false, PARAM_BOOL); -$confirmedit = optional_param('confirmedit', false, PARAM_BOOL); -$sesskey = optional_param('sesskey', '', PARAM_RAW); - -if (!empty($sesskey) && confirm_sesskey()) { - $done = false; - $arraydates = []; - if (!empty($timetoarray)) { - $arraydates['timeto'] = $timetoarray; - } - if (!empty($timefromarray)) { - $arraydates['timefrom'] = $timefromarray; - } - // ...mktime all dates. - foreach ($arraydates as $key => $date) { - if (!empty($date)) { - $$key = mktime( - $date['hour'], - $date['minute'], - 0, - $date['month'], - $date['day'], - $date['year'], - ); - } else { - $$key = 0; - } - } - $dataobject = new stdClass; - $dataobject->cond = $condition; - $dataobject->percent = $percentage; - $dataobject->category = $catid; - $dataobject->timemodified = time(); - $dataobject->usermodified = $USER->id; - if (!empty($timeto)) { - $dataobject->timeto = $timeto; - } - if (!empty($timefrom)) { - $dataobject->timefrom = $timefrom; - } - - if ($add) { - $dataobject->timecreated = time(); - $done = $DB->insert_record('enrol_wallet_cond_discount', $dataobject); - } else if ($confirmedit && !empty($id)) { - $dataobject->id = $id; - $done = $DB->update_record('enrol_wallet_cond_discount', $dataobject); - } else if ($delete && !empty($id)) { - $done = $DB->delete_records('enrol_wallet_cond_discount', ['id' => $id]); - } - if (!empty($done)) { - redirect($url); - } -} - // Setup the page. $PAGE->set_pagelayout('admin'); $PAGE->set_context($systemcontext); @@ -117,58 +45,53 @@ $PAGE->set_title(get_string('conditionaldiscount', 'enrol_wallet')); $PAGE->set_heading(get_string('conditionaldiscount', 'enrol_wallet')); -// -------------------------------------------------------------------------------------- -// Form. -// Setup the filtration form. -$mform = new \MoodleQuickForm('conditionaldiscount', 'get', 'conditionaldiscount.php'); - -$mform->addElement('header', 'conditionaldiscount', get_string('conditionaldiscount', 'enrol_wallet')); +$id = optional_param('id', 0, PARAM_INT); +$delete = optional_param('delete', false, PARAM_BOOL); +$add = optional_param('add', false, PARAM_BOOL); +$edit = optional_param('edit', false, PARAM_BOOL); +$confirmedit = optional_param('confirmedit', false, PARAM_BOOL); -$options = enrol_wallet\category\options::get_all_categories_options(); -$mform->addElement('select', 'category', get_string('category'), $options); +$customdata = [ + 'edit' => $edit, + 'id' => $id, +]; +$mform = new enrol_wallet\form\conditional_discount(null, $customdata, 'get'); +$done = false; +if ($data = $mform->get_data()) { -$mform->addElement('text', 'cond', get_string('conditionaldiscount_condition', 'enrol_wallet')); -$mform->setType('cond', PARAM_FLOAT); -$mform->addHelpButton('cond', 'conditionaldiscount_condition', 'enrol_wallet'); -if (!empty($condition)) { - $mform->setDefault('cond', $condition); -} - -$mform->addElement('text', 'percent', get_string('conditionaldiscount_percent', 'enrol_wallet')); -$mform->setType('percent', PARAM_FLOAT); -$mform->addHelpButton('percent', 'conditionaldiscount_percent', 'enrol_wallet'); -if (!empty($percentage)) { - $mform->setDefault('percent', $percentage); -} - -$mform->addElement('date_time_selector', 'timefrom', - get_string('conditionaldiscount_timefrom', 'enrol_wallet'), ['optional' => true]); -$mform->addHelpButton('timefrom', 'conditionaldiscount_timefrom', 'enrol_wallet'); -if (!empty($timefrom)) { - $mform->setDefault('timefrom', $timefrom); -} - -$mform->addElement('date_time_selector', 'timeto', - get_string('conditionaldiscount_timeto', 'enrol_wallet'), ['optional' => true]); -$mform->addHelpButton('timefrom', 'conditionaldiscount_timeto', 'enrol_wallet'); -if (!empty($timeto)) { - $mform->setDefault('timeto', $timeto); + $dataobject = new stdClass; + $dataobject->cond = $data->cond; + $dataobject->percent = $data->percent; + $dataobject->category = $data->category; + $dataobject->timemodified = time(); + $dataobject->usermodified = $USER->id; + $dataobject->timeto = $data->timeto ?? null; + $dataobject->timefrom = $data->timefrom ?? null; + + if (!empty($data->have_bundle)) { + $dataobject->bundle = $data->bundle_value; + $dataobject->bundledesc = $data->bundle_desc['text']; + $dataobject->descformat = $data->bundle_desc['format']; + } else { + $dataobject->bundle = null; + $dataobject->bundledesc = null; + $dataobject->descformat = null; + } + if ($add) { + $dataobject->timecreated = time(); + $done = $DB->insert_record('enrol_wallet_cond_discount', $dataobject); + } else if ($confirmedit && !empty($data->id)) { + $dataobject->id = $data->id; + $done = $DB->update_record('enrol_wallet_cond_discount', $dataobject); + } } -if (!empty($id)) { - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - $mform->setDefault('id', $id); +if ($delete && !empty($id) && confirm_sesskey()) { + $done = $DB->delete_records('enrol_wallet_cond_discount', ['id' => $id]); } -$mform->addElement('hidden', 'sesskey'); -$mform->setType('sesskey', PARAM_RAW); -$mform->setConstant('sesskey', sesskey()); - -if ($edit) { - $mform->addElement('submit', 'confirmedit', get_string('confirmedit', 'enrol_wallet')); -} else { - $mform->addElement('submit', 'add', get_string('add')); +if (!empty($done)) { + redirect($url); } echo $OUTPUT->header(); @@ -190,6 +113,8 @@ 'category' => get_string('category'), 'timeto' => get_string('conditionaldiscount_timeto', 'enrol_wallet'), 'timefrom' => get_string('conditionaldiscount_timefrom', 'enrol_wallet'), + 'bundle' => get_string('bundle_value', 'enrol_wallet'), + 'bundledesc' => get_string('bundle_desc', 'enrol_wallet'), 'edit' => null, 'delete' => null, ]; @@ -215,7 +140,14 @@ if (empty($record->timefrom)) { unset($record->timefrom); } + $editparams = array_merge(['edit' => true, 'sesskey' => sesskey()], (array)$record); + if (!empty($record->bundle)) { + $editparams['have_bundle'] = 1; + $editparams['bundle_value'] = $record->bundle; + $editparams['bundle_desc[text]'] = $record->bundledesc; + $editparams['bundle_desc[format]'] = $record->descformat; + } $deleteparams = ['delete' => true, 'sesskey' => sesskey(), 'id' => $record->id]; $editurl = new moodle_url('conditionaldiscount.php', $editparams); $deleteurl = new moodle_url('conditionaldiscount.php', $deleteparams); @@ -238,6 +170,8 @@ 'category' => $catname, 'timefrom' => !empty($record->timefrom) ? userdate($record->timefrom) : '', 'timeto' => !empty($record->timeto) ? userdate($record->timeto) : '', + 'bundle' => $record->bundle ?? '', + 'bundledesc' => !empty($record->bundledesc) ? format_text($record->bundledesc, $record->descformat) : '', 'edit' => $editbutton, 'delete' => $deletebutton, ]; diff --git a/extra/generator.php b/extra/generator.php deleted file mode 100644 index 7aaa1a8f..00000000 --- a/extra/generator.php +++ /dev/null @@ -1,113 +0,0 @@ -. - -/** - * Action page to generate 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'); -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); -} diff --git a/extra/offers.php b/extra/offers.php index 72a67f68..675e6ac2 100644 --- a/extra/offers.php +++ b/extra/offers.php @@ -26,87 +26,16 @@ require_login(); +$title = get_string('offers', 'enrol_wallet'); + $PAGE->set_context(context_course::instance(SITEID)); $PAGE->set_url(new moodle_url('/enrol/wallet/extra/offers.php')); -$PAGE->set_heading('Offers'); -$PAGE->set_title('Offers'); +$PAGE->set_heading($title); +$PAGE->set_title($title); $PAGE->set_pagelayout('frontpage'); -global $DB; -$renderer = $PAGE->get_renderer('core', 'course'); - -$courses = enrol_wallet\util\offers::get_courses_with_offers(); - -$free = ''; -$withoffers = ''; - -$dom = new DOMDocument(); -$injected = new DOMDocument(); -libxml_use_internal_errors(true); - -foreach ($courses as $course) { - $coursebox = mb_convert_encoding($renderer->course_info_box($course), 'HTML-ENTITIES', "UTF-8"); - $dom->loadHTML($coursebox); - - $fragment = $dom->createDocumentFragment(); - foreach ($course->offers as $offer) { - $injected->loadHTML(html_writer::div($offer, 'card-body')); - $injectednode = $dom->importNode($injected->documentElement, true); - $fragment->appendChild($injectednode); - } - - $innercoursenodes = $dom->getElementsByTagName('div'); - $innercoursenode = $innercoursenodes->item(1); - - $innercoursenode->appendChild($fragment); - - if ($course->free) { - $free .= $dom->saveHTML($innercoursenode); - } else { - $withoffers .= $dom->saveHTML($innercoursenode); - } -} - -$out = $OUTPUT->header(); - -$rules = enrol_wallet\util\discount_rules::get_the_discount_line(-1); -if (!empty($rules)) { - $out .= $OUTPUT->heading(get_string('topupoffers', 'enrol_wallet')); - $out .= $OUTPUT->box(get_string('topupoffers_desc', 'enrol_wallet')); - $out .= $rules; - $out .= "
"; -} -$config = get_config('enrol_wallet'); -if (!empty($config->cashback)) { - $cashbackvalue = (float)$config->cashbackpercent; - if ($cashbackvalue > 0 && $cashbackvalue <= 100) { - $out .= $OUTPUT->heading(get_string('cashback', 'enrol_wallet')); - $out .= $OUTPUT->box(get_string('cashback_desc', 'enrol_wallet', $cashbackvalue)); - $out .= "
"; - } -} - -if (!empty($config->referral_enabled) && (float)$config->referral_amount > 0) { - $out .= $OUTPUT->heading(get_string('referral_program', 'enrol_wallet')); - $url = new moodle_url('/enrol/wallet/extra/referral.php'); - $text = get_string('clickhere'); - $link = html_writer::link($url, $text); - $out .= $OUTPUT->box(get_string('referral_site_desc', 'enrol_wallet') . $link); - $out .= "
"; -} - -if (!empty($free)) { - $out .= $OUTPUT->heading(get_string('freecourses', 'enrol_wallet')); - $out .= html_writer::div($free, 'courses courses-flex-cards'); - $out .= "
"; -} - -if (!empty($withoffers)) { - $out .= $OUTPUT->heading(get_string('courseswithdiscounts', 'enrol_wallet')); - $out .= html_writer::div($withoffers, 'courses courses-flex-cards'); - $out .= "
"; -} - -$out .= $OUTPUT->footer(); +$out = enrol_wallet\pages::get_offers_content(); -echo $out; +echo $OUTPUT->header(), + $out, + $OUTPUT->footer(); diff --git a/extra/referral.php b/extra/referral.php index 51f579aa..3f601106 100644 --- a/extra/referral.php +++ b/extra/referral.php @@ -24,8 +24,8 @@ require_once('../../../config.php'); require_once(__DIR__.'/../lib.php'); -require_once($CFG->libdir.'/tablelib.php'); -require_once($CFG->libdir.'/formslib.php'); + +global $DB, $USER; $isparent = false; if (file_exists("$CFG->dirroot/auth/parent/auth.php")) { @@ -39,24 +39,14 @@ redirect(new moodle_url('/'), 'Parents not allow to access referral program.'); } -global $DB, $USER; +if (!(bool)get_config('referral_enabled', 'enrol_wallet')) { + redirect(new moodle_url('/')); +} + // Adding some security. require_login(); $thisurl = new moodle_url('/enrol/wallet/extra/referral.php'); -$amount = get_config('enrol_wallet', 'referral_amount'); -$maxref = get_config('enrol_wallet', 'referral_max'); - -$exist = $DB->get_record('enrol_wallet_referral', ['userid' => $USER->id]); -if (!$exist) { - $data = (object)[ - 'userid' => $USER->id, - 'code' => random_string(15) . $USER->id, - ]; - $DB->insert_record('enrol_wallet_referral', $data); - $exist = $DB->get_record('enrol_wallet_referral', ['userid' => $USER->id]); -} - $PAGE->set_url($thisurl); $context = context_user::instance($USER->id); @@ -66,85 +56,8 @@ $PAGE->set_heading(get_string('referral_user', 'enrol_wallet')); $PAGE->set_pagelayout('frontpage'); -$holdgift = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $USER->username]); - -$refusers = $DB->get_records('enrol_wallet_hold_gift', ['referrer' => $USER->id]); - -$output = ''; -if (!empty($holdgift)) { - $referrer = \core_user::get_user($holdgift->referrer); - $a = [ - 'name' => fullname($referrer), - 'amount' => format_float($holdgift->amount, 2), - ]; - $message = get_string('referral_holdgift', 'enrol_wallet', ); - $output .= $OUTPUT->notification($message); -} - -if (!empty($refusers)) { - $table = new html_table; - $headers = [ - get_string('user'), - get_string('status'), - get_string('referral_amount', 'enrol_wallet'), - get_string('referral_timecreated', 'enrol_wallet'), - get_string('referral_timereleased' , 'enrol_wallet'), - ]; - $table->data[] = $headers; - foreach ($refusers as $data) { - $referred = \core_user::get_user_by_username($data->referred); - $status = empty($data->released) ? get_string('referral_hold', 'enrol_wallet') - : get_string('referral_done', 'enrol_wallet'); - $table->data[] = [ - $referred->firstname . ' ' . $referred->lastname, - $status, - format_float($data->amount, 2), - userdate($data->timecreated), - !empty($data->timemodified) ? userdate($data->timemodified) : '', - ]; - } - $output .= html_writer::table($table); -} else { - $message = get_string('noreferraldata', 'enrol_wallet'); - $output .= $OUTPUT->notification($message); -} - -$mform = new MoodleQuickForm('referral_info', 'get', $thisurl); - -$signup = new moodle_url('/login/signup.php', ['refcode' => $exist->code]); -$mform->addElement('static', 'refurl', get_string('referral_url', 'enrol_wallet'), $signup->out(false)); -$mform->addHelpButton('refurl', 'referral_url', 'enrol_wallet'); - -$mform->addElement('static', 'refcode', get_string('referral_code', 'enrol_wallet'), $exist->code); -$mform->addHelpButton('refcode', 'referral_code', 'enrol_wallet'); - -$mform->addElement('text', 'refamount', get_string('referral_amount', 'enrol_wallet')); -$mform->addHelpButton('refamount', 'referral_amount', 'enrol_wallet'); -$mform->setType('refamount', PARAM_FLOAT); -$mform->setConstant('refamount', $amount); - -$mform->addElement('hidden', 'disable'); -$mform->setType('disable', PARAM_INT); -$mform->setConstant('disable', 0); - -$mform->disabledIf('refamount', 'disable', 'neq', 1); - -if (!empty($maxref)) { - $mform->addElement('text', 'refremain', get_string('referral_remain', 'enrol_wallet')); - $mform->setType('refremain', PARAM_INT); - $mform->addHelpButton('refremain', 'referral_remain', 'enrol_wallet'); - $mform->setConstant('refremain', $maxref - $exist->usetimes); - $mform->disabledIf('refremain', 'disable', 'neq', 1); -} - echo $OUTPUT->header(); -echo $OUTPUT->heading(get_string('referral_past', 'enrol_wallet')); - -echo $output; - -echo $OUTPUT->heading(get_string('referral_data', 'enrol_wallet')); - -$mform->display(); +enrol_wallet\pages::process_referral_page(); echo $OUTPUT->footer(); diff --git a/extra/transaction.php b/extra/transaction.php index b99db892..5ad4fa98 100644 --- a/extra/transaction.php +++ b/extra/transaction.php @@ -25,7 +25,6 @@ require_once('../../../config.php'); require_once(__DIR__.'/../lib.php'); require_once(__DIR__.'/../locallib.php'); -require_once($CFG->libdir.'/tablelib.php'); require_once($CFG->libdir.'/formslib.php'); global $DB, $USER; diff --git a/extra/transfer.php b/extra/transfer.php index 8da54aae..d8f61620 100644 --- a/extra/transfer.php +++ b/extra/transfer.php @@ -42,28 +42,14 @@ $PAGE->set_heading($title); $PAGE->set_url($url); -$mform = new \enrol_wallet\form\transfer_form(); +ob_start(); -if ($data = $mform->get_data()) { +enrol_wallet\pages::process_transfer_page($url); - $catid = $data->category; - $op = new enrol_wallet\util\balance_op(0, $catid); +$form = ob_get_clean(); - $msg = $op->transfer_to_other($data, $mform); - if (stristr($msg, 'error')) { - $type = 'error'; - } else { - $type = 'success'; - } - // All done. - redirect($url, $msg, null, $type); +echo $OUTPUT->header(); -} else { - - echo $OUTPUT->header(); - - $mform->display(); - - echo $OUTPUT->footer(); -} +echo $form; +echo $OUTPUT->footer(); diff --git a/lang/ar/enrol_wallet.php b/lang/ar/enrol_wallet.php index 87658668..1dc459d6 100644 --- a/lang/ar/enrol_wallet.php +++ b/lang/ar/enrol_wallet.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Strings for component 'enrol_wallet', language 'en'. + * Strings for component 'enrol_wallet', language 'ar'. * * @package enrol_wallet * @copyright 2023 Mo Farouk diff --git a/lang/en/enrol_wallet.php b/lang/en/enrol_wallet.php index f485fd4f..30dd8ae1 100644 --- a/lang/en/enrol_wallet.php +++ b/lang/en/enrol_wallet.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['addbundle'] = 'Add bundle'; +$string['addbundle_help'] = 'Add a quick top up value associated with this conditional discount rule.'; $string['agreepolicy_intro'] = 'In order to perform any wallet top up process it means that you have read and agreed the manual refund policy.
Click on the link below to read the policy.
'; $string['agreepolicy_label'] = 'I agree to the manual refund policy.'; @@ -50,6 +52,7 @@ $string['awardvalue_help'] = 'How much did the student get for each one grade above the condition?'; +$string['balance'] = 'Balance'; $string['balance_after'] = 'Balance after'; $string['balance_before'] = 'Balance before'; $string['borrow'] = 'Borrowing balance'; @@ -67,6 +70,12 @@ $string['bulk_instancesno'] = 'No instances created or updated'; $string['bulk_instancestitle'] = 'Bulk wallet enrol instances edit'; $string['bulk_instancesyes'] = '{$a->updated} enrol instances has been updated AND {$a->created} has been created.'; +$string['bundlevalidin'] = 'valid to be used in'; +$string['bundle_desc'] = 'The bundle description'; +$string['bundle_desc_help'] = 'Add a description for this bundle. (ex. enough for 11 courses in the price of 9)'; +$string['bundle_value'] = 'Quick top up value'; +$string['bundle_value_error'] = 'bundle value should be greater than or equals the condition'; +$string['bundle_value_help'] = 'This value must be greater than or equal to the condition. Also make sure this is the value before discount.'; $string['cachedef_balance'] = 'This is store the user\'s balance details'; @@ -91,6 +100,7 @@ $string['category_options_help'] = 'Same as fixed coupons except it is restricted to be used unless in the chosen category'; $string['characters'] = 'Characters in code.'; $string['characters_help'] = 'Choose the type of characters in the generated codes.'; +$string['charge'] = 'Charge'; $string['charger_credit_desc'] = 'charging manually by {$a}'; $string['charger_debit_desc'] = '(deduct manually by {$a})'; $string['charger_debit_err'] = 'The value ({$a->value}) is greater that the user\'s balance ({$a->before})'; @@ -127,7 +137,7 @@ $string['conditionaldiscount_condition'] = 'Condition for applying discount'; $string['conditionaldiscount_condition_help'] = 'Discounts won\'t be applied unless the user\'s wallet charged by more than or equal the value entered here.'; $string['conditionaldiscount_desc'] = 'charge wallet due to conditional discounts by {$a->rest} for charging wallet for more than {$a->condition}'; -$string['conditionaldiscount_link_desc'] = 'Add, Edit or delete conditional discount rules'; +$string['conditionaldiscount_link_desc'] = 'Add, edit or delete conditional discount rules'; $string['conditionaldiscount_percent'] = 'The percentage amount of discount'; $string['conditionaldiscount_percentage'] = 'Percentage'; $string['conditionaldiscount_percent_help'] = 'The users get credited by this percent. (Applied only for charging the wallet)
@@ -220,7 +230,7 @@ $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_table'] = 'View Wallet Coupons'; +$string['coupon_table'] = 'View wallet coupons'; $string['coupon_type'] = 'Type of coupons'; $string['coupon_type_help'] = 'choose the type of coupons to generate.
Fixed value coupons: used any where and top up the user\'s wallet by its value, and if used in enrollment page, it will enrol the user to the course if it is sufficient.
@@ -476,6 +486,8 @@ $string['paymenttopup_desc'] = 'Payment to top up the wallet'; $string['percentcoupondisabled'] = 'Discount coupons disabled in this website.'; $string['percentdiscountcoupon'] = 'Percentage discount coupon'; +$string['percent_error'] = 'Percentage value should be between 0-100'; +$string['pluginconfig'] = 'Enrol wallet configuration'; $string['pluginname'] = 'Wallet enrolment'; $string['pluginname_desc'] = ''; $string['privacy:metadata'] = 'The Wallet enrolment plugin does not store any personal data.'; @@ -531,6 +543,7 @@ $string['randomcoupons'] = 'Random Coupons'; $string['receiver'] = 'Receiver'; +$string['referral'] = 'Referral'; $string['referral_amount'] = 'Referral Amount.'; $string['referral_amount_desc'] = 'The gift amount that both referring and referred users will receive in their wallets.'; $string['referral_amount_help'] = 'The gift amount that you and the new user will receive in the wallet.'; @@ -583,7 +596,7 @@ $string['restrictionenabled'] = 'Enable restriction.'; $string['restrictionenabled_desc'] = 'If disabled, not restrictions will be checked.'; $string['restrictions'] = 'Enrolment restrictions'; -$string['restrictions_desc'] = 'Like sections and course modules, now Wallet Enrollments offers an option to add restriction to the enrolment, not all availability plugins tested well, so you can choose from here what works fine and please report ant error so we can improve this functionality.'; +$string['restrictions_desc'] = 'Like sections and course modules, now Wallet Enrollments offers an option to add restriction to the enrolment, not all availability plugins tested well, so you can choose from here what works fine and please report any error so we can improve this functionality.'; $string['role'] = 'Default assigned role'; @@ -616,6 +629,7 @@ $string['topup'] = 'topup'; $string['topupafterdiscount'] = 'Actual payment'; $string['topupafterdiscount_help'] = 'The amount after discount.'; +$string['topupbundle'] = 'Top up your wallet and pay only:'; $string['topupbycoupon'] = 'Using Coupons'; $string['topupbypayment'] = 'Using Payment'; $string['topupbytellerman'] = 'Manually from our side'; @@ -627,6 +641,7 @@ $string['topupvalue'] = 'TopUp value'; $string['topupvalue_help'] = 'Value to topup your wallet by using payment methods'; $string['transactions'] = 'Wallet transactions'; +$string['transactions_details'] = 'More transaction details'; $string['transaction_perpage'] = 'Transactions per page'; $string['transaction_type'] = 'Type of transaction'; $string['transfer'] = 'Transfer balance to other user'; diff --git a/locallib.php b/locallib.php index b9f62b82..3634fc2a 100644 --- a/locallib.php +++ b/locallib.php @@ -406,7 +406,7 @@ function enrol_wallet_process_coupon_data($data) { * @return string */ function enrol_wallet_display_coupon_urls() { - if (get_config('enrol_wallet', 'walletsource') !== balance::MOODLE) { + if (get_config('enrol_wallet', 'walletsource') != balance::MOODLE) { return ''; } $context = context_system::instance(); @@ -420,15 +420,15 @@ function enrol_wallet_display_coupon_urls() { $render .= html_writer::link($url, get_string('coupon_table', 'enrol_wallet')).'
'; $url = new moodle_url('/enrol/wallet/extra/couponusage.php'); - $render .= html_writer::link($url, get_string('coupon_usage', 'enrol_wallet')).'
'; + $render .= html_writer::link($url, get_string('coupon_usage', 'enrol_wallet')); if ($cangeneratecoupon) { $url = new moodle_url('/enrol/wallet/extra/coupon.php'); - $render .= html_writer::link($url, get_string('coupon_generation', 'enrol_wallet')); + $render .= '
' . html_writer::link($url, get_string('coupon_generation', 'enrol_wallet')); } if ($cangeneratecoupon && $caneditcoupon) { $url = new moodle_url('/enrol/wallet/extra/couponupload.php'); - $render .= html_writer::link($url, get_string('upload_coupons', 'enrol_wallet')); + $render .= '
' . html_writer::link($url, get_string('upload_coupons', 'enrol_wallet')); } } return $render; @@ -493,10 +493,13 @@ function enrol_wallet_display_topup_options() { if (enrol_wallet_is_valid_account($account)) { // If the user don't have capability to charge others. // Display options to charge with coupons or other payment methods. + $bundles = enrol_wallet\util\discount_rules::bundles_buttons(); + if (!empty($bundles)) { + $render .= enrol_wallet_topup_option($bundles, get_string('bundle_value', 'enrol_wallet')); + } $topupurl = new moodle_url('/enrol/wallet/extra/topup.php'); $topupform = new \enrol_wallet\form\topup_form($topupurl, $data); - $rules = discount_rules::get_the_discount_line(-1); - $render .= enrol_wallet_topup_option($rules . $topupform->render(), get_string('topupbypayment', 'enrol_wallet')); + $render .= enrol_wallet_topup_option($topupform->render(), get_string('topupbypayment', 'enrol_wallet')); } // Check if fixed coupons enabled. diff --git a/styles.css b/styles.css index d3e38531..2ccd1bfe 100644 --- a/styles.css +++ b/styles.css @@ -13,13 +13,29 @@ font-variant-caps: all-small-caps; text-align: center; } + .enrol-wallet-balance-details { display: flex; align-items: stretch; justify-content: center; - align-content: stretch; + align-content: center; flex-wrap: wrap; gap: 10px; + flex-direction: column; +} + +.enrol-wallet-cat-balance { + border: solid 2px #a3a3a3; + border-radius: 15px; + padding: 5px; + margin: 0 5px; +} + +.enrol-wallet-administration { + display: flex; + flex-direction: column; + align-items: stretch; + justify-content: space-evenly; } .enrol_wallet_offer { @@ -61,11 +77,55 @@ justify-content: center; align-items: center; } - .enrol-wallet-inner-offer { width: max-content; margin: 25%; } +.enrol-wallet-bundle-button { + padding: 0; + height: 100%; + background: linear-gradient(153deg, rgba(255, 203, 203, 1) 5%, rgba(254, 255, 119, 1) 43%, rgba(190, 186, 255, 1) 100%); +} +.enrol-wallet-bundle-button:hover { + background: linear-gradient(333deg, rgba(255, 203, 203, 1) 5%, rgba(254, 255, 119, 1) 43%, rgba(190, 186, 255, 1) 100%); +} +.enrol-wallet-bundles { + display: flex; + flex-direction: row; + flex-wrap: wrap; + justify-content: center; + align-content: center; + align-items: stretch; +} +.enrol-wallet-ribbon { + display: block; + position: relative; + overflow: hidden; + height: 100%; + border: dashed 3px #cb3737; + border-radius: inherit; +} +.enrol-wallet-ribbon .bundle-inner { + padding: 24px 39px 0 10px; + position: relative; + z-index: 1; +} + +.enrol-wallet-ribbon span { + width: 40%; + height: min-content; + top: 5%; + right: -13%; + position: absolute; + display: block; + background: #cb3737; + font-size: 140%; + color: #fff; + text-align: center; + transform: rotate(45deg); + -webkit-transform: rotate(45deg); + -ms-transform: rotate(45deg); +} @media (min-width: 768px) { #page-enrol-wallet-confirm #region-main { @@ -101,37 +161,29 @@ .enrol-wallet-discounts-container .discount-line { display: flex; - position: relative; - flex-direction: row; + flex-direction: row-reverse; flex-wrap: nowrap; align-items: flex-end; height: 12px; - margin-top: 75px; + margin-top: 86px; background-color: white; border-color: black; border-style: outset; border-radius: 15px; } + .enrol-wallet-discounts-container .discount-line .rule-container { - position: absolute; border-left: 1px solid #000; - font-size: 1em; -} -.enrol-wallet-discounts-container .discount-line .rule-container .discount-percent { - font-size: 1.1em; + /* text-wrap: nowrap; */ + border-bottom-style: solid; + border-bottom-width: 7px; } + .enrol-wallet-discounts-container .discount-cat-container { margin-top: 10px; -} - -@media screen and (max-width: 600px) { - .enrol-wallet-discounts-container .discount-line .rule-container .discount-percent { - font-size: 1em; - } - .enrol-wallet-discounts-container .discount-line .rule-container { - max-width: min-content; - font-size: 0.8em; - } + min-width: fit-content; + max-width: 100%; + overflow-x: auto; } .enrol_wallet_topup .accordion .accordion__title { @@ -153,7 +205,6 @@ } .enrol_wallet_topup .accordion .accordion__title:after { - /* font-family: "Font Awesome 5 Free"; */ text-transform: unset; transition: all 0.25s ease; font-weight: 900; @@ -169,7 +220,6 @@ } .enrol_wallet_topup .accordion .accordion__title:has(input:checked):after { - /* font-family: "Font Awesome 5 Free"; */ font-weight: 900; content: '\2B9F'; float: right; diff --git a/templates/discount-line.mustache b/templates/discount-line.mustache index 3c0b265e..e5abcfbb 100644 --- a/templates/discount-line.mustache +++ b/templates/discount-line.mustache @@ -22,22 +22,20 @@ "data": [ { "heading": "Site", + "catid": "0", "discounts": [ { "discount": "15%", "condition": "> 150 EGP", "percent": "12", - "order": "2", "color": "135" }, { "discount": "20%", "condition": "> 250 EGP", "percent": "18", - "order": "3", "color": "200" } - ] } ] @@ -45,22 +43,18 @@ }}
{{# data}} +
{{{heading}}}
{{# discounts}} -
+
{{discount}}

{{condition}}

- {{/ discounts}}
diff --git a/templates/display.mustache b/templates/display.mustache index 70180bde..99a2cc25 100644 --- a/templates/display.mustache +++ b/templates/display.mustache @@ -68,12 +68,14 @@ {{/ hasdetails}} {{# policy}} diff --git a/templates/single-bundle.mustache b/templates/single-bundle.mustache new file mode 100644 index 00000000..17ebdb1d --- /dev/null +++ b/templates/single-bundle.mustache @@ -0,0 +1,55 @@ +{{! + 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 . +}} +{{! + @template enrol_wallet/single-bundle + + TODO describe template single-bundle + + Example context (json): + { + } +}} + +
+
+ {{#params}} + + {{/params}} + + {{#hasenddate}}{{/hasenddate}} +
+
+{{#hasactions}} + {{> core/actions }} +{{/hasactions}} + diff --git a/tests/enrol_wallet_test.php b/tests/enrol_wallet_test.php index ffead5ee..efc2b15e 100644 --- a/tests/enrol_wallet_test.php +++ b/tests/enrol_wallet_test.php @@ -23,9 +23,9 @@ */ namespace enrol_wallet; -use enrol_wallet\transactions; use enrol_wallet\util\balance; use enrol_wallet\util\balance_op; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot.'/enrol/wallet/lib.php'); @@ -210,7 +210,7 @@ public function test_expired(): void { global $DB; $this->resetAfterTest(); - $walletplugin = enrol_get_plugin('wallet'); + $walletplugin = new enrol_wallet_plugin; $this->assertNotEmpty($walletplugin); $manualplugin = enrol_get_plugin('manual'); $this->assertNotEmpty($manualplugin); @@ -708,8 +708,11 @@ public function test_can_self_enrol(): void { $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); - transactions::payment_topup(250, $user1->id); - transactions::payment_topup(250, $user2->id); + $op = new balance_op($user1->id); + $op->credit(250); + + $op = new balance_op($user2->id); + $op->credit(250); $studentrole = $DB->get_record('role', ['shortname' => 'student']); $this->assertNotEmpty($studentrole); @@ -1109,13 +1112,16 @@ public function test_hide_due_cheaper_instance(): void { $walletplugin = new enrol_wallet_plugin; $user1 = $this->getDataGenerator()->create_user(); - transactions::payment_topup(250, $user1->id); + $op = new balance_op($user1->id); + $op->credit(250); $user2 = $this->getDataGenerator()->create_user(); - transactions::payment_topup(50, $user2->id); + $op = new balance_op($user2->id); + $op->credit(50); $user3 = $this->getDataGenerator()->create_user(); - transactions::payment_topup(100, $user3->id); + $op = new balance_op($user3->id); + $op->credit(100); $course = $this->getDataGenerator()->create_course(); @@ -1181,7 +1187,8 @@ public function test_unenrol_user(): void { $context = \context_course::instance($course->id); $user = $this->getDataGenerator()->create_user(); - transactions::payment_topup(100, $user->id); + $op = new balance_op($user->id); + $op->credit(100); $wallet = new enrol_wallet_plugin; // Update the instance such that the enrol duration is 2 hours. @@ -1272,7 +1279,9 @@ public function test_unenrol_user(): void { $this->assertEquals(0, $balance); // Now test the refund fee. - transactions::payment_topup(100, $user->id); + $op = new balance_op($user->id); + $op->credit(100); + $this->setAdminUser(); // Set to 10%. set_config('unenrolrefundfee', 10, 'enrol_wallet'); @@ -1306,7 +1315,8 @@ public function test_get_unenrolself_link(): void { $context = \context_course::instance($course->id); $user = $this->getDataGenerator()->create_user(); - transactions::payment_topup(100, $user->id); + $op = new balance_op($user->id); + $op->credit(100); $wallet = new enrol_wallet_plugin; // Update the instance such that the enrol duration is 2 hours. diff --git a/tests/observer_test.php b/tests/observer_test.php index 4fd420ac..ce625a1b 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -26,7 +26,7 @@ use enrol_wallet\observer; use enrol_wallet\util\balance; use enrol_wallet\util\balance_op; -use enrol_wallet\transactions; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot.'/enrol/wallet/lib.php'); diff --git a/tests/turn_non_refundable_test.php b/tests/turn_non_refundable_test.php index 34023816..cdd78d5c 100644 --- a/tests/turn_non_refundable_test.php +++ b/tests/turn_non_refundable_test.php @@ -23,7 +23,6 @@ */ namespace enrol_wallet; -use enrol_wallet\transactions; use enrol_wallet\task\turn_non_refundable; use enrol_wallet\util\balance; use enrol_wallet\util\balance_op; diff --git a/tests/util/balance_op_test.php b/tests/util/balance_op_test.php index c63beb13..4cc41ec1 100644 --- a/tests/util/balance_op_test.php +++ b/tests/util/balance_op_test.php @@ -946,6 +946,8 @@ public function test_transfer_to_other(): void { $user1 = $gen->create_user(); $user2 = $gen->create_user(); + $cat1 = $gen->create_category(); + $enabled = get_config('enrol_wallet', 'transfer_enabled'); $this->assertEmpty($enabled); @@ -1023,6 +1025,30 @@ public function test_transfer_to_other(): void { // Let's start fresh. $this->reset_balance($user1->id); $this->reset_balance($user2->id); + + set_config('transferpercent', 0, 'enrol_wallet'); + + $op = new balance_op($user1->id, $cat1->id); + $op->credit(500); + + $this->assertEquals(500, $op->get_cat_balance($cat1->id)); + + $data->amount = 150; + $op->transfer_to_other($data); + + $this->assertStringContainsString('Sorry, you have insufficient balance for this operation.', $error); + + $data->category = $cat1->id; + $msg = $op->transfer_to_other($data); + + $balance = new balance($user1->id, $cat1->id); + $this->assertEquals(350, $balance->get_valid_balance(), $msg); + + $balance = new balance($user2->id, $cat1->id); + $this->assertEquals(150, $balance->get_valid_balance()); + $this->assertEquals(0, $balance->get_main_balance()); + $this->assertEquals(150, $balance->get_valid_nonrefundable()); + $this->assertEquals(0, $balance->get_valid_free()); } /** diff --git a/version.php b/version.php index 900388db..3f2bb620 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024021200; +$plugin->version = 2024022500; $plugin->requires = 2020061500; $plugin->component = 'enrol_wallet'; $plugin->release = '5.2.0'; diff --git a/wallet.php b/wallet.php new file mode 100644 index 00000000..2feb666c --- /dev/null +++ b/wallet.php @@ -0,0 +1,161 @@ +. + +/** + * TODO describe file wallet + * + * @package enrol_wallet + * @copyright 2024 2024, Mohammad 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"); + +require_login(null, false); + +$context = context_system::instance(); +$url = new moodle_url('/enrol/wallet/wallet.php'); +$PAGE->set_url($url); +$PAGE->set_context($context); + +$PAGE->set_heading($SITE->fullname); +$PAGE->set_pagelayout('frontpage'); +$PAGE->set_secondary_navigation(true, true); + +$tabnames = [ + 'balance' => get_string('balance', 'enrol_wallet'), + 'topup' => get_string('topup', 'enrol_wallet'), + 'charge' => get_string('charge', 'enrol_wallet'), + 'referral' => get_string('referral', 'enrol_wallet'), + 'transactions' => get_string('transactions', 'enrol_wallet'), + 'transfer' => get_string('transfer', 'enrol_wallet'), + 'offers' => get_string('offers', 'enrol_wallet'), + 'admin' => get_string('administration'), +]; + +if (!has_capability('enrol/wallet:creditdebit', $context)) { + unset($tabnames['charge']); +} + +$tabs = []; +$contents = []; +foreach ($tabnames as $key => $name) { + switch ($key) { + case 'balance': + $text = enrol_wallet_display_current_user_balance(); + break; + case 'topup': + $text = enrol_wallet_display_topup_options(); + break; + case 'charge': + $text = enrol_wallet_display_charger_form(); + break; + case 'transactions': + $transactionurl = new moodle_url('/enrol/wallet/extra/transaction.php'); + $class = ['class' => 'btn btn-primary']; + $text = html_writer::link($transactionurl, get_string('transactions_details', 'enrol_wallet'), $class); + $table = new enrol_wallet\table\transactions($USER->id); + $table->define_baseurl($url->out()."#linktransactions"); + ob_start(); + $table->out(15, true); + $text .= ob_get_clean(); + break; + case 'referral': + if ((bool)get_config( 'enrol_wallet', 'referral_enabled')) { + ob_start(); + enrol_wallet\pages::process_referral_page(); + $text = ob_get_clean(); + } else { + $text = ''; + } + break; + case 'transfer': + ob_start(); + enrol_wallet\pages::process_transfer_page($url->out()."#transfer"); + $text = ob_get_clean(); + break; + case 'offers': + $text = enrol_wallet\pages::get_offers_content(); + break; + case 'admin': + $attributes = ['class' => 'btn btn-secondary']; + $text = ''; + if (has_capability('moodle/site:config', $context)) { + $configurl = new moodle_url('/admin/settings.php', ['section' => 'enrolsettingswallet']); + $text .= html_writer::link($configurl, get_string('pluginconfig', 'enrol_wallet'), $attributes) . '
'; + $configurl = new moodle_url('/enrol/wallet/extra/conditionaldiscount.php'); + $text .= html_writer::link($configurl, get_string('conditionaldiscount_link_desc', 'enrol_wallet'), $attributes); + $text .= '
'; + } + + $coupons = enrol_wallet_display_coupon_urls(); + if (!empty($coupons)) { + $links = explode('
', $coupons); + $class = html_writer::attribute('class', 'btn btn-secondary'); + foreach ($links as $link) { + $text .= str_replace(''; + } + } + + if (has_capability('enrol/wallet:bulkedit', $context)) { + $configurl = new moodle_url('/enrol/wallet/extra/bulkedit.php'); + $text .= html_writer::link($configurl, get_string('bulkeditor', 'enrol_wallet'), $attributes) . '
'; + $configurl = new moodle_url('/enrol/wallet/extra/bulkinstances.php'); + $text .= html_writer::link($configurl, get_string('walletbulk', 'enrol_wallet'), $attributes) . '
'; + } + if (!empty($text)) { + $text = html_writer::div($text, 'enrol-wallet-administration'); + } + break; + default: + $text = ''; + } + + if (!empty($text)) { + $link = '#link' . $key; + $contents[$key] = $text; + $tabitem = html_writer::start_tag('li', ['class' => 'nav-item', 'data-key' => $key]); + $attributes = [ + 'title' => $name, + 'role' => 'tab', + 'class' => 'nav-link', + 'data-toggle' => 'tab', + 'data-text' => $name, + ]; + $tabitem .= html_writer::link($link, $name, $attributes); + $tabitem .= html_writer::end_tag('li'); + $tabs[$key] = $tabitem; + } +} + +echo $OUTPUT->header(); + +echo html_writer::start_tag('ul', ['class' => 'nav nav-tabs', 'role' => 'tablist']); +foreach ($tabs as $id => $tab) { + echo $tab; +} +echo html_writer::end_tag('ul'); + +echo html_writer::start_div('tab-content mt-3'); + +foreach ($contents as $id => $value) { + echo html_writer::div($value, 'tab-pane', ['id' => 'link'.$id, 'role' => 'tabpanel']); +} + +echo html_writer::end_div(); + +echo $OUTPUT->footer();