From 16f1cd48fd8aa5614cdc3b440aea76ffbdfca60b Mon Sep 17 00:00:00 2001
From: Mo Farouk <122031508+fmido88@users.noreply.github.com>
Date: Sun, 4 Feb 2024 20:15:11 +0200
Subject: [PATCH] v3
---
classes/condition.php | 62 +++++++++++++++++++++---------------
classes/privacy/provider.php | 13 +++++---
lib.php | 21 ------------
process.php | 61 ++++++++++++++++++-----------------
version.php | 6 ++--
5 files changed, 79 insertions(+), 84 deletions(-)
diff --git a/classes/condition.php b/classes/condition.php
index fec9812..3478bf5 100644
--- a/classes/condition.php
+++ b/classes/condition.php
@@ -24,7 +24,16 @@
namespace availability_wallet;
-use enrol_wallet\transactions;
+use enrol_wallet\util\balance;
+use enrol_wallet\util\cm;
+use enrol_wallet\util\section;
+use enrol_wallet\coupons;
+use core_availability\info;
+use core_availability\info_module;
+use core_availability\info_section;
+use core_availability\condition as core_condition;
+use enrol_wallet\form\applycoupon_form;
+use moodle_url;
/**
* Wallet condition.
*
@@ -32,7 +41,7 @@
* @copyright 2023 Mohammad Farouk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class condition extends \core_availability\condition {
+class condition extends core_condition {
/** @var float the cost required.*/
protected $cost;
@@ -71,14 +80,14 @@ public static function get_json($cost = 0) {
* according to this availability condition.
*
* @param bool $not Set true if we are inverting the condition
- * @param \core_availability\info $info Item we're checking
+ * @param info $info Item we're checking
* @param bool $grabthelot Performance hint: if true, caches information
* required for all course-modules, to make the front page and similar
* pages work more quickly (works only for current user)
* @param int $userid User ID to check availability for
* @return bool True if available
*/
- public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {
+ public function is_available($not, info $info, $grabthelot, $userid) {
global $DB;
$context = $info->get_context();
@@ -105,7 +114,7 @@ public function is_available($not, \core_availability\info $info, $grabthelot, $
/**
* Check the availability of course module.
* @param int $userid
- * @param \core_availability\info_module $info
+ * @param info_module $info
* @return bool
*/
private function is_cm_available($userid, $info) {
@@ -122,7 +131,7 @@ private function is_cm_available($userid, $info) {
/**
* Check the availability of a section.
* @param int $userid
- * @param \core_availability\info_section $info
+ * @param info_section $info
* @return bool
*/
private function is_section_available($userid, $info) {
@@ -135,7 +144,6 @@ private function is_section_available($userid, $info) {
return $this->is_payment_sufficient($records);
}
-
/**
* Check is payments were sufficient for this thing.
* @param array[object] $records
@@ -155,7 +163,7 @@ private function is_payment_sufficient($records) {
* @param \context $context
* @return array
*/
- private function format_fake_instance($courseid, $someid, $context) {
+ private function create_parameters($courseid, $someid, $context) {
$params = [
'id' => 0,
'cost' => $this->cost,
@@ -179,11 +187,11 @@ private function format_fake_instance($courseid, $someid, $context) {
*
* @param bool $full Set true if this is the 'full information' view
* @param bool $not Set true if we are inverting the condition
- * @param \core_availability\info $info Item we're checking
+ * @param info $info Item we're checking
* @return string Information string (for admin) about all restrictions on
* this item
*/
- public function get_description($full, $not, \core_availability\info $info) {
+ public function get_description($full, $not, info $info) {
global $USER, $DB, $OUTPUT, $CFG;
$context = $info->get_context();
require_once($CFG->dirroot.'/enrol/wallet/locallib.php');
@@ -192,13 +200,18 @@ public function get_description($full, $not, \core_availability\info $info) {
return get_string('invalidcost', 'availability_wallet');
}
- $balance = transactions::get_user_balance($USER->id);
- $someid = ($context->contextlevel === CONTEXT_MODULE) ? $info->get_course_module()->id : $info->get_section()->id;
- $params = $this->format_fake_instance($info->get_course()->id, $someid, $context);
+ if ($context->contextlevel === CONTEXT_MODULE) {
+ $someid = $info->get_course_module()->id;
+ $helper = new cm($someid);
+ } else {
+ $someid = $info->get_section()->id;
+ $helper = new section($someid);
+ }
+ $bal = new balance(0, $helper->get_category_id());
+ $balance = $bal->get_valid_balance();
- $wallet = enrol_get_plugin('wallet');
- $coupon = $wallet->check_discount_coupon();
- $costafter = $wallet->get_cost_after_discount($USER->id, (object)$params, $coupon);
+ $params = $this->create_parameters($info->get_course()->id, $someid, $context);
+ $costafter = $helper->get_cost_after_discount($cost);
$curr = get_config('enrol_wallet', 'currency');
$curr = !empty($curr) ? $curr : '';
@@ -207,7 +220,6 @@ public function get_description($full, $not, \core_availability\info $info) {
$a->cost = $cost . ' ' . $curr;
} else {
$a->cost = "$cost $curr $costafter $curr";
- $params['cost'] = $costafter;
}
$a->balance = "$balance $curr";
@@ -222,23 +234,23 @@ public function get_description($full, $not, \core_availability\info $info) {
}
// Pay button.
$label = get_string('paybuttonlabel', 'availability_wallet');
- $url = new \moodle_url('/availability/condition/wallet/process.php', $params);
+ $url = new moodle_url('/availability/condition/wallet/process.php', $params);
$a->paybutton = $OUTPUT->single_button($url, $label, 'post', ['primary' => true]);
// Coupon form.
- $actionurl = new \moodle_url('/enrol/wallet/extra/action.php');
+ $actionurl = new moodle_url('/enrol/wallet/extra/action.php');
$data = (object)['instance' => (object)$params];
- $couponform = new \enrol_wallet\form\applycoupon_form(null, $data);
+ $couponaction = new moodle_url('/enrol/wallet/extra/coupon_action.php');
+ $couponform = new applycoupon_form($couponaction, $data);
if ($couponform->is_cancelled()) {
- $_SESSION['coupon'] = '';
- unset($coupon);
+ coupons::unset_session_coupon();
}
+
if ($submitteddata = $couponform->get_data()) {
enrol_wallet_process_coupon_data($submitteddata);
}
- ob_start();
- $couponform->display();
- $a->couponform = ob_get_clean();
+
+ $a->couponform = $couponform->render();
if ($not) {
return get_string('eithernotdescription', 'availability_wallet', $a);
diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php
index 9b089f7..523bb35 100644
--- a/classes/privacy/provider.php
+++ b/classes/privacy/provider.php
@@ -15,7 +15,7 @@
// along with Moodle. If not, see .
/**
- * Privacy Subsystem implementation for availability_wallet.
+ * Privacy Subsystem implementation for enrol_wallet.
*
* @package availability_wallet
* @copyright 2023 Mo Farouk
@@ -24,17 +24,20 @@
namespace availability_wallet\privacy;
-use \core_privacy\local\metadata\provider as core_provider;
-use \core_privacy\local\request\data_provider;
+use core_privacy\local\request\approved_contextlist;
+use core_privacy\local\request\approved_userlist;
+use core_privacy\local\request\userlist;
use core_privacy\local\metadata\collection;
/**
- * Privacy Subsystem for availability_wallet implementing null_provider.
+ * Privacy Subsystem for enrol_wallet implementing null_provider.
*
* @copyright 2023 Mo Farouk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class provider implements core_provider, data_provider {
+class provider implements
+ \core_privacy\local\metadata\provider,
+ \core_privacy\local\request\data_provider {
/**
* Returns meta data about this system.
diff --git a/lib.php b/lib.php
index 0aa059b..08dda59 100644
--- a/lib.php
+++ b/lib.php
@@ -22,24 +22,3 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
- /**
- * Check if the cost passed to the process page is the same as the cost
- * defined in at least one of the conditions.
- * @param stdClass $conditions the availability tree.
- * @param float $cost the cost passed to the process page.
- */
-function availability_wallet_check_cost($conditions, $cost) {
-
- foreach ($conditions->c as $child) {
- if (!empty($child->c) && !empty($child->op)) {
- if (availability_wallet_check_cost($child, $cost)) {
- return true;
- }
- } else if ($child->type === 'wallet') {
- if ($cost == $child->cost) {
- return true;
- }
- }
- }
- return false;
-}
diff --git a/process.php b/process.php
index 1b53cd6..85d026e 100644
--- a/process.php
+++ b/process.php
@@ -26,11 +26,16 @@
// Notice and warnings cases a double payments in case of refreshing the page.
set_debugging(DEBUG_NONE);
require_once(__DIR__.'/lib.php');
+global $DB;
+use enrol_wallet\util\cm;
+use enrol_wallet\util\section;
+use enrol_wallet\util\balance_op;
+
$cost = required_param('cost', PARAM_NUMBER);
$courseid = required_param('courseid', PARAM_INT);
$contextid = required_param('contextid', PARAM_INT);
-$cmid = optional_param('cmid', 0, PARAM_INT);
-$sectionid = optional_param('sectionid', 0, PARAM_INT);
+$cmid = optional_param('cmid', null, PARAM_INT);
+$sectionid = optional_param('sectionid', null, PARAM_INT);
$contextlevel = required_param('contextlevel', PARAM_INT);
$context = get_context_info_array($contextid);
@@ -44,17 +49,20 @@
throw new moodle_exception('invalidsesskey');
};
-global $USER, $DB;
-
if (!empty($cmid)) {
- $record = $DB->get_record('course_modules', ['id' => $cmid]);
+ $helper = new cm($cmid);
+} else if (!empty($sectionid)) {
+ $helper = new section($sectionid);
} else {
- $record = $DB->get_record('course_sections', ['id' => $sectionid]);
+ $msg = get_string('noid', 'availability_wallet');
+ redirect($url, $msg, null, 'error');
}
-$conditions = json_decode($record->availability);
+// This function will validate and check the passed $cost if it is really one of the costs...
+// ... in the conditions of the cm or section.
+$costafter = $helper->get_cost_after_discount($cost);
-if (!availability_wallet_check_cost($conditions, $cost)) {
+if (is_null($costafter)) {
$msg = get_string('paymentnotenought', 'availability_wallet');
redirect($url, $msg, null, 'error');
}
@@ -64,45 +72,38 @@
'courseid' => $courseid,
'cmid' => (!empty($cmid)) ? $cmid : null,
'sectionid' => (!empty($sectionid)) ? $sectionid : null,
- 'cost' => $cost,
+ 'cost' => $cost, // The cost before discount.
'timecreated' => time(),
];
-
$DB->insert_record('availability_wallet', $data);
-$wallet = enrol_get_plugin('wallet');
-$coupon = $wallet->check_discount_coupon();
-
-$coursename = get_course($courseid)->fullname;
+$coursename = $helper->get_course()->fullname;
if (!empty($cmid)) {
- list($course, $module) = get_course_and_cm_from_cmid($cmid);
-
- $name = $course->fullname;
+ $module = $helper->cm;
+ $name = $coursename;
$name .= ': ';
$name .= get_string('module', 'availability_wallet');
$name .= '(' . $module->name . ')';
+ $op = balance_op::create_from_cm($cm);
+ $by = balance_op::D_CM_ACCESS;
} else if (!empty($sectionid)) {
- $section = $DB->get_record('course_sections', ['id' => $sectionid]);
- $course = get_course($courseid);
-
- $name = $course->fullname;
+ $section = $helper->section;
+ $name = $coursename;
$name .= ': ';
$name .= get_string('section');
$name .= (!empty($section->name)) ? "($section->name)" : "($section->section)";
-} else {
-
- $msg = get_string('noid', 'availability_wallet');
- redirect($url, $msg, null, 'error');
-}
-
-if (!empty($coupon)) {
- enrol_wallet\transactions::mark_coupon_used($coupon, $USER->id, 0);
+ $op = balance_op::create_from_section($section);
+ $by = balance_op::D_SECTION_ACCESS;
}
$desc = get_string('debitdesc', 'availability_wallet', $name);
-enrol_wallet\transactions::debit($USER->id, $cost, '', '', $desc, $courseid);
+$op->debit($costafter, $by, $cmid ?? $sectionid, $desc);
+
+if (!empty($helper->couponutil->coupon) && $costafter < $cost) {
+ $helper->couponutil->mark_coupon_used();
+}
$msg = get_string('success', 'availability_wallet');
redirect($url, $msg);
diff --git a/version.php b/version.php
index 33dbc32..d9586c2 100644
--- a/version.php
+++ b/version.php
@@ -24,11 +24,11 @@
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2023121423;
+$plugin->version = 2024020300;
$plugin->requires = 2014111000;
$plugin->component = 'availability_wallet';
-$plugin->release = '2.2.0';
+$plugin->release = '3.0.0';
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [
- 'enrol_wallet' => 2023102303,
+ 'enrol_wallet' => 2024020300,
];