import MoodleContextLevel from '@maynoothuniversity/moodle-context-level/src/index.js'
MoodleContextLevel
A class for representing Context Levels within the Moodle VLE's permissions system.
As well as the various functions and properties described in the documetation below there are also dynamically created properties with each valid context level name which get MoodleContextLevel instances for the matching level. In many instances these accessors will obviate the need to use a contructor.
const sysCtx = MoodleContextLevel.system;
const courseCtx = MoodleContextLevel.CONTEXT_COURSE;
Static Member Summary
Static Public Members | ||
public static get |
An alphabetic list of all defined level names, be they full names as they appear in the Moodle source code, base names, or aliases. |
|
public static get |
An alphabetic list of all defined base names, including aliases. |
|
public static get |
A sorted list of all defined context level numbers. |
|
public static get |
A list of all context levels sorted by context level number. |
|
public static get |
A list of all existing context level names as they appear in the Moodle source code sorted from lowest context level number to highest. |
Static Method Summary
Static Public Methods | ||
public static |
Compare two values to see if they represent the same context level, a greater context level, or a lesser context level. |
|
public static |
isContextLevelName(val: *, strictCheck: boolean): boolean Test if a given value is a valid Moodle Context Level Name. |
|
public static |
isContextLevelNumber(val: *, strictTypeCheck: boolean): boolean Test if a given value is a valid Moodle Context Level Number. |
|
public static |
numberFromName(name: ContextLevelName, ContextLevelBaseName, ContextLevelAlias): ContextLevelNumber | NaN Convert any valid name to a context level number. |
|
public static |
parse(level: number | string | MoodleContextLevel): MoodleContextLevel A factory method to build a MoodleContextLevel object from any parsable value. |
|
public static |
parseToName(level: number | string | MoodleContextLevel): MoodleContextLevelName Try to convert a value to a context level name as used in the Moodle source code. |
|
public static |
parseToNumber(level: number | string | MoodleContextLevel): MoodleContextLevelNumber Try to convert a value to a context level number. |
Constructor Summary
Public Constructor | ||
public |
constructor(context: ContextLevelNumber | ContextLevelName | ContextLevelBaseName | ContextLevelAlias) The default context is |
Member Summary
Public Members | ||
public get |
All the level's aliases. |
|
public get |
The level's base name. |
|
public get |
The level's name as it appears in the Moodle sourse code. |
|
public set |
The level's name in any valid form. Any name that can be parsed by the |
|
public get |
All the level's valid names in alphabetical order. This includes the level's name as used in the Moodle source code, the level's base name, and all the level's aliases. |
|
public get |
The level's numeric value. |
|
public set |
The level's numeric value, must be one of the levels defined in
|
Method Summary
Public Methods | ||
public |
Create a new Moodle context level object representing the same context level. |
|
public |
Compare this context level to another. |
|
public |
Test if a given value is a Moodle context level object representing the same context level. |
|
public |
The version as a plain object indexed by: |
|
public |
The context level as a string consisting of the name followed by a space then the level number in parentheses, e.g. |
Static Public Members
public static get allNames: string[] source
An alphabetic list of all defined level names, be they full names as they appear in the Moodle source code, base names, or aliases.
public static get baseNames: string[] source
An alphabetic list of all defined base names, including aliases.
public static get levels: MoodleContextLevel[] source
A list of all context levels sorted by context level number.
Static Public Methods
public static compare(val1: *, val2: *): number source
Compare two values to see if they represent the same context level, a greater context level, or a lesser context level.
Context levels are compared based on their context level number.
Params:
Name | Type | Attribute | Description |
val1 | * | ||
val2 | * |
Return:
number | Unless both values are context level objects, |
public static isContextLevelName(val: *, strictCheck: boolean): boolean source
Test if a given value is a valid Moodle Context Level Name.
By default names, base names, and aliases are considered valid, but with strict checking only the full context level names as used in the Moodle source code will be accepted.
Params:
Name | Type | Attribute | Description |
val | * | the value to test. |
|
strictCheck | boolean |
|
By default any name that can be resolved to a context level number, ignoring case, will be considered valid, but if a truthy value is passed only full context level names in the correct case exactly as used in the Moodle source code will be accepted. |
public static isContextLevelNumber(val: *, strictTypeCheck: boolean): boolean source
Test if a given value is a valid Moodle Context Level Number.
Params:
Name | Type | Attribute | Description |
val | * | the value to test. |
|
strictTypeCheck | boolean |
|
whether or not to enable
strict type checking. With strict type cheking enabled, string
representation of otherwise valid values will return |
public static numberFromName(name: ContextLevelName, ContextLevelBaseName, ContextLevelAlias): ContextLevelNumber | NaN source
Convert any valid name to a context level number. Valid names are context level names as they appear in the Moodle code, context level base names, and context level aliases.
Params:
Name | Type | Attribute | Description |
name | ContextLevelName, ContextLevelBaseName, ContextLevelAlias |
Return:
ContextLevelNumber | NaN | If the passed value can't be converted
to a context level number |
public static parse(level: number | string | MoodleContextLevel): MoodleContextLevel source
A factory method to build a MoodleContextLevel object from any parsable value. The following are supported:
- A valid context level number (as a number or string)
- A valid context level name as used in the Moodle code base (in any case).
- A valid context level base name (in any case).
- A valid context level alias (in any case).
- A context level object.
Params:
Name | Type | Attribute | Description |
level | number | string | MoodleContextLevel | the context level value to parse. |
Throw:
public static parseToName(level: number | string | MoodleContextLevel): MoodleContextLevelName source
Try to convert a value to a context level name as used in the Moodle source code. The following values are supported:
- A valid context level number (as a number or string)
- A valid context level name as used in the Moodle code base (in any case).
- A valid context level base name (in any case).
- A valid context level alias (in any case).
- A context level object.
Params:
Name | Type | Attribute | Description |
level | number | string | MoodleContextLevel | the context level value to parse. |
Return:
MoodleContextLevelName |
Throw:
public static parseToNumber(level: number | string | MoodleContextLevel): MoodleContextLevelNumber source
Try to convert a value to a context level number. The following values are supported:
- A valid context level number (as a number or string)
- A valid context level name as used in the Moodle code base (in any case).
- A valid context level base name (in any case).
- A valid context level alias (in any case).
- A context level object.
Params:
Name | Type | Attribute | Description |
level | number | string | MoodleContextLevel | the context level value to parse. |
Return:
MoodleContextLevelNumber |
Throw:
Public Constructors
public constructor(context: ContextLevelNumber | ContextLevelName | ContextLevelBaseName | ContextLevelAlias) source
The default context is CONTEXT_SYSTEM
.
Params:
Name | Type | Attribute | Description |
context | ContextLevelNumber | ContextLevelName | ContextLevelBaseName | ContextLevelAlias |
Throw:
* |
TypeError |
* |
RangeError |
Public Members
public set name: ContextLevelName | ContextLevelBaseName | ContextLevelAlias source
The level's name in any valid form.
Any name that can be parsed by the nameFromNumber()
static function
is acceptable.
Throw:
public get names: string[] source
All the level's valid names in alphabetical order. This includes the level's name as used in the Moodle source code, the level's base name, and all the level's aliases.
public set number: ContextLevelNumber source
The level's numeric value, must be one of the levels defined in
lib/accesslib.php
in the Moodle source code.
Throw:
Public Methods
public clone(): MoodleContextLevel source
Create a new Moodle context level object representing the same context level.
public compareTo(mv: MoodleContextLevel): number source
Compare this context level to another.
Params:
Name | Type | Attribute | Description |
mv | MoodleContextLevel |
Return:
number |
|
public equals(val: *): boolean source
Test if a given value is a Moodle context level object representing the same context level.
Params:
Name | Type | Attribute | Description |
val | * |