From 2369610903d54c36b1fa5624613cb5cbd7c47549 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Mon, 25 Mar 2024 23:43:27 +0100 Subject: [PATCH] Fixed issue with recursive scan for categories with a given permission causing system hangup --- classes/courseservice.php | 10 +++--- classes/local/helpers/webservicehelper.php | 37 +++++----------------- version.php | 2 +- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/classes/courseservice.php b/classes/courseservice.php index 2b49c4f..b044abc 100644 --- a/classes/courseservice.php +++ b/classes/courseservice.php @@ -30,6 +30,7 @@ use \local_treestudyplan\associationservice; use \local_treestudyplan\local\helpers\webservicehelper; use \local_treestudyplan\completionscanner; use \local_treestudyplan\gradingscanner; +use \local_treestudyplan\debug; use \core_course_category; use moodle_exception; @@ -79,7 +80,7 @@ class courseservice extends \external_api { $rs->close(); } else { - // We were primarily searching for a + // Context is system, but system may not be visible // Return the top visible categories for this user. // Recurses only once. return self::user_tops($userid); @@ -97,9 +98,10 @@ class courseservice extends \external_api { ORDER BY ctx.depth ASC, cat.sortorder ASC"; // Use recordset to handle the eventuality of a really big and complex moodle setup. - $recordset = $DB->get_recordset_sql($sql, ["userid" => $userid, "capability" => $capability, + $recordset = $DB->get_records_sql($sql, ["userid" => $userid, "capability" => $capability, "ctxl_coursecat" => \CONTEXT_COURSECAT,]); - + $params = ["userid" => $userid, "capability" => $capability, + "ctxl_coursecat" => \CONTEXT_COURSECAT,]; $contextids = []; foreach ($recordset as $r) { // Get the paths as an array. @@ -132,7 +134,7 @@ class courseservice extends \external_api { } } } - $recordset->close(); + //$recordset->close(); } return $tops; diff --git a/classes/local/helpers/webservicehelper.php b/classes/local/helpers/webservicehelper.php index 119c288..bd8fac4 100644 --- a/classes/local/helpers/webservicehelper.php +++ b/classes/local/helpers/webservicehelper.php @@ -23,6 +23,7 @@ namespace local_treestudyplan\local\helpers; use \core_course_category; use local_treestudyplan\courseservice; +use local_treestudyplan\debug; defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/webservice/lib.php'); @@ -65,38 +66,16 @@ class webservicehelper { /** * Test if the current user has a certain capability in any of the categories they have access to * @param string $capability The capability to scan for in the categories - * @param core_course_category $parent The parent category to use as a scanning base. Used internally. * @return boolean */ - public static function has_capability_in_any_category($capability, core_course_category $parent = null) { - - // List the categories in which the user has a specific capability. - $list = []; - // Initialize parent if needed. - if ($parent == null) { - $children = courseservice::user_tops(); - } else { - $children = $parent->get_children(); - array_unshift($children,$parent); + public static function has_capability_in_any_category($capability,$userid=null ) { + global $USER; + if ($userid == null) { + $userid = $USER->id; } - // Since the change for a category permission is greatest at the lower levels,. - // We scan in two stages, to focus the search more on the lower levels instead of diving deep into the first category. - // Stage one (surface check): check all children for the capability. - foreach ($children as $child) { - // Check if we should add this category. - if (has_capability($capability, $child->get_context())) { - return true; - } - } - // Stage two (deep dive): recurse into the child categories. - foreach ($children as $child) { - if ($child->get_children_count() > 0) { - if (self::has_capability_in_any_category($capability, $child)) { - return true; - } - } - } - return false; + $list = courseservice::user_tops($userid,$capability); + + return boolval(count($list) > 0); } /** diff --git a/version.php b/version.php index 9707f81..ab7c67d 100644 --- a/version.php +++ b/version.php @@ -22,7 +22,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494). -$plugin->version = 2024032200; // YYYYMMDDHH (year, month, day, iteration). +$plugin->version = 2024032503; // YYYYMMDDHH (year, month, day, iteration). $plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11). $plugin->release = "1.1.6";