Skip to content

Commit

Permalink
Prevent exceptions from missing categories.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Aug 7, 2024
1 parent 1e9e47c commit 460a093
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 51 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
20 changes: 14 additions & 6 deletions classes/category/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class helper {
/**
* @var array[int] the parents ids.
*/
protected $parents;
protected $parents = [];
/**
* Create a category helper object.
*
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions classes/coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion classes/form/transfer_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions classes/table/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion classes/util/cm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 11 additions & 3 deletions classes/util/discount_rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions classes/util/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,27 @@ 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;
$cache->discounts = $this->discounts;
self::$cached[$this->id . '-' . $this->userid] = $cache;
}

/**
* Reset static values.
* @return void
*/
public static function reset_static_cache() {
self::$cached = [];
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
34 changes: 27 additions & 7 deletions classes/util/offers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -478,6 +488,7 @@ private function validate_category_enrol_count($offer, $catid) {
if (count($records) >= $number) {
return true;
}

return false;
}
/**
Expand Down Expand Up @@ -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++) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down
9 changes: 7 additions & 2 deletions extra/bulkedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 7 additions & 9 deletions extra/bulkinstances.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions extra/conditionaldiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion extra/coupontable.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
$chkbox = ($candelete) ? '<input id="id_select_'.$record->id.'" type="checkbox" name="select['.$record->id.']" value="1"' : '';

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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion extra/couponusage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 0 additions & 1 deletion lang/ar/enrol_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,3 @@


$string['youhavebalance'] = 'لديك رصيد:';

3 changes: 1 addition & 2 deletions lang/en/enrol_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}';


Expand Down Expand Up @@ -776,5 +777,3 @@


$string['youhavebalance'] = 'You have balance:';


14 changes: 5 additions & 9 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 460a093

Please sign in to comment.