/// Some sanity checks
if (debugging('',DEBUG_DEVELOPER)) {
- static $capsnames = null; // one request per page only
-
- if (is_null($capsnames)) {
- if ($caps = $DB->get_records('capabilities', null, '', 'id, name')) {
- $capsnames = array();
- foreach ($caps as $cap) {
- $capsnames[$cap->name] = true;
- }
- }
- }
- if ($capsnames) { // ignore if can not fetch caps
- if (!isset($capsnames[$capability])) {
- debugging('Capability "'.$capability.'" was not found! This should be fixed in code.');
- }
+ if (!is_valid_capability($capability)) {
+ debugging('Capability "'.$capability.'" was not found! This should be fixed in code.');
}
if (!is_bool($doanything)) {
debugging('Capability parameter "doanything" is wierd ("'.$doanything.'"). This should be fixed in code.');
}
}
+function is_valid_capability($capabilityname) {
+ static $capsnames = null; // one request per page only
+
+ if (is_null($capsnames)) {
+ global $DB;
+ $capsnames = $DB->get_records_menu('capabilities', null, '', 'name, 1');
+ }
+
+ return array_key_exists($capabilityname, $capsnames);
+}
+
/**
* Returns the human-readable, translated version of the capability.
* Basically a big switch statement.
*/
define('PARAM_BASE64', 0x20000);
+/**
+ * PARAM_CAPABILITY - A capability name, like 'moodle/role:manage'. Actually
+ * checked against the list of capabilties in the database.
+ */
+define('PARAM_CAPABILITY', 0x40000);
/// Page types ///
/**
return '';
}
+ case PARAM_CAPABILITY:
+ if (is_valid_capability($param)) {
+ return $param;
+ } else {
+ return '';
+ }
+
default: // throw error, switched parameters in optional_param or another serious problem
print_error("unknowparamtype", '', '', $type);
}