Skip to content

Commit

Permalink
compatible to moodle 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Aug 5, 2024
1 parent 372ce26 commit e4d3926
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 10 deletions.
1 change: 1 addition & 0 deletions classes/coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ public function mark_coupon_used() {
'value' => $this->value,
'userid' => $this->userid,
'area' => $this->area,
'areaid' => $this->areaid,
'instanceid' => $instanceid,
'timeused' => time(),
];
Expand Down
93 changes: 93 additions & 0 deletions classes/hooks_callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
// 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 <http://www.gnu.org/licenses/>.

namespace enrol_wallet;
use core\hook\output\before_standard_top_of_body_html_generation;
use core\hook\output\before_footer_html_generation;
use core\hook\navigation\primary_extend;
use pix_icon_fontawesome;

/**
* Class hooks_callbacks
*
* @package enrol_wallet
* @copyright 2024 Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hooks_callbacks {
/**
* Hook callback to inject price on the enrollment icon.
*
* @param \core\hook\output\before_footer_html_generation $hook
* @return void
*/
public static function show_price(before_footer_html_generation $hook) {
$showprice = (bool)get_config('enrol_wallet', 'showprice');
if ($showprice) {
$page = $hook->renderer->get_page();
$page->requires->js_call_amd('enrol_wallet/overlyprice', 'init');
}
}

/**
* Hook callback to display notice about low balance.
*
* @param \core\hook\output\before_standard_top_of_body_html_generation $hook
* @return void
*/
public static function low_balance_warning(before_standard_top_of_body_html_generation $hook) {
// Don't display notice for guests or logged out.
if (!isloggedin() || isguestuser()) {
return;
}

// Check if notice is enabled.
$notice = get_config('enrol_wallet', 'lowbalancenotice');
if (empty($notice)) {
return;
}

// Check the conditions.
$condition = get_config('enrol_wallet', 'noticecondition');

$op = new util\balance();
$balance = $op->get_total_balance();
if ($balance <= (int)$condition) {
// Display the warning.
\core\notification::warning(get_string('lowbalancenotification', 'enrol_wallet', $balance));
}
}

