Fixed issue with recursive scan for categories with a given permission causing system hangup

This commit is contained in:
PMKuipers 2024-03-25 23:43:27 +01:00
parent 88744c2b66
commit 2369610903
3 changed files with 15 additions and 34 deletions

View File

@ -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;

View File

@ -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);
}
/**

View File

@ -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";