MDL-79174 availability: Allow group condition to use hidden groups
Previously, groups with GROUP_VISIBILITY_NONE could be selected for an availability condition, but as the group API did not return a user's own groups with this visibility, the condition's is_available check always failed. This change uses the new $includehidden parameter to get all of a user's groups from groups_get_user_groups when evaluating the condition, so these groups work as expected. Due to the enforced hiding of conditions containing these groups, they will never be seen by the user.
This commit is contained in:
@@ -49,6 +49,9 @@ abstract class info {
|
||||
/** @var tree Availability configuration, decoded from JSON; null if unset */
|
||||
protected $availabilitytree;
|
||||
|
||||
/** @var array The groups the current user belongs to. */
|
||||
protected $groups;
|
||||
|
||||
/** @var array|null Array of information about current restore if any */
|
||||
protected static $restoreinfo = null;
|
||||
|
||||
@@ -65,6 +68,7 @@ abstract class info {
|
||||
$this->course = $course;
|
||||
$this->visible = (bool)$visible;
|
||||
$this->availability = $availability;
|
||||
$this->groups = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -800,4 +804,28 @@ abstract class info {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns groups that the current user belongs to on the course. Note: If not already
|
||||
* available, this may make a database query.
|
||||
*
|
||||
* This will include groups the user is not allowed to see themselves, so check visibility
|
||||
* before displaying groups to the user.
|
||||
*
|
||||
* @param int $groupingid Grouping ID or 0 (default) for all groups
|
||||
* @return int[] Array of int (group id) => int (same group id again); empty array if none
|
||||
*/
|
||||
public function get_groups(int $groupingid = 0): array {
|
||||
global $USER;
|
||||
if (is_null($this->groups)) {
|
||||
$allgroups = groups_get_user_groups($this->course->id, $USER->id, true);
|
||||
$this->groups = $allgroups;
|
||||
} else {
|
||||
$allgroups = $this->groups;
|
||||
}
|
||||
if (!isset($allgroups[$groupingid])) {
|
||||
return [];
|
||||
}
|
||||
return $allgroups[$groupingid];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user