Skip to content

Commit

Permalink
V 5.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Jun 23, 2024
1 parent f27d3f2 commit 18cf2e7
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Wallet Enrollment for Moodle #
==========
## V 5.4.5 ##
- Add more to operation logging.
- Fix logic in completion award.

## V 5.4.1 ##
- Fix balance operation bug.

Expand Down
14 changes: 13 additions & 1 deletion classes/coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ public function mark_coupon_used() {
'type' => $this->get_key($this->type, self::TYPES),
'value' => $this->value,
'userid' => $this->userid,
'area' => $this->area,
'instanceid' => $instanceid,
'timeused' => time(),
];
Expand All @@ -787,11 +788,22 @@ public function mark_coupon_used() {
];

if (!empty($instanceid)) {

$instance = $DB->get_record('enrol', ['enrol' => 'wallet', 'id' => $instanceid], '*', MUST_EXIST);

$eventdata['courseid'] = $instance->courseid;
$eventdata['context'] = \context_course::instance($instance->courseid);

} else if ($this->area == self::AREA_CM) {

$eventdata['context'] = \context_module::instance($this->areaid);

} else if ($this->area == self::AREA_SECTION) {

$courseid = $DB->get_field('course_sections', 'course', ['id' => $this->areaid]);
$eventdata['courseid'] = $courseid;
$eventdata['context'] = \context_course::instance($courseid);

} else {

$eventdata['context'] = \context_system::instance();
Expand Down Expand Up @@ -824,7 +836,7 @@ public function get_user_use() {
}
global $DB;
$count = $DB->count_records('enrol_wallet_coupons_usage', [
'code' => $this->code,
'code' => $this->code,
'userid' => $this->userid,
]);
return $count;
Expand Down
7 changes: 5 additions & 2 deletions classes/form/coupons_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/** Enrollment form Appear when the user's balance is sufficient for enrollment.
/**
* Coupon Generation form.
*
* @package enrol_wallet
* @copyright 2023 Mo Farouk <[email protected]>
Expand All @@ -27,8 +28,10 @@

require_once($CFG->libdir.'/formslib.php');

/** Enrollment form.
/**
* Coupon Generation form.
*
* @package enrol_wallet
*/
class coupons_generator extends \moodleform {

Expand Down
6 changes: 3 additions & 3 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function wallet_completion_awards(\core\event\course_completed $ev

$percentage = ($usergrade / $maxgrade) * 100;

// Getting the enrol wallet instance in the course (there is only one because multiple isn't allowed).
// Getting the enrol wallet instance in the course.
$instances = enrol_get_instances($courseid, true);
$instance = null;
$ta = 0; // Estimated Total award (Just for check and not used again).
Expand All @@ -80,10 +80,10 @@ public static function wallet_completion_awards(\core\event\course_completed $ev
if ($inst->enrol === 'wallet' // Wallet enrollments only.
&& !empty($inst->customint8) // Awards enabled.
&& $inst->customdec1 <= $percentage // Condition for award applied.
&& $inst->customdec2 * (100 - $percentage) > $ta // Maximum award available.
&& $inst->customdec2 * ($percentage - $inst->customdec1) > $ta // Maximum award available.
) {
$instance = $inst;
$ta = $inst->customdec2 * (100 - $percentage);
$ta = $inst->customdec2 * ($percentage - $inst->customdec1);
}
}

Expand Down
24 changes: 20 additions & 4 deletions classes/util/balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ public function __construct($userid = 0, $category = 0) {
$this->set_details_from_cache();
}

/**
* Return the valid balance as string.
* @return string
*/
public function __toString() {
return format_float($this->get_valid_balance(), 2);
}

/**
* Set the main balance of the given user.
* this is called in case of using wordpress as source
Expand Down Expand Up @@ -279,9 +287,9 @@ private function set_balance_details() {
// Main.
$details = [
'mainrefundable' => $record->refundable,
'mainnonrefund' => $record->nonrefundable,
'mainbalance' => $record->refundable + $record->nonrefundable,
'mainfree' => $record->freegift ?? 0,
'mainnonrefund' => $record->nonrefundable,
'mainbalance' => $record->refundable + $record->nonrefundable,
'mainfree' => $record->freegift ?? 0,
];

// The id of the record to be saved in the cache.
Expand Down Expand Up @@ -424,7 +432,7 @@ protected function reset() {

/**
* Return array of the balance details
* keys area mainrefundable, mainnonrefund, mainbalance, total, total_nonrefundable, total_refundable
* keys are mainrefundable, mainnonrefund, mainbalance, total, total_nonrefundable, total_refundable
* And catbalance, the last one is an array of objects keyed by category id, each object with keys as following:
* refundable, nonrefundable, balance.
* @return array
Expand Down Expand Up @@ -496,6 +504,7 @@ public function get_main_refundable() {
}
return $this->details['mainrefundable'] ?? 0;
}

/**
* Get the valid balance to be used in the specified category,
* this includes the sum of balance in this category, parents and main balance.
Expand All @@ -519,12 +528,15 @@ public function get_valid_nonrefundable() {
if (!$this->catenabled) {
return $this->get_total_nonrefundable();
}

$nonrefundable = $this->details['mainnonrefund'];
if (!empty($this->catop)) {
$nonrefundable += @$this->catop->get_non_refundable_balance() ?? 0;
}

return $nonrefundable;
}

/**
* Return the main free balance due to gifts and so.
* @return float
Expand All @@ -548,6 +560,7 @@ public function get_total_free() {
}
return $total;
}

/**
* Get the free balance in main and category passed in construction
* @return float
Expand All @@ -562,6 +575,7 @@ public function get_valid_free() {
}
return $valid;
}

/**
* Return a balance for certain category.
* @param int $catid
Expand All @@ -588,6 +602,7 @@ public static function create_from_instance($instance, $userid = 0) {
$category = $util->get_course_category();
return new self($userid, $category);
}

/**
* Create a balance util helper class to obtain balance data of a given user
* by providing the course module record or its id.
Expand All @@ -600,6 +615,7 @@ public static function create_from_cm($cm, $userid = 0) {
$category = $util->get_course_category();
return new self($userid, $category);
}

/**
* Create a balance util helper class to obtain balance data of a given user
* by providing the section record or its id.
Expand Down
4 changes: 4 additions & 0 deletions classes/util/balance_op.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ public function debit($amount, $for = self::USER, $thingid = 0, $desc = '', $neg
'balance' => $newbalance,
'norefund' => $newnonrefund,
'category' => $this->catid ?? 0,
'opby' => $for,
'thingid' => $thingid,
'descripe' => $description,
'timecreated' => time(),
];
Expand Down Expand Up @@ -523,6 +525,8 @@ public function credit($amount, $by = self::OTHER, $thingid = 0, $desc = '', $re
'balance' => $newbalance,
'norefund' => $newnonrefund,
'category' => $this->catid ?? 0,
'opby' => $by,
'thingid' => $thingid,
'descripe' => $description,
'timecreated' => time(),
];
Expand Down
1 change: 1 addition & 0 deletions confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
$params = [
'instance' => $instanceid,
'confirm' => $confirm,
'id' => $courseid,
];

$pageurl = new moodle_url('/enrol/wallet/confirm.php', $params);
Expand Down
5 changes: 4 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="enrol/wallet/db" VERSION="20240616" COMMENT="XMLDB file for Moodle enrol/wallet"
<XMLDB PATH="enrol/wallet/db" VERSION="20240622" COMMENT="XMLDB file for Moodle enrol/wallet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -42,6 +42,8 @@
<FIELD NAME="balbefore" TYPE="number" LENGTH="25" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="5"/>
<FIELD NAME="balance" TYPE="number" LENGTH="25" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="5"/>
<FIELD NAME="norefund" TYPE="number" LENGTH="25" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="5"/>
<FIELD NAME="opby" TYPE="char" LENGTH="25" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="thingid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="category" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="The category id if the transaction done for a category balance"/>
<FIELD NAME="descripe" TYPE="char" LENGTH="1333" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand Down Expand Up @@ -78,6 +80,7 @@
<FIELD NAME="type" TYPE="char" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="value" TYPE="number" LENGTH="25" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="5"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="area" TYPE="int" LENGTH="5" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="instanceid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timeused" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
Expand Down
31 changes: 31 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,5 +462,36 @@ function xmldb_enrol_wallet_upgrade($oldversion) {
// Wallet savepoint reached.
upgrade_plugin_savepoint(true, 2024061600, 'enrol', 'wallet');
}

if ($oldversion < 2024062300) {

// Define field opby to be added to enrol_wallet_transactions.
$table = new xmldb_table('enrol_wallet_transactions');
$field = new xmldb_field('opby', XMLDB_TYPE_CHAR, '25', null, null, null, null, 'norefund');

// Conditionally launch add field opby.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define field thingid to be added to enrol_wallet_transactions.
$field = new xmldb_field('thingid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'opby');

// Conditionally launch add field thingid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field area to be added to enrol_wallet_coupons_usage.
$table = new xmldb_table('enrol_wallet_coupons_usage');
$field = new xmldb_field('area', XMLDB_TYPE_INTEGER, '5', null, null, null, null, 'userid');

// Conditionally launch add field area.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Wallet savepoint reached.
upgrade_plugin_savepoint(true, 2024062300, 'enrol', 'wallet');
}
return true;
}
40 changes: 35 additions & 5 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
*/
namespace enrol_wallet;

use enrol_wallet\observer;
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');
use enrol_wallet_plugin;

/**
* Wallet enrolment tests.
*
Expand Down Expand Up @@ -73,6 +72,34 @@ public function test_wallet_completion_awards(): void {
$DB->update_record('enrol', $instance1);
$walletplugin->update_status($instance1, ENROL_INSTANCE_ENABLED);

// Adding another instances to see which reward the user gets.
$instance2id = $walletplugin->add_default_instance($course1);
$instance2 = $DB->get_record('enrol', ['id' => $instance2id], '*', MUST_EXIST);
$instance2->customint6 = 1;
// Enable awarding.
$instance2->customint8 = 1;
// Award condition.
$instance2->customdec1 = 40;
// Credit per each mark above condition.
$instance2->customdec2 = 0.2;
$instance2->cost = 150;
$DB->update_record('enrol', $instance2);
$walletplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);

$instance3id = $walletplugin->add_default_instance($course1);
$instance3 = $DB->get_record('enrol', ['id' => $instance3id], '*', MUST_EXIST);
$instance3->customint6 = 1;
// Enable awarding.
$instance3->customint8 = 1;
// Award condition.
$instance3->customdec1 = 95;
// Credit per each mark above condition.
$instance3->customdec2 = 5;
$instance3->cost = 100;
$DB->update_record('enrol', $instance3);
$walletplugin->update_status($instance3, ENROL_INSTANCE_ENABLED);

// Let's start.
$walletplugin->enrol_self($instance1, $user1);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');

Expand All @@ -82,9 +109,9 @@ public function test_wallet_completion_awards(): void {
$this->assertEquals(0, $DB->count_records('enrol_wallet_awards'));

$cm = $this->course_completion_init($course1);
$this->course_completion_trigger($cm, $user1, $course1, 90);

$this->course_completion_trigger($cm, $user2, $course1, 40);
$this->course_completion_trigger($cm, $user1, $course1, 90);
$this->course_completion_trigger($cm, $user2, $course1, 35);

// The event should be triggered and caught by our observer.
$balance = new balance($user1->id, $course1->category);
Expand All @@ -100,21 +127,24 @@ public function test_wallet_completion_awards(): void {
$this->course_completion_trigger($cm, $user1, $course1, 90);
$balance = new balance($user1->id, $course1->category);
$this->assertEquals(70, $balance->get_valid_balance());

$this->assertEquals(1, $DB->count_records('enrol_wallet_awards'));
}

/**
* Add assignment with completion to a course.
* @param object $course
* @param int $maxgrade
* @return \stdClass
*/
public function course_completion_init($course) {
public function course_completion_init($course, $maxgrade = 100) {
// Make an assignment.
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$params = [
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
'completionusegrade' => 1,
'grade' => $maxgrade,
];
$assign = $assigngenerator->create_instance($params);

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024061800;
$plugin->version = 2024062300;
$plugin->requires = 2020061500;
$plugin->component = 'enrol_wallet';
$plugin->release = '5.4.1';
$plugin->release = '5.4.5';
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [
'enrol_manual' => ANY_VERSION,
Expand Down

0 comments on commit 18cf2e7

Please sign in to comment.