From 460a093126fef6ee5254967640ff0e67c92b17e0 Mon Sep 17 00:00:00 2001 From: Mohammad Farouk Date: Wed, 7 Aug 2024 11:10:55 +0300 Subject: [PATCH] Prevent exceptions from missing categories. --- CHANGELOG.md | 10 ++++++++++ classes/category/helper.php | 20 +++++++++++++------ classes/coupons.php | 2 ++ classes/form/transfer_form.php | 2 +- classes/table/transactions.php | 4 ++-- classes/util/cm.php | 2 +- classes/util/discount_rules.php | 14 +++++++++++--- classes/util/instance.php | 12 ++++++++++-- classes/util/offers.php | 34 ++++++++++++++++++++++++++------- extra/bulkedit.php | 9 +++++++-- extra/bulkinstances.php | 16 +++++++--------- extra/conditionaldiscount.php | 4 ++-- extra/coupontable.php | 2 +- extra/couponusage.php | 2 +- lang/ar/enrol_wallet.php | 1 - lang/en/enrol_wallet.php | 3 +-- lib.php | 14 +++++--------- version.php | 4 ++-- 18 files changed, 104 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 221d047f..214ca956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Wallet Enrollment for Moodle # ========== +## V 5.6.0 ## +- Fix free cut calculations. +- Add static cashes for discount calculation. +- Fix deleted or hidden category balance error. +- Skip confirm and transaction record for free instances. +- Replace deprecated function mb_convert_encoding(). + +## V 5.5.0 ## +- Changes for moodle 4.3 and 4.4 compatibility. + ## V 5.4.6 ## - Fix discount line bug when no discount available. diff --git a/classes/category/helper.php b/classes/category/helper.php index b23e0e84..71e615a3 100644 --- a/classes/category/helper.php +++ b/classes/category/helper.php @@ -42,7 +42,7 @@ class helper { /** * @var array[int] the parents ids. */ - protected $parents; + protected $parents = []; /** * Create a category helper object. * @@ -52,18 +52,20 @@ public function __construct($categoryorid) { if (is_number($categoryorid)) { $this->catid = $categoryorid; - $this->category = core_course_category::get($categoryorid); + $this->category = core_course_category::get($categoryorid, IGNORE_MISSING, true); } else if ($categoryorid instanceof core_course_category) { $this->catid = $categoryorid->id; $this->category = $categoryorid; } else if (is_object($categoryorid)) { $this->catid = $categoryorid->id; - $this->category = core_course_category::get($categoryorid->id); + $this->category = core_course_category::get($categoryorid->id, IGNORE_MISSING, true); } - $this->parents = $this->category->get_parents(); - // Include the catid with the parents array for easy search. - $this->parents[$this->catid] = $this->catid; + if (!empty($this->category)) { + $this->parents = $this->category->get_parents(); + // Include the catid with the parents array for easy search. + $this->parents[$this->catid] = $this->catid; + } } /** @@ -95,10 +97,16 @@ public function is_belong_to_this($catid) { if ($catid == $this->catid) { return true; } + + if (empty($this->category)) { + return false; + } + $ids = $this->category->get_all_children_ids(); if (in_array($catid, $ids)) { return true; } + return false; } diff --git a/classes/coupons.php b/classes/coupons.php index 4fca0379..5b2c7d00 100644 --- a/classes/coupons.php +++ b/classes/coupons.php @@ -898,6 +898,7 @@ public static function check_discount_coupon() { */ public static function set_session_coupon($code) { $_SESSION['coupon'] = $code; + util\instance::reset_static_cache(); } /** @@ -906,5 +907,6 @@ public static function set_session_coupon($code) { public static function unset_session_coupon() { $_SESSION['coupon'] = null; unset($_SESSION['coupon']); + util\instance::reset_static_cache(); } } diff --git a/classes/form/transfer_form.php b/classes/form/transfer_form.php index af7216d8..128394fc 100644 --- a/classes/form/transfer_form.php +++ b/classes/form/transfer_form.php @@ -96,7 +96,7 @@ protected function definition() { if (!empty($details['catbalance'])) { $options = [0 => get_string('site')]; foreach ($details['catbalance'] as $id => $obj) { - $category = core_course_category::get($id, IGNORE_MISSING); + $category = core_course_category::get($id, IGNORE_MISSING, true); if (!empty($category)) { $name = $category->get_nested_name(false); $catbalance = $obj->balance ?? $obj->refundable + $obj->nonrefundable; diff --git a/classes/table/transactions.php b/classes/table/transactions.php index ef3a5b92..6bfe98c9 100644 --- a/classes/table/transactions.php +++ b/classes/table/transactions.php @@ -230,10 +230,10 @@ protected function col_timecreated($record) { */ protected function col_category($record) { if (!empty($record->category)) { - if ($category = \core_course_category::get($record->category, IGNORE_MISSING)) { + if ($category = \core_course_category::get($record->category, IGNORE_MISSING, true)) { return $category->get_nested_name(false); } - return get_string('deleted'); + return get_string('unknowncategory'); } return get_string('site'); } diff --git a/classes/util/cm.php b/classes/util/cm.php index 4846fee1..079fb346 100644 --- a/classes/util/cm.php +++ b/classes/util/cm.php @@ -127,7 +127,7 @@ public function get_course() { public function get_course_category() { $catid = $this->get_category_id(); if (!empty($catid)) { - return core_course_category::get($catid); + return core_course_category::get($catid, IGNORE_MISSING, true); } return null; } diff --git a/classes/util/discount_rules.php b/classes/util/discount_rules.php index 7d903fcf..3c11150a 100644 --- a/classes/util/discount_rules.php +++ b/classes/util/discount_rules.php @@ -46,7 +46,10 @@ public static function get_current_discount_rules($catid = 0) { ]; $select = '(timefrom <= :time1 OR timefrom = 0) AND (timeto >= :time2 OR timeto = 0)'; if (!empty($catid)) { - $all = core_course_category::get($catid)->get_parents(); + $all = []; + if ($category = core_course_category::get($catid, IGNORE_MISSING, true)) { + $all = $category->get_parents(); + } $all[] = $catid; list($catin, $catparams) = $DB->get_in_or_equal($all, SQL_PARAMS_NAMED); $select .= " AND category $catin"; @@ -262,8 +265,13 @@ public static function get_the_discount_line($catid = 0) { if (empty($catid)) { $name = get_string('site'); } else { - $category = core_course_category::get($catid); - $name = $category->get_nested_name(false); + $category = core_course_category::get($catid, IGNORE_MISSING); + if ($category) { + $name = $category->get_nested_name(false); + } else { + // Don't display hidden or deleted categories. + continue; + } } $data->data[$catid]->heading = $OUTPUT->heading($name, 4); $prevwidth = 0; diff --git a/classes/util/instance.php b/classes/util/instance.php index 0cc5999c..779d5143 100644 --- a/classes/util/instance.php +++ b/classes/util/instance.php @@ -128,12 +128,16 @@ public function __construct($instanceorid, $userid = 0) { $this->userid = $userid; } - $this->behavior = (int)get_config('enrol_wallet', 'discount_behavior'); $this->calculate_cost_after_discount(); $this->set_static_cache(); } + /** + * Set static values to prevent recalculating the discounts + * for multiple callings. + * @return void + */ private function set_static_cache() { $cache = new \stdClass; $cache->costafter = $this->costafter; @@ -141,6 +145,10 @@ private function set_static_cache() { self::$cached[$this->id . '-' . $this->userid] = $cache; } + /** + * Reset static values. + * @return void + */ public static function reset_static_cache() { self::$cached = []; } @@ -178,7 +186,7 @@ public function get_course() { */ public function get_course_category() { $catid = $this->get_category_id(); - return core_course_category::get($catid); + return core_course_category::get($catid, IGNORE_MISSING, true, $this->userid); } /** diff --git a/classes/util/offers.php b/classes/util/offers.php index e1956075..da344424 100644 --- a/classes/util/offers.php +++ b/classes/util/offers.php @@ -182,7 +182,10 @@ public function get_detailed_offers() { break; case self::COURSE_ENROL_COUNT: $course = get_course($this->instance->courseid); - $category = core_course_category::get($course->category); + $category = core_course_category::get($course->category, IGNORE_MISSING, false, $this->userid); + if (!$category) { + continue 2; + } $a = [ 'catname' => $category->get_nested_name(), 'number' => $offer->number ?? $offer->courses, @@ -191,7 +194,10 @@ public function get_detailed_offers() { $descriptions[$key] = get_string('offers_nc_desc', 'enrol_wallet', $a); break; case self::OTHER_CATEGORY_COURSES: - $category = core_course_category::get($offer->cat); + $category = core_course_category::get($offer->cat, IGNORE_MISSING, false, $this->userid); + if (!$category) { + continue 2; + } $a = [ 'catname' => $category->get_nested_name(), 'number' => $offer->number ?? $offer->courses, @@ -460,7 +466,11 @@ private function validate_category_enrol_count($offer, $catid) { return false; } $ids = [$catid]; - $category = core_course_category::get($catid); + $category = core_course_category::get($catid, IGNORE_MISSING, false, $this->userid); + if (!$category) { + return false; + } + $ids = array_merge($ids, $category->get_all_children_ids()); list($in, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); @@ -478,6 +488,7 @@ private function validate_category_enrol_count($offer, $catid) { if (count($records) >= $number) { return true; } + return false; } /** @@ -719,7 +730,10 @@ protected static function add_elements_for_otherc(&$mform, $inc, $courseid) { */ protected static function add_elements_for_nc(&$mform, $inc, $courseid) { $thiscourse = get_course($courseid); - $category = core_course_category::get($thiscourse->category); + $category = core_course_category::get($thiscourse->category, IGNORE_MISSING); + if (!$category) { + return; + } $count = $category->get_courses_count(['recursive' => true]); $options = ['' => get_string('choosedots')]; for ($i = 1; $i <= $count; $i++) { @@ -738,7 +752,10 @@ protected static function add_elements_for_nc(&$mform, $inc, $courseid) { */ protected static function add_elements_for_ce(&$mform, $i, $courseid) { $thiscourse = get_course($courseid); - $category = core_course_category::get($thiscourse->category); + $category = core_course_category::get($thiscourse->category, IGNORE_MISSING); + if (!$category) { + return; + } $courses = $category->get_courses(['recursive' => true]); $options = []; foreach ($courses as $course) { @@ -1021,8 +1038,11 @@ public static function get_courses_with_offers($categoryid = 0) { 'wallet' => 'wallet', ]; if (!empty($categoryid)) { - $category = core_course_category::get($categoryid); - $catids = $category->get_all_children_ids(); + $category = core_course_category::get($categoryid, IGNORE_MISSING); + $catids = []; + if ($category) { + $catids = $category->get_all_children_ids(); + } $catids[] = $categoryid; list($in, $inparams) = $DB->get_in_or_equal($catids, SQL_PARAMS_NAMED); $sql .= " AND c.category $in"; diff --git a/extra/bulkedit.php b/extra/bulkedit.php index db5d9cc1..b0ddd783 100644 --- a/extra/bulkedit.php +++ b/extra/bulkedit.php @@ -45,14 +45,19 @@ $form = new MoodleQuickForm('courses', 'post', 'bulkedit_action.php'); // Prepare the course selector. -$courses = get_courses(); +$courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.fullname'); foreach ($courses as $course) { if ($course->id == 1) { continue; } - $categoryname = core_course_category::get($course->category)->get_nested_name(); + $category = core_course_category::get($course->category, IGNORE_MISSING, true); + if (!$category) { + continue; + } + + $categoryname = $category->get_nested_name(); $options[$course->id] = $categoryname . ': ' . $course->fullname; } diff --git a/extra/bulkinstances.php b/extra/bulkinstances.php index 4e40f566..317f7183 100644 --- a/extra/bulkinstances.php +++ b/extra/bulkinstances.php @@ -45,22 +45,20 @@ $mform = new MoodleQuickForm('bulkinstances_edit', 'post', 'bulkinstances_action.php'); // Prepare the course selector. -$courses = get_courses(); +$courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.fullname'); foreach ($courses as $course) { if ($course->id == 1) { continue; } - $category = core_course_category::get($course->category); - $parentname = $category->name.': '; - - while ($category->parent > 0) { - $parent = core_course_category::get($category->parent); - $parentname = $parent->name . ': ' . $parentname; - $category = $parent; + $category = core_course_category::get($course->category, IGNORE_MISSING, true); + if (!$category) { + continue; } - $options[$course->id] = $parentname.$course->fullname; + $catname = $category->get_nested_name(false, ':'); + + $options[$course->id] = $catname . $course->fullname; } $select = $mform->addElement('select', 'courses', get_string('courses'), $options); diff --git a/extra/conditionaldiscount.php b/extra/conditionaldiscount.php index 6631db1e..9b690d33 100644 --- a/extra/conditionaldiscount.php +++ b/extra/conditionaldiscount.php @@ -157,10 +157,10 @@ if ($category = core_course_category::get($record->category, IGNORE_MISSING)) { $catname = $category->get_nested_name(false); } else { - $catname = get_string('deleted'); + $catname = get_string('unknowncategory'); } } else { - $catname = $SITE->fullname; + $catname = format_string($SITE->fullname); } $row = [ diff --git a/extra/coupontable.php b/extra/coupontable.php index 40b8730f..db382adf 100644 --- a/extra/coupontable.php +++ b/extra/coupontable.php @@ -521,7 +521,7 @@ $chkbox = ($candelete) ? 'category)) { - if ($category = core_course_category::get($record->category, IGNORE_MISSING)) { + if ($category = core_course_category::get($record->category, IGNORE_MISSING, true)) { $category = $category->get_nested_name(false); } } diff --git a/extra/couponusage.php b/extra/couponusage.php index c3e7d97b..44b009ea 100644 --- a/extra/couponusage.php +++ b/extra/couponusage.php @@ -495,7 +495,7 @@ foreach ($records as $record) { if (!empty($record->category)) { - if ($category = core_course_category::get($record->category, IGNORE_MISSING)) { + if ($category = core_course_category::get($record->category, IGNORE_MISSING, true)) { $category = $category->get_nested_name(false); } } diff --git a/lang/ar/enrol_wallet.php b/lang/ar/enrol_wallet.php index 85a770ea..ddec9aff 100644 --- a/lang/ar/enrol_wallet.php +++ b/lang/ar/enrol_wallet.php @@ -660,4 +660,3 @@ $string['youhavebalance'] = 'لديك رصيد:'; - diff --git a/lang/en/enrol_wallet.php b/lang/en/enrol_wallet.php index aa15161b..55016dee 100644 --- a/lang/en/enrol_wallet.php +++ b/lang/en/enrol_wallet.php @@ -731,6 +731,7 @@ $string['upperletters'] = 'UPPER case'; $string['usedfrom'] = 'Used From'; $string['usedto'] = 'Used To'; +$string['usernotexist'] = 'User not exist'; $string['usernotfound'] = 'No user found with this email {$a}'; @@ -776,5 +777,3 @@ $string['youhavebalance'] = 'You have balance:'; - - diff --git a/lib.php b/lib.php index bde776a7..6625d42f 100644 --- a/lib.php +++ b/lib.php @@ -1236,17 +1236,13 @@ public function get_courses_options($courseid) { continue; } - $category = core_course_category::get($course->category); - $parentname = $category->name.': '; - // For sites with greate number of course. - // This will make it clearer for selections. - while ($category->parent > 0) { - $parent = core_course_category::get($category->parent); - $parentname = $parent->name . ': ' . $parentname; - $category = $parent; + $category = core_course_category::get($course->category, IGNORE_MISSING, true); + if (!$category) { + continue; } + $catname = $category->get_nested_name(false, ':') . ': '; - $options[$course->id] = $parentname.$course->fullname; + $options[$course->id] = $catname . $course->fullname; } return $options; } diff --git a/version.php b/version.php index 7e05c4f5..ff13a929 100644 --- a/version.php +++ b/version.php @@ -24,10 +24,10 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024070600; +$plugin->version = 2024080700; $plugin->requires = 2020061500; $plugin->component = 'enrol_wallet'; -$plugin->release = '5.4.6'; +$plugin->release = '5.6.0'; $plugin->maturity = MATURITY_STABLE; $plugin->dependencies = [ 'enrol_manual' => ANY_VERSION,