/**
* Add offers to primary navigation.
*
* @param \core\hook\navigation\primary_extend $hook
* @return void
*/
public static function add_offers(primary_extend $hook) {
$enabled = (bool)get_config('enrol_wallet', 'offers_nav');
if (empty($enabled)) {
return;
}

$alt = get_string('offers', 'enrol_wallet');
$pix = new \pix_icon('i/offer', $alt, 'enrol_wallet');
$url = new \moodle_url('/enrol/wallet/extra/offers.php');

$primaryview = $hook->get_primaryview();
$primaryview->add($alt, $url, \navigation_node::TYPE_CUSTOM, null, null, $pix);
}
}
2 changes: 2 additions & 0 deletions classes/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ public static function get_offers_content() {
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();
Expand Down
5 changes: 5 additions & 0 deletions classes/util/offers.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function __construct($instance, $userid = 0) {
global $USER;
$this->userid = $USER->id;
}

if (!empty($instance->customtext3)) {
$this->offers = (array)json_decode($instance->customtext3);
} else {
Expand All @@ -167,6 +168,7 @@ public function get_detailed_offers() {
if (empty($this->offers)) {
return $descriptions;
}

foreach ($this->offers as $key => $offer) {
$formatteddiscount = format_float($offer->discount, 2);
switch($offer->type) {
Expand Down Expand Up @@ -295,6 +297,7 @@ public function get_max_discount() {

return min(1, $max) * 100;
}

/**
* Return the max discount valid for the passed user.
* @return float
Expand Down Expand Up @@ -327,6 +330,7 @@ public function get_sum_discounts() {
}
return min($sum, 100);
}

/**
* Return array with available valid discounts for the passed user.
* @return array[float]
Expand Down Expand Up @@ -438,6 +442,7 @@ private function validate_courses_enrollments_same_cat($offer) {
} else if ($condition == 'all' && count($records) >= count($ids)) {
return true;
}

return false;
}
/**
Expand Down
41 changes: 41 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// 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 <http://www.gnu.org/licenses/>.

/**
* Hook callbacks for Wallet enrolment
*
* @package enrol_wallet
* @copyright 2024 Mohammad Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$callbacks = [
[
'hook' => core\hook\output\before_standard_top_of_body_html_generation::class,
'callback' => [enrol_wallet\hooks_callbacks::class, 'low_balance_warning'],
'priority' => 500,
],
[
'hook' => \core\hook\output\before_footer_html_generation::class,
'callback' => [enrol_wallet\hooks_callbacks::class, 'show_price'],
],
[
'hook' => core\hook\navigation\primary_extend::class,
'callback' => [enrol_wallet\hooks_callbacks::class, 'add_offers'],
],
];
5 changes: 4 additions & 1 deletion extendlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use enrol_wallet\util\balance;

defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* To add the category and node information into the my profile page.
* If is a regular user, it show the balance, refund policy and topping up options.
Expand Down Expand Up @@ -403,6 +404,7 @@ function enrol_wallet_post_change_password_requests($data) {
return enrol_wallet_update_wordpress_user($user);
}


/**
* Callback function in every page to notify users for low balance.
* @return void
Expand Down Expand Up @@ -436,6 +438,7 @@ function enrol_wallet_before_standard_top_of_body_html() {
}
}


/**
* Callback to login the user to wordpress.
* @return void
Expand Down
1 change: 1 addition & 0 deletions lang/en/enrol_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
$string['offers'] = 'Offers';
$string['offers_ce_desc'] = '{$a->discount}% DISCOUNT if you are enrolled in {$a->condition} of these courses:<br> {$a->courses}';
$string['offers_course_enrol_based'] = 'Another course enrolment based offer';
$string['offers_desc'] = 'Offers and Free courses';
$string['offers_error_ce'] = 'Please select at least one course';
$string['offers_error_discountvalue'] = 'Invalid discount value.';
$string['offers_error_ncnumber'] = 'Please choose number of courses.';
Expand Down
19 changes: 16 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1541,19 +1541,22 @@ function restrictByCourse() {
* @param context $context
*/
protected function include_availability($instance, $mform, $context) {
global $CFG;
global $CFG, $OUTPUT;
if (empty($this->get_config('restrictionenabled')) || empty($this->get_config('availability_plugins'))) {
return;
}

if (!$course = self::get_course_by_instance_id($instance->id)) {
$courseid = optional_param('courseid', null, PARAM_INT);
if (!empty($courseid) && $courseid != SITEID) {
$course = get_course($courseid);
}
}

if (empty($course)) {
return;
}

$courses = [$course->id => $course];
if (!empty($instance->customchar3)) {
$coursesids = explode(',', $instance->customchar3);
Expand All @@ -1569,13 +1572,23 @@ protected function include_availability($instance, $mform, $context) {
$mform->addElement('static', 'availabilitynotice',
get_string('notice'),
get_string('availability_form_desc', 'enrol_wallet'));

// Availability field. This is just a textarea; the user interface
// interaction is all implemented in JavaScript. The field is named
// availabilityconditionsjson for consistency with moodleform_mod.
$mform->addElement('textarea', 'availabilityconditionsjson',
$json = $mform->addElement('textarea', 'availabilityconditionsjson',
get_string('accessrestrictions', 'availability'));
frontend::include_availability_javascript($course, $courses);

if ($CFG->version >= 2023100900) { // Version 403.
$json->updateAttributes(['class' => 'd-none']);
// Availability loading indicator.
$loadingcontainer = $OUTPUT->container($OUTPUT->render_from_template('core/loading', []),
'd-flex justify-content-center py-5 icon-size-5',
'availabilityconditions-loading');
$mform->addElement('html', $loadingcontainer);
}

frontend::include_availability_javascript($course, $courses);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions pix/offer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 15 additions & 6 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,23 @@
get_string('frontpageoffers', 'enrol_wallet'),
get_string('frontpageoffers_desc', 'enrol_wallet'), 0));

if ($PAGE->has_set_url()) {
$return = $PAGE->url->out();
if ((int)$CFG->branch < 404) {
if ($PAGE->has_set_url()) {
$return = $PAGE->url->out();
} else {
$return = (new moodle_url('/admin/settings.php', ['section' => 'enrolsettingswallet']))->out();
}
$url = new moodle_url('/enrol/wallet/extra/offers_nav.php', ['return' => $return]);
$button = html_writer::link($url, get_string('offersnav', 'enrol_wallet'), ['class' => 'btn btn-secondary']);
$settings->add(new admin_setting_description('enrol_wallet/offers_nav',
get_string('offersnav_desc', 'enrol_wallet'), $button));
} else {
$return = (new moodle_url('/admin/settings.php', ['section' => 'enrolsettingswallet']))->out();
$settings->add(new admin_setting_configcheckbox('enrol_wallet/offers_nav',
get_string('offersnav', 'enrol_wallet'),
get_string('offersnav', 'enrol_wallet'),
'0'));
}
$url = new moodle_url('/enrol/wallet/extra/offers_nav.php', ['return' => $return]);
$button = html_writer::link($url, get_string('offersnav', 'enrol_wallet'), ['class' => 'btn btn-secondary']);
$settings->add(new admin_setting_description('enrol_wallet/offers_nav', get_string('offersnav_desc', 'enrol_wallet'), $button));


$behaviors = [
instance::B_SEQ => get_string('discount_behavior_sequential', 'enrol_wallet'),
Expand Down
5 changes: 5 additions & 0 deletions wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
require_login(null, false);

$context = context_system::instance();
$frontpagecontext = context_course::instance(SITEID);

$url = new moodle_url('/enrol/wallet/wallet.php');
$PAGE->set_url($url);
$PAGE->set_context($context);
Expand Down Expand Up @@ -97,6 +99,9 @@
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) . '<hr>';
}

if (has_all_capabilities(['enrol/wallet:config', 'enrol/wallet:manage'] , $frontpagecontext)) {
$configurl = new moodle_url('/enrol/wallet/extra/conditionaldiscount.php');
$text .= html_writer::link($configurl, get_string('conditionaldiscount_link_desc', 'enrol_wallet'), $attributes);
$text .= '<hr>';
Expand Down

0 comments on commit e4d3926

Please sign in to comment.