Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Feb 4, 2024
1 parent 04714d6 commit 16f1cd4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 84 deletions.
62 changes: 37 additions & 25 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@

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.
*
* @package availability_wallet
* @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;

Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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');
Expand All @@ -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 : '';
Expand All @@ -207,7 +220,6 @@ public function get_description($full, $not, \core_availability\info $info) {
$a->cost = $cost . ' ' . $curr;
} else {
$a->cost = "<del>$cost $curr</del> $costafter $curr";
$params['cost'] = $costafter;
}

$a->balance = "$balance $curr";
Expand All @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Privacy Subsystem implementation for availability_wallet.
* Privacy Subsystem implementation for enrol_wallet.
*
* @package availability_wallet
* @copyright 2023 Mo Farouk <[email protected]>
Expand All @@ -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 <[email protected]>
* @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.
Expand Down
21 changes: 0 additions & 21 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
61 changes: 31 additions & 30 deletions process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
}
Expand All @@ -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);
6 changes: 3 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

0 comments on commit 16f1cd4

Please sign in to comment.