$parents = array();
switch ($type) {
-
case CONTEXT_COURSECAT:
-
if (!$cat = get_record('course_categories','id',$context->instanceid)) {
break;
}
$parents[] = $context->id;
$cat = get_record('course_categories','id',$cat->parent);
}
-
break;
case CONTEXT_COURSE:
-
if (!$course = get_record('course', 'id', $context->instanceid)) {
break;
}
default:
break;
-
}
-
return array_reverse($parents);
}
* @return permission (int)
*/
function capability_search($capability, $context, $capabilities) {
-
+
global $USER, $CFG;
if (isset($capabilities[$context->id][$capability])) {
/**
- * Get the roles that have a given capability.
+ * Get the roles that have a given capability assigned to it. This function
+ * does not resolve the actual permission of the capability. It just checks
+ * for assignment only.
* @param $capability - capability name (string)
* @param $permission - optional, the permission defined for this capability
* either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT
}
$records = get_records_sql($SQL.' '.$sort);
+ $contextindependentcaps = fetch_context_independent_capabilities();
+ $records = array_merge($records, $contextindependentcaps);
// special sorting of core system capabiltites and enrollments
if ($context->aggregatelevel == CONTEXT_SYSTEM) {
}
}
// end of special sorting
-
return $records;
}
+/**
+ * Gets the context-independent capabilities that should be overrridable in
+ * any context.
+ * @return array of capability records from the capabilities table.
+ */
+function fetch_context_independent_capabilities() {
+
+ $contextindependentcaps = array(
+ 'moodle/site:accessallgroups'
+ );
+
+ $records = array();
+
+ foreach ($contextindependentcaps as $capname) {
+ $record = get_record('capabilities', 'name', $capname);
+ array_push($records, $record);
+ }
+ return $records;
+}
+
+
/**
* This function pulls out all the resolved capabilities (overrides and
- * defaults) of a role used in capability overrieds in contexts at a given
+ * defaults) of a role used in capability overrides in contexts at a given
* context.
* @param obj $context
* @param int $roleid
and rc.roleid = $roleid
and rc.contextid = c.id $search
ORDER BY c.aggregatelevel DESC, rc.capability DESC";
-
+
$capabilities = array();
if ($records = get_records_sql($SQL)) {
* @return array()
*/
function get_parent_contexts($context) {
-
+
switch ($context->aggregatelevel) {
case CONTEXT_SYSTEM: // no parent
}
}
-/** gets a string for sql calls, searching for stuff
- * in this context or above
+
+/**
+ * Gets a string for sql calls, searching for stuff in this context or above
* @param object $context
* @return string
*/
return (' ='.$context->id);
}
}
+
+
/**
* This function gets the capability of a role in a given context.
* It is needed when printing override forms.
* @return int (allow, prevent, prohibit, inherit)
*/
function get_role_context_capability($contextid, $capability, $capabilities) {
- return $capabilities[$contextid][$capability];
+ if (isset($capabilities[$contextid][$capability])) {
+ return $capabilities[$contextid][$capability];
+ }
+ else {
+ return false;
+ }
}
return $string;
}
-/** gets the list of roles assigned to this context
- * and up (parents)
+/**
+ * Gets the list of roles assigned to this context and up (parents)
* @param object $context
* @return array
*/
function get_roles_used_in_context($context) {
global $CFG;
-
$contextlist = get_related_contexts_string($context);
- return get_records_sql("SELECT distinct r.id, r.name, r.shortname
- FROM {$CFG->prefix}role_assignments ra,
- {$CFG->prefix}role r
- WHERE r.id = ra.roleid
- AND ra.contextid $contextlist
- ORDER BY r.sortorder ASC");
+
+ $sql = "SELECT DISTINCT r.id,
+ r.name,
+ r.shortname,
+ r.sortorder
+ FROM {$CFG->prefix}role_assignments ra,
+ {$CFG->prefix}role r
+ WHERE r.id = ra.roleid
+ AND ra.contextid $contextlist
+ ORDER BY r.sortorder ASC";
+
+ return get_records_sql($sql);
}
/** this function is used to print roles column in user profile page.
}
}
return $usercourses;
-}
-?>
\ No newline at end of file
+}
\ No newline at end of